package com.cool.store.service.impl;

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.ShopStageInfoDAO;
import com.cool.store.entity.*;
import com.cool.store.enums.*;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.enums.prepare.newStore.BusinessDistrictEnum;
import com.cool.store.enums.prepare.newStore.OfflineCompetitorEnum;
import com.cool.store.enums.prepare.newStore.PassengerFlowEnum;
import com.cool.store.enums.prepare.newStore.StoreOperatorEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.*;
import com.cool.store.mq.producer.SimpleMessageService;
import com.cool.store.request.AuditResultRequest;
import com.cool.store.request.NewStoreRequest;
import com.cool.store.request.SysStoreAppRequest;
import com.cool.store.response.ResponseResult;
import com.cool.store.response.SysStoreAppResponse;
import com.cool.store.service.CoolStoreStartFlowService;
import com.cool.store.service.PreparationService;
import com.cool.store.service.SysStoreAppService;
import com.cool.store.service.UserAuthMappingService;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.utils.poi.StringUtils;
import com.cool.store.utils.poi.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
public class SysStoreAppServiceImpl implements SysStoreAppService, AuditResultService {

    @Resource
    private CoolStoreStartFlowService coolStoreStartFlowService;

    @Resource
    private SystemBuildingShopMapper systemBuildingShopMapper;

    @Resource
    private LineInfoMapper lineInfoMapper;

    @Resource
    PointInfoMapper pointInfoMapper;

    @Resource
    RegionMapper regionMapper;

    @Resource
    ThirdDepartmentMapper thirdDepartmentMapper;

    @Resource
    EnterpriseUserMapper enterpriseUserMapper;

    @Resource
    UserAuthMappingService userAuthMappingService;

    @Resource
    PreparationService preparationService;

    @Resource
    ShopAuditInfoMapper shopAuditInfoMapper;

    @Resource
    ShopInfoMapper shopInfoMapper;

    @Resource
    ShopStageInfoDAO shopStageInfoDAO;

    @Resource
    CommonService commonService;

    @Resource
    SimpleMessageService simpleMessageService;

    @Resource
    PointDetailInfoMapper pointDetailInfoMapper;

    @Resource
    RedisUtilPool redisUtilPool;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult submitSysBuildStore(SysStoreAppRequest request, LoginUserInfo user) {
        log.info("submitSysBuildStore request :{}", JSONObject.toJSONString(request));
        if (Objects.isNull(request)) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        SystemBuildingShopDO isExist = systemBuildingShopMapper.selectByShopId(request.getShopId());
        if (Objects.nonNull(isExist) && Objects.isNull(request.getId())){
            throw new ServiceException(ErrorCodeEnum.DUPLICATE_SUBMISSION);
        }
        String lockKey = "submitSysBuildStore:" + request.getShopId();
        String lockValue = UUID.randomUUID().toString();
        boolean acquired = false;
        try {
            acquired = redisUtilPool.setNxExpire(lockKey, lockValue, CommonConstants.TEN_SECONDS);
            if (Boolean.TRUE.equals(acquired)) {
                //1.操作数据库
                SystemBuildingShopDO systemBuildingShopDO = SystemBuildingShopDO.convertToSystemBuildingShopDO(request);
                if (request.getId() == null) {
                    systemBuildingShopMapper.insertSelective(systemBuildingShopDO);
                } else {
                    systemBuildingShopDO.setId(request.getId());
                    systemBuildingShopMapper.updateByPrimaryKeySelective(systemBuildingShopDO);
                }
                //2.查找、组装数组
                LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(request.getLineId());//线索信息
                PointInfoDO pointInfoDO = pointInfoMapper.getDataByShopIdAndLineId(request.getLineId(), request.getShopId());//查铺位信息确定铺位所在大区、战区、门店所在省市区街道地址
                //3.请求鲜丰接口
                NewStoreRequest apiRequest = convertToNewStoreRequest(request, lineInfoDO, pointInfoDO, user);
                log.info("submitSysBuildStore apiRequest：{}",JSONObject.toJSONString(apiRequest));
                return coolStoreStartFlowService.newStore(apiRequest, request.getShopId());
            }else {
                throw new ServiceException(ErrorCodeEnum.DUPLICATE_SUBMISSION);
            }
        }finally {
            if (Boolean.TRUE.equals(acquired)) {
                String currentValue = redisUtilPool.getString(lockKey);
                if (lockValue.equals(currentValue)) {
                    redisUtilPool.delKey(lockKey);
                }
            }
        }
    }

    @Override
    public SysStoreAppResponse getDefaultValueSysBuildStore(Long lineId, Long shopId, LoginUserInfo user) {
        SysStoreAppResponse sysStoreAppResponse = new SysStoreAppResponse();
        //1.查找数据
        LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(lineId);
        SystemBuildingShopDO systemBuildingShopDO = SystemBuildingShopDO.builder().lineId(lineId).shopId(shopId).build();
        systemBuildingShopDO = systemBuildingShopMapper.selectOne(systemBuildingShopDO);
        PointInfoDO pointInfoDO = pointInfoMapper.getDataByShopIdAndLineId(lineId, shopId);
        //2.组装数据并返回
        sysStoreAppResponse = polymerization(sysStoreAppResponse, systemBuildingShopDO, lineInfoDO, pointInfoDO);
        ShopAuditInfoDO shopAuditInfoDO = shopAuditInfoMapper.selectBykeyAndType(shopId);
        if (Objects.nonNull(shopAuditInfoDO)) {
            SysStoreAppResponse.AuditDetail auditDetail = new SysStoreAppResponse.AuditDetail();
            auditDetail.setAuditType(shopAuditInfoDO.getAuditType());
            auditDetail.setPassReason(shopAuditInfoDO.getPassReason());
            auditDetail.setRejectReason(shopAuditInfoDO.getRejectReason());
            auditDetail.setResultType(shopAuditInfoDO.getResultType());
            sysStoreAppResponse.setAuditDetail(auditDetail);
        }
        return sysStoreAppResponse;
    }

    /**
     * 聚合返回体
     */
    private SysStoreAppResponse polymerization(SysStoreAppResponse sysStoreAppResponse,
                                               SystemBuildingShopDO systemBuildingShopDO,
                                               LineInfoDO lineInfoDO,
                                               PointInfoDO pointInfoDO) {
        sysStoreAppResponse = from(systemBuildingShopDO, lineInfoDO, pointInfoDO);
        return sysStoreAppResponse;
    }

    public SysStoreAppResponse from(SystemBuildingShopDO systemBuildingShopDO,
                                    LineInfoDO lineInfoDO,
                                    PointInfoDO pointInfoDO) {
        SysStoreAppResponse.FranInfo franInfo = new SysStoreAppResponse.FranInfo();
        SysStoreAppResponse.StoreDetail storeDetail = new SysStoreAppResponse.StoreDetail();
        SysStoreAppResponse.SupervisorDetail supervisorDetail = new SysStoreAppResponse.SupervisorDetail();
        SysStoreAppResponse sysStoreAppResponse = new SysStoreAppResponse();
        Long regionId = pointInfoDO.getRegionId();
        RegionDO bigRegion = regionMapper.getBigRegionByRegionId(regionId);//大区
        RegionDO fightRegion = regionMapper.getByRegionId(regionId);//战区
        EnterpriseUserDO regionManager = userAuthMappingService.getUserByRoleEnumAndRegionId(UserRoleEnum.REGION_MANAGER, bigRegion.getId());
//        EnterpriseUserDO supervisor = userAuthMappingService.getUserByRoleEnumAndRegionId(UserRoleEnum.SUPERVISION, fightRegion.getId());
        if (Objects.nonNull(regionManager)) {
            // regioGeneral
            supervisorDetail.setRegioGeneral(regionManager.getName());
            // regioGeneralId
            supervisorDetail.setRegioGeneralId(regionManager.getUserId());
        }
//        if (Objects.nonNull(supervisor)) {
//            // supervisorName
//            supervisorDetail.setSupervisorName(supervisor.getName());
//            // supervisorId
//            supervisorDetail.setSupervisorId(supervisor.getUserId());
//        }
        storeDetail.setBigName(bigRegion.getName());
        storeDetail.setBigCode(bigRegion.getSynDingDeptId());
        storeDetail.setFightName(fightRegion.getName());
        storeDetail.setFightCode(fightRegion.getSynDingDeptId());
        storeDetail.setFightId(fightRegion.getId());

        if (Objects.nonNull(systemBuildingShopDO)) {
            sysStoreAppResponse.setId(systemBuildingShopDO.getId());
            sysStoreAppResponse.setLineId(systemBuildingShopDO.getLineId());
            sysStoreAppResponse.setShopId(systemBuildingShopDO.getShopId());
            sysStoreAppResponse.setAuditId(systemBuildingShopDO.getAuditId());
            franInfo.setFranchiseeType(systemBuildingShopDO.getFranchiseeType());
            franInfo.setStoreInviteesName(systemBuildingShopDO.getStoreInviteesName());
            franInfo.setInviteUserId(systemBuildingShopDO.getStoreInviteesId());
            storeDetail.setStoreNature(systemBuildingShopDO.getStoreNature());
            storeDetail.setStoreOperator(systemBuildingShopDO.getStoreOperator());
            storeDetail.setStoreName(systemBuildingShopDO.getStoreName());
            // bigDesc
            storeDetail.setBigDesc(systemBuildingShopDO.getBigDesc());
            // fightDesc
            storeDetail.setFightDesc(systemBuildingShopDO.getFightDesc());
            // smallCode
            storeDetail.setSmallCode(systemBuildingShopDO.getSmallCode());
            // smallDesc
            storeDetail.setSmallDesc(systemBuildingShopDO.getSmallDesc());
            // smallName
            storeDetail.setSmallName(systemBuildingShopDO.getSmallName());
            // mainBusinessType
            storeDetail.setMainBusinessType(systemBuildingShopDO.getMainBusinessType());
            // subBusinessType
            storeDetail.setSubBusinessType(systemBuildingShopDO.getSubBusinessType());
            // offlineCompetitor
            storeDetail.setOfflineCompetitor(systemBuildingShopDO.getOfflineCompetitor());
            // offlineCompetingBrand
            storeDetail.setOfflineCompetingBrand(systemBuildingShopDO.getOfflineCompetingBrand());
            // averagePriceSh
            storeDetail.setAveragePriceSh(systemBuildingShopDO.getAveragePriceSh());
            // averageDailyRs
            storeDetail.setAverageDailyRs(systemBuildingShopDO.getAverageDailyRs());
            // businessStoreType
            storeDetail.setBusinessStoreType(systemBuildingShopDO.getBusinessStoreType());
            // estimatedTurnover
            storeDetail.setEstimatedTurnover(systemBuildingShopDO.getEstimatedTurnover());
            // signDate
            storeDetail.setSignDate(systemBuildingShopDO.getSignDate());
            // landlordName
            storeDetail.setLandlordName(systemBuildingShopDO.getLandlordName());
            // contractDeliveryDate
            storeDetail.setContractDeliveryDate(systemBuildingShopDO.getContractDeliveryDate());
            // landlordMobile
            storeDetail.setLandlordMobile(systemBuildingShopDO.getLandlordMobile());
            // reservation
            storeDetail.setReservation(systemBuildingShopDO.getReservation());
            // storeRent
            storeDetail.setStoreRent(systemBuildingShopDO.getStoreRent());
            // storeFranchiseFee
            storeDetail.setStoreFranchiseFee(systemBuildingShopDO.getStoreFranchiseFee());
            // storeSecurityDeposit
            storeDetail.setStoreSecurityDeposit(systemBuildingShopDO.getStoreSecurityDeposit());
            // reasons
            storeDetail.setReasons(systemBuildingShopDO.getReasons());
            storeDetail.setUsageRate(systemBuildingShopDO.getUsageRate());
            //
            supervisorDetail.setSupervisorName(systemBuildingShopDO.getSupervisorName());
            // supervisorId
            supervisorDetail.setSupervisorId(systemBuildingShopDO.getSupervisorId());
        }
        PointDetailInfoDO shopPointDetailInfoByPointId = pointDetailInfoMapper.getShopPointDetailInfoByPointId(pointInfoDO.getId());
        if (Objects.isNull(systemBuildingShopDO) && Objects.nonNull(shopPointDetailInfoByPointId)){
            storeDetail.setLandlordMobile(shopPointDetailInfoByPointId.getLandlordMobile());
            storeDetail.setLandlordName(shopPointDetailInfoByPointId.getLandlordUsername());
            storeDetail.setStoreRent(Integer.valueOf(shopPointDetailInfoByPointId.getMonthRent()));
        }
        if (Objects.nonNull(lineInfoDO)) {
            RegionDO byRegionId = regionMapper.getByRegionId(lineInfoDO.getRegionId());
            franInfo.setSubregion(byRegionId.getName());
            franInfo.setPartnerNum(lineInfoDO.getPartnerNum());
            franInfo.setFranchiseeMobile(lineInfoDO.getMobile());
            franInfo.setFranchiseeName(lineInfoDO.getUsername());
            franInfo.setInvestmentUserId(lineInfoDO.getInvestmentManager());
            EnterpriseUserDO userInfoById = enterpriseUserMapper.getUserInfoById(lineInfoDO.getInvestmentManager());
            franInfo.setInvestmentName(userInfoById.getName());
        }
        EnterpriseUserDO locationPeople = enterpriseUserMapper.getUserInfoById(pointInfoDO.getDevelopmentManager());//门店选址人
        franInfo.setSitterId(pointInfoDO.getDevelopmentManager());
        franInfo.setSitterName(locationPeople.getName());
        storeDetail.setProvince(pointInfoDO.getProvince());
        storeDetail.setCity(pointInfoDO.getCity());
        storeDetail.setDistrict(pointInfoDO.getDistrict());
        storeDetail.setTownship(pointInfoDO.getTownship());
        storeDetail.setDetailAddress(pointInfoDO.getAddress());
        storeDetail.setStoreName(pointInfoDO.getPointName());

        sysStoreAppResponse.setStoreDetail(storeDetail);
        sysStoreAppResponse.setSupervisorDetail(supervisorDetail);
        sysStoreAppResponse.setFranInfo(franInfo);
        return sysStoreAppResponse;
    }

    public NewStoreRequest convertToNewStoreRequest(
            SysStoreAppRequest sysStoreAppRequest,
            LineInfoDO lineInfoDO,
            PointInfoDO pointInfoDO,
            LoginUserInfo user) {
        //用户信息map
        List<String> userIdList = new ArrayList<>();
        userIdList.add(sysStoreAppRequest.getSupervisorDetail().getSupervisorId());//督导
        userIdList.add(sysStoreAppRequest.getFranInfo().getInviteUserId());//邀约人
        userIdList.add(pointInfoDO.getDevelopmentManager());//选址人
        userIdList.add(lineInfoDO.getInvestmentManager());//招商人
        List<EnterpriseUserDO> userInfoByUserIds = enterpriseUserMapper.getUserInfoByUserIds(userIdList);
        Map<String, EnterpriseUserDO> userMap = userInfoByUserIds.stream().collect(Collectors.toMap(EnterpriseUserDO::getUserId, Function.identity()));

        NewStoreRequest newStoreRequest = new NewStoreRequest();
        ArrayList<String> objects = new ArrayList<>();
        objects.add("123");
        // accessory_address
        newStoreRequest.setAccessory_address(objects);
        // apply_user
        newStoreRequest.setApply_user(user.getJobNumber());
        // city1
        newStoreRequest.setCity1(pointInfoDO.getCity());
        // csgs
        ThirdDepartmentDO thirdDepartmentDO = thirdDepartmentMapper.getByName(sysStoreAppRequest.getStoreDetail().getBigName());
        newStoreRequest.setCsgs(thirdDepartmentDO.getDepartmentCode());
        // csqmc
        newStoreRequest.setCsqmc(BusinessDistrictEnum.getByCode(sysStoreAppRequest.getStoreDetail().getSubBusinessType()).getDesc());
        //ddxm  督导
        newStoreRequest.setDdxm(sysStoreAppRequest.getSupervisorDetail().getSupervisorId());
        // dpzlht
        newStoreRequest.setDpzlht(objects);
        // end_time
        newStoreRequest.setEnd_time(null);
        // forecast_turnover
        newStoreRequest.setForecast_turnover(sysStoreAppRequest.getStoreDetail().getEstimatedTurnover());
        // invite_people
        //邀约人
        newStoreRequest.setInvite_people(userMap.get(sysStoreAppRequest.getFranInfo().getInviteUserId()).getJobnumber());
        // jms_id
        newStoreRequest.setJms_id(lineInfoDO.getPartnerNum());
        // jmskhdjbwzjt
        newStoreRequest.setJmskhdjbwzjt(objects);
        // join_channel
        newStoreRequest.setJoin_channel(sysStoreAppRequest.getFranInfo().getFranchiseeType());
        // jzdsmc
        newStoreRequest.setJzdsmc(OfflineCompetitorEnum.getByCode(sysStoreAppRequest.getStoreDetail().getOfflineCompetitor()).getDesc());
        // kdzBusinessid
        newStoreRequest.setKdzBusinessId(AuditEnum.SYS_BUILD.getCode() + "_" + sysStoreAppRequest.getShopId() + "_" + pointInfoDO.getId());
        // kllzsmc
        newStoreRequest.setKllzsmc(PassengerFlowEnum.getByCode(sysStoreAppRequest.getStoreDetail().getAverageDailyRs()).getDesc());
        // landlord_name
        newStoreRequest.setLandlord_name(sysStoreAppRequest.getStoreDetail().getLandlordName());
        // landlord_tel
        newStoreRequest.setLandlord_tel(sysStoreAppRequest.getStoreDetail().getLandlordMobile());
        //location_people 门店选址人
        newStoreRequest.setLocation_people(userMap.get(pointInfoDO.getDevelopmentManager()).getJobnumber());
        // mdyzj
        newStoreRequest.setMdyzj(sysStoreAppRequest.getStoreDetail().getStoreRent());
        //merchant_people 招商人工号
        newStoreRequest.setMerchant_people(userMap.get(lineInfoDO.getInvestmentManager()).getJobnumber());
        // name1
        newStoreRequest.setName1(sysStoreAppRequest.getStoreDetail().getStoreName());
        // regio
        newStoreRequest.setRegio(pointInfoDO.getProvince());
        // reservation_date
        newStoreRequest.setReservation_date(DateUtil.format(sysStoreAppRequest.getStoreDetail().getReservation(), Constants.TIME_STAMP_FLAG));
        // room_date
        newStoreRequest.setRoom_date(DateUtil.format(sysStoreAppRequest.getStoreDetail().getContractDeliveryDate(), Constants.TIME_STAMP_FLAG));
        // scjdxz
        newStoreRequest.setScjdxz(objects);
        // signing_date
        newStoreRequest.setSigning_date(DateUtil.format(sysStoreAppRequest.getStoreDetail().getSignDate(), Constants.TIME_STAMP_FLAG));
        // sqmdlx
        newStoreRequest.setSqmdlx(sysStoreAppRequest.getStoreDetail().getBusinessStoreType());
        // sqrq
        newStoreRequest.setSqrq(DateUtil.today());
        // ssdq
        newStoreRequest.setSsdq(sysStoreAppRequest.getStoreDetail().getBigCode());
        // ssxq
//        RegionDO smallRegion = regionMapper.getByRegionId(Long.valueOf(sysStoreAppRequest.getStoreDetail().getSmallCode()));
        RegionDO smallRegion = regionMapper.getRegionInfoBySynDingDeptId(Long.valueOf(sysStoreAppRequest.getStoreDetail().getSmallCode()));
        newStoreRequest.setSsxq(smallRegion.getSynDingDeptId());
        // sszq
        newStoreRequest.setSszq(sysStoreAppRequest.getStoreDetail().getFightCode());
        // start_time
        newStoreRequest.setStart_time(null);
        // store_bzj
        newStoreRequest.setStore_bzj(sysStoreAppRequest.getStoreDetail().getStoreSecurityDeposit());
        // store_jmf
        newStoreRequest.setStore_jmf(sysStoreAppRequest.getStoreDetail().getStoreFranchiseFee());
        // store_reason
        newStoreRequest.setStore_reason(sysStoreAppRequest.getStoreDetail().getReasons());
        // street
        newStoreRequest.setStreet(pointInfoDO.getTownship());
        // title
        newStoreRequest.setTitle("SAP-新店开业申请-" + lineInfoDO.getUsername() + "-" + DateUtil.today());
        // zcsqlx
        newStoreRequest.setZcsqlx(sysStoreAppRequest.getStoreDetail().getSubBusinessType());
        // zdqms
        newStoreRequest.setZdqms(sysStoreAppRequest.getStoreDetail().getBigName());
        // zhkkz
        newStoreRequest.setZhkkz("Y");
        // zjmsbm
        newStoreRequest.setZjmsbm(lineInfoDO.getPartnerNum());
        // zmddh
        newStoreRequest.setZmddh(lineInfoDO.getMobile());
        // zmdjyz
        newStoreRequest.setZmdjyz(sysStoreAppRequest.getStoreDetail().getStoreOperator());
        // zmdjyz_name
        newStoreRequest.setZmdjyz_name(StoreOperatorEnum.getByCode(sysStoreAppRequest.getStoreDetail().getStoreOperator()).getDesc());
        // zmdxz
        newStoreRequest.setZmdxz(20);
        // zmdxz_name
        newStoreRequest.setZmdxz_name("加盟");
        // zmdzt
        newStoreRequest.setZmdzt(10);
        // zmrjkllzs1
        newStoreRequest.setZmrjkllzs1(PassengerFlowEnum.getByCode(sysStoreAppRequest.getStoreDetail().getAverageDailyRs()).getType());
        // zppsyfy
        newStoreRequest.setZppsyfy(Double.valueOf(sysStoreAppRequest.getStoreDetail().getUsageRate()));
        // zq
        newStoreRequest.setZq(pointInfoDO.getDistrict());
        // zsqmc
        newStoreRequest.setZsqmc(BusinessDistrictEnum.getByCode(sysStoreAppRequest.getStoreDetail().getMainBusinessType()).getDesc());
        // zssgs
        newStoreRequest.setZssgs("1060");
        // ztype
        newStoreRequest.setZtype("Z1");
        // zwesfj
        newStoreRequest.setZwesfj(sysStoreAppRequest.getStoreDetail().getAveragePriceSh());
        // zxqms
        newStoreRequest.setZxqms(sysStoreAppRequest.getStoreDetail().getSmallName());
        // zxszyjzds
        newStoreRequest.setZxszyjzds(sysStoreAppRequest.getStoreDetail().getOfflineCompetingBrand());
        // zxxdz
        newStoreRequest.setZxxdz(pointInfoDO.getAddress());
        // zxxzyjzds
        newStoreRequest.setZxxzyjzds(sysStoreAppRequest.getStoreDetail().getOfflineCompetitor());
//        // zymdbm
        newStoreRequest.setZymdbm("");
        // zzqms
        newStoreRequest.setZzqms(sysStoreAppRequest.getStoreDetail().getFightName());
        newStoreRequest.setZzsqlx(sysStoreAppRequest.getStoreDetail().getMainBusinessType());
        // zzsqlx
        return newStoreRequest;
    }

    @Override
    public Boolean auditResult(AuditResultRequest request) {
//        String kdzBusinessId = request.getKdzBusinessId();
//        Long shopId = getShopId(kdzBusinessId);
//        ShopAuditInfoDO shopAuditInfoDO = new ShopAuditInfoDO();
//        ShopSubStageStatusEnum shopSubStageStatusEnum = null;
//        //1.成功/失败原因
//        try {
//            //删除上次提交数据
//            ShopAuditInfoDO deleteShopAuditDo = shopAuditInfoMapper.selectBykeyAndType(shopId);
//            if (Objects.nonNull(deleteShopAuditDo)){
//                deleteShopAuditDo.setDeleted(false);
//                shopAuditInfoMapper.updateByPrimaryKeySelective(deleteShopAuditDo);
//            }
//            //新增本次数据
//            shopAuditInfoDO.setShopId(shopId);
//            shopAuditInfoDO.setAuditType(AuditTypeEnum.SYS_BUILD.getCode());
//            shopAuditInfoDO.setSubmittedUserId("");
//            shopAuditInfoDO.setSubmittedUserName("");
//            if (Constants.ZERO_INTEGER.equals(request.getAuditResult())) {
//                shopAuditInfoDO.setResultType(Constants.ONE_INTEGER);
//                shopAuditInfoDO.setRejectReason(request.getCause());
//                shopSubStageStatusEnum = null;
//            } else if (Constants.ONE_INTEGER.equals(request.getAuditResult())) {
//                ShopInfoDO shopInfoDO = shopInfoMapper.selectByPrimaryKey(shopId);
//                EnterpriseUserDO supervisor = userAuthMappingService.hierarchicalSearch(UserRoleEnum.SUPERVISION, shopInfoDO.getRegionId());
//                shopInfoDO.setStoreNum(request.getStoreNum()); //更新门店编码
//                shopInfoDO.setSupervisorUserId(Objects.isNull(supervisor) ? "" : supervisor.getUserId());//更新督导
//                SystemBuildingShopDO systemBuildingShopDO = systemBuildingShopMapper.selectOne(SystemBuildingShopDO.builder().shopId(shopId).build());
//                if (StringUtils.isEmpty(shopInfoDO.getSupervisorUserId())&&StringUtils.isNotEmpty(systemBuildingShopDO.getSupervisorId())){
//                    EnterpriseUserDO user = enterpriseUserMapper.getUserInfoByJobnumber(systemBuildingShopDO.getSupervisorId());
//                    shopInfoDO.setSupervisorUserId(user!=null?user.getUserId():"");
//                }
//                shopInfoDO.setShopName(systemBuildingShopDO.getStoreName());//更新铺位名称
//                shopInfoMapper.updateByPrimaryKeySelective(shopInfoDO);
//                shopAuditInfoDO.setResultType(Constants.ZERO_INTEGER);
//                shopAuditInfoDO.setPassReason(request.getCause());
//                shopSubStageStatusEnum = null;
//                //校验建店与加盟签约合同是否完成 并初始化后续流程数据
//                preparationService.contractAndBuildStoreCompletion(shopId);
//                LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(shopInfoDO.getLineId());
//                commonService.sendSms(lineInfoDO.getMobile(), SMSMsgEnum.SYS_BUILD_STORE_DONE);
//            }
//            shopAuditInfoMapper.insertSelective(shopAuditInfoDO);
//            //更新阶段状态
//            shopStageInfoDAO.updateShopStageAndAuditInfo(shopId, shopSubStageStatusEnum, shopAuditInfoDO.getId());
//        } catch (Exception e) {
//            log.error("auditResult message:{}",e.getMessage());
//            throw new ServiceException(ErrorCodeEnum.UNKNOWN);
//        }
        return true;
    }

    private Long getShopId(String kdzBusinessId) {
        String shopId = kdzBusinessId.substring(kdzBusinessId.indexOf("_") + 1, kdzBusinessId.lastIndexOf("_"));
        return Long.valueOf(shopId);
    }
}
