package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.InvestmentCountDTO;
import com.cool.store.dto.PendingCountDTO;
import com.cool.store.dto.openPreparation.PlanLineDTO;
import com.cool.store.entity.LineInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.WorkflowSubStageEnum;
import com.cool.store.enums.WorkflowSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.LineInfoMapper;
import com.cool.store.request.LineListRequest;
import com.cool.store.request.PartnerRequest;
import com.cool.store.request.PointLinePageRequest;
import com.cool.store.request.PublicLineListRequest;
import com.cool.store.utils.RandomEightCharCodeUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangchenbiao
 * @FileName: LineInfoDAO
 * @Description:
 * @date 2024-03-19 10:49
 */
@Slf4j
@Repository
public class LineInfoDAO {

    @Resource
    private LineInfoMapper lineInfoMapper;


    public LineInfoDO getLineInfo(Long lineId) {
        LineInfoDO lineInfo = lineInfoMapper.selectByPrimaryKey(lineId);
        if(Objects.nonNull(lineInfo) && !lineInfo.getDeleted()){
            return lineInfo;
        }
        return null;
    }

    public Integer updateLineInfo(LineInfoDO param){
        if(Objects.isNull(param.getId())){
            log.error("线索id为空");
            return null;
        }
        return lineInfoMapper.updateByPrimaryKeySelective(param);
    }

    public Integer updateWorkflowStage(Long lineId, WorkflowSubStageEnum workflowSubStage, WorkflowSubStageStatusEnum workflowSubStageStatus,String userId) {
        if(Objects.isNull(workflowSubStageStatus)){
            log.info("更新线索阶段，子阶段 和 子阶段状态不能同时为空");
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        LineInfoDO lineInfo = new LineInfoDO();
        lineInfo.setId(lineId);
        if(Objects.nonNull(workflowSubStage)){
            lineInfo.setWorkflowSubStage(workflowSubStage.getCode());
        }
        if(StringUtils.isNotEmpty(userId)){
            lineInfo.setUpdateUserId(userId);
        }
        //准加盟商
        lineInfo.setJoinStatus(CommonConstants.ONE);
        lineInfo.setPartnerNum(RandomEightCharCodeUtils.getCode());
        lineInfo.setWorkflowSubStageStatus(workflowSubStageStatus.getCode());
        return lineInfoMapper.updateByPrimaryKeySelective(lineInfo);
    }


    public Integer batchUpdateInterviewWorkflowStage(List<Long> lineIds, WorkflowSubStageEnum workflowSubStage, WorkflowSubStageStatusEnum workflowSubStageStatus) {
        if(Objects.isNull(workflowSubStageStatus) || CollectionUtils.isEmpty(lineIds)){
            log.info("更新线索阶段，子阶段 和 子阶段状态不能同时为空");
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        return lineInfoMapper.batchUpdateInterviewWorkflowStage(lineIds, workflowSubStage.getCode(), workflowSubStageStatus.getCode());
    }

    public Integer updateWorkflowStageAndInterviewer(Long lineId, WorkflowSubStageStatusEnum workflowSubStageStatus, String firstInterviewer, String secondInterviewer) {
        LineInfoDO lineInfo = new LineInfoDO();
        lineInfo.setId(lineId);
        lineInfo.setWorkflowSubStageStatus(workflowSubStageStatus.getCode());
        lineInfo.setFirstInterviewer(firstInterviewer);
        lineInfo.setSecondInterviewer(secondInterviewer);
        return lineInfoMapper.updateByPrimaryKeySelective(lineInfo);
    }


    public LineInfoDO getByPartnerId(String partnerId) {
        LineInfoDO lineInfo = lineInfoMapper.getByPartnerId(partnerId);
        if(Objects.nonNull(lineInfo) && !lineInfo.getDeleted()){
            return lineInfo;
        }
        return null;
    }

    public List<LineInfoDO> listByInvestmentManager(String investmentManagerUserId,List<Integer> subStageStatus) {
        if (StringUtils.isBlank(investmentManagerUserId)){
            return null;
        }
        List<LineInfoDO> lineInfo = lineInfoMapper.listByInvestmentManager(investmentManagerUserId,subStageStatus);
        return lineInfo;
    }
    public List<LineInfoDO> listByInterview(String interviewId, Integer interviewType,List<Integer> pendingInterviewStatusList,
                                            Integer notPassingTheInterview,List<Integer> subStageStatus) {
        if (StringUtils.isBlank(interviewId)){
            return null;
        }
        List<LineInfoDO> lineInfo = lineInfoMapper.listByInterview(interviewId,interviewType,pendingInterviewStatusList,notPassingTheInterview,subStageStatus);
        return lineInfo;
    }

    public List<LineInfoDO> lineList(LineListRequest lineListRequest,String wantShopAreaName, String userId, List<Long> wantShopAreaIds) {
        List<LineInfoDO> lineInfo = lineInfoMapper.lineList(lineListRequest,wantShopAreaName,userId,wantShopAreaIds);
        return lineInfo;
    }

    public List<LineInfoDO> partnerList(PartnerRequest partnerRequest,String wantShopAreaName, String userId, List<Long> wantShopAreaIds) {
        List<LineInfoDO> lineInfo = lineInfoMapper.partnerList(partnerRequest,wantShopAreaName,userId,wantShopAreaIds);
        return lineInfo;
    }

    public List<LineInfoDO> publicLineList(PublicLineListRequest publicLineListRequest,Boolean publicFlag) {
        List<LineInfoDO> lineInfo = lineInfoMapper.publicLineList(publicLineListRequest,publicFlag);
        return lineInfo;
    }

    public void insertOrUpdate(LineInfoDO lineInfoParam){
        lineInfoMapper.insertOrUpdate(lineInfoParam);
    }

    public Page<LineInfoDO> getLinePageByDevelopmentManager(PointLinePageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        return lineInfoMapper.getLinePageByDevelopmentManager(request);
    }


    public PendingCountDTO pendingCount(String userId) {
        if (StringUtils.isBlank(userId)){
            return null;
        }
        PendingCountDTO pendingCount = lineInfoMapper.pendingCount(userId);
        return pendingCount;
    }

    public InvestmentCountDTO investmentData(String userId) {
        if (StringUtils.isBlank(userId)){
            return null;
        }
        InvestmentCountDTO investmentData = lineInfoMapper.investmentData(userId);
        return investmentData;
    }


    public List<PlanLineDTO> getLines(List<Long> lineIdList){
        return lineInfoMapper.getLines(lineIdList);
    }

    /**
     * 获取线索手机号
     * @param lineIds
     * @return
     */
    public Map<Long, String> getLineMobileMap(List<Long> lineIds){
        if(CollectionUtils.isEmpty(lineIds)){
            return Maps.newHashMap();
        }
        List<LineInfoDO> lineMobile = lineInfoMapper.getLineMobile(lineIds);
        return lineMobile.stream().filter(o->StringUtils.isNotBlank(o.getMobile())).collect(Collectors.toMap(LineInfoDO::getId, LineInfoDO::getMobile, (k1, k2)-> k1));
    }

    public Integer batchUpdateInvestmentManager( List<Long> lineIds, Integer status, String investmentManager){
        if(CollectionUtils.isEmpty(lineIds)|| status == null|| StringUtils.isBlank(investmentManager)){
            return 0;
        }
        return lineInfoMapper.batchUpdateInvestmentManager(lineIds, status, investmentManager);
    }
}
