package com.cool.store.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.CommonConstants;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.*;
import com.cool.store.entity.*;
import com.cool.store.enums.*;
import com.cool.store.enums.point.PayBusinessTypeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.IntentAgreementMapper;
import com.cool.store.mapper.JoinIntentionMapper;
import com.cool.store.mapper.RegionMapper;
import com.cool.store.request.*;
import com.cool.store.service.*;
import com.cool.store.utils.CoolDateUtils;
import com.cool.store.utils.StringUtil;
import com.cool.store.utils.UUIDUtils;
import com.cool.store.utils.poi.DateUtils;
import com.cool.store.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2024/3/25 13:48
 * @Version 1.0
 */
@Slf4j
@Service
public class LineServiceImpl implements LineService {

    @Resource
    LineInfoDAO lineInfoDAO;
    @Resource
    HyPartnerUserInfoDAO hyPartnerUserInfoDAO;
    @Resource
    private RegionAreaConfigDao regionAreaConfigDao;
    @Resource
    HyOpenAreaInfoDAO hyOpenAreaInfoDAO;
    @Resource
    DeskService deskService;
    @Resource
    HyPartnerUserChannelDAO hyPartnerUserChannelDAO;
    @Resource
    EnterpriseUserDAO enterpriseUserDAO;
    @Resource
    RegionService regionService;
    @Resource
    RegionDao regionDao;
    @Resource
    SysRoleService sysRoleService;
    @Resource
    UserAuthMappingService userAuthMappingService;
    @Resource
    TransferLogService transferLogService;
    @Resource
    IntentAgreementMapper intentAgreementMapper;
    @Resource
    JoinIntentionMapper joinIntentionMapper;
    @Resource
    LineInterviewDAO  lineInterviewDAO;
    @Resource
    LineAuditInfoDAO  lineAuditInfoDAO;
    @Resource
    LinePayDAO   linePayDAO;
    @Resource
    TrainingExperienceService trainingExperienceService;
    @Resource
    private RegionQrcodeConfigDao regionQrcodeConfigDao;
    @Resource
    QualificationsInfoDAO qualificationsInfoDAO;


    @Override
    public LineInfoVO getLineInfo(Long lineId) {
        LineInfoVO result = new LineInfoVO();
        // 查询线索信息
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(lineId);
        if (lineInfo==null){
            throw new ServiceException(ErrorCodeEnum.LINE_ID_IS_NOT_EXIST);
        }
        if (StringUtil.isBlank(lineInfo.getInvestmentManager())){
            return null;
        }
        if (lineInfo.getLineSource()!=null){
            Map<Integer, String> channelMapByIds = hyPartnerUserChannelDAO.getChannelMapByIds(Arrays.asList(lineInfo.getLineSource()));
            result.setLineSourceName(channelMapByIds.get(lineInfo.getLineSource()));
        }
        if (StringUtil.isNotEmpty(lineInfo.getInvestmentManager())){
            EnterpriseUserDO user = enterpriseUserDAO.getUserInfoById(lineInfo.getInvestmentManager());
            result.setInvestmentManagerName(user.getName());
            result.setInvestmentManagerMobile(user.getMobile());
        }
        if(lineInfo.getRegionId() != null && lineInfo.getRegionId() != 0){
            Long bigRegionIdByAreaId = regionService.getBigRegionIdByAreaId(lineInfo.getWantShopAreaId());
            Map<Long, String> regionNameMap = regionDao.getRegionNameMap(Arrays.asList(bigRegionIdByAreaId, lineInfo.getRegionId()));
            result.setRegionId(lineInfo.getRegionId());
            result.setRegionName(regionNameMap.get(lineInfo.getRegionId()));
        }
        LineAuditInfoDO lineAuditInfoDO = lineAuditInfoDAO.getAuditInfoByLineId(lineInfo.getId());
        if (lineAuditInfoDO!=null){
            result.setRejectPublicReason(lineAuditInfoDO.getRejectPublicReason());
        }

        QualificationsInfoDO qualificationsInfoDO = qualificationsInfoDAO.getByLineId(lineInfo.getId());
        Map<Long, HyPartnerLabelDO> userPortraitMap = deskService.getUserPortraitMap(Arrays.asList(lineInfo));
        Map<Long, String>  wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(Arrays.asList(lineInfo.getWantShopAreaId()));
        if (lineInfo.getWantShopAreaId()!=null){
            HyOpenAreaInfoDO hyOpenAreaInfoDO = hyOpenAreaInfoDAO.selectById(lineInfo.getWantShopAreaId());
            result.setWantShopProvinceId(hyOpenAreaInfoDO.getParentId());
        }
        BaseInfoVO baseInfoVO = deskService.convertToBaseInfoVO(lineInfo, userPortraitMap, wantShopAreaMap);
        result.setWantShopAreaName(baseInfoVO.getWantShopAreaName());
        result.setUserPortraitList(baseInfoVO.getUserPortraitList());
        if (qualificationsInfoDO != null){
            result.setJoinType(qualificationsInfoDO.getJoinType());
        }
        BeanUtil.copyProperties(lineInfo,result);
        return result;
    }

    @Override
    public PageInfo<LineListVO> getLineList(LineListRequest lineListRequest, LoginUserInfo loginUserInfo,Boolean teamFlag) {
        List<Long> wantShopAreaIds = new ArrayList<>();
        String userId = null;
        if(!teamFlag){
            userId = loginUserInfo.getUserId();
        }
        if (teamFlag && !sysRoleService.checkIsAdmin(loginUserInfo.getUserId())){
            List<Long> list = userAuthMappingService.listWantShopAreaIdByUserId(loginUserInfo.getUserId());
            if (CollectionUtils.isNotEmpty(list)){
                wantShopAreaIds = list;
            }else {
                userId = loginUserInfo.getUserId();
            }
        }
        String areaName = null;
        if (lineListRequest.getWantShopAreaId() != null){
            HyOpenAreaInfoDO hyOpenAreaInfoDO = hyOpenAreaInfoDAO.selectById(lineListRequest.getWantShopAreaId());
            areaName =  hyOpenAreaInfoDO.getAreaPath();
        }
        PageHelper.startPage(lineListRequest.getPageNum(), lineListRequest.getPageSize());
        List<LineInfoDO> lineInfoDOS = lineInfoDAO.lineList(lineListRequest,areaName, userId, wantShopAreaIds);
        PageInfo page = new PageInfo(lineInfoDOS);
        Map<Long, HyPartnerLabelDO> userPortraitMap = deskService.getUserPortraitMap(lineInfoDOS);
        List<Long> wantShopAreaIdList = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
        Map<Long, String>  wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIdList);
        List<Integer> lineSourceIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getLineSource() != null).map(LineInfoDO::getLineSource).collect(Collectors.toList());
        Map<Integer, String> channelMapByIds = hyPartnerUserChannelDAO.getChannelMapByIds(lineSourceIds);
        List<String> userIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getInvestmentManager() != null).map(LineInfoDO::getInvestmentManager).collect(Collectors.toList());
        userIds.addAll(lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getUpdateUserId() != null).map(LineInfoDO::getUpdateUserId).collect(Collectors.toList()));
        Map<String, String> userNameMap = enterpriseUserDAO.getUserNameMap(userIds);

        List<LineListVO>  result = new ArrayList<>();
        lineInfoDOS.forEach(x->{
            BaseInfoVO baseInfoVO = deskService.convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
            LineListVO lineListVO = new LineListVO(baseInfoVO);
            lineListVO.setCreateTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, x.getCreateTime()));
            lineListVO.setUpdateTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, x.getUpdateTime()));
            lineListVO.setLineSourceName(channelMapByIds.get(x.getLineSource()));
            lineListVO.setJoinMode(x.getJoinMode());
            lineListVO.setInvestmentManagerUserName(userNameMap.get(x.getInvestmentManager()));
            lineListVO.setUpdateUserName(userNameMap.get(x.getUpdateUserId()));
            result.add(lineListVO);
        });
        page.setList(result);
        return page;
    }

    @Override
    public PageInfo<PublicLineListVO> getPublicLineList(PublicLineListRequest publicLineListRequest, LoginUserInfo loginUserInfo, Boolean publicFlag) {
        PageHelper.startPage(publicLineListRequest.getPageNum(), publicLineListRequest.getPageSize());
        List<LineInfoDO> lineInfoDOS = lineInfoDAO.publicLineList(publicLineListRequest,publicFlag);
        PageInfo page = new PageInfo(lineInfoDOS);
        Map<Long, HyPartnerLabelDO> userPortraitMap = deskService.getUserPortraitMap(lineInfoDOS);
        List<Long> wantShopAreaIdList = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
        Map<Long, String>  wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIdList);
        List<Integer> lineSourceIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getLineSource() != null).map(LineInfoDO::getLineSource).collect(Collectors.toList());
        Map<Integer, String> channelMapByIds = hyPartnerUserChannelDAO.getChannelMapByIds(lineSourceIds);
        List<String> userIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getInvestmentManager() != null).map(LineInfoDO::getInvestmentManager).collect(Collectors.toList());
        userIds.addAll(lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getCreateUserId() != null).map(LineInfoDO::getCreateUserId).collect(Collectors.toList()));
        userIds.addAll(lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getDevelopmentManager() != null).map(LineInfoDO::getDevelopmentManager).collect(Collectors.toList()));
        Map<String, String> userNameMap = enterpriseUserDAO.getUserNameMap(userIds);

        List<PublicLineListVO>  result = new ArrayList<>();
        lineInfoDOS.forEach(x->{
            BaseInfoVO baseInfoVO = deskService.convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
            PublicLineListVO publicLineListVO = new PublicLineListVO(baseInfoVO);
            publicLineListVO.setCreateTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, x.getCreateTime()));
            publicLineListVO.setUpdateTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, x.getUpdateTime()));
            publicLineListVO.setLineSourceName(channelMapByIds.get(x.getLineSource()));
            publicLineListVO.setJoinMode(x.getJoinMode());
            publicLineListVO.setLastInvestmentManagerUserName(userNameMap.get(x.getInvestmentManager()));
            publicLineListVO.setLastDevelopmentManagerUserName(userNameMap.get(x.getDevelopmentManager()));
            publicLineListVO.setUpdateUserName(userNameMap.get(x.getUpdateUserId()));
            result.add(publicLineListVO);
        });
        page.setList(result);
        return page;
    }

    @Override
    public PageInfo<PartnerListVO> getPartnerList(PartnerRequest partnerRequest, LoginUserInfo loginUserInfo, Boolean teamFlag) {

        List<Long> wantShopAreaIds = new ArrayList<>();
        String userId = null;
        if(!teamFlag){
            userId = loginUserInfo.getUserId();
        }
        if (teamFlag && !sysRoleService.checkIsAdmin(loginUserInfo.getUserId())){
            List<Long> list = userAuthMappingService.listWantShopAreaIdByUserId(loginUserInfo.getUserId());
            if (CollectionUtils.isNotEmpty(list)){
                wantShopAreaIds = list;
            }else {
                userId = loginUserInfo.getUserId();
            }
        }
        String areaName = null;
        if (partnerRequest.getWantShopAreaId() != null){
            HyOpenAreaInfoDO hyOpenAreaInfoDO = hyOpenAreaInfoDAO.selectById(partnerRequest.getWantShopAreaId());
            areaName =  hyOpenAreaInfoDO.getAreaPath();
        }
        PageHelper.startPage(partnerRequest.getPageNum(), partnerRequest.getPageSize());
        List<LineInfoDO> lineInfoDOS = lineInfoDAO.partnerList(partnerRequest,areaName, userId, wantShopAreaIds);
        PageInfo page = new PageInfo(lineInfoDOS);
        Map<Long, HyPartnerLabelDO> userPortraitMap = deskService.getUserPortraitMap(lineInfoDOS);
        List<Long> wantShopAreaIdList = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
        Map<Long, String>  wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIdList);
        List<Integer> lineSourceIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getLineSource() != null).map(LineInfoDO::getLineSource).collect(Collectors.toList());
        Map<Integer, String> channelMapByIds = hyPartnerUserChannelDAO.getChannelMapByIds(lineSourceIds);

        List<String> userIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getInvestmentManager() != null).map(LineInfoDO::getInvestmentManager).collect(Collectors.toList());
        userIds.addAll(lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getDevelopmentManager() != null).map(LineInfoDO::getDevelopmentManager).collect(Collectors.toList()));
        userIds.addAll(lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getUpdateUserId() != null).map(LineInfoDO::getUpdateUserId).collect(Collectors.toList()));
        Map<String, String> userNameMap = enterpriseUserDAO.getUserNameMap(userIds);


        List<Long> lineIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getId() != null).map(LineInfoDO::getId).collect(Collectors.toList());
        List<SigningBaseInfoDO> signingBaseInfoDOS = intentAgreementMapper.selectByLineIds(lineIds);
        Map<Long, Date> dateMap = signingBaseInfoDOS.stream().collect(Collectors.toMap(SigningBaseInfoDO::getLineId, SigningBaseInfoDO::getCreateTime));

        List<PartnerListVO>  result = new ArrayList<>();
        lineInfoDOS.forEach(x->{
            BaseInfoVO baseInfoVO = deskService.convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
            PartnerListVO partnerListVO = new PartnerListVO(baseInfoVO);
            Date date = dateMap.get(x.getId());
            if (date != null){
                partnerListVO.setJoinTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date));
            }
            partnerListVO.setUpdateTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, x.getUpdateTime()));
            partnerListVO.setLineSourceName(channelMapByIds.get(x.getLineSource()));
            partnerListVO.setJoinStatus(x.getJoinStatus());
            partnerListVO.setJoinMode(x.getJoinMode());
            partnerListVO.setPartnerNum(x.getPartnerNum());
            partnerListVO.setInvestmentManagerUserName(userNameMap.get(x.getInvestmentManager()));
            partnerListVO.setUpdateUserName(userNameMap.get(x.getUpdateUserId()));
            partnerListVO.setDevelopmentManagerUserName(userNameMap.get(x.getDevelopmentManager()));
            partnerListVO.setWantShopNum(x.getWantShopNum());
            result.add(partnerListVO);
        });
        page.setList(result);
        return page;
    }

    @Override
    public Boolean addTags(LoginUserInfo user, AddTagsRequest addTagsRequest) {
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(addTagsRequest.getLineId());
        if (lineInfo==null){
            throw new ServiceException(ErrorCodeEnum.LINE_ID_IS_NOT_EXIST);
        }
        lineInfo.setUpdateUserId(user.getUserId());
        lineInfo.setUpdateTime(new Date());
        lineInfo.setUserPortrait(CollectionUtils.isNotEmpty(addTagsRequest.getTags())?
                addTagsRequest.getTags().stream().map(Object::toString).collect(Collectors.joining(CommonConstants.COMMA, CommonConstants.COMMA, CommonConstants.COMMA)):"");
        lineInfoDAO.updateLineInfo(lineInfo);
        return Boolean.TRUE;
    }

    @Override
    public Boolean transferInvestmentManager(ChangeInvestmentRequest changeInvestmentRequest, LoginUserInfo userInfo,Boolean transferFlag) {
        //查询线索
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(changeInvestmentRequest.getLineId());
        if (lineInfo==null){
            throw new ServiceException(ErrorCodeEnum.LINE_ID_IS_NOT_EXIST);
        }
        //分配拦截 分配的时候在公海才能分配      转让不拦截
        if (!lineInfo.getLineStatus().equals(LineStatusEnum.PUBLIC_SEAS.getCode())&&!transferFlag){
            throw new ServiceException(ErrorCodeEnum.NOT_IN_PUBLIC_SEA);
        }
        //转让的时候拦截  且不能转让给自己
        if (lineInfo.getInvestmentManager().equals(changeInvestmentRequest.getInvestmentManagerId())&&transferFlag){
            throw new ServiceException(ErrorCodeEnum.NO_TRANSFER_REQUIRED);
        }

        //如果线索招商经理为空 直接分配  如果不为空 且线索在公海 说明线索被结束 需要重置ID 生成一条新的线索
        lineInfo.setLineStatus(LineStatusEnum.PRIVATE_SEAS.getCode());
        lineInfo.setInvestmentManager(changeInvestmentRequest.getInvestmentManagerId());
        lineInfo.setUpdateUserId(userInfo.getUserId());
        lineInfoDAO.updateLineInfo(lineInfo);
        if (transferFlag){
            //添加日志
            OperationLogTypeEnum operationLogTypeEnum = OperationLogTypeEnum.TRANSFER_INVESTMENT_MANAGER;
           if (lineInfo.getJoinStatus()!=0){
               operationLogTypeEnum = OperationLogTypeEnum.TRANSFER_INVESTMENT_MANAGER_3;
           }
           transferLogService.addLog(lineInfo,userInfo.getUserId(),changeInvestmentRequest.getInvestmentManagerId(),operationLogTypeEnum);
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean changeLineInfo(UpdateLineRequest updateLineRequest,LoginUserInfo userInfo) {
        log.info("changeJoinMode request={}",JSONObject.toJSONString(updateLineRequest));
        if (updateLineRequest.getLineId()==null||updateLineRequest.getJoinMode()==null){
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        //查询线索
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(updateLineRequest.getLineId());
        if (lineInfo==null){
            throw new ServiceException(ErrorCodeEnum.LINE_ID_IS_NOT_EXIST);
        }
        lineInfo.setJoinMode(updateLineRequest.getJoinMode());
        Long regionId = regionAreaConfigDao.getByWantShopAreaId(updateLineRequest.getWantShopAreaId());
        lineInfo.setRegionId(regionId);
        lineInfo.setWantShopAreaId(updateLineRequest.getWantShopAreaId());
        lineInfo.setUsername(updateLineRequest.getLineName());
        lineInfo.setUpdateTime(new Date());
        lineInfo.setUpdateUserId(userInfo.getUserId());
        lineInfoDAO.updateLineInfo(lineInfo);
        return Boolean.TRUE;
    }

    @Override
    public Boolean distributionLine(DistributionInvestmentRequest request, LoginUserInfo userInfo) {
        log.info("changeJoinMode request={}",JSONObject.toJSONString(request));
        if (CollectionUtils.isEmpty(request.getLineIds())||request.getInvestmentManagerId()==null){
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        //批量更新线索
        lineInfoDAO.batchUpdateInvestmentManager(request.getLineIds(),LineStatusEnum.PRIVATE_SEAS.getCode(), request.getInvestmentManagerId());
        return Boolean.TRUE;
    }


    @Override
    public IntendProcessTotalVO getIntendProcessTotal(Long lineId) {
        QualificationsInfoDO qualificationsInfoDO = qualificationsInfoDAO.getByLineId(lineId);
        if (qualificationsInfoDO==null){
            return null;
        }
        //总天数
        Integer totalDays = 10;
        IntendProcessTotalVO intendProcessTotalVO = new IntendProcessTotalVO();
        intendProcessTotalVO.setTotalDays(totalDays);
        intendProcessTotalVO.setCompletionTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.addDays(qualificationsInfoDO.getCreateTime(),totalDays)));

        List<Long> auditIds = new ArrayList<>();
        if (qualificationsInfoDO.getAuditId()!=null){
            auditIds.add(qualificationsInfoDO.getAuditId());
        }

        List<LineInterviewDO> interviewList = lineInterviewDAO.getInterviewByLineId(lineId);
        auditIds.addAll(interviewList.stream().filter(x->x.getAuditId()!=null).map(LineInterviewDO::getAuditId).collect(Collectors.toList()));
        Map<Integer, Long> interviewDOMap = interviewList.stream().filter(x->x.getAuditId()!=null).collect(Collectors.toMap(k -> k.getInterviewType(), v -> v.getAuditId()));
        interviewDOMap.put(10,qualificationsInfoDO.getAuditId());
        List<LineAuditInfoDO> lineAuditInfoList = lineAuditInfoDAO.getLineAuditInfoList(auditIds);
        Map<Long, Date> dateMap = lineAuditInfoList.stream().collect(Collectors.toMap(k -> k.getId(), v -> v.getCreateTime()));

        LinePayDO linePay = linePayDAO.getByLineIdAndPayTypeAndShopId(lineId,PayBusinessTypeEnum.INTENT_MONEY.getCode(),null);
        SigningBaseInfoDO signingBaseInfoDO = intentAgreementMapper.selectByPartnerIdOrLineId(qualificationsInfoDO.getPartnerId(), lineId);


        HashMap<Integer, Date> map = new HashMap<>();
        interviewDOMap.forEach((k, v) ->{
            if (k.equals(10)){
                map.put(WorkflowSubStageEnum.INTEND.getCode(),dateMap.get(v));
            }
        });
        if (linePay != null){
            map.put(WorkflowSubStageEnum.PAY_DEPOSIT.getCode(),linePay.getPayTime());
        }
        if (signingBaseInfoDO != null){
            map.put(WorkflowSubStageEnum.SIGN_INTENT_AGREEMENT.getCode(),signingBaseInfoDO.getCreateTime());
        }

        List<IntendProcessVO> list = new ArrayList<>();
        for (WorkflowSubStageEnum workflowSubStageEnum : WorkflowSubStageEnum.values()) {
            IntendProcessVO intend = getIntend(qualificationsInfoDO.getCreateTime(), workflowSubStageEnum);
            if (map.get(workflowSubStageEnum.getCode())!=null){
                intend.setActualTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,map.get(workflowSubStageEnum.getCode())));
            }
            list.add(intend);
        }
        intendProcessTotalVO.setIntendProcessVOS(list);
        log.info("intendProcessTotalVO:{}", JSONObject.toJSONString(intendProcessTotalVO));
        return intendProcessTotalVO;
    }



    @Override
    public Boolean addLine(AddLineRequest addLineRequest,LoginUserInfo userInfo) {
        //必填参数
        log.info("addLine:{}", JSONObject.toJSONString(addLineRequest));
        if (!StringUtil.isNoneBlank(addLineRequest.getMobile(),addLineRequest.getUserName())){
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        HyPartnerUserInfoDO hyPartnerUserInfoDO = hyPartnerUserInfoDAO.selectByMobile(addLineRequest.getMobile());
        if (hyPartnerUserInfoDO!=null){
            throw new ServiceException(ErrorCodeEnum.MOBILE_EXIST);
        }
        hyPartnerUserInfoDO = new HyPartnerUserInfoDO();
        hyPartnerUserInfoDO.setMobile(addLineRequest.getMobile());
        hyPartnerUserInfoDO.setUserChannelId(Integer.valueOf(UserChannelEnum.ADD.getCode()));
        hyPartnerUserInfoDO.setPartnerId(UUIDUtils.get32UUID());
        hyPartnerUserInfoDAO.insertSelective(hyPartnerUserInfoDO);
        LineInfoDO lineInfoDO = new LineInfoDO();
        lineInfoDO.setLineStatus(LineStatusEnum.PRIVATE_SEAS.getCode());
        lineInfoDO.setWorkflowSubStage(WorkflowSubStageEnum.INTEND.getCode());
        lineInfoDO.setMobile(addLineRequest.getMobile());
        lineInfoDO.setJoinMode(addLineRequest.getJoinMode());
        lineInfoDO.setWorkflowStage(WorkflowStageEnum.INTENT.getCode());
        lineInfoDO.setWorkflowSubStageStatus(WorkflowSubStageStatusEnum.INTENT_0.getCode());
        lineInfoDO.setWantShopAreaId(addLineRequest.getWantShopAreaId());
        lineInfoDO.setUsername(addLineRequest.getUserName());
        lineInfoDO.setLineSource(Integer.valueOf(UserChannelEnum.ADD.getCode()));
        lineInfoDO.setPartnerId(hyPartnerUserInfoDO.getPartnerId());
        if (addLineRequest.getJoinMode()==1){
            Long regionId = regionAreaConfigDao.getByWantShopAreaId(addLineRequest.getWantShopAreaId());
            lineInfoDO.setRegionId(regionId);
        }
        lineInfoDO.setInvestmentManager(getInvestmentManagerUserId(addLineRequest, userInfo));
        lineInfoDO.setCreateUserId(userInfo.getUserId());
        lineInfoDO.setUpdateUserId(userInfo.getUserId());
        lineInfoDAO.insertOrUpdate(lineInfoDO);
        return Boolean.TRUE;
    }

    /**
     * 获取招商经理id
     * @param addLineRequest
     * @return
     */
    private String getInvestmentManagerUserId(AddLineRequest addLineRequest,LoginUserInfo userInfo){
        //如果选择了招商经理 直接使用该招商经理
        if (StringUtils.isNotBlank(addLineRequest.getInvestmentManagerUserId())){
            return addLineRequest.getInvestmentManagerUserId();
        }
        //如果是强加盟 使用新建线索人员跟进线索
        if (CommonConstants.TWO==addLineRequest.getJoinMode()){
           return userInfo.getUserId();
        }
        EnterpriseUserDO user = userAuthMappingService.getUserByRoleEnumAndWantShopAreaId(UserRoleEnum.SUPERVISION, addLineRequest.getWantShopAreaId());
        return user.getUserId();
    }




    @Override
    public String getPayPicByLineId(Long lineId) {
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(lineId);
        if (lineInfo==null){
            throw new ServiceException(ErrorCodeEnum.LINE_ID_IS_NOT_EXIST);
        }
        Long bigRegionId = regionService.getBigRegionIdByAreaId(lineInfo.getWantShopAreaId());
        return regionQrcodeConfigDao.getPayPicByRegionId(bigRegionId);
    }

    /**
     * 计算预期时间
     * @param startTime
     * @param workflowSubStageEnum
     * @return
     */
    public IntendProcessVO getIntend(Date startTime, WorkflowSubStageEnum workflowSubStageEnum){
        IntendProcessVO intendProcessVO = new IntendProcessVO();
        intendProcessVO.setCode(workflowSubStageEnum.getCode());
        intendProcessVO.setExpectedTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.addDays(startTime,workflowSubStageEnum.getProgress())));
       return intendProcessVO;
    }

}
