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.constants.RedisConstant;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.context.PartnerUserHolder;
import com.cool.store.dao.*;
import com.cool.store.dto.log.LineLogInfo;
import com.cool.store.dto.log.UserInfoUpdateDTO;
import com.cool.store.dto.log.WantInfoUpdateDTO;
import com.cool.store.dto.partner.PartnerIntentApplyInfoDTO;
import com.cool.store.entity.*;
import com.cool.store.enums.*;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.BaseUserInfoRequest;
import com.cool.store.request.IndustryCognitionInfoRequest;
import com.cool.store.request.PartnerIntentInfoRequest;
import com.cool.store.request.PartnerWantShopInfoRequest;
import com.cool.store.service.HyPartnerIntentInfoService;
import com.cool.store.service.HyPartnerLineInfoService;
import com.cool.store.service.HyPhoneLocationService;
import com.cool.store.service.LogService;
import com.cool.store.utils.CoolDateUtils;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.utils.StringUtil;
import com.cool.store.vo.PartnerIntentApplyInfoVO;
import com.cool.store.vo.PartnerIntentInfoVO;
import com.cool.store.vo.PartnerUserInfoVO;
import com.cool.store.vo.WantShopInfoVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author suzhuhong
 * @Date 2023/6/9 15:00
 * @Version 1.0
 */
@Slf4j
@Service
public class HyPartnerIntentInfoServiceImpl implements HyPartnerIntentInfoService {

    @Resource
    HyPartnerIntentInfoDAO hyPartnerIntentInfoDAO;
    @Resource
    HyPartnerUserInfoDAO hyPartnerUserInfoDAO;
    @Resource
    HyPartnerLineInfoDAO hyPartnerLineInfoDAO;
    @Resource
    HyPartnerBaseInfoDAO hyPartnerBaseInfoDAO;
    @Resource
    private RedisUtilPool redisUtilPool;
    @Resource
    HyOpenAreaInfoDAO hyOpenAreaInfoDAO;
    @Resource
    HyPartnerLineInfoService hyPartnerLineInfoService;
    @Resource
    HyPhoneLocationService hyPhoneLocationService;
    @Resource
    HyPartnerTaskInfoLogDAO hyPartnerTaskInfoLogDAO;
    @Autowired
    private LogService logService;

    @Autowired
    private NoticeService noticeService;

    @Override
    public PageInfo<PartnerIntentApplyInfoVO> getPartnerIntentApplyList(String userId, String type, Integer pageSize, Integer pageNumber) {
        PartnerIntentApplyInfoVO partnerIntentApplyInfoVO = new PartnerIntentApplyInfoVO();
        String workflowStatus = "";
        if (CommonConstants.PENDING.equals(type)) {
            workflowStatus = WorkflowStatusEnum.INTENT_1.getCode();
        }
        if (CommonConstants.FOLLOW.equals(type)) {
            workflowStatus = WorkflowStatusEnum.INTENT_0.getCode();
        }
        PageHelper.startPage(pageNumber,pageSize);
        PageInfo partnerIntentApplyInfo = new PageInfo(hyPartnerIntentInfoDAO.selectPartnerIntentApplyInfoList(userId, WorkflowStageEnum.INTENT.getCode(), workflowStatus));
        if (partnerIntentApplyInfo==null){
            return new PageInfo<>();
        }
        List<PartnerIntentApplyInfoDTO> list = partnerIntentApplyInfo.getList();
        List<String> partnerIds = list.stream().map(PartnerIntentApplyInfoDTO::getPartnerId).collect(Collectors.toList());
        List<HyPartnerUserInfoDO> hyPartnerUserInfoDOS = hyPartnerUserInfoDAO.selectByPartnerIds(partnerIds);
        Map<String, HyPartnerUserInfoDO> infoDOMap = hyPartnerUserInfoDOS.stream().collect(Collectors.toMap(HyPartnerUserInfoDO::getPartnerId, data -> data));
        List<Long> wantShopAreaList = list.stream().filter(x->StringUtils.isNotEmpty(x.getWantShopArea())).map(PartnerIntentApplyInfoDTO::getWantShopArea).map(Long::parseLong).distinct().collect(Collectors.toList());
        Map<String, String> wantShopAreaNameMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantShopAreaList);
        List<PartnerIntentApplyInfoVO> resultList = PartnerIntentApplyInfoVO.convertList(list, infoDOMap, wantShopAreaNameMap, workflowStatus);
        partnerIntentApplyInfo.setList(resultList);
        return partnerIntentApplyInfo;
    }

    @Override
    public Boolean updatePartnerIntentInfo(LoginUserInfo userInfo, BaseUserInfoRequest baseUserInfoRequest) {

        HyPartnerUserInfoDO hyPartnerUserInfoDO = hyPartnerUserInfoDAO.selectByPartnerId(String.valueOf(baseUserInfoRequest.getPartnerId()));
        if (hyPartnerUserInfoDO==null){
            throw new ServiceException(ErrorCodeEnum.PARTNER_USER_NOT_EXIST);
        }
        UserInfoUpdateDTO.UserInfoUpdate beforeUserinfoUpdate = new UserInfoUpdateDTO.UserInfoUpdate(hyPartnerUserInfoDO.getUsername(), hyPartnerUserInfoDO.getMobile(), hyPartnerUserInfoDO.getLiveArea(), hyPartnerUserInfoDO.getWantShopArea(), hyPartnerUserInfoDO.getAcceptAdjustType());
        hyPartnerUserInfoDO.setUsername(baseUserInfoRequest.getUsername());
        hyPartnerUserInfoDO.setMobile(baseUserInfoRequest.getMobile());
        hyPartnerUserInfoDO.setLiveArea(baseUserInfoRequest.getLiveArea());
        hyPartnerUserInfoDO.setWantShopArea(baseUserInfoRequest.getWantShopArea());
        hyPartnerUserInfoDO.setAcceptAdjustType(baseUserInfoRequest.getAcceptAdjustType());
        hyPartnerUserInfoDAO.updateByPrimaryKeySelective(hyPartnerUserInfoDO);

        UserInfoUpdateDTO.UserInfoUpdate afterUserinfoUpdate = new UserInfoUpdateDTO.UserInfoUpdate(baseUserInfoRequest.getUsername(), baseUserInfoRequest.getMobile(), baseUserInfoRequest.getLiveArea(), baseUserInfoRequest.getWantShopArea(), baseUserInfoRequest.getAcceptAdjustType());


        //修改意向申请信息中的加盟商名称与手机号
        hyPartnerBaseInfoDAO.updateByPartnerId(baseUserInfoRequest.getUsername(),baseUserInfoRequest.getMobile(),baseUserInfoRequest.getPartnerId());

        // 更新线索状态
        HyPartnerLineInfoDO hyPartnerLineInfoDO =  hyPartnerLineInfoService.generateDefaultLineInfo(hyPartnerUserInfoDO.getPartnerId(), baseUserInfoRequest.getWantShopArea(), baseUserInfoRequest.getAcceptAdjustType(),Boolean.TRUE);

        //更新意向申请表中信息
        HyPartnerIntentInfoDO hyPartnerIntentInfoDO = hyPartnerIntentInfoDAO.selectByLineId(hyPartnerLineInfoDO.getId());
        if (hyPartnerIntentInfoDO!=null){
            hyPartnerIntentInfoDO.setLiveArea(baseUserInfoRequest.getLiveArea());
            hyPartnerIntentInfoDO.setWantShopArea(baseUserInfoRequest.getWantShopArea());
            hyPartnerIntentInfoDO.setAcceptAdjustType(baseUserInfoRequest.getAcceptAdjustType());
            hyPartnerIntentInfoDAO.updateByPrimaryKeySelective(hyPartnerIntentInfoDO);
        }


        String cacheKey = MessageFormat.format(RedisConstant.PARTNER_INTENTINFO_CACHE_KEY, baseUserInfoRequest.getPartnerId(), hyPartnerLineInfoDO.getId());
        if (StringUtils.isNotBlank(redisUtilPool.getString(cacheKey))) {
            PartnerIntentInfoRequest request = JSONObject.parseObject(redisUtilPool.getString(cacheKey), PartnerIntentInfoRequest.class);
            if(!baseUserInfoRequest.getWantShopArea().equals(request.getWantShopArea())
                    || !baseUserInfoRequest.getAcceptAdjustType().equals(request.getAcceptAdjustType())){
                request.setWantShopArea(baseUserInfoRequest.getWantShopArea());
                request.setAcceptAdjustType(baseUserInfoRequest.getAcceptAdjustType());
                redisUtilPool.setString(cacheKey, JSONObject.toJSONString(request), RedisConstant.ONE_DAY_SECONDS);
            }
        }

        LineLogInfo lineLogInfo = new LineLogInfo(hyPartnerLineInfoDO.getPartnerId(), hyPartnerLineInfoDO.getId(), userInfo.getUserId(),
                userInfo.getName(), OperateTypeEnum.USERINFO_UPDATE,
                WorkflowStageEnum.getWorkflowStageByCode(hyPartnerLineInfoDO.getWorkflowStage()),
                hyPartnerLineInfoDO.getWorkflowStatus(), "");
        UserInfoUpdateDTO logDTO = UserInfoUpdateDTO.builder().operateUserId(userInfo.getUserId()).operateUsername(userInfo.getName())
                .beforeUserinfoUpdate(beforeUserinfoUpdate).afterUserinfoUpdate(afterUserinfoUpdate)
                .mobile(userInfo.getMobile()).operateTime(DateUtil.format(new Date(), CoolDateUtils.DATE_FORMAT_SEC_2)).build();
        lineLogInfo.setData(logDTO);
        hyPartnerTaskInfoLogDAO.addOperateLog(lineLogInfo);
        return Boolean.TRUE;
    }

    @Override
    public PartnerIntentInfoVO getPartnerIntentApplyDetail(Long lineId) {
        HyPartnerIntentInfoDO hyPartnerIntentInfoDO= hyPartnerIntentInfoDAO.selectByLineId(lineId);
        if(Objects.isNull(hyPartnerIntentInfoDO)){
            return null;
        }
        HyPartnerLineInfoDO hyPartnerLineInfoDO = hyPartnerLineInfoDAO.selectByPrimaryKeySelective(lineId);
        PartnerIntentInfoVO partnerIntentInfoVO = convertPartnerIntentApplyInfoDOToVO(hyPartnerIntentInfoDO);
        HyPartnerUserInfoDO hyPartnerUserInfoDO = hyPartnerUserInfoDAO.selectByPartnerId(hyPartnerIntentInfoDO.getPartnerId());
        partnerIntentInfoVO.setPartnerUserName(hyPartnerUserInfoDO.getUsername());
        partnerIntentInfoVO.setWorkflowStage(hyPartnerLineInfoDO.getWorkflowStage());
        partnerIntentInfoVO.setWorkflowStatus(hyPartnerLineInfoDO.getWorkflowStatus());
        partnerIntentInfoVO.setPartnerUserPhone(hyPartnerUserInfoDO.getMobile());
        HyOpenAreaInfoDO hyOpenAreaInfoDO = hyOpenAreaInfoDAO.selectById(Long.valueOf(hyPartnerUserInfoDO.getWantShopArea()));
        partnerIntentInfoVO.setWantShopAreaName(hyOpenAreaInfoDO.getAreaPath().replace("/"," "));
        return partnerIntentInfoVO;
    }

    @Override
    public Integer submitPartnerIntentInfo(PartnerIntentInfoRequest request) {
        boolean isUpdateIntentInfo = true;
        log.info("HyPartnerClerkServiceImpl#submitPartnerIntentInfo request:{}", JSONObject.toJSONString(request));
        if (StringUtil.isBlank(request.getPartnerId()) || Objects.isNull(request.getPartnerLineId())){
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        String cacheKey = MessageFormat.format(RedisConstant.PARTNER_INTENTINFO_CACHE_KEY, request.getPartnerId(), request.getPartnerLineId());
        if(!request.getSubmitFlag()){
            // 自动保存时
            redisUtilPool.setString(cacheKey, JSONObject.toJSONString(request), RedisConstant.ONE_DAY_SECONDS);
            return null;
        }
        WantInfoUpdateDTO.IntentInfoUpdate beforeIntentInfoUpdate = new WantInfoUpdateDTO.IntentInfoUpdate();
        WantInfoUpdateDTO.IntentInfoUpdate afterIntentInfoUpdate = new WantInfoUpdateDTO.IntentInfoUpdate();
        OperateTypeEnum operateTypeEnum = OperateTypeEnum.INTENT_INFO_UPDATE;
        HyPartnerIntentInfoDO intentInfoDO = hyPartnerIntentInfoDAO.getByPartnerIdAndLineId(request.getPartnerId(), request.getPartnerLineId());
        if(intentInfoDO == null){
            isUpdateIntentInfo = false;
            intentInfoDO = new HyPartnerIntentInfoDO();
            fillIntentInfo(intentInfoDO, request);
            hyPartnerIntentInfoDAO.insertSelective(intentInfoDO);
            BeanUtil.copyProperties(intentInfoDO, afterIntentInfoUpdate);
            operateTypeEnum = OperateTypeEnum.INTENT_INFO_SUBMIT;
        }else {
            BeanUtil.copyProperties(intentInfoDO, beforeIntentInfoUpdate);
            fillIntentInfo(intentInfoDO, request);
            hyPartnerIntentInfoDAO.updateByPrimaryKeySelective(intentInfoDO);
            BeanUtil.copyProperties(intentInfoDO, afterIntentInfoUpdate);
        }
        redisUtilPool.delKey(cacheKey);
        HyPartnerUserInfoDO hyPartnerUserInfoDO = hyPartnerUserInfoDAO.selectByPartnerId(request.getPartnerId());
        HyPartnerLineInfoDO hyPartnerLineInfoDO = hyPartnerLineInfoDAO.selectByPrimaryKeySelective(request.getPartnerLineId());
        List<String> userIdList = new ArrayList<>();
        if(!request.getWantShopArea().equals(hyPartnerUserInfoDO.getWantShopArea()) || !request.getAcceptAdjustType().equals(hyPartnerUserInfoDO.getAcceptAdjustType())){
            hyPartnerUserInfoDO.setLiveArea(request.getLiveArea());
            hyPartnerUserInfoDO.setWantShopArea(request.getWantShopArea());
            hyPartnerUserInfoDO.setAcceptAdjustType(request.getAcceptAdjustType());
            hyPartnerUserInfoDAO.updateByPrimaryKeySelective(hyPartnerUserInfoDO);
        }
        // 更新线索状态和招商经理
        //是公海线索 才会修改线索状态与招商经理
        if (LineStatusEnum.PUBLIC_SEAS.getCode().equals(hyPartnerLineInfoDO.getLineStatus())){
            Boolean flag = hyPartnerLineInfoService.assignFollowUser(request.getPartnerId(), request.getWantShopArea(), request.getAcceptAdjustType(),Boolean.TRUE);
            hyPartnerLineInfoDO.setLineStatus(flag ? LineStatusEnum.PRIVATE_SEAS.getCode() : LineStatusEnum.PUBLIC_SEAS.getCode());
            if (flag){
                String investmentManager = hyPartnerLineInfoService.getAssignFollowUser(request.getPartnerId(), "intent");
                hyPartnerLineInfoDO.setInvestmentManager(investmentManager);
                userIdList.add(investmentManager);
            }
            hyPartnerLineInfoDAO.updateByPrimaryKeySelective(hyPartnerLineInfoDO);

            if(flag){//分配招商经理成功才发送分配招商经理的飞书工作通知
                noticeService.sendFeiShuNotice(FeiShuNoticeMsgEnum.ALLOCATION_INVESTMENT_MANAGER,userIdList,DateUtil.formatDateTime(new Date()),hyPartnerUserInfoDO.getUsername(),hyPartnerUserInfoDO.getMobile());
            }
        }
        if(!isUpdateIntentInfo){
            if(CollectionUtils.isEmpty(userIdList)){
                userIdList.add(hyPartnerLineInfoDO.getInvestmentManager());
            }
            noticeService.sendFeiShuNotice(FeiShuNoticeMsgEnum.INTENTION_APPLY,userIdList,hyPartnerUserInfoDO.getUsername(),hyPartnerUserInfoDO.getMobile(),DateUtil.formatDateTime(new Date()));
        }
        //记录日志
        PartnerUserInfoVO operator = PartnerUserHolder.getUser();
        WantInfoUpdateDTO log = WantInfoUpdateDTO.builder().mobile(operator.getMobile()).operateUserId(operator.getPartnerId()).operateUsername(operator.getUsername())
                .operateTime(DateUtil.format(new Date(), CoolDateUtils.DATE_FORMAT_SEC))
                .beforeIntentInfoUpdate(beforeIntentInfoUpdate).afterIntentInfoUpdate(afterIntentInfoUpdate).build();
        logService.recordPartnerBizLog(operator,hyPartnerLineInfoDO.getId(), operateTypeEnum, log);
        return hyPartnerLineInfoDO.getLineStatus();
    }

    @Override
    public String submitWantShopInfo(PartnerWantShopInfoRequest request) {

        log.info("HyPartnerClerkServiceImpl#submitWantShopInfo request:{}", JSONObject.toJSONString(request));
        if (StringUtil.isBlank(request.getPartnerId()) || Objects.isNull(request.getPartnerLineId())){
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        String cacheKey = MessageFormat.format(RedisConstant.PARTNER_WANTSHOPINFO_CACHE_KEY, request.getPartnerId(), request.getPartnerLineId());
        if(!request.getSubmitFlag()){
            // 自动保存时
            redisUtilPool.setString(cacheKey, JSONObject.toJSONString(request), RedisConstant.ONE_DAY_SECONDS);
            return "";
        }
        HyPartnerIntentInfoDO intentInfoDO = hyPartnerIntentInfoDAO.getByPartnerIdAndLineId(request.getPartnerId(), request.getPartnerLineId());
        if(intentInfoDO == null){
            intentInfoDO = new HyPartnerIntentInfoDO();
            fillWantShopInfo(intentInfoDO, request);
            hyPartnerIntentInfoDAO.insertSelective(intentInfoDO);
        }else {
            fillWantShopInfo(intentInfoDO, request);
            hyPartnerIntentInfoDAO.updateByPrimaryKeySelective(intentInfoDO);
        }
        redisUtilPool.delKey(cacheKey);
        if(StringUtils.isNotBlank(request.getWantShopArea())){
            HyPartnerUserInfoDO hyPartnerUserInfoDO = hyPartnerUserInfoDAO.selectByPartnerId(request.getPartnerId());
            hyPartnerUserInfoDO.setWantShopArea(request.getWantShopArea());
            hyPartnerUserInfoDO.setLiveArea(request.getLiveArea());
            hyPartnerUserInfoDO.setAcceptAdjustType(request.getAcceptAdjustType());
            hyPartnerUserInfoDAO.updateByPrimaryKeySelective(hyPartnerUserInfoDO);
            HyOpenAreaInfoDO hyOpenAreaInfoDO = hyOpenAreaInfoDAO.selectById(Long.valueOf(request.getWantShopArea()));
            return hyOpenAreaInfoDO != null ? hyOpenAreaInfoDO.getAreaStatus() : null;
        }
        return null;
    }

    @Override
    public Boolean submitIndustryCognitionInfo(IndustryCognitionInfoRequest request) {
        log.info("HyPartnerClerkServiceImpl#submitPartnerIntentInfo request:{}", JSONObject.toJSONString(request));
        if (StringUtil.isBlank(request.getPartnerId()) || Objects.isNull(request.getPartnerLineId())){
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        String cacheKey = MessageFormat.format(RedisConstant.PARTNER_INDUSTRYCOGNITIONINFO_CACHE_KEY, request.getPartnerId(), request.getPartnerLineId());
        if(!request.getSubmitFlag()){
            // 自动保存时
            redisUtilPool.setString(cacheKey, JSONObject.toJSONString(request), RedisConstant.ONE_DAY_SECONDS);
            return true;
        }
        HyPartnerIntentInfoDO intentInfoDO = hyPartnerIntentInfoDAO.getByPartnerIdAndLineId(request.getPartnerId(), request.getPartnerLineId());
        if(intentInfoDO == null){
            intentInfoDO = new HyPartnerIntentInfoDO();
            fillIndustryCognitionInfo(intentInfoDO, request);
            hyPartnerIntentInfoDAO.insertSelective(intentInfoDO);
        }else {
            fillIndustryCognitionInfo(intentInfoDO, request);
            hyPartnerIntentInfoDAO.updateByPrimaryKeySelective(intentInfoDO);
        }
        redisUtilPool.delKey(cacheKey);
        return true;
    }

    @Override
    public PartnerIntentInfoVO queryPartnerIntentInfo(PartnerUserInfoVO userInfoVO, Long lineId) {
        log.info("HyPartnerClerkServiceImpl#queryPartnerIntentInfo userInfoVO:{}", JSONObject.toJSONString(userInfoVO));

        String cacheKey = MessageFormat.format(RedisConstant.PARTNER_INTENTINFO_CACHE_KEY, userInfoVO.getPartnerId(), lineId);
        if (StringUtils.isNotBlank(redisUtilPool.getString(cacheKey))) {
            PartnerIntentInfoVO intentInfoVO = JSONObject.parseObject(redisUtilPool.getString(cacheKey), PartnerIntentInfoVO.class);
            intentInfoVO.setWantShopAreaName(userInfoVO.getWantShopAreaName());
            return intentInfoVO;
        }
        HyPartnerIntentInfoDO intentInfoDO = hyPartnerIntentInfoDAO.getByPartnerIdAndLineId(userInfoVO.getPartnerId(), lineId);
        PartnerIntentInfoVO intentInfoVO = null;
        if (intentInfoDO != null){
            intentInfoVO = convertPartnerIntentApplyInfoDOToVO(intentInfoDO);
        }else {
            intentInfoVO = new PartnerIntentInfoVO();
        }
        if(StringUtils.isBlank(intentInfoVO.getLiveArea())){
            intentInfoVO.setLiveArea(userInfoVO.getLiveArea());
        }
        if(StringUtils.isBlank(intentInfoVO.getWantShopArea())){
            intentInfoVO.setWantShopArea(userInfoVO.getWantShopArea());
        }
        if(Objects.isNull(intentInfoVO.getAcceptAdjustType())){
            intentInfoVO.setAcceptAdjustType(userInfoVO.getAcceptAdjustType());
        }
        intentInfoVO.setWantShopAreaName(userInfoVO.getWantShopAreaName());
        log.info("HyPartnerClerkServiceImpl#queryPartnerIntentInfo intentInfoVO:{}", JSONObject.toJSONString(intentInfoVO));

        return intentInfoVO;
    }

    @Override
    public PartnerIntentInfoVO queryWantShopInfo(PartnerUserInfoVO userInfoVO, Long lineId) {
        String cacheKey = MessageFormat.format(RedisConstant.PARTNER_WANTSHOPINFO_CACHE_KEY, userInfoVO.getPartnerId(), lineId);
        if (StringUtils.isNotBlank(redisUtilPool.getString(cacheKey))) {
            PartnerIntentInfoVO intentInfoVO = JSONObject.parseObject(redisUtilPool.getString(cacheKey), PartnerIntentInfoVO.class);
            return intentInfoVO;
        }
        HyPartnerIntentInfoDO intentInfoDO = hyPartnerIntentInfoDAO.getByPartnerIdAndLineId(userInfoVO.getPartnerId(), lineId);
        PartnerIntentInfoVO intentInfoVO = null;
        if (intentInfoDO != null){
            intentInfoVO = convertPartnerIntentApplyInfoDOToVO(intentInfoDO);
        }else {
            intentInfoVO = new PartnerIntentInfoVO();
        }
        if(StringUtils.isBlank(intentInfoVO.getLiveArea())){
            intentInfoVO.setLiveArea(userInfoVO.getLiveArea());
        }
        if(StringUtils.isBlank(intentInfoVO.getWantShopArea())){
            intentInfoVO.setWantShopArea(userInfoVO.getWantShopArea());
        }
        if(Objects.isNull(intentInfoVO.getAcceptAdjustType())){
            intentInfoVO.setAcceptAdjustType(userInfoVO.getAcceptAdjustType());
        }
        return intentInfoVO;
    }

    @Override
    public PartnerIntentInfoVO queryIndustryCognitionInfo(PartnerUserInfoVO userInfoVO, Long lineId) {
        String cacheKey = MessageFormat.format(RedisConstant.PARTNER_INDUSTRYCOGNITIONINFO_CACHE_KEY, userInfoVO.getPartnerId(), lineId);
        if (StringUtils.isNotBlank(redisUtilPool.getString(cacheKey))) {
            PartnerIntentInfoVO intentInfoVO = JSONObject.parseObject(redisUtilPool.getString(cacheKey), PartnerIntentInfoVO.class);
            return intentInfoVO;
        }
        HyPartnerIntentInfoDO intentInfoDO = hyPartnerIntentInfoDAO.getByPartnerIdAndLineId(userInfoVO.getPartnerId(), lineId);
        PartnerIntentInfoVO intentInfoVO = null;
        if (intentInfoDO != null){
            intentInfoVO = convertPartnerIntentApplyInfoDOToVO(intentInfoDO);
        }
        return intentInfoVO;
    }

    private void fillWantShopInfo(HyPartnerIntentInfoDO intentInfoDO, PartnerWantShopInfoRequest request) {
        intentInfoDO.setPartnerId(request.getPartnerId());
        intentInfoDO.setPartnerLineId(request.getPartnerLineId());
        intentInfoDO.setLiveArea(request.getLiveArea());
        intentInfoDO.setWantShopArea(request.getWantShopArea());
        intentInfoDO.setAcceptAdjustType(request.getAcceptAdjustType());
        intentInfoDO.setIsHaveWantShop(request.getIsHaveWantShop());
        if (CollectionUtils.isNotEmpty(request.getWantShopInfo())){
            intentInfoDO.setWantShopInfo(JSONObject.toJSONString(request.getWantShopInfo()));
        }
        intentInfoDO.setMaxBudget(request.getMaxBudget());
        intentInfoDO.setMoneySource(request.getMoneySource());
        if(CollectionUtils.isNotEmpty(request.getMoneyProve())){
            intentInfoDO.setMoneyProve(JSONObject.toJSONString(request.getMoneyProve()));
        }
        intentInfoDO.setEducation("");
        intentInfoDO.setWorkYear("");
        intentInfoDO.setWorkExp("");
        intentInfoDO.setOtherBand("");
        intentInfoDO.setBrandStrength("");
        intentInfoDO.setNeedImprove("");
        intentInfoDO.setStrength("");
        intentInfoDO.setWeakness("");
    }

    private void fillIndustryCognitionInfo(HyPartnerIntentInfoDO intentInfoDO, IndustryCognitionInfoRequest request) {
        intentInfoDO.setPartnerId(request.getPartnerId());
        intentInfoDO.setPartnerLineId(request.getPartnerLineId());
        intentInfoDO.setEducation(request.getEducation());
        intentInfoDO.setWorkYear(request.getWorkYear());
        intentInfoDO.setIsHaveWorkExp(request.getIsHaveWorkExp());
        intentInfoDO.setWorkExp(request.getWorkExp());
        intentInfoDO.setIsConsumer(request.getIsConsumer());
        intentInfoDO.setOtherBand(request.getOtherBand());
        intentInfoDO.setBrandStrength(request.getBrandStrength());
        intentInfoDO.setNeedImprove(request.getNeedImprove());
        intentInfoDO.setStrength(request.getStrength());
        intentInfoDO.setWeakness(request.getWeakness());
    }

    private void fillIntentInfo(HyPartnerIntentInfoDO intentInfoDO, PartnerIntentInfoRequest request) {
        intentInfoDO.setPartnerId(request.getPartnerId());
        intentInfoDO.setPartnerLineId(request.getPartnerLineId());
        intentInfoDO.setLiveArea(request.getLiveArea());
        intentInfoDO.setWantShopArea(request.getWantShopArea());
        intentInfoDO.setAcceptAdjustType(request.getAcceptAdjustType());
        intentInfoDO.setIsHaveWantShop(request.getIsHaveWantShop());
        intentInfoDO.setWantShopInfo("");
        if (CollectionUtils.isNotEmpty(request.getWantShopInfo()) && request.getIsHaveWantShop() == 1){
            intentInfoDO.setWantShopInfo(JSONObject.toJSONString(request.getWantShopInfo()));
        }
        intentInfoDO.setMaxBudget(request.getMaxBudget());
        intentInfoDO.setMoneySource(request.getMoneySource());
        if(CollectionUtils.isNotEmpty(request.getMoneyProve())){
            intentInfoDO.setMoneyProve(JSONObject.toJSONString(request.getMoneyProve()));
        }
        intentInfoDO.setEducation(request.getEducation());
        intentInfoDO.setWorkYear(request.getWorkYear());
        intentInfoDO.setIsHaveWorkExp(request.getIsHaveWorkExp());
        intentInfoDO.setWorkExp(request.getWorkExp());
        intentInfoDO.setIsConsumer(request.getIsConsumer());
        intentInfoDO.setOtherBand(request.getOtherBand());
        intentInfoDO.setBrandStrength(request.getBrandStrength());
        intentInfoDO.setNeedImprove(request.getNeedImprove());
        intentInfoDO.setStrength(request.getStrength());
        intentInfoDO.setWeakness(request.getWeakness());
    }



    /**
     *
     * @param hyPartnerIntentInfoDO
     * @return
     */
    private PartnerIntentInfoVO convertPartnerIntentApplyInfoDOToVO(HyPartnerIntentInfoDO hyPartnerIntentInfoDO){
        PartnerIntentInfoVO partnerIntentInfoVO = new PartnerIntentInfoVO();
        partnerIntentInfoVO.setId(hyPartnerIntentInfoDO.getId());
        partnerIntentInfoVO.setPartnerId(hyPartnerIntentInfoDO.getPartnerId());
        partnerIntentInfoVO.setPartnerLineId(hyPartnerIntentInfoDO.getPartnerLineId());
        partnerIntentInfoVO.setAcceptAdjustType(hyPartnerIntentInfoDO.getAcceptAdjustType());
        partnerIntentInfoVO.setLiveArea(hyPartnerIntentInfoDO.getLiveArea());
        partnerIntentInfoVO.setWantShopArea(hyPartnerIntentInfoDO.getWantShopArea());
        partnerIntentInfoVO.setEducation(hyPartnerIntentInfoDO.getEducation());
        partnerIntentInfoVO.setMaxBudget(hyPartnerIntentInfoDO.getMaxBudget());
        partnerIntentInfoVO.setBrandStrength(hyPartnerIntentInfoDO.getBrandStrength());
        partnerIntentInfoVO.setIsConsumer(hyPartnerIntentInfoDO.getIsConsumer());
        partnerIntentInfoVO.setIsHaveWantShop(hyPartnerIntentInfoDO.getIsHaveWantShop());
        partnerIntentInfoVO.setIsHaveWorkExp(hyPartnerIntentInfoDO.getIsHaveWorkExp());
        partnerIntentInfoVO.setMoneyProve(StringUtil.isEmpty(hyPartnerIntentInfoDO.getMoneyProve())?
                Lists.newArrayList(): JSONObject.parseArray(hyPartnerIntentInfoDO.getMoneyProve(),String.class));
        partnerIntentInfoVO.setMoneySource(hyPartnerIntentInfoDO.getMoneySource());
        partnerIntentInfoVO.setWorkYear(hyPartnerIntentInfoDO.getWorkYear());
        partnerIntentInfoVO.setWorkExp(hyPartnerIntentInfoDO.getWorkExp());
        partnerIntentInfoVO.setWeakness(hyPartnerIntentInfoDO.getWeakness());
        partnerIntentInfoVO.setCreateTime(hyPartnerIntentInfoDO.getCreateTime());
        partnerIntentInfoVO.setOtherBand(hyPartnerIntentInfoDO.getOtherBand());
        partnerIntentInfoVO.setStrength(hyPartnerIntentInfoDO.getStrength());
        partnerIntentInfoVO.setNeedImprove(hyPartnerIntentInfoDO.getNeedImprove());
        if(StringUtils.isNotEmpty(hyPartnerIntentInfoDO.getWantShopInfo())){
            partnerIntentInfoVO.setWantShopInfo(JSONObject.parseArray(hyPartnerIntentInfoDO.getWantShopInfo(), WantShopInfoVO.class));
        }
        return partnerIntentInfoVO;
    }



}
