package com.ruoyi.ibs.task.service.impl;

import com.aliyun.oss.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.ibs.grid.mapper.AnchorMapper;
import com.ruoyi.ibs.list.domain.SysCampaign;
import com.ruoyi.ibs.list.mapper.SysCampaignMapper;
import com.ruoyi.ibs.list.service.ISysCampaignService;
import com.ruoyi.ibs.task.domain.dto.VisitTargetDTO;
import com.ruoyi.ibs.task.domain.dto.VisitTaskCountDTO;
import com.ruoyi.ibs.task.domain.dto.VisitTaskListDTO;
import com.ruoyi.ibs.task.domain.entity.AppointmentInfo;
import com.ruoyi.ibs.task.domain.vo.VisitTargetVO;
import com.ruoyi.ibs.task.domain.vo.VisitTaskListVO;
import com.ruoyi.ibs.task.mapper.AppointmentMapper;
import com.ruoyi.ibs.task.mapper.VisitTaskMapper;
import com.ruoyi.ibs.task.service.AppointmentService;
import com.ruoyi.ibs.task.service.VisitTaskService;
import org.springframework.stereotype.Service;

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

/**
 * @Author 吴凯程
 * @Date 2025/6/12
 **/
@Service
public class VisitTaskServiceImpl implements VisitTaskService {

    @Resource
    private VisitTaskMapper visitTaskMapper;

    @Resource
    private AnchorMapper anchorMapper;

    @Resource
    private SysCampaignMapper sysCampaignMapper;

    @Resource
    private AppointmentService appointmentService;

    @Resource
    private AppointmentMapper appointmentMapper;

    @Resource
    private ISysCampaignService sysCampaignService;

    /**
     * @param visitTaskListDTO
     * @return
     */
    @Override
    public List<VisitTaskListVO> getVisitTaskList(VisitTaskListDTO visitTaskListDTO) {
        visitTaskListDTO.setUserId(SecurityUtils.getUserId());
        visitTaskListDTO.setDeptId(SecurityUtils.getDeptId());
        List<VisitTaskListVO> visitTaskList = visitTaskMapper.getVisitTaskList(visitTaskListDTO);
        for (VisitTaskListVO visitTaskListVO : visitTaskList) {
            VisitTaskCountDTO countByCampaignId = getCountByCampaignId(visitTaskListVO.getCampaignId());
            BeanUtils.copyProperties(countByCampaignId, visitTaskListVO);
        }
        //将预约任务新增至走访活动顶部
        List<AppointmentInfo> appointmentUnCompleted = appointmentService.appointmentCustListByUnCompleted(SecurityUtils.getUsername());
        if(appointmentUnCompleted.size()>0){
            List<AppointmentInfo> appointmentUnFinished = appointmentService.appointmentCustListByUserNameFinish("false");
            VisitTaskListVO v = new VisitTaskListVO();
            v.setCampaignId("-1");
            v.setCampaignName("预约走访任务");
            int completeCount = appointmentService.appointmentCustListByUserNameFinish("true").size();
            v.setCompleteCount((long)completeCount);
            v.setUnCompleteCount((long)appointmentUnFinished.size());
            v.setTotalCount((long)appointmentUnFinished.size()+completeCount);
            visitTaskList.add(0,v);
        }
        return visitTaskList;
    }

    private VisitTaskCountDTO getCountByCampaignId(String campaignId) {
        SysCampaign sysCampaign = sysCampaignMapper.selectSysCampaignByCampaignId(campaignId);
        VisitTaskCountDTO visitTaskCountDTO = new VisitTaskCountDTO();
        if (sysCampaign.getCooperate()){
            visitTaskCountDTO.setTotalCount(visitTaskMapper.countCoopCampaignTotal(campaignId, SecurityUtils.getUserId()));
        }else{
            visitTaskCountDTO.setTotalCount(visitTaskMapper.countCampaignTotal(campaignId, SecurityUtils.getUserId()));
        }
        visitTaskCountDTO.setCompleteCount(visitTaskMapper.countCampaignComplete(campaignId, SecurityUtils.getUserId()));
        visitTaskCountDTO.setUnCompleteCount(visitTaskCountDTO.getTotalCount() - visitTaskCountDTO.getCompleteCount());
        return visitTaskCountDTO;
    }

    /**
     * @param visitTargetDTO
     * @return
     */
    @Override
    public List<VisitTargetVO> getVisitTargetList(VisitTargetDTO visitTargetDTO) {
        SysCampaign sysCampaign = sysCampaignService.selectSysCampaignByCampaignId(visitTargetDTO.getCampaignId());
        if (Objects.isNull(sysCampaign)){
            throw new ServiceException("活动不存在");
        }
        visitTargetDTO.setUserId(SecurityUtils.getUserId());
        visitTargetDTO.setStart( (visitTargetDTO.getNum() - 1)  * visitTargetDTO.getSize());
        List<VisitTargetVO> targetVOS = new ArrayList<>();
        if ( sysCampaign.getIsCooperate()){
            targetVOS = visitTaskMapper.getCoopVisitTargetList(visitTargetDTO);
//            if (visitTargetDTO.getCustType().equals("0")){
//                targetVOS = visitTaskMapper.getCoopVisitTargetRetailList(visitTargetDTO);
//            }else if (visitTargetDTO.getCustType().equals("1")){
//                targetVOS = visitTaskMapper.getCoopVisitTargetMerchantList(visitTargetDTO);
//            }else if (visitTargetDTO.getCustType().equals("2")){
//                targetVOS = visitTaskMapper.getCoopVisitTargetBusinessList(visitTargetDTO);
//            }
        }else{
            targetVOS = visitTaskMapper.getVisitTargetList(visitTargetDTO);
//            if (visitTargetDTO.getCustType().equals("0")){
//                targetVOS = visitTaskMapper.getVisitTargetRetailList(visitTargetDTO);
//            }else if (visitTargetDTO.getCustType().equals("1")){
//                targetVOS = visitTaskMapper.getVisitTargetMerchantList(visitTargetDTO);
//            }else if (visitTargetDTO.getCustType().equals("2")){
//                targetVOS = visitTaskMapper.getVisitTargetBusinessList(visitTargetDTO);
//            }
        }
        return targetVOS;
    }

    @Override
    public List<VisitTargetVO> getVisitTargetListByAppointment(String finishFlag) {
        // finishFlag: true 已走访 false 未走访
        List<AppointmentInfo> appointmentInfos = appointmentService.appointmentCustListByUserNameFinish(finishFlag);
        List<VisitTargetVO> visitTargetVOS = new ArrayList<>();
        if (appointmentInfos.isEmpty()){
            return visitTargetVOS;
        }
        //遍历appointmentInfos，转换成visitTargetVOS
        for (AppointmentInfo appointmentInfo : appointmentInfos) {
            VisitTargetVO visitTargetVO = new VisitTargetVO();
            String feedbackId = null;
            String recordId = appointmentInfo.getRecordId();
            if (recordId != null && !recordId.isEmpty()){
                feedbackId = visitTaskMapper.selectFeedbackIdByRecordId(recordId);
            }
            visitTargetVO.setFeedbackId(feedbackId);
            String custType = appointmentInfo.getCustType();
            if (custType.equals("0")){
                visitTargetVO.setCustIdc(appointmentInfo.getCustIdc());
                visitTargetVO.setCustId("101" + appointmentInfo.getCustIdc());
                visitTargetVO.setFeedbackType("0");
            }else {
                visitTargetVO.setCustIdc(appointmentInfo.getCustIdc());
                visitTargetVO.setCustId("202" + appointmentInfo.getCustIdc());
                visitTargetVO.setSocialCreditCode(appointmentInfo.getCustIdc());
                visitTargetVO.setFeedbackType("1");
            }
            visitTargetVO.setCustType(custType);
            visitTargetVO.setAppointmentId(String.valueOf(appointmentInfo.getId()));
            visitTargetVO.setCustName(appointmentInfo.getCustName());
            visitTargetVO.setAddress(anchorMapper.selectAddressByCustIdc(appointmentInfo.getCustIdc()));
            visitTargetVO.setFeedbackTemplateId("-1");
            visitTargetVO.setRecordId(appointmentInfo.getRecordId());
            if(StringUtils.isNotEmpty(appointmentInfo.getSignFlag())){
                visitTargetVO.setSignFlag("0".equalsIgnoreCase(appointmentInfo.getSignFlag()) ? true : false);
            }else {
                visitTargetVO.setSignFlag(true);
            }
            visitTargetVOS.add(visitTargetVO);
        }
        return visitTargetVOS;
    }

}
