package com.ruoyi.ibs.cmpm.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.ibs.cmpm.domain.dto.VisitCustQueryDTO;
import com.ruoyi.ibs.cmpm.domain.dto.VisitCustReachDTO;
import com.ruoyi.ibs.cmpm.domain.dto.VisitCustReachListDTO;
import com.ruoyi.ibs.cmpm.domain.entity.VisitCustReach;
import com.ruoyi.ibs.cmpm.domain.vo.VisitCustReachListVO;
import com.ruoyi.ibs.cmpm.domain.vo.VisitCustReachVO;
import com.ruoyi.ibs.cmpm.mapper.VisitCustReachMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author 吴凯程
 * @Date 2025/12/11
 **/
@Service
public class VisitCustReachService {

    @Resource
    private VisitCustReachMapper visitCustReachMapper;

    @Resource
    private ISysDeptService sysDeptService;

    @Resource
    private ISysUserService sysUserService;


    public String saveVisitCustReachDTO(VisitCustReachDTO visitCustReachDTO){
        VisitCustReach visitCustReach;
        if (Objects.isNull(visitCustReachDTO.getId())){
            visitCustReach = new VisitCustReach();
            BeanUtils.copyProperties(visitCustReachDTO,visitCustReach);
            visitCustReach.setDeptId(SecurityUtils.getDeptId());
            visitCustReachMapper.insert(visitCustReach);
        }else {
            visitCustReach = visitCustReachMapper.selectById(visitCustReachDTO.getId());
            BeanUtils.copyProperties(visitCustReachDTO,visitCustReach);
            visitCustReachMapper.updateById(visitCustReach);
        }
        return "客户触达信息已保存";
    }

    public List<VisitCustReachVO> queryVisitCust(String custName){
        VisitCustQueryDTO visitCustQueryDTO = new VisitCustQueryDTO();
        visitCustQueryDTO.setCustName(custName);
        visitCustQueryDTO.setUserName(SecurityUtils.getUsername());
        visitCustQueryDTO.setBranchId(getBranchIdByUserName(SecurityUtils.getUsername()));
        return visitCustReachMapper.queryVisitCust(visitCustQueryDTO);
    }

    public PageInfo<VisitCustReachListVO> queryVisitCustReachList(VisitCustReachListDTO visitCustReachListDTO){
        visitCustReachListDTO.setDeptId(SecurityUtils.getDeptId());
        visitCustReachListDTO.setEditUserName(SecurityUtils.getUsername());
        if ("manager".equals(SecurityUtils.userRole())){
            visitCustReachListDTO.setAliasUserName(SecurityUtils.getUsername());
        }
        PageMethod.startPage(visitCustReachListDTO.getPageNum(), visitCustReachListDTO.getPageSize());
        Page<VisitCustReachListVO> visitCustReachListVOPage = visitCustReachMapper.queryVisitCustReachList(visitCustReachListDTO);
        return visitCustReachListVOPage.toPageInfo();
    }

    public List<VisitCustReachListVO> queryVisitCustReachListByCustId(String custId){
        return visitCustReachMapper.queryVisitCustReachListByCustId(custId);
    }

    private Long getBranchIdByUserName(String userName){
        SysUser sysUser = sysUserService.selectUserByUserName(userName);
        SysDept sysDept = sysDeptService.selectDeptById(sysUser.getDeptId());
        if (sysDept.getDeptType().equals("branch")){
            return sysDept.getDeptId();
        }
        if (sysDept.getDeptType().equals("outlet")){
            return sysDept.getParentId();
        }
        return null;
    }
}
