package com.cool.store.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.*;
import com.cool.store.dto.InvestmentCountDTO;
import com.cool.store.dto.PendingCountDTO;
import com.cool.store.dto.DeskStageDTO;
import com.cool.store.dto.openPreparation.PlanLineDTO;
import com.cool.store.entity.*;
import com.cool.store.enums.WorkflowSubStageStatusEnum;
import com.cool.store.enums.point.PayBusinessTypeEnum;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.mapper.EnterpriseUserRoleMapper;
import com.cool.store.mapper.IntentAgreementMapper;
import com.cool.store.mapper.TrainingExperienceMapper;
import com.cool.store.service.DeskService;
import com.cool.store.service.RegionService;
import com.cool.store.service.SysRoleService;
import com.cool.store.service.UserAuthMappingService;
import com.cool.store.utils.StringUtil;
import com.cool.store.utils.poi.DateUtils;
import com.cool.store.vo.BaseInfoVO;
import com.cool.store.vo.LabelBaseInfoVO;
import com.cool.store.vo.desk.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import static com.cool.store.enums.point.ShopSubStageStatusEnum.*;
import static com.cool.store.utils.poi.DateUtils.YYYY_MM_DD_HH_MM_SS;

/**
 * @Author suzhuhong
 * @Date 2024/3/26 15:15
 * @Version 1.0
 */
@Service
public class DeskServiceImpl implements DeskService {

    @Resource
    LineInfoDAO lineInfoDAO;
    @Resource
    HyPartnerLabelDAO hyPartnerLabelDAO;
    @Resource
    HyOpenAreaInfoDAO hyOpenAreaInfoDAO;
    @Resource
    LineInterviewDAO lineInterviewDAO;
    @Resource
    LinePayDAO linePayDAO;
    @Resource
    IntentAgreementMapper intentAgreementMapper;
    @Resource
    TrainingExperienceMapper trainingExperienceMapper;
    @Resource
    ShopStageInfoDAO shopStageInfoDAO;
    @Resource
    ShopInfoDAO shopInfoDAO;
    @Resource
    EnterpriseUserDAO enterpriseUserDAO;
    @Resource
    RegionService regionService;
    @Resource
    SysRoleService sysRoleService;
    @Resource
    UserAuthMappingService userAuthMappingService;
    @Autowired
    private EnterpriseUserRoleMapper enterpriseUserRoleMapper;
    @Autowired
    private OperationLogDAO operationLogDAO;
    @Autowired
    private DecorationDesignInfoDAO decorationDesignInfoDAO;
    @Autowired
    private AcceptanceInfoDAO acceptanceInfoDAO;

    @Override
    public PageInfo<IntendPendingVO> intendPendingList(Integer pageNum, Integer pageSize, String userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<LineInfoDO> lineInfoDOS = lineInfoDAO.listByInvestmentManager(userId, Arrays.asList(WorkflowSubStageStatusEnum.INTENT_5.getCode()));
        PageInfo page = new PageInfo(lineInfoDOS);
        Map<Long, HyPartnerLabelDO> userPortraitMap = this.getUserPortraitMap(lineInfoDOS);
        List<Long> wantShopAreaIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
        Map<Long, String> wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIds);
        List<IntendPendingVO> list = new ArrayList<>();
        lineInfoDOS.forEach(x -> {
            BaseInfoVO baseInfoVO = convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
            IntendPendingVO intendPendingVO = new IntendPendingVO(baseInfoVO);
            intendPendingVO.setJoinTime(new Date());
            intendPendingVO.setJoinMode(x.getJoinMode());
            list.add(intendPendingVO);
        });

        page.setList(list);
        return page;
    }

    @Override
    public PageInfo<InterviewPendingVO> interviewPendingList(Integer pageNum, Integer pageSize, String userId) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<LineInfoDO> lineInfoDOS = lineInfoDAO.listByInvestmentManager(userId,Arrays.asList(WorkflowSubStageStatusEnum.INVITING_INTERVIEWS_20.getCode()));
//        PageInfo page = new PageInfo(lineInfoDOS);
//        Map<Long, HyPartnerLabelDO> userPortraitMap = this.getUserPortraitMap(lineInfoDOS);
//        List<Long> wantShopAreaIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
//        Map<Long, String>  wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIds);
//        List<Long> lineIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getId() != null).map(LineInfoDO::getId).collect(Collectors.toList());
//        List<LineInterviewDO> interviewByLindIds = lineInterviewDAO.getInterviewByLindIds(lineIds, InterviewTypeEnum.MEET);
//        Map<Long, LineInterviewDO> interviewDOMap = interviewByLindIds.stream().collect(Collectors.toMap(LineInterviewDO::getLineId, x -> x, (k1, k2) -> k1));
//
//        List<InterviewPendingVO> list = new ArrayList<>();
//        lineInfoDOS.forEach(x->{
//            BaseInfoVO baseInfoVO = convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
//            InterviewPendingVO interviewPendingVO = new InterviewPendingVO(baseInfoVO);
//            LineInterviewDO lineInterviewDO = interviewDOMap.get(x.getId());
//            if (lineInterviewDO != null){
//                interviewPendingVO.setStartTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START,lineInterviewDO.getStartTime()));
//                interviewPendingVO.setEndTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_END,lineInterviewDO.getEndTime()));
//            }
//            list.add(interviewPendingVO);
//        });
//        page.setList(list);
        return new PageInfo<>();
    }

    @Override
    public PageInfo<InterviewPendingVO> firstInterviewPendingList(Integer pageNum, Integer pageSize, LoginUserInfo userInfo) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<LineInfoDO> lineInfoDOS = lineInfoDAO.listByInterview(userInfo.getUserId(),InterviewTypeEnum.INTERVIEW.getCode(),
//                Arrays.asList(WorkflowSubStageStatusEnum.FIRST_INTERVIEWS_30.getCode(),WorkflowSubStageStatusEnum.FIRST_INTERVIEWS_35.getCode()),WorkflowSubStageStatusEnum.FIRST_INTERVIEWS_40.getCode(),null);
//        PageInfo page = new PageInfo(lineInfoDOS);
//        Map<Long, HyPartnerLabelDO> userPortraitMap = this.getUserPortraitMap(lineInfoDOS);
//        List<Long> wantShopAreaIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
//        Map<Long, String>  wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIds);
//        List<Long> lineIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getId() != null).map(LineInfoDO::getId).collect(Collectors.toList());
//        List<LineInterviewDO> interviewByLindIds = lineInterviewDAO.getInterviewByLindIds(lineIds, InterviewTypeEnum.INTERVIEW);
//        Map<Long, LineInterviewDO> interviewDOMap = interviewByLindIds.stream().collect(Collectors.toMap(LineInterviewDO::getLineId, x -> x, (k1, k2) -> k1));
//        List<String> userIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getFirstInterviewer() != null).map(LineInfoDO::getFirstInterviewer).collect(Collectors.toList());
//        Map<String, EnterpriseUserDO> userInfoMap = enterpriseUserDAO.getUserInfoMap(userIds);
//
//        List<InterviewPendingVO> list = new ArrayList<>();
//        lineInfoDOS.forEach(x->{
//            BaseInfoVO baseInfoVO = convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
//            InterviewPendingVO interviewPendingVO = new InterviewPendingVO(baseInfoVO);
//            LineInterviewDO lineInterviewDO = interviewDOMap.get(x.getId());
//            if (lineInterviewDO != null){
//                interviewPendingVO.setStartTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START,lineInterviewDO.getStartTime()));
//                interviewPendingVO.setEndTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_END,lineInterviewDO.getEndTime()));
//                interviewPendingVO.setInterviewer(userInfoMap.getOrDefault(x.getFirstInterviewer(),new EnterpriseUserDO()).getName());
//                interviewPendingVO.setInterviewId(lineInterviewDO.getId());
//            }
//            list.add(interviewPendingVO);
//        });
//        page.setList(list);
        return new PageInfo<>();
    }


    @Override
    public PageInfo<InterviewPendingVO> secondInterviewPendingList(Integer pageNum, Integer pageSize, LoginUserInfo userInfo) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<LineInfoDO> lineInfoDOS = lineInfoDAO.listByInterview(userInfo.getUserId(),InterviewTypeEnum.SECOND_INTERVIEW.getCode(),
//                Arrays.asList(WorkflowSubStageStatusEnum.SECOND_INTERVIEWS_105.getCode(),WorkflowSubStageStatusEnum.SECOND_INTERVIEWS_110.getCode()), WorkflowSubStageStatusEnum.SECOND_INTERVIEWS_115.getCode(),null);
//        PageInfo page = new PageInfo(lineInfoDOS);
//        Map<Long, HyPartnerLabelDO> userPortraitMap = this.getUserPortraitMap(lineInfoDOS);
//        List<Long> wantShopAreaIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
//        Map<Long, String>  wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIds);
//        List<Long> lineIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getId() != null).map(LineInfoDO::getId).collect(Collectors.toList());
//        List<LineInterviewDO> interviewByLindIds = lineInterviewDAO.getInterviewByLindIds(lineIds, InterviewTypeEnum.SECOND_INTERVIEW);
//        Map<Long, LineInterviewDO> interviewDOMap = interviewByLindIds.stream().collect(Collectors.toMap(LineInterviewDO::getLineId, x -> x, (k1, k2) -> k1));
//        List<String> userIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getSecondInterviewer() != null).map(LineInfoDO::getSecondInterviewer).collect(Collectors.toList());
//        Map<String, EnterpriseUserDO> userInfoMap = enterpriseUserDAO.getUserInfoMap(userIds);
//
//
//        List<InterviewPendingVO> list = new ArrayList<>();
//        lineInfoDOS.forEach(x->{
//            BaseInfoVO baseInfoVO = convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
//            InterviewPendingVO interviewPendingVO = new InterviewPendingVO(baseInfoVO);
//            LineInterviewDO lineInterviewDO = interviewDOMap.get(x.getId());
//            if (lineInterviewDO != null){
//                interviewPendingVO.setStartTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START,lineInterviewDO.getStartTime()));
//                interviewPendingVO.setEndTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_END,lineInterviewDO.getEndTime()));
//                interviewPendingVO.setInterviewer(userInfoMap.getOrDefault(x.getSecondInterviewer(),new EnterpriseUserDO()).getName());
//                interviewPendingVO.setInterviewId(lineInterviewDO.getId());
//            }
//            list.add(interviewPendingVO);
//        });
//        page.setList(list);
        return new PageInfo<>();
    }

    @Override
    public PageInfo<PayStagePendingVO> payStagePendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        PageHelper.startPage(pageNum, pageSize);
        List<LineInfoDO> lineInfoDOS = lineInfoDAO.listByInvestmentManager(user.getUserId(), Arrays.asList(WorkflowSubStageStatusEnum.PAY_DEPOSIT_50.getCode()));
        PageInfo page = new PageInfo(lineInfoDOS);
        Map<Long, HyPartnerLabelDO> userPortraitMap = this.getUserPortraitMap(lineInfoDOS);
        List<Long> wantShopAreaIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
        Map<Long, String> wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIds);
        List<Long> lineIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getId() != null).map(LineInfoDO::getId).collect(Collectors.toList());
        Map<Long, LinePayDO> payMap = linePayDAO.getLinePayByLineIds(lineIds, PayBusinessTypeEnum.INTENT_MONEY.getCode());
        List<PayStagePendingVO> list = new ArrayList<>();
        lineInfoDOS.forEach(x -> {
            BaseInfoVO baseInfoVO = convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
            PayStagePendingVO payStagePendingVO = new PayStagePendingVO(baseInfoVO);
            LinePayDO linePayDO = payMap.get(x.getId());
            if (linePayDO != null && linePayDO.getPayTime() != null) {
                payStagePendingVO.setPayTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START, linePayDO.getPayTime()));
            }

            list.add(payStagePendingVO);
        });
        page.setList(list);
        return page;
    }

    @Override
    public PageInfo<SigningPendingVO> signingPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        PageHelper.startPage(pageNum, pageSize);
        List<LineInfoDO> lineInfoDOS = lineInfoDAO.listByInvestmentManager(user.getUserId(),
                Arrays.asList(WorkflowSubStageStatusEnum.SIGN_INTENT_AGREEMENT_70.getCode()));
        PageInfo page = new PageInfo(lineInfoDOS);
        Map<Long, HyPartnerLabelDO> userPortraitMap = this.getUserPortraitMap(lineInfoDOS);
        List<Long> wantShopAreaIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
        Map<Long, String> wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIds);
        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<SigningPendingVO> list = new ArrayList<>();
        lineInfoDOS.forEach(x -> {
            BaseInfoVO baseInfoVO = convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
            SigningPendingVO signingPendingVO = new SigningPendingVO(baseInfoVO);
            Date date = dateMap.get(x.getId());
            if (date != null) {
                signingPendingVO.setSubmitTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START, date));
            }
            list.add(signingPendingVO);
        });
        page.setList(list);
        return page;
    }

    @Override
    public PageInfo<StoreExperiencePendingVO> storeExperiencePendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<LineInfoDO> lineInfoDOS = lineInfoDAO.listByInvestmentManager(user.getUserId(),
//                Arrays.asList(WorkflowSubStageStatusEnum.STORE_EXPERIENCE_85.getCode(),
//                        WorkflowSubStageStatusEnum.STORE_EXPERIENCE_95.getCode()));
//        PageInfo page = new PageInfo(lineInfoDOS);
//        Map<Long, HyPartnerLabelDO> userPortraitMap = this.getUserPortraitMap(lineInfoDOS);
//        List<Long> wantShopAreaIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getWantShopAreaId() != null).map(LineInfoDO::getWantShopAreaId).collect(Collectors.toList());
//        Map<Long, String>  wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaIds);
//        List<Long> lineIds = lineInfoDOS.stream().filter(lineInfoDO -> lineInfoDO.getId() != null).map(LineInfoDO::getId).collect(Collectors.toList());
//        Map<Long, LeaseBaseInfoDO> infoDOMap = new HashMap<>();
//        if (CollectionUtils.isNotEmpty(lineIds)){
//            List<LeaseBaseInfoDO> leaseBaseInfoDOS = trainingExperienceMapper.selectByLineIds(lineIds);
//            infoDOMap = leaseBaseInfoDOS.stream().collect(Collectors.toMap(LeaseBaseInfoDO::getLineId, x -> x));
//        }
//        List<StoreExperiencePendingVO> list = new ArrayList<>();
//        Map<Long, LeaseBaseInfoDO> finalInfoDOMap = infoDOMap;
//        lineInfoDOS.forEach(x->{
//            BaseInfoVO baseInfoVO = convertToBaseInfoVO(x, userPortraitMap, wantShopAreaMap);
//            StoreExperiencePendingVO storeExperiencePendingVO = new StoreExperiencePendingVO(baseInfoVO);
//            LeaseBaseInfoDO date = finalInfoDOMap.get(x.getId());
//            if (date != null && date.getExperienceStartTime() != null && date.getExperienceEndTime() != null){
//                storeExperiencePendingVO.setExperienceStartTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_1,date.getExperienceStartTime()));
//                storeExperiencePendingVO.setExperienceEndTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START_2,date.getExperienceEndTime()));
//            }
//            list.add(storeExperiencePendingVO);
//        });
//        page.setList(list);
        return new PageInfo<>();
    }


    /**
     * convertToBaseInfoVO
     *
     * @param lineInfoDO
     * @param userPortraitMap
     * @param wantShopAreaMap
     * @return
     */
    @Override
    public BaseInfoVO convertToBaseInfoVO(LineInfoDO lineInfoDO, Map<Long, HyPartnerLabelDO> userPortraitMap, Map<Long, String> wantShopAreaMap) {
        BaseInfoVO baseInfoVO = new BaseInfoVO();
        BeanUtil.copyProperties(lineInfoDO, baseInfoVO);
        baseInfoVO.setLineId(lineInfoDO.getId());
        String userPortrait = lineInfoDO.getUserPortrait();
        List<LabelBaseInfoVO> labelBaseInfoList = new ArrayList<>();
        if (StringUtil.isNotEmpty(userPortrait)) {
            Arrays.asList(userPortrait.split(",")).subList(1, userPortrait.split(",").length).forEach(x -> {
                LabelBaseInfoVO labelBaseInfoVO = new LabelBaseInfoVO();
                HyPartnerLabelDO hyPartnerLabelDO = userPortraitMap.get(Long.valueOf(x));
                if (hyPartnerLabelDO != null) {
                    labelBaseInfoVO.setLabelId(userPortraitMap.getOrDefault(Long.valueOf(x), new HyPartnerLabelDO()).getId());
                    labelBaseInfoVO.setLabelName(userPortraitMap.getOrDefault(Long.valueOf(x), new HyPartnerLabelDO()).getLabelName());
                    labelBaseInfoList.add(labelBaseInfoVO);
                }
            });
        }
        baseInfoVO.setUserPortraitList(labelBaseInfoList);
        baseInfoVO.setWantShopAreaName(wantShopAreaMap.get(lineInfoDO.getWantShopAreaId()));
        return baseInfoVO;
    }


    /**
     * 用户画像MAP
     *
     * @param lineInfoDOList
     * @return
     */
    @Override
    public Map<Long, HyPartnerLabelDO> getUserPortraitMap(List<LineInfoDO> lineInfoDOList) {
        List<Long> libelIds = new ArrayList<>();
        lineInfoDOList.stream().forEach(x -> {
            if (StringUtil.isNotEmpty(x.getUserPortrait())) {
                List<String> list = Arrays.asList(x.getUserPortrait().split(",")).subList(1, x.getUserPortrait().split(",").length);
                libelIds.addAll(list.stream().map(Long::valueOf).collect(Collectors.toList()));
            }
        });
        List<HyPartnerLabelDO> hyPartnerLabelDOS = hyPartnerLabelDAO.listByIds(libelIds);
        if (CollectionUtils.isEmpty(hyPartnerLabelDOS)) {
            return new HashMap<>();
        }
        return hyPartnerLabelDOS.stream().collect(Collectors.toMap(HyPartnerLabelDO::getId, x -> x));
    }

    @Override
    public PendingCountDTO getPendingCount(LoginUserInfo user) {
        PendingCountDTO pendingCount = lineInfoDAO.pendingCount(user.getUserId());
        return pendingCount;
    }

    @Override
    public InvestmentCountDTO getInvestmentCount(LoginUserInfo user) {
        return lineInfoDAO.investmentData(user.getUserId());
    }


    @Override
    public PageInfo<PreparationCommonPendingVO> systemBuildStorePendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return null;
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> payFranchiseFeesPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, user, ShopSubStageEnum.SHOP_STAGE_7,
                Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_70.getShopSubStageStatus(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_72.getShopSubStageStatus()), Boolean.FALSE);
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> signingOfFranchiseContractPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, user, ShopSubStageEnum.SHOP_STAGE_8,
                Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_80.getShopSubStageStatus(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_85.getShopSubStageStatus()), Boolean.TRUE);
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> openingAndOperationPlanPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, user, ShopSubStageEnum.SHOP_STAGE_14, Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_140.getShopSubStageStatus()), Boolean.FALSE);
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> firstOrderListPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return null;
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> trainListPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
      return   commonPendingVOPageInfo(pageNum, pageSize, user, ShopSubStageEnum.SHOP_STAGE_5,
                Collections.singletonList(SHOP_SUB_STAGE_STATUS_50.getShopSubStageStatus()), Boolean.TRUE);
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> xinFaListPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, null, ShopSubStageEnum.SHOP_STAGE_23,
                Collections.singletonList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_230.getShopSubStageStatus()), Boolean.FALSE);

    }

    @Override
    public PageInfo<PreparationCommonPendingVO> posListPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, null, ShopSubStageEnum.SHOP_STAGE_16,
                Collections.singletonList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_160.getShopSubStageStatus()), Boolean.FALSE);

    }

    @Override
    public PageInfo<PreparationCommonPendingVO> tentPassListPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, null, ShopSubStageEnum.SHOP_STAGE_24,
                Collections.singletonList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_240.getShopSubStageStatus()), Boolean.FALSE);

    }

    @Override
    public PageInfo<PreparationCommonPendingVO> orderSysPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, null, ShopSubStageEnum.SHOP_STAGE_17,
                Collections.singletonList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_170.getShopSubStageStatus()), Boolean.FALSE);
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> designPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, user, ShopSubStageEnum.SHOP_STAGE_9,
                Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_90.getShopSubStageStatus()), Boolean.TRUE);
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> constructionPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, user, ShopSubStageEnum.SHOP_STAGE_11,
                Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_110.getShopSubStageStatus(),
                        ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_111.getShopSubStageStatus()), Boolean.TRUE);
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> fitmentPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPendingVOPageInfo(pageNum, pageSize, user, ShopSubStageEnum.SHOP_STAGE_12,
                Collections.singletonList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_122.getShopSubStageStatus()), Boolean.FALSE);
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> DYPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPlatformBuild(pageNum, pageSize, user, Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_181.getShopSubStageStatus()));
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> KSPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPlatformBuild(pageNum, pageSize, user, Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_221.getShopSubStageStatus()));
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> MTPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPlatformBuild(pageNum, pageSize, user, Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_191.getShopSubStageStatus(), SHOP_SUB_STAGE_STATUS_193.getShopSubStageStatus()));
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> MTBuyPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPlatformBuild(pageNum, pageSize, user, Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_201.getShopSubStageStatus()));
    }

    @Override
    public PageInfo<PreparationCommonPendingVO> ELMEPendingList(Integer pageNum, Integer pageSize, LoginUserInfo user) {
        return commonPlatformBuild(pageNum, pageSize, user, Arrays.asList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_211.getShopSubStageStatus(), SHOP_SUB_STAGE_STATUS_213.getShopSubStageStatus()));
    }


    /**
     * 通用查询
     *
     * @param pageNum
     * @param pageSize
     * @param user
     * @param shopSubStageEnum
     * @param subStageStatusList
     * @return
     */
    private PageInfo<PreparationCommonPendingVO> commonPendingVOPageInfo(Integer pageNum, Integer pageSize, LoginUserInfo user, ShopSubStageEnum shopSubStageEnum, List<Integer> subStageStatusList, Boolean filterFlag) {
        //user.getJobNumber()
        List<String> authRegionIds = new ArrayList<>();
        if (filterFlag) {
            if (!sysRoleService.checkIsAdmin(user.getUserId())) {
                authRegionIds = userAuthMappingService.getAuthRegionIdAndSubRegionIdByUserId(user.getUserId());
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<ShopStageInfoDO> specialShopStageInfo = shopStageInfoDAO.getSpecialShopStageInfo(null, shopSubStageEnum.getShopSubStage(),
                subStageStatusList, user == null ? null : user.getUserId(), authRegionIds);
        PageInfo result = new PageInfo<>(specialShopStageInfo);
        List<Long> shopIds = specialShopStageInfo.stream().map(ShopStageInfoDO::getShopId).collect(Collectors.toList());
        List<Long> lineIds = specialShopStageInfo.stream().map(ShopStageInfoDO::getLineId).collect(Collectors.toList());
        List<ShopInfoDO> shopInfoList = shopInfoDAO.getShopListByIds(shopIds);
        List<PlanLineDTO> lines = lineInfoDAO.getLines(lineIds);

        //将shopInfoList 转为map
        Map<Long, ShopInfoDO> shopInfoMap = shopInfoList.stream().collect(Collectors.toMap(ShopInfoDO::getId, shop -> shop));
        //将lines 转为map
        Map<Long, PlanLineDTO> lineMap = lines.stream().collect(Collectors.toMap(PlanLineDTO::getLineId, line -> line));


        List<Long> regionIds = shopInfoList.stream().map(ShopInfoDO::getRegionId).distinct().collect(Collectors.toList());
        List<String> developmentManagers = shopInfoList.stream().filter(x -> StringUtil.isNotEmpty(x.getShopManagerUserId())).map(ShopInfoDO::getShopManagerUserId).distinct().collect(Collectors.toList());
        Map<String, String> userNameMap = enterpriseUserDAO.getUserNameMap(developmentManagers);
        Map<Long, String> regionNameMap = regionService.getBelongWarRegionNameMap(regionIds);


        Map<Long, LinePayDO> shopPayDoMap = new HashMap<>(16);
        if (ShopSubStageEnum.SHOP_STAGE_7.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())) {
            shopPayDoMap = linePayDAO.getLinePayByShopIds(shopIds, PayBusinessTypeEnum.FRANCHISE_FEE.getCode());
        }
        Map<Long, List<DeskStageDTO>> deskStageMap = new HashMap<>();
        if (ShopSubStageEnum.SHOP_STAGE_16.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())
                || ShopSubStageEnum.SHOP_STAGE_17.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())
                || ShopSubStageEnum.SHOP_STAGE_9.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())
                || ShopSubStageEnum.SHOP_STAGE_23.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())
                || ShopSubStageEnum.SHOP_STAGE_24.getShopSubStage().equals(shopSubStageEnum.getShopSubStage()) ){
            List<ShopStageInfoDO> subStageList = shopStageInfoDAO.getSubStages(shopIds, ShopSubStageEnum.SHOP_STAGE_8.getShopSubStage());
            doThing(deskStageMap, subStageList);
        } else if (ShopSubStageEnum.SHOP_STAGE_8.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())) {
            List<ShopStageInfoDO> subStageList = shopStageInfoDAO.getSubStages(shopIds, ShopSubStageEnum.SHOP_STAGE_7.getShopSubStage());
            doThing(deskStageMap, subStageList);
        } else if (ShopSubStageEnum.SHOP_STAGE_11.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())) {
            List<ShopStageInfoDO> subStageList = shopStageInfoDAO.getSubStages(shopIds, ShopSubStageEnum.SHOP_STAGE_8.getShopSubStage());
            List<DecorationDesignInfoDO> decorationDesignInfos = decorationDesignInfoDAO.getByIds(shopIds);
            doThing(deskStageMap, subStageList);
            for (DecorationDesignInfoDO decorationDesignInfoDO : decorationDesignInfos) {
                List<DeskStageDTO> deskStages = new ArrayList<>();
                deskStages.addAll(deskStageMap.get(decorationDesignInfoDO.getShopId()));
                if (CollectionUtils.isEmpty(deskStages)) {
                    DeskStageDTO dto = new DeskStageDTO();
                    dto.setActualCompleteTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START, decorationDesignInfoDO.getCreateTime()));
                    dto.setShopSubStageStatus(SHOP_SUB_STAGE_STATUS_111.getShopSubStageStatus());
                    deskStageMap.put(decorationDesignInfoDO.getShopId(), Collections.singletonList(dto));
                } else {
                    DeskStageDTO dto = new DeskStageDTO();
                    dto.setActualCompleteTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START, decorationDesignInfoDO.getCreateTime()));
                    dto.setShopSubStageStatus(SHOP_SUB_STAGE_STATUS_111.getShopSubStageStatus());
                    deskStages.add(dto);
                    deskStageMap.put(decorationDesignInfoDO.getShopId(), deskStages);
                }
            }
        } else if (ShopSubStageEnum.SHOP_STAGE_12.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())) {
            List<AcceptanceInfoDO> list = acceptanceInfoDAO.selectByShopIds(shopIds);
            for (AcceptanceInfoDO acceptanceInfoDO : list) {
                DeskStageDTO dto = new DeskStageDTO();
                dto.setActualCompleteTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START, acceptanceInfoDO.getCreateTime()));
                dto.setShopSubStageStatus(SHOP_SUB_STAGE_STATUS_122.getShopSubStageStatus());
                deskStageMap.put(acceptanceInfoDO.getShopId(), Collections.singletonList(dto));
            }
        }
        List<PreparationCommonPendingVO> list = new ArrayList<>();
        Map<Long, LinePayDO> finalShopPayDoMap = shopPayDoMap;
        specialShopStageInfo.forEach(x -> {
            PreparationCommonPendingVO preparationCommonPendingVO = new PreparationCommonPendingVO();
            preparationCommonPendingVO.setLineId(x.getLineId());
            preparationCommonPendingVO.setShopId(x.getShopId());
            preparationCommonPendingVO.setStage(x.getShopStage());
            preparationCommonPendingVO.setSubStage(x.getShopSubStage());
            preparationCommonPendingVO.setSubStageStatus(x.getShopSubStageStatus());
            ShopInfoDO shopInfoDO = shopInfoMap.getOrDefault(x.getShopId(), new ShopInfoDO());
            if (ShopSubStageEnum.SHOP_STAGE_7.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())) {
                preparationCommonPendingVO.setSubmitTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START, finalShopPayDoMap.getOrDefault(x.getShopId(), new LinePayDO()).getCreateTime()));
            }
            if (ShopSubStageEnum.SHOP_STAGE_8.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())) {
                preparationCommonPendingVO.setSubmitTime(deskStageMap.get(x.getShopId()).get(0).getActualCompleteTime());
            }
            if (ShopSubStageEnum.SHOP_STAGE_16.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())
                    || ShopSubStageEnum.SHOP_STAGE_17.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())
                    || ShopSubStageEnum.SHOP_STAGE_9.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())
                    || ShopSubStageEnum.SHOP_STAGE_23.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())
                    || ShopSubStageEnum.SHOP_STAGE_24.getShopSubStage().equals(shopSubStageEnum.getShopSubStage()) ){
                preparationCommonPendingVO.setSubmitTime(deskStageMap.get(x.getShopId()).get(0).getActualCompleteTime());
            }
            if (ShopSubStageEnum.SHOP_STAGE_11.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())) {
                for (DeskStageDTO deskStageDTO : deskStageMap.get(x.getShopId())) {
                    if (x.getShopSubStageStatus().equals(deskStageDTO.getShopSubStageStatus())) {
                        preparationCommonPendingVO.setSubmitTime(deskStageDTO.getActualCompleteTime());
                        break;
                    }
                }
            }
            if (ShopSubStageEnum.SHOP_STAGE_12.getShopSubStage().equals(shopSubStageEnum.getShopSubStage())) {
                for (DeskStageDTO deskStageDTO : deskStageMap.get(x.getShopId())) {
                    if (x.getShopSubStageStatus().equals(deskStageDTO.getShopSubStageStatus())) {
                        preparationCommonPendingVO.setSubmitTime(deskStageDTO.getActualCompleteTime());
                        break;
                    }
                }
            }
            preparationCommonPendingVO.setStoreName(shopInfoDO.getShopName());
            PlanLineDTO planLineDTO = lineMap.getOrDefault(x.getLineId(), new PlanLineDTO());
            preparationCommonPendingVO.setPartnerName(planLineDTO.getUsername());
            preparationCommonPendingVO.setPartnerPhone(planLineDTO.getMobile());
            preparationCommonPendingVO.setShopManagerUserName(userNameMap.getOrDefault(shopInfoDO.getShopManagerUserId(), ""));
            preparationCommonPendingVO.setRegionNodeName(regionNameMap.getOrDefault(shopInfoDO.getRegionId(), ""));
            list.add(preparationCommonPendingVO);
        });
        result.setList(list);
        return result;
    }

    private void doThing(Map<Long, List<DeskStageDTO>> deskStageMap, List<ShopStageInfoDO> subStageList) {
        for (ShopStageInfoDO shopStageInfoDO : subStageList) {
            List<DeskStageDTO> deskStageDTOS = deskStageMap.get(shopStageInfoDO.getShopId());
            if (CollectionUtils.isEmpty(deskStageDTOS)) {
                DeskStageDTO dto = new DeskStageDTO();
                Date date = DateUtils.dateTime(YYYY_MM_DD_HH_MM_SS, shopStageInfoDO.getActualCompleteTime());
                dto.setActualCompleteTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START, date));
                dto.setShopSubStageStatus(shopStageInfoDO.getShopSubStageStatus());
                deskStageMap.put(shopStageInfoDO.getShopId(), Collections.singletonList(dto));
            } else {
                DeskStageDTO dto = new DeskStageDTO();
                Date date = DateUtils.dateTime(YYYY_MM_DD_HH_MM_SS, shopStageInfoDO.getActualCompleteTime());
                dto.setActualCompleteTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START, date));
                dto.setShopSubStageStatus(shopStageInfoDO.getShopSubStageStatus());
                deskStageDTOS.add(dto);
                deskStageMap.put(shopStageInfoDO.getShopId(), deskStageDTOS);
            }
        }
    }

    //平台建店通用查询
    private PageInfo<PreparationCommonPendingVO> commonPlatformBuild(Integer pageNum, Integer pageSize, LoginUserInfo user, List<Integer> subStageStatusList) {
        PageHelper.startPage(pageNum, pageSize);
        List<OperationLogDO> operationLogDOList = operationLogDAO.getBySubStageStatusAndUser(user.getUserId(), subStageStatusList);
        if (CollectionUtils.isEmpty(operationLogDOList)) {
            return new PageInfo<>();
        }
        PageInfo result = new PageInfo<>(operationLogDOList);
        List<Long> shopIds = operationLogDOList.stream().map(OperationLogDO::getShopId).collect(Collectors.toList());
        List<ShopInfoDO> shopInfoList = shopInfoDAO.getShopListByIds(shopIds);
        List<Long> lineIds = shopInfoList.stream().map(ShopInfoDO::getLineId).collect(Collectors.toList());
        List<PlanLineDTO> lines = lineInfoDAO.getLines(lineIds);


        //将shopInfoList 转为map
        Map<Long, ShopInfoDO> shopInfoMap = shopInfoList.stream().collect(Collectors.toMap(ShopInfoDO::getId, shop -> shop));
        //将lines 转为map
        Map<Long, PlanLineDTO> lineMap = lines.stream().collect(Collectors.toMap(PlanLineDTO::getLineId, line -> line));

        List<Long> regionIds = shopInfoList.stream().map(ShopInfoDO::getRegionId).distinct().collect(Collectors.toList());
        List<String> developmentManagers = shopInfoList.stream().filter(x -> StringUtil.isNotEmpty(x.getShopManagerUserId())).map(ShopInfoDO::getShopManagerUserId).distinct().collect(Collectors.toList());
        Map<String, String> userNameMap = enterpriseUserDAO.getUserNameMap(developmentManagers);
        Map<Long, String> regionNameMap = regionService.getBelongWarRegionNameMap(regionIds);

        List<PreparationCommonPendingVO> list = new ArrayList<>();
        operationLogDOList.forEach(x -> {
            PreparationCommonPendingVO preparationCommonPendingVO = new PreparationCommonPendingVO();
            ShopInfoDO shopInfoDO = shopInfoMap.getOrDefault(x.getShopId(), new ShopInfoDO());
            preparationCommonPendingVO.setLineId(shopInfoDO.getLineId());
            preparationCommonPendingVO.setShopId(x.getShopId());
            preparationCommonPendingVO.setSubStage(x.getShopSubStage());
            preparationCommonPendingVO.setSubStageStatus(x.getShopSubStageStatus());
            preparationCommonPendingVO.setSubmitTime(DateUtils.parseDateToStr(DateUtils.SPECIAL_DATE_START, x.getCreateTime()));
            preparationCommonPendingVO.setStoreName(shopInfoDO.getShopName());
            PlanLineDTO planLineDTO = lineMap.getOrDefault(shopInfoDO.getLineId(), new PlanLineDTO());
            preparationCommonPendingVO.setPartnerName(planLineDTO.getUsername());
            preparationCommonPendingVO.setPartnerPhone(planLineDTO.getMobile());
            preparationCommonPendingVO.setShopManagerUserName(userNameMap.getOrDefault(shopInfoDO.getShopManagerUserId(), ""));
            preparationCommonPendingVO.setRegionNodeName(regionNameMap.getOrDefault(shopInfoDO.getRegionId(), ""));
            list.add(preparationCommonPendingVO);
        });
        result.setList(list);
        return result;
    }
}
