package com.cool.store.service.impl;


import com.cool.store.constants.CommonConstants;
import com.cool.store.dao.*;
import com.cool.store.dto.PlatformBuildStageDTO;
import com.cool.store.dto.Preparation.ScheduleDTO;
import com.cool.store.entity.*;
import com.cool.store.enums.*;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.ApplyLicenseMapper;
import com.cool.store.mapper.RegionMapper;
import com.cool.store.request.PlatformBuildAuditRequest;
import com.cool.store.request.PlatformBuildRequest;
import com.cool.store.request.platformBuildListRequest;
import com.cool.store.response.PlatformBuildListResponse;
import com.cool.store.response.PlatformBuildResponse;
import com.cool.store.service.OperationLogService;
import com.cool.store.service.PlatformBuildService;
import com.cool.store.mapper.PlatformBuildMapper;
import com.cool.store.service.RegionService;
import com.cool.store.service.UserAuthMappingService;
import com.cool.store.utils.poi.DateUtils;
import com.cool.store.utils.poi.StringUtils;
import com.cool.store.vo.PartnerUserInfoVO;
import com.cool.store.vo.PlatformBuildInfoVO;
import com.cool.store.vo.Preparation.PreparationScheduleDetailVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.swagger2.mappers.LicenseMapper;

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

import static com.cool.store.enums.point.ShopSubStageStatusEnum.*;

/**
 * @author EDY
 * @description 针对表【xfsg_platform_build(平台建店表)】的数据库操作Service实现
 * @createDate 2024-10-09 14:54:40
 */
@Service
public class PlatformBuildServiceImpl implements PlatformBuildService {

    @Autowired
    private BuildInformationDAO buildInformationDAO;
    @Autowired
    private PlatformBuildDAO platformBuildDAO;
    @Resource
    private OperationLogService operationLogService;
    @Resource
    private ShopStageInfoDAO shopStageInfoDAO;
    @Autowired
    private UserAuthMappingService userAuthMappingService;
    @Autowired
    private ShopInfoDAO shopInfoDAO;
    @Autowired
    private ShopAuditInfoDAO shopAuditInfoDAO;
    @Autowired
    private OperationLogDAO operationLogDAO;
    @Autowired
    private LineInfoDAO lineInfoDAO;
    @Autowired
    private RegionService regionService;
    @Autowired
    private EnterpriseUserDAO enterpriseUserDAO;
    @Autowired
    private PointInfoDAO pointInfoDAO;
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public String juridicalIdCard(Long shopId) {
        BuildInformationDO informationDO = buildInformationDAO.selectOneByShopId(shopId);
        if (Objects.isNull(informationDO) || StringUtils.isBlank(informationDO.getJuridicalIdCardNo())) {
            throw new ServiceException(ErrorCodeEnum.GET_JURIDICAL_ID_CARD_NO_FAIL);
        }
        return informationDO.getJuridicalIdCardNo();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer submitOrUpdate(PlatformBuildRequest request, PartnerUserInfoVO partnerUserInfoVO) {
        Integer type = request.getType();
        Long shopId = request.getShopId();
        PlatformBuildDO platformBuildDO = platformBuildDAO.selectOneByShopId(shopId, type);
        PlatformBuildDO platformBuild = request.toDO();
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(shopId);
        Long regionId = shopInfo.getRegionId();
        //抖音
        if (type.equals(PlatformBuildEnum.DOU_YIN.getCode())) {
            ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_18);
            if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_180.getShopSubStageStatus())
                    && !shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_182.getShopSubStageStatus())) {
                throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
            }
            if (Objects.isNull(platformBuildDO)) {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_180, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_181,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.DOU_YIN_CUSTOMER, regionId));
            } else {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_182, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_181,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.DOU_YIN_CUSTOMER, regionId));
                return platformBuildDAO.updateByShopIdAndTypeSelective(platformBuild);
            }
        }
        //快手
        if (type.equals(PlatformBuildEnum.KUAI_SHOU.getCode())) {
            ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_22);
            if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_220.getShopSubStageStatus())
                    && !shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_222.getShopSubStageStatus())) {
                throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
            }
            if (Objects.isNull(platformBuildDO)) {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_220, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_221,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.KUAI_SHOU_CUSTOMER, regionId));
            } else {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_222, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_221,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.KUAI_SHOU_CUSTOMER, regionId));
                return platformBuildDAO.updateByShopIdAndTypeSelective(platformBuild);
            }
        }
        //美团团购
        if (type.equals(PlatformBuildEnum.MEI_TUAN_TUAN_GOU.getCode())) {
            ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_20);
            if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_200.getShopSubStageStatus())
                    && !shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_202.getShopSubStageStatus())) {
                throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
            }
            if (Objects.isNull(platformBuildDO)) {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_200, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_201,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.MEI_TUAN_BUY_CUSTOMER, regionId));
            } else {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_202, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_201,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.MEI_TUAN_BUY_CUSTOMER, regionId));
                return platformBuildDAO.updateByShopIdAndTypeSelective(platformBuild);
            }
        }
        //饿了么
        if (type.equals(PlatformBuildEnum.E_LE_ME.getCode())) {
            ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_21);
            if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_210.getShopSubStageStatus())
                    && !shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_214.getShopSubStageStatus())) {
                throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
            }
            if (Objects.isNull(platformBuildDO)) {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_210, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_211,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.E_L_ME_TUAN_OPERATIONS_CUSTOMER, regionId));
            } else {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_214, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_211,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.E_L_ME_TUAN_OPERATIONS_CUSTOMER, regionId));
                return platformBuildDAO.updateByShopIdAndTypeSelective(platformBuild);
            }
        }
        //美团外卖
        if (type.equals(PlatformBuildEnum.MEI_TUAN_WAI_MAI.getCode())) {
            ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_19);
            if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_190.getShopSubStageStatus())
                    && !shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_194.getShopSubStageStatus())) {
                throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
            }
            if (Objects.isNull(platformBuildDO)) {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_190, SHOP_SUB_STAGE_STATUS_191,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.MEI_TUAN_OPERATIONS_CUSTOMER, regionId));
            } else {
                extracted(partnerUserInfoVO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_194, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_191,
                        userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.MEI_TUAN_OPERATIONS_CUSTOMER, regionId));
                return platformBuildDAO.updateByShopIdAndTypeSelective(platformBuild);
            }
        }
        return platformBuildDAO.insertSelective(platformBuild);

    }

    private void extracted(PartnerUserInfoVO partnerUserInfoVO, Long shopId, ShopSubStageStatusEnum nowShopSubStageStatusEnum, ShopSubStageStatusEnum nextShopSubStageStatusEnum, List<EnterpriseUserDO> operationUserList) {
        shopStageInfoDAO.updateShopStageInfo(shopId, nextShopSubStageStatusEnum);
        operationLogService.addOperationLog(shopId, nowShopSubStageStatusEnum,
                partnerUserInfoVO.getPartnerId(), partnerUserInfoVO.getUsername(), OperationTypeEnum.OPERATION_TYPE_0, null, OperationStatusEnum.PROCESSED);
        operationLogService.addOperationLog(shopId, nextShopSubStageStatusEnum, partnerUserInfoVO.getPartnerId(),
                operationUserList, OperationTypeEnum.OPERATION_TYPE_1, null, OperationStatusEnum.NOT_PROCESSED);
    }

    @Override
    public PlatformBuildInfoVO getPlatformBuildInfo(Long shopId) {
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(shopId);
        if (shopInfo == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_ID_IS_NULL);
        }
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(shopInfo.getLineId());
        List<ScheduleDTO> shopContractActualCompletionTime = shopStageInfoDAO.getPlatformBuildList(Collections.singletonList(shopId));
        PlatformBuildInfoVO platformBuildInfoVO = new PlatformBuildInfoVO();
        Map<Long, String> regionNameMap = regionService.getBelongWarRegionNameMap(Collections.singletonList(shopInfo.getRegionId()));
        String investmentManager = enterpriseUserDAO.getUserName(lineInfo.getInvestmentManager());
        PointInfoDO pointInfoDO = pointInfoDAO.getPointInfoById(shopInfo.getPointId());
        platformBuildInfoVO.setShopId(shopId);
        platformBuildInfoVO.setUsername(lineInfo.getUsername());
        platformBuildInfoVO.setMobile(lineInfo.getMobile());
        platformBuildInfoVO.setJoinStatus(lineInfo.getJoinStatus());
        platformBuildInfoVO.setShopName(shopInfo.getShopName());
        platformBuildInfoVO.setShopCode(shopInfo.getShopCode());
        platformBuildInfoVO.setRegionName(regionNameMap.get(shopInfo.getRegionId()));
        platformBuildInfoVO.setProvince(pointInfoDO.getProvince());
        platformBuildInfoVO.setCity(pointInfoDO.getCity());
        platformBuildInfoVO.setDistrict(pointInfoDO.getDistrict());
        platformBuildInfoVO.setAddress(pointInfoDO.getAddress());
        if (CollectionUtils.isNotEmpty(shopContractActualCompletionTime)) {
            ScheduleDTO scheduleDTO = shopContractActualCompletionTime.get(CommonConstants.ZERO);
            platformBuildInfoVO.setCompletionColumn(scheduleDTO.getCompletionColumn());
            platformBuildInfoVO.setTotalColumn(scheduleDTO.getTotalColumn());

        }
        platformBuildInfoVO.setInvestmentManagerName(investmentManager);
        return platformBuildInfoVO;
    }

    @Override
    public PageInfo<PlatformBuildListResponse> platformBuildList(platformBuildListRequest request) {
        String userId = request.getOperateUserId();
        List<String> regionIds = userAuthMappingService.getAuthRegionIdAndSubRegionIdByUserId(userId);
        if (CollectionUtils.isEmpty(regionIds)) {
            return new PageInfo<>();
        }
        List<Long> regionIdList = new ArrayList<>();
        for (String regionId : regionIds) {
            regionIdList.add(Long.parseLong(regionId));
        }
        List<Long> subRegionIdsByRegionIdList = regionMapper.getSubRegionIdsByRegionIdList(request.getRegionIds());
        if (CollectionUtils.isNotEmpty(subRegionIdsByRegionIdList)) {
            request.setRegionIds(subRegionIdsByRegionIdList);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<PlatformBuildListResponse> responses = shopInfoDAO.platformBuildList(regionIdList, request);
        PageInfo<PlatformBuildListResponse> pageInfo = new PageInfo<>(responses);
        List<Long> shopIds = responses.stream().map(PlatformBuildListResponse::getShopId).collect(Collectors.toList());
        List<Long> regionIds1 = responses.stream().map(PlatformBuildListResponse::getRegionId).collect(Collectors.toList());
        Map<Long, String> regionNameMap = regionService.getBelongWarRegionNameMap(regionIds1);
        List<PlatformBuildStageDTO> shopStageInfos = shopStageInfoDAO.getPlatformBuildStage(shopIds);
        Map<Long, List<PlatformBuildStageDTO>> shopStageInfoMap = shopStageInfos.stream().collect(Collectors.groupingBy(PlatformBuildStageDTO::getShopId));
        for (PlatformBuildListResponse dto : responses) {
            String regionName = regionNameMap.get(dto.getRegionId());
            dto.setRegionName(regionName);
            List<PlatformBuildStageDTO> list = shopStageInfoMap.get(dto.getShopId());
            if (CollectionUtils.isNotEmpty(list)) {
                for (PlatformBuildStageDTO stageDTO : list) {
                    if (stageDTO.getShopSubStage().equals(ShopSubStageEnum.SHOP_STAGE_18.getShopSubStage())) {
                        dto.setDyStageStatus(stageDTO.getShopSubStageStatus());
                    } else if (stageDTO.getShopSubStage().equals(ShopSubStageEnum.SHOP_STAGE_19.getShopSubStage())) {
                        dto.setMtStageStatus(stageDTO.getShopSubStageStatus());
                    } else if (stageDTO.getShopSubStage().equals(ShopSubStageEnum.SHOP_STAGE_20.getShopSubStage())) {
                        dto.setMtBuyStageStatus(stageDTO.getShopSubStageStatus());
                    } else if (stageDTO.getShopSubStage().equals(ShopSubStageEnum.SHOP_STAGE_21.getShopSubStage())) {
                        dto.setElMeStageStatus(stageDTO.getShopSubStageStatus());
                    } else if (stageDTO.getShopSubStage().equals(ShopSubStageEnum.SHOP_STAGE_22.getShopSubStage())) {
                        dto.setKsStageStatus(stageDTO.getShopSubStageStatus());
                    }
                }
            }
        }
        pageInfo.setList(responses);
        return pageInfo;
    }

    @Override
    public PlatformBuildResponse get(Long shopId, Integer type) {
        PlatformBuildDO platformBuildDO = platformBuildDAO.selectOneByShopId(shopId, type);
        PlatformBuildResponse platformBuildResponse = new PlatformBuildResponse();
        if (Objects.isNull(platformBuildDO)) {
            BuildInformationDO informationDO = buildInformationDAO.selectOneByShopId(shopId);
            BeanUtils.copyProperties(informationDO, platformBuildResponse);
            return platformBuildResponse;
        }
        BeanUtils.copyProperties(platformBuildDO, platformBuildResponse);
        if (type.equals(PlatformBuildEnum.E_LE_ME.getCode())) {
            OperationLogDO trial = operationLogDAO.getByCondition(shopId, SHOP_SUB_STAGE_STATUS_211.getShopSubStageStatus());
            ShopAuditInfoDO auditInfo = shopAuditInfoDAO.getAuditInfo(platformBuildDO.getAuditId());
            doSomething(platformBuildResponse, trial, auditInfo);
        } else if (type.equals(PlatformBuildEnum.MEI_TUAN_WAI_MAI.getCode())) {
            OperationLogDO trial = operationLogDAO.getByCondition(shopId, SHOP_SUB_STAGE_STATUS_191.getShopSubStageStatus());
            ShopAuditInfoDO auditInfo = shopAuditInfoDAO.getAuditInfo(platformBuildDO.getAuditId());
            doSomething(platformBuildResponse, trial, auditInfo);

        } else{
        ShopAuditInfoDO auditInfo = shopAuditInfoDAO.getAuditInfo(platformBuildDO.getAuditId());
        if (Objects.nonNull(auditInfo)) {
            if (auditInfo.getResultType().equals(AuditResultTypeEnum.PASS.getCode())) {
                platformBuildResponse.setReason(auditInfo.getPassReason());
            } else {
                platformBuildResponse.setReason(auditInfo.getRejectReason());
            }
        }
    }
        return platformBuildResponse;
}

    private void doSomething(PlatformBuildResponse platformBuildResponse, OperationLogDO trial,  ShopAuditInfoDO auditInfo ) {
        if (Objects.nonNull(trial)) {
            ShopAuditInfoDO atrialAuditInfo = shopAuditInfoDAO.getAuditInfo(trial.getAuditResultId());
            if (Objects.nonNull(atrialAuditInfo)) {
                if (atrialAuditInfo.getResultType().equals(AuditResultTypeEnum.PASS.getCode())) {
                    platformBuildResponse.setReason(atrialAuditInfo.getPassReason());
                } else {
                    platformBuildResponse.setReason(atrialAuditInfo.getRejectReason());
                }
            }
        }
        if (Objects.nonNull(auditInfo)) {
                if (auditInfo.getResultType().equals(AuditResultTypeEnum.PASS.getCode())) {
                    platformBuildResponse.setNewReason(auditInfo.getPassReason());
                } else {
                    platformBuildResponse.setNewReason(auditInfo.getRejectReason());
                }

        }
    }

    @Override
@Transactional(rollbackFor = Exception.class)
public Integer firstAudit(PlatformBuildAuditRequest request) {
    Integer type = request.getType();
    Long shopId = request.getShopId();
    PlatformBuildDO platformBuildDO = platformBuildDAO.selectOneByShopId(shopId, type);
    ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(request.getShopId());
    Long regionId = shopInfo.getRegionId();
    if (type.equals(PlatformBuildEnum.DOU_YIN.getCode())) {
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_18);
        if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_181.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
        }
        ShopAuditInfoDO shopAuditInfoDO = PlatformBuildAuditRequest.convert(request, AuditTypeEnum.DOU_YIN);
        return doThing(platformBuildDO, request, shopAuditInfoDO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_181, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_183, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_182);
    }

    if (type.equals(PlatformBuildEnum.KUAI_SHOU.getCode())) {
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_22);
        if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_221.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
        }
        ShopAuditInfoDO shopAuditInfoDO = PlatformBuildAuditRequest.convert(request, AuditTypeEnum.KUAI_SHOU);
        return doThing(platformBuildDO, request, shopAuditInfoDO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_221, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_223, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_222);

    }
    if (type.equals(PlatformBuildEnum.MEI_TUAN_TUAN_GOU.getCode())) {
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_20);
        if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_201.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
        }
        ShopAuditInfoDO shopAuditInfoDO = PlatformBuildAuditRequest.convert(request, AuditTypeEnum.MEI_TUAN_TUAN_GOU);
        return doThing(platformBuildDO, request, shopAuditInfoDO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_201, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_203, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_202);

    }
    if (type.equals(PlatformBuildEnum.E_LE_ME.getCode())) {
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_21);
        if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_211.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
        }
        ShopAuditInfoDO shopAuditInfoDO = PlatformBuildAuditRequest.convert(request, AuditTypeEnum.E_LE_ME);
        doThing(platformBuildDO, request, shopAuditInfoDO, shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_211, SHOP_SUB_STAGE_STATUS_213, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_214);
        if (request.getResultType().equals(AuditResultTypeEnum.PASS.getCode())) {
            operationLogService.addOperationLog(shopId, SHOP_SUB_STAGE_STATUS_213, request.getOperateUserId(),
                    userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.E_L_ME_HEADQUARTERS_BUILD_CUSTOMER, regionId), OperationTypeEnum.OPERATION_TYPE_1, null, OperationStatusEnum.NOT_PROCESSED);
        }
        return CommonConstants.ONE;
    }

    if (type.equals(PlatformBuildEnum.MEI_TUAN_WAI_MAI.getCode())) {
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_19);
        if (!shopSubStageInfo.getShopSubStageStatus().equals(SHOP_SUB_STAGE_STATUS_191.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
        }
        ShopAuditInfoDO shopAuditInfoDO = PlatformBuildAuditRequest.convert(request, AuditTypeEnum.MEI_TUAN_WAI_MAI);
        doThing(platformBuildDO, request, shopAuditInfoDO, shopId, SHOP_SUB_STAGE_STATUS_191, SHOP_SUB_STAGE_STATUS_193, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_194);
        if (request.getResultType().equals(AuditResultTypeEnum.PASS.getCode())) {
            operationLogService.addOperationLog(shopId, SHOP_SUB_STAGE_STATUS_193, request.getOperateUserId(),
                    userAuthMappingService.getAllUserByRoleEnumAndRegionId(UserRoleEnum.MEI_TUAN_HEADQUARTERS_BUILD_CUSTOMER, regionId), OperationTypeEnum.OPERATION_TYPE_1, null, OperationStatusEnum.NOT_PROCESSED);
        }
        return CommonConstants.ONE;
    }
    return 0;
}

@Override
public Integer secondAudit(PlatformBuildAuditRequest request) {
    Integer type = request.getType();
    Long shopId = request.getShopId();
    PlatformBuildDO platformBuildDO = platformBuildDAO.selectOneByShopId(shopId, type);
    if (type.equals(PlatformBuildEnum.E_LE_ME.getCode())) {
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_21);
        if (!shopSubStageInfo.getShopSubStageStatus().equals(SHOP_SUB_STAGE_STATUS_213.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
        }
        ShopAuditInfoDO shopAuditInfoDO = PlatformBuildAuditRequest.convert(request, AuditTypeEnum.E_LE_ME);
        return doThing(platformBuildDO, request, shopAuditInfoDO, shopId, SHOP_SUB_STAGE_STATUS_213, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_215, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_214);
    }

    if (type.equals(PlatformBuildEnum.MEI_TUAN_WAI_MAI.getCode())) {
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_19);
        if (!shopSubStageInfo.getShopSubStageStatus().equals(SHOP_SUB_STAGE_STATUS_193.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
        }
        ShopAuditInfoDO shopAuditInfoDO = PlatformBuildAuditRequest.convert(request, AuditTypeEnum.MEI_TUAN_WAI_MAI);
        return doThing(platformBuildDO, request, shopAuditInfoDO, shopId, SHOP_SUB_STAGE_STATUS_193, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_195, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_194);
    }
    return 0;
}

private Integer doThing(PlatformBuildDO platformBuildDO, PlatformBuildAuditRequest request, ShopAuditInfoDO shopAuditInfoDO, Long shopId, ShopSubStageStatusEnum nowStatus, ShopSubStageStatusEnum passStatus, ShopSubStageStatusEnum rejectStatus) {
    Long auditId = shopAuditInfoDAO.addAuditInfo(shopAuditInfoDO);
    if (request.getResultType().equals(AuditResultTypeEnum.PASS.getCode())) {
        shopStageInfoDAO.updateShopStageInfo(shopId, passStatus);
    } else {
        shopStageInfoDAO.updateShopStageInfo(shopId, rejectStatus);
    }
    List<OperationLogDO> operationLogs = operationLogDAO.getBySubStageStatusEnumAndsStatus(shopId, nowStatus, OperationTypeEnum.OPERATION_TYPE_1.getCode());
    operationLogService.batchUpdateProcessed(operationLogs, auditId, request.getOperateUserId(), request.getReason());
    platformBuildDO.setAuditId(auditId);
    platformBuildDO.setUpdateTime(new Date());
    platformBuildDO.setUpdateUser(request.getOperateUserId());
    return platformBuildDAO.updateByShopIdAndTypeSelective(platformBuildDO);
}
}




