package com.cool.store.response;

import cn.hutool.core.date.DateUtil;
import com.cool.store.entity.LicenseTransactDO;
import com.cool.store.entity.ShopAuditInfoDO;
import com.cool.store.enums.LicenseTypeEnum;
import com.cool.store.vo.ShopAuditInfoVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Data
public class SubmitLicenseResponse {

    @ApiModelProperty("审批人")
    private String approver;

    @ApiModelProperty("主键id")
    private Long id;

    private Long shopId;

    @ApiModelProperty("营业执照拍照上传")
    private String licenseUrl;

    @ApiModelProperty("营业执照名称")
    private String licenseName;

    @ApiModelProperty("营业执照类型 0.个体工商户 1.有限责任公司 2.独资企业 3.自然人经营")
    private Integer licenseType;

    @ApiModelProperty("营业执照上的法人")
    private String licenseLegalPerson;

    @ApiModelProperty("统一社会信用代码")
    private String socialCreditCode;

    @ApiModelProperty("发证日期")
    private Date issueTime;

    @ApiModelProperty("营业执照经营场所")
    private String licenseAddress;

    @ApiModelProperty("有效期(如果为空则是长期有效)")
    private Date validity;

    @ApiModelProperty("有效期类型 0：长期有效（不用传validity） 1：效期内")
    private Integer validityType;


    @ApiModelProperty("法人双手持身份证正面+营业执照")
    private String idCardAndLicense1;

    @ApiModelProperty("法人双手持身份证反面+营业执照")
    private String idCardAndLicense2;

    @ApiModelProperty("食品经营许可证图片上传")
    private String foodLicenseUrl;

    @ApiModelProperty("经营者")
    private String operator;

    @ApiModelProperty("食品经营许可证上的法人")
    private String foodLicenseLegalPerson;

    @ApiModelProperty("食营经营场所")
    private String businessPremises;

    @ApiModelProperty("主体业态")
    private String mainBusiness;

    @ApiModelProperty("经营项目")
    private String businessProject;

    @ApiModelProperty("许可证编号")
    private String foodLicenseCode;

    @ApiModelProperty("许可证开始时间")
    private Date foodLicenseStartTime;

    @ApiModelProperty("许可证截止时间")
    private Date foodLicenseEndTime;

    @ApiModelProperty("营业执照备注")
    private String businessLicenseRemark;

    @ApiModelProperty("备注")
    private String foodLicenseRemark;

    @ApiModelProperty("备注图片")
    private String remarkUrl;

    @ApiModelProperty("0:保存 1：提交到待审核 2:未通过 3:已通过 ")
    private Integer submitStatus;

    @ApiModelProperty("流程记录")
    private List<ShopAuditInfoVO> processRecords;

    @ApiModelProperty("鲜丰拉取方法体")
    private StoreDocument storeDocument;

    @Data
    public static class StoreDocument {
        @ApiModelProperty("营业执照拍照上传")
        private String licenseUrl;

        @ApiModelProperty("营业执照名称")
        private String licenseName;

        @ApiModelProperty("营业执照类型 0:有限责任公司 1:工体工商")
        private Integer licenseType;

        @ApiModelProperty("营业执照上的法人")
        private String licenseLegalPerson;

        @ApiModelProperty("统一社会信用代码")
        private String socialCreditCode;

        @ApiModelProperty("发证日期")
        private Date issueTime;

        @ApiModelProperty("营业执照经营场所")
        private String licenseAddress;

        @ApiModelProperty("有效期(如果为空则是长期有效)")
        private Date validity;

        @ApiModelProperty("有效期类型 0：长期有效（不用传validity） 1：效期内")
        private Integer validityType;


        @ApiModelProperty("法人双手持身份证正面+营业执照")
        private String idCardAndLicense1;

        @ApiModelProperty("法人双手持身份证反面+营业执照")
        private String idCardAndLicense2;

        @ApiModelProperty("食品经营许可证图片上传")
        private String foodLicenseUrl;

        @ApiModelProperty("经营者")
        private String operator;

        @ApiModelProperty("食品经营许可证上的法人")
        private String foodLicenseLegalPerson;

        @ApiModelProperty("食营经营场所")
        private String businessPremises;

        @ApiModelProperty("主体业态")
        private String mainBusiness;

        @ApiModelProperty("经营项目")
        private String businessProject;

        @ApiModelProperty("许可证编号")
        private String foodLicenseCode;

        @ApiModelProperty("许可证开始时间")
        private Date foodLicenseStartTime;

        @ApiModelProperty("许可证截止时间")
        private Date foodLicenseEndTime;

        @ApiModelProperty("备注")
        private String remark;

        @ApiModelProperty("备注图片")
        private String remarkUrl;

    }

    public static SubmitLicenseResponse from(GetStoreInfoByCodeResponse storeInfoCode,
                                             SubmitLicenseResponse submitLicenseResponse){
        if (Objects.isNull(submitLicenseResponse)){
            submitLicenseResponse = new SubmitLicenseResponse();
        }
        GetStoreInfoByCodeResponse.StoreDocument storeDocument = storeInfoCode.getData().getStoreDocument();
        boolean storeBusinessValidPeriod = "9".equals(storeDocument.getStoreBusinessValidPeriod());
        submitLicenseResponse.setLicenseType(LicenseTypeEnum.matchName(storeDocument.getStoreBusinessType()).getCode());
        submitLicenseResponse.setLicenseLegalPerson(storeDocument.getStoreDirector());
        submitLicenseResponse.setIssueTime(DateUtil.parseDate(storeDocument.getStoreBusinessDate()));
        submitLicenseResponse.setLicenseAddress(storeDocument.getStoreBusinessAdd());
        submitLicenseResponse.setValidity(storeBusinessValidPeriod ? null : DateUtil.parse(storeDocument.getStoreBusinessValidPeriod(),"yyyy-MM-dd"));
        submitLicenseResponse.setValidityType(storeBusinessValidPeriod ? 0 : 1);
        submitLicenseResponse.setOperator(storeDocument.getStoreFoodLicenseOperatorName());
        submitLicenseResponse.setFoodLicenseLegalPerson(storeDocument.getStoreFoodLicenseLegalRepresentative());
        submitLicenseResponse.setMainBusiness(storeDocument.getStoreFoodLicenseMainBusiness());
        submitLicenseResponse.setBusinessProject(storeDocument.getStoreFoodLicenseBusinessScope());
        submitLicenseResponse.setLicenseUrl(storeDocument.getStoreBusinessImg());
        submitLicenseResponse.setLicenseName(storeDocument.getStoreBusinessName());
        submitLicenseResponse.setSocialCreditCode(storeDocument.getStoreBusinessNumber());
        submitLicenseResponse.setFoodLicenseUrl(storeDocument.getStoreFoodLicenseImg());
        submitLicenseResponse.setBusinessPremises(storeDocument.getStoreFoodLicenseBusinessAddress());
        submitLicenseResponse.setFoodLicenseCode(storeDocument.getStoreFoodLicense());
        submitLicenseResponse.setFoodLicenseStartTime(DateUtil.parseDate(storeDocument.getStoreFoodLicenseBeginDate()));
        submitLicenseResponse.setFoodLicenseEndTime(DateUtil.parseDate(storeDocument.getStoreFoodLicenseEndDate()));
        return submitLicenseResponse;
    }

    public static SubmitLicenseResponse from(LicenseTransactDO licenseTransactDO) {
        if (Objects.isNull(licenseTransactDO)) {
            return null;
        }
        SubmitLicenseResponse submitLicenseResponse = new SubmitLicenseResponse();
        submitLicenseResponse.setId(licenseTransactDO.getId());
        submitLicenseResponse.setShopId(licenseTransactDO.getShopId());
        submitLicenseResponse.setLicenseType(licenseTransactDO.getLicenseType());
        submitLicenseResponse.setLicenseLegalPerson(licenseTransactDO.getLicenseLegalPerson());
        submitLicenseResponse.setIssueTime(licenseTransactDO.getIssueTime());
        submitLicenseResponse.setLicenseAddress(licenseTransactDO.getLicenseAddress());
        submitLicenseResponse.setValidity(licenseTransactDO.getValidity());
        if (Objects.nonNull(licenseTransactDO.getValidity())){
            submitLicenseResponse.setValidityType(1);
        }else {
            submitLicenseResponse.setValidityType(0);
        }
        submitLicenseResponse.setOperator(licenseTransactDO.getOperator());
        submitLicenseResponse.setFoodLicenseLegalPerson(licenseTransactDO.getFoodLicenseLegalPerson());
        submitLicenseResponse.setMainBusiness(licenseTransactDO.getMainBusiness());
        submitLicenseResponse.setBusinessProject(licenseTransactDO.getBusinessProject());
        submitLicenseResponse.setFoodLicenseRemark(licenseTransactDO.getFoodLicenseRemark());
        submitLicenseResponse.setBusinessLicenseRemark(licenseTransactDO.getBusinessLicenseRemark());
        submitLicenseResponse.setRemarkUrl(licenseTransactDO.getRemarkUrl());
        submitLicenseResponse.setSubmitStatus(licenseTransactDO.getSubmitStatus());
        submitLicenseResponse.setLicenseUrl(licenseTransactDO.getCreditUrl());
        submitLicenseResponse.setLicenseName(licenseTransactDO.getBusinessLicense());
        submitLicenseResponse.setSocialCreditCode(licenseTransactDO.getCreditCode());
        submitLicenseResponse.setIdCardAndLicense2(licenseTransactDO.getIdCardNegativeCreditUrl());
        submitLicenseResponse.setIdCardAndLicense1(licenseTransactDO.getIdCardPositiveCreditUrl());
        submitLicenseResponse.setFoodLicenseUrl(licenseTransactDO.getFoodBusinessLicenseUrl());
        submitLicenseResponse.setBusinessPremises(licenseTransactDO.getFoodLicenseAddress());
        submitLicenseResponse.setFoodLicenseCode(licenseTransactDO.getFoodBusinessLicenseCode());
        submitLicenseResponse.setFoodLicenseStartTime(licenseTransactDO.getFoodBusinessStartTime());
        submitLicenseResponse.setFoodLicenseEndTime(licenseTransactDO.getFoodBusinessEndTime());
        return submitLicenseResponse;
    }
}
