package com.cool.store.service.impl;

import cn.hutool.core.convert.Convert;
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.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.*;
import com.cool.store.request.AddSignFranchiseRequest;
import com.cool.store.request.AuditResultRequest;
import com.cool.store.request.FranchiseAgreementRequest;
import com.cool.store.response.AddSignFranchiseResponse;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.CoolStoreStartFlowService;
import com.cool.store.service.PreparationService;
import com.cool.store.service.SignFranchiseService;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.utils.poi.DateUtils;
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.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class SignFranchiseServiceImpl implements SignFranchiseService, AuditResultService {
    @Resource
    SignFranchiseMapper signFranchiseMapper;

    @Resource
    CoolStoreStartFlowService coolStoreStartFlowService;

    @Resource
    FranchiseFeeMapper franchiseFeeMapper;

    @Resource
    ShopInfoMapper shopInfoMapper;

    @Resource
    IntentAgreementMapper intentAgreementMapper;

    @Resource
    LinePayMapper linePayMapper;

    @Resource
    PointInfoMapper pointInfoMapper;

    @Resource
    LineInfoMapper lineInfoMapper;

    @Resource
    SystemBuildingShopMapper systemBuildingShopMapper;

    @Resource
    ApplyLicenseMapper applyLicenseMapper;

    @Resource
    ThirdDepartmentMapper thirdDepartmentMapper;

    @Resource
    JoinIntentionMapper joinIntentionMapper;

    @Resource
    ShopStageInfoDAO shopStageInfoDAO;

    @Resource
    ShopAuditInfoMapper shopAuditInfoMapper;

    @Resource
    PreparationService preparationService;

    @Resource
    CommonService commonService;

    @Resource
    RedisUtilPool redisUtilPool;


    @Override
    public Boolean auditResult(AuditResultRequest request) {
        log.info("SignFranchiseServiceImpl auditResult request：{}", JSONObject.toJSONString(request));
        Long shopId = getShopId(request.getKdzBusinessId());
        ShopSubStageStatusEnum shopSubStageStatusEnum = null;
        if (request.getAuditResult() == 1) {
            shopSubStageStatusEnum = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_84;
        } else if (request.getAuditResult() == 0) {
            shopSubStageStatusEnum = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_85;
        }
        ShopAuditInfoDO shopAuditInfoDO = new ShopAuditInfoDO();
        shopAuditInfoDO.setShopId(shopId);
        shopAuditInfoDO.setAuditType(AuditTypeEnum.SIGN_FRANCHISE.getCode());
        shopAuditInfoDO.setSubmittedUserId("");
        shopAuditInfoDO.setSubmittedUserName("");
        if (Constants.ZERO_INTEGER.equals(request.getAuditResult())) {
            shopAuditInfoDO.setResultType(Constants.ONE_INTEGER);
            shopAuditInfoDO.setRejectReason(request.getCause());
            Map<String, String> requestMap = new HashMap<>();
            ShopInfoDO shopInfoDO = shopInfoMapper.selectByPrimaryKey(shopId);
            LineInfoDO lineInfo = lineInfoMapper.getByLineId(shopInfoDO.getLineId());
            requestMap.put("storeName", shopInfoDO.getShopName());
            requestMap.put("partnerName", lineInfo.getUsername());
            requestMap.put("partnerMobile", lineInfo.getMobile());
            requestMap.put("lineId", String.valueOf(lineInfo.getId()));
            requestMap.put("shopId", String.valueOf(shopInfoDO.getId()));
            commonService.sendMessage(Collections.singletonList(lineInfo.getInvestmentManager()), MessageEnum.MESSAGE_20, requestMap);
        } else if (Constants.ONE_INTEGER.equals(request.getAuditResult())) {
            shopAuditInfoDO.setResultType(Constants.ZERO_INTEGER);
            shopAuditInfoDO.setPassReason(request.getCause());
            //校验建店与加盟签约合同是否完成 并初始化后续流程数据
            preparationService.contractAndBuildStoreCompletion(shopId);
        }
        shopAuditInfoMapper.insertSelective(shopAuditInfoDO);
        Long auditId = shopAuditInfoDO.getId();
        shopStageInfoDAO.updateShopStageAndAuditInfo(shopId, shopSubStageStatusEnum, auditId);
        signFranchiseMapper.updateAuditByShopId(auditId, shopId);
        return true;
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult submitSignFranchise(AddSignFranchiseRequest request, LoginUserInfo user) {
        log.info("submitSignFranchise request:{}", JSONObject.toJSONString(request));
        if (Objects.isNull(request)) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        if (Objects.isNull(request.getShopId())) {
            throw new ServiceException(ErrorCodeEnum.SHOP_ID_NOT_EXIST);
        }
        String lockKey = "submitSignFranchise:" + request.getShopId();
        //流水
        String lockValue = UUID.randomUUID().toString();
        boolean acquired = false;
        try {
            //10s过期
            acquired = redisUtilPool.setNxExpire(lockKey, lockValue, CommonConstants.TEN_SECONDS);
            if (Boolean.TRUE.equals(acquired)) {
                SignFranchiseDO signFranchiseDO = request.toSignFranchiseDO();
                if (Objects.isNull(request.getId())) {
                    signFranchiseMapper.insertSelective(signFranchiseDO);
                } else {
                    signFranchiseMapper.updateByPrimaryKeySelective(signFranchiseDO);
                }
                //店铺信息
                ShopInfoDO shopInfoDO = shopInfoMapper.selectByPrimaryKey(request.getShopId());
                FranchiseAgreementRequest franchiseAgreementRequest = convertFranchiseAgreement(request, shopInfoDO, user);
                MemberQuestionDO memberQuestionDO = joinIntentionMapper.getByLineId(shopInfoDO.getLineId());
                log.info("submitSignFranchise franchiseAgreementRequest :{}", JSONObject.toJSONString(franchiseAgreementRequest));
                ResponseResult responseResult = coolStoreStartFlowService.franchiseAgreement(franchiseAgreementRequest, memberQuestionDO.getJoinType());
                //更新状态为加盟商
                LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(shopInfoDO.getLineId());
                lineInfoDO.setJoinStatus(2);
                lineInfoMapper.insertOrUpdate(lineInfoDO);
                shopStageInfoDAO.updateShopStageInfo(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_83);
                return responseResult;
            } 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);
                }
            }
        }

    }


    public FranchiseAgreementRequest convertFranchiseAgreement(AddSignFranchiseRequest request,
                                                               ShopInfoDO shopInfoDO,
                                                               LoginUserInfo user) {

        //shopId
        Long shopId = request.getShopId();
        //线索信息
        LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(shopInfoDO.getLineId());
        if (Objects.isNull(lineInfoDO)) {
            throw new ServiceException(109014, ApiErrorCodeEnum.getByCode("lineInfoDO").getErrorMessage());
        }
        //缴费信息
        FranchiseFeeDO franchiseFeeDO = franchiseFeeMapper.selectByShopId(shopId);
        if (Objects.isNull(franchiseFeeDO)) {
            throw new ServiceException(109014, ApiErrorCodeEnum.getByCode("franchiseFeeDO").getErrorMessage());
        }
        //线索id
        Long lineId = shopInfoDO.getLineId();
        //签约信息
        SigningBaseInfoDO signingBaseInfoDO = intentAgreementMapper.selectByPartnerIdOrLineId(null, lineId);
        if (Objects.isNull(signingBaseInfoDO)) {
            throw new ServiceException(109014, ApiErrorCodeEnum.getByCode("signingBaseInfoDO").getErrorMessage());
        }
        //缴款信息
        LinePayDO linePayDO = linePayMapper.getByLineIdAndPayTypeAndShopId(lineId, 1, shopId);
        if (Objects.isNull(linePayDO)) {
            throw new ServiceException(109014, ApiErrorCodeEnum.getByCode("linePayDO").getErrorMessage());
        }
        //铺位信息
        PointInfoDO pointInfoDO = pointInfoMapper.getDataByShopIdAndLineId(lineId, shopId);
        if (Objects.isNull(pointInfoDO)) {
            throw new ServiceException(109014, ApiErrorCodeEnum.getByCode("pointInfoDO").getErrorMessage());
        }
        //建店数据
        SystemBuildingShopDO systemBuildingShopDO = systemBuildingShopMapper.selectOne(SystemBuildingShopDO.builder().shopId(shopId).lineId(lineId).build());
        if (Objects.isNull(systemBuildingShopDO)) {
            throw new ServiceException(109014, ApiErrorCodeEnum.getByCode("systemBuildingShopDO").getErrorMessage());
        }
        //证照办理
        LicenseTransactDO licenseTransactDO = applyLicenseMapper.selectByShopId(shopId);
        if (Objects.isNull(licenseTransactDO)) {
            throw new ServiceException(109014, ApiErrorCodeEnum.getByCode("licenseTransactDO").getErrorMessage());
        }
        ThirdDepartmentDO thirdDepartmentDO = new ThirdDepartmentDO();
        if (Objects.nonNull(systemBuildingShopDO)) {
            //三方部门表
            thirdDepartmentDO = thirdDepartmentMapper.getByName(systemBuildingShopDO.getBigName());
            if (Objects.isNull(thirdDepartmentDO)) {
                throw new ServiceException(109014, ApiErrorCodeEnum.getByCode("thirdDepartmentDO").getErrorMessage());
            }
        }
        //找不到的url统一用这个
        List<String> temp = new ArrayList<>();
        temp.add("object");
        return FranchiseAgreementRequest.builder()
                .apply_user(user.getJobNumber())
                .company(0)
                .start1(getDateTimeFormat(Constants.TIME_STAMP_FLAG_TWO, franchiseFeeDO.getFirstYearStartTime()))
                .start2(getDateTimeFormat(Constants.TIME_STAMP_FLAG_TWO, franchiseFeeDO.getSecondYearStartTime()))
                .start3(getDateTimeFormat(Constants.TIME_STAMP_FLAG_TWO, franchiseFeeDO.getThirdYearStartTime()))
                .end1(getDateTimeFormat(Constants.TIME_STAMP_FLAG_TWO, franchiseFeeDO.getFirstYearEndTime()))
                .end2(getDateTimeFormat(Constants.TIME_STAMP_FLAG_TWO, franchiseFeeDO.getSecondYearEndTime()))
                .end3(getDateTimeFormat(Constants.TIME_STAMP_FLAG_TWO, franchiseFeeDO.getThirdYearEndTime()))
                .fjsc(temp)
                .fqgsmc("鲜丰水果股份有限公司")
                .fzzd1(0)
                .gczxhtfqr("20230724")//没有
                .hkbzj(Integer.valueOf(franchiseFeeDO.getLoanMargin()))
                .hkbzjdx(Integer.valueOf(franchiseFeeDO.getLoanMargin()))
                .hkbzjwb(Convert.digitToChinese(Integer.valueOf(franchiseFeeDO.getLoanMargin())))
                .ht_day(timeUtils(request.getContractStartTime(), "day"))
                .ht_day2(timeUtils(request.getContractStartEndTime(), "day"))
                .ht_month1(timeUtils(request.getContractStartTime(), "month"))
                .ht_month2(timeUtils(request.getContractStartEndTime(), "month"))
                .ht_year1(timeUtils(request.getContractStartTime(), "year"))
                .ht_year2(timeUtils(request.getContractStartEndTime(), "year"))
                .idcard_1(Collections.singletonList(signingBaseInfoDO.getIdCardFront()))
                .idcard_2(Collections.singletonList(signingBaseInfoDO.getIdCardReverse()))
                .idcard_address(signingBaseInfoDO.getIdCardAddress())
                .is_business_license(request.getIsBusinessLicense())
                .is_deposit(1)
                .is_food_license(request.getIsFoodLicense())
                .jiaokuandate(getDateTimeFormat(Constants.TIME_STAMP_FLAG, linePayDO.getPayTime()))
                .jkr(linePayDO.getPayUserName())
                .jmd_area(pointInfoDO.getDistrict())
                .jmd_city(pointInfoDO.getCity())
                .jmd_province(pointInfoDO.getProvince())
                .jmd_street(pointInfoDO.getTownship())
                .jmf(Integer.valueOf(franchiseFeeDO.getYearFranchiseFee()))
                .jmf2(Integer.valueOf(franchiseFeeDO.getFirstYearFee()))
                .jmf3(Integer.valueOf(franchiseFeeDO.getSecondYearFee()))
                .jmf4(Integer.valueOf(franchiseFeeDO.getThirdYearFee()))
                .jmfday1(timeUtils(franchiseFeeDO.getFirstYearStartTime(), "day"))
                .jmfday2(timeUtils(franchiseFeeDO.getFirstYearEndTime(), "day"))
                .jmfday3(timeUtils(franchiseFeeDO.getSecondYearStartTime(), "day"))
                .jmfday4(timeUtils(franchiseFeeDO.getSecondYearEndTime(), "day"))
                .jmfday5(timeUtils(franchiseFeeDO.getThirdYearStartTime(), "day"))
                .jmfday6(timeUtils(franchiseFeeDO.getThirdYearEndTime(), "day"))
                .jmfdx(Integer.valueOf(franchiseFeeDO.getYearFranchiseFee()))
                .jmfmonth1(timeUtils(franchiseFeeDO.getFirstYearStartTime(), "month"))
                .jmfmonth2(timeUtils(franchiseFeeDO.getFirstYearEndTime(), "month"))
                .jmfmonth3(timeUtils(franchiseFeeDO.getSecondYearStartTime(), "month"))
                .jmfmonth4(timeUtils(franchiseFeeDO.getSecondYearEndTime(), "month"))
                .jmfmonth5(timeUtils(franchiseFeeDO.getThirdYearStartTime(), "month"))
                .jmfmonth6(timeUtils(franchiseFeeDO.getThirdYearEndTime(), "month"))
                .jmfwb(Convert.digitToChinese(Integer.valueOf(franchiseFeeDO.getYearFranchiseFee())))
                .jmfyear1(timeUtils(franchiseFeeDO.getFirstYearStartTime(), "year"))
                .jmfyear2(timeUtils(franchiseFeeDO.getFirstYearEndTime(), "year"))
                .jmfyear3(timeUtils(franchiseFeeDO.getSecondYearStartTime(), "year"))
                .jmfyear4(timeUtils(franchiseFeeDO.getSecondYearEndTime(), "year"))
                .jmfyear5(timeUtils(franchiseFeeDO.getThirdYearStartTime(), "year"))
                .jmfyear6(timeUtils(franchiseFeeDO.getThirdYearEndTime(), "year"))
                .jmsdkjt(temp)
                .jmsdm(lineInfoDO.getPartnerNum())
                .jmshtzt(0)
                .jmslx(systemBuildingShopDO.getFranchiseeType())
                .jmslxfs(request.getMobile())
                .jmssfzhm(signingBaseInfoDO.getIdCardNo())
                .jmsxm(lineInfoDO.getPartnerNum())
                .jmsxm1(lineInfoDO.getUsername())
                .jsrq(getDateTimeFormat(Constants.TIME_STAMP_FLAG, request.getContractStartEndTime()))
                .jszhhz(linePayDO.getPayAccount())
                .kdzBusinessId(AuditEnum.SIGN_FRANCHISE.getCode() + "_" + shopId + "_" + lineId)
                .khhjzh(linePayDO.getBankName())
                .lvbzj(Integer.valueOf(franchiseFeeDO.getPerformanceBond()))
                .lybzjdx(Integer.valueOf(franchiseFeeDO.getPerformanceBond()))
                .lybzjwb(Convert.digitToChinese(Integer.valueOf(franchiseFeeDO.getPerformanceBond())))
                .mddz(pointInfoDO.getAddress())
                .mdmc(shopInfoDO.getStoreNum())
                .mdmcwb(systemBuildingShopDO.getStoreName())
                .oacompany(thirdDepartmentDO.getDepartmentCode())
                .partyb_name(licenseTransactDO.getBusinessLicense())
                .partyb_operator(licenseTransactDO.getOperator())
                .partyb_type(licenseTransactDO.getLicenseType())
                .ppsyfbl(Double.parseDouble(request.getBrandFee()))
                .psfl("以实际结算为准")
                .qsdz("PUBLIC_COMPANY,PUBLIC_OPERATOR")
                .qsrq(getDateTimeFormat(Constants.TIME_STAMP_FLAG, request.getContractStartTime()))
                .qylx(SignTypeEnum.getWorkflowStageByCode(request.getSignType()).getCode())
                .qys_dzyz("2731707107593166911")
                .qys_htywfl("3051420203180101971")
                .reason2(request.getIrregularReason())
                .remark(request.getRemark())
                .remark2(systemBuildingShopDO.getReasons())
                .sfbq__add(request.getResign())
                .sj1(getDateTimeFormat(Constants.TIME_STAMP_FLAG_THREE, request.getBusinessStartHours()))
                .sj2(getDateTimeFormat(Constants.TIME_STAMP_FLAG_THREE, request.getBusinessEndHours()))
                .social_credit_code(licenseTransactDO.getCreditCode())
                .spltxkz(temp)
                .sqr_name(user.getName())
                .sqrq(getDateTimeFormat(Constants.TIME_STAMP_FLAG, new Date()))
                .title("特许经营合同审批申请2023-" + lineInfoDO.getUsername() + "-" + DateUtils.getDate())
                .wlyzbm("201905081151092R")
                .yycs(Integer.valueOf(request.getImpressionNum()))
                .yyzzblsqs(temp)
                .yyzzfyj(temp)
                .zhihang(linePayDO.getBranchBankName())
                .zqyty(SignTypeEnum.getWorkflowStageByCode(request.getSignType()).getSap())
                .fddbr(licenseTransactDO.getLicenseLegalPerson())
                .build();
    }


    @Override
    public AddSignFranchiseResponse getSignFranchise(Long shopId) {
        SignFranchiseDO signFranchiseDO = signFranchiseMapper.selectByShopId(shopId);
        AddSignFranchiseResponse response = from(signFranchiseDO, shopId);
        ShopAuditInfoDO shopAuditInfoDO = null;
        if (Objects.nonNull(signFranchiseDO)) {
            shopAuditInfoDO = shopAuditInfoMapper.selectByPrimaryKey(signFranchiseDO.getAuditId());
        }
        if (Objects.nonNull(shopAuditInfoDO)) {
            response.setStatus(shopAuditInfoDO.getResultType());
            if (shopAuditInfoDO.getResultType() == 0) {
                response.setResult(shopAuditInfoDO.getPassReason());
            } else {
                response.setResult(shopAuditInfoDO.getRejectReason());
            }
            response.setResultTime(shopAuditInfoDO.getCreateTime());
        }
        return response;
    }

    public AddSignFranchiseResponse from(SignFranchiseDO signFranchiseDO,
                                         Long shopId) {
        AddSignFranchiseResponse addSignFranchiseResponse = new AddSignFranchiseResponse();
        //店铺信息
        ShopInfoDO shopInfoDO = shopInfoMapper.selectByPrimaryKey(shopId);
        //线索信息
        LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(shopInfoDO.getLineId());
        //加盟问卷信息
        MemberQuestionDO memberQuestionDO = joinIntentionMapper.getByLineId(shopInfoDO.getLineId());
        //缴费信息
        FranchiseFeeDO franchiseFeeDO = franchiseFeeMapper.selectOne(FranchiseFeeDO.builder().shopId(shopId).build());
        //线索id
        Long lineId = shopInfoDO.getLineId();
        //签约信息
        SigningBaseInfoDO signingBaseInfoDO = intentAgreementMapper.selectByPartnerIdOrLineId(null, lineId);
        //缴款信息
        LinePayDO linePayDO = linePayMapper.getByLineIdAndPayTypeAndShopId(lineId, 1, shopId);
        //铺位信息
        PointInfoDO pointInfoDO = pointInfoMapper.getDataByShopIdAndLineId(lineId, shopId);
        //建店数据
        SystemBuildingShopDO systemBuildingShopDO = systemBuildingShopMapper.selectOne(SystemBuildingShopDO.builder().shopId(shopId).lineId(lineId).build());
        //证照办理
        LicenseTransactDO licenseTransactDO = applyLicenseMapper.selectByShopId(shopId);
        if (Objects.nonNull(signFranchiseDO)) {
            addSignFranchiseResponse.setId(signFranchiseDO.getId());
            addSignFranchiseResponse.setShopId(signFranchiseDO.getShopId());
            addSignFranchiseResponse.setSignType(signFranchiseDO.getSignType());
            addSignFranchiseResponse.setBrandFee(signFranchiseDO.getBrandFee());
            addSignFranchiseResponse.setIsBusinessLicense(signFranchiseDO.getIsBusinessLicense());
            addSignFranchiseResponse.setIsFoodLicense(signFranchiseDO.getIsFoodLicense());
            addSignFranchiseResponse.setContractStartTime(signFranchiseDO.getContractStartTime());
            addSignFranchiseResponse.setContractStartEndTime(signFranchiseDO.getContractEndTime());
            addSignFranchiseResponse.setImpressionNum(signFranchiseDO.getImpressionNum());
            addSignFranchiseResponse.setBusinessStartHours(signFranchiseDO.getBusinessStartHours());
            addSignFranchiseResponse.setBusinessEndHours(signFranchiseDO.getBusinessEndHours());
            addSignFranchiseResponse.setIrregularReason(signFranchiseDO.getIrregularReason());
            addSignFranchiseResponse.setRemark(signFranchiseDO.getRemark());
            addSignFranchiseResponse.setResign(signFranchiseDO.getResign());
            addSignFranchiseResponse.setMobile(signFranchiseDO.getMobile());
        } else {
            addSignFranchiseResponse.setMobile(lineInfoDO.getMobile());
        }
        if (Objects.nonNull(systemBuildingShopDO)) {
            addSignFranchiseResponse.setStoreName(systemBuildingShopDO.getStoreName());
            addSignFranchiseResponse.setBigRegion(systemBuildingShopDO.getBigName());
            addSignFranchiseResponse.setBigRegionManager(systemBuildingShopDO.getRegioGeneral());
            addSignFranchiseResponse.setFranchiseeType(Objects.requireNonNull(FranchiseeTypeEnum.getByCode(systemBuildingShopDO.getFranchiseeType())).getMessage());
            addSignFranchiseResponse.setReasons(systemBuildingShopDO.getReasons());
        }
        if (Objects.nonNull(pointInfoDO)) {
            addSignFranchiseResponse.setStoreAddress(pointInfoDO.getAddress());
        }
        if (Objects.nonNull(linePayDO)) {
            addSignFranchiseResponse.setPayName(linePayDO.getPayUserName());
            addSignFranchiseResponse.setBankCardNo(linePayDO.getPayAccount());
            addSignFranchiseResponse.setBank(linePayDO.getBankName());
            addSignFranchiseResponse.setSubBank(linePayDO.getBranchBankName());
            addSignFranchiseResponse.setPayDate(linePayDO.getPayTime());
        }
        if (Objects.nonNull(licenseTransactDO)) {
            addSignFranchiseResponse.setLicenseName(licenseTransactDO.getBusinessLicense());
            addSignFranchiseResponse.setOpeAddress(licenseTransactDO.getLicenseAddress());
            addSignFranchiseResponse.setOperator(licenseTransactDO.getOperator());
            addSignFranchiseResponse.setLicenseCode(licenseTransactDO.getCreditCode());
            addSignFranchiseResponse.setType(LicenseTypeEnum.match(licenseTransactDO.getLicenseType()).getMessage());
        }
        if (Objects.nonNull(lineInfoDO)) {
            addSignFranchiseResponse.setPartnerName(lineInfoDO.getUsername());
        }
        if (Objects.nonNull(signingBaseInfoDO) && Objects.nonNull(memberQuestionDO)) {
            if (JoinTypeEnum.JOIN_TYPE_ONE.getCode().equals(memberQuestionDO.getJoinType())) {
                addSignFranchiseResponse.setIdCardNo(signingBaseInfoDO.getIdCardNo());
            } else if (JoinTypeEnum.JOIN_TYPE_TWO.getCode().equals(memberQuestionDO.getJoinType())) {
                addSignFranchiseResponse.setIdCardNo(signingBaseInfoDO.getBusinessLicenseCode());
            }
            addSignFranchiseResponse.setIdCardAddress(signingBaseInfoDO.getIdCardAddress());
        }
        if (Objects.nonNull(franchiseFeeDO)) {
            addSignFranchiseResponse.setYearFranchiseFee(franchiseFeeDO.getYearFranchiseFee());
            addSignFranchiseResponse.setBigYearFranchiseFee(Convert.digitToChinese(Integer.valueOf(franchiseFeeDO.getYearFranchiseFee())));
            addSignFranchiseResponse.setLoanMargin(franchiseFeeDO.getLoanMargin());
            addSignFranchiseResponse.setBigLoanMargin(Convert.digitToChinese(Integer.valueOf(franchiseFeeDO.getLoanMargin())));
            addSignFranchiseResponse.setFirstYearStartTime(franchiseFeeDO.getFirstYearStartTime());
            addSignFranchiseResponse.setFirstYearEndTime(franchiseFeeDO.getFirstYearEndTime());
            addSignFranchiseResponse.setFirstYearFee(franchiseFeeDO.getFirstYearFee());
            addSignFranchiseResponse.setSecondYearStartTime(franchiseFeeDO.getSecondYearStartTime());
            addSignFranchiseResponse.setSecondYearEndTime(franchiseFeeDO.getSecondYearEndTime());
            addSignFranchiseResponse.setSecondYearFee(franchiseFeeDO.getSecondYearFee());
            addSignFranchiseResponse.setThirdYearStartTime(franchiseFeeDO.getThirdYearStartTime());
            addSignFranchiseResponse.setThirdYearEndTime(franchiseFeeDO.getThirdYearEndTime());
            addSignFranchiseResponse.setThirdYearFee(franchiseFeeDO.getThirdYearFee());
            addSignFranchiseResponse.setPerformanceBond(franchiseFeeDO.getPerformanceBond());
            addSignFranchiseResponse.setBigPerformanceBond(Convert.digitToChinese(Integer.valueOf(franchiseFeeDO.getPerformanceBond())));
        }
        return addSignFranchiseResponse;
    }

    public static String getDateTimeFormat(String pattern, Date date) {
        return new SimpleDateFormat(pattern).format(date);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean rePay(Long shopId) {
        shopStageInfoDAO.updateShopStageInfo(shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_74);
        shopStageInfoDAO.updateShopStageToNotStarted(shopId, ShopSubStageEnum.SHOP_STAGE_8);
        return true;
    }

    private int timeUtils(Date time, String type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        switch (type) {
            case "year":
                return calendar.get(Calendar.YEAR);
            case "month":
                return calendar.get(Calendar.MONTH) + 1;
            case "day":
                return calendar.get(Calendar.DAY_OF_MONTH);
            default:
                return 0;
        }

    }
}
