package cn.pluss.platform.leshua;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.util.StringUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * @author yuchen
 *
 * @Description
 */
@Data
@Accessors(chain = true)
public class LeshuaMerchantAuditDTO {

    @ApiModelProperty(value = "当前用户userId")
    private String userId;

//	@ApiModelProperty(value="所属商户经营类目编码")
//	private String mccCode;
//	
//	@ApiModelProperty(value="所属商户经营类目名称")
//	private String mccName;
//	
//	@ApiModelProperty(value="商户简称")
//	private String merchantName;
//	
//	@ApiModelProperty(value="联系人号码")
//	private String contactPhone;
//	
//	@ApiModelProperty(value="联系人名称")
//	private String contactName;
//	
//	@ApiModelProperty(value="乐刷商户地址")
//	private String leshuaMerchantAddress;
//	
//	@ApiModelProperty(value="商户具体地址")
//	private String merchantDetailAddress;
//	
//	@ApiModelProperty(value="营业执照号")
//	private String bussAuthNum;
//	
//	@ApiModelProperty(value="营业执照有效期")
//	private String bussAuthPeriod;
//	
//	@ApiModelProperty(value="营业执照名称")
//	private String leshuaLicenseName;
//	
//	@ApiModelProperty(value="营业执照开始时间")
//	private String licenseStart;
//	
//	@ApiModelProperty(value="银行卡照片")
//	private String bankPhoto;
//	
//	@ApiModelProperty(value="银行名称")
//	private String bankName;
//	
//	@ApiModelProperty(value="银行卡号")
//	private String bankNo;
//	
//	@ApiModelProperty(value="开户地区")
//	private String bankAddress;
//	
//	@ApiModelProperty(value="开户支行")
//	private String bankBranch;
//	
//	@ApiModelProperty(value="联行号")
//	private String unionpay;
//	
//	@ApiModelProperty(value="营业执照图片")
//	private String  businessLicensePic;
//	
//	@ApiModelProperty(value="店铺门头照图片")
//	private String shopSignBoardPic;
//	
//	@ApiModelProperty(value="店内环境照片")
//	private String indoorPic;
//	
//	@ApiModelProperty(value="收银台照片")
//	private String cashierDeskPic;


    public MerchantRegisterDTO convert(IdCard idCard, BankCard bankCard, MerchantBaseInfo merchantBaseInfo, List<MerchantImage> list) {
        MerchantRegisterDTO merchantRegisterDTO = new MerchantRegisterDTO();
        //商户基本信息
        BaseInfo baseInfo = new BaseInfo();
        baseInfo.setMerchantType(Integer.valueOf(merchantBaseInfo.getMerchantType()));
        baseInfo.setMccCode(merchantBaseInfo.getMcc());
        baseInfo.setMerchantName(merchantBaseInfo.getMerchantName());
        baseInfo.setMerchantShortName(merchantBaseInfo.getAlias());
        //法人信息，个人类型填联系人信息
        LegalPerson legalPerson = new LegalPerson();
        legalPerson.setCredentialType(1);
        legalPerson.setIdCardEnd(idCard.getCertEndTime());
        legalPerson.setIdCardNo(idCard.getCertNo());
        legalPerson.setIdCardStart(idCard.getCertStartTime());
        legalPerson.setLegalName(idCard.getCertName());
        //联系人信息
        ContactInfo contactInfo = new ContactInfo();
        contactInfo.setMobile(merchantBaseInfo.getContactMobile().replaceAll(" ", ""));
        contactInfo.setName(merchantBaseInfo.getContactName());
        //店铺地址信息
        AddressInfo addressInfo = new AddressInfo();
        addressInfo.setAddress(merchantBaseInfo.getAddress());
        //默认用英文逗号隔开
//		String[] split = merchantBaseInfo.getLeshuaMerchantAddress().split("-");
//		addressInfo.setArea(split[2]);
//		addressInfo.setCity(split[1]);
//		addressInfo.setProvince(split[0]);
        String addressNo = merchantBaseInfo.getAddressNo();
        if (StringUtil.isNotEmpty(addressNo)) {
            String[] split = addressNo.split("-");
            addressInfo.setProvinceCode(split[0]);
            if (split[0].equals(split[1])) {
                addressInfo.setCityCode(split[1].substring(0, 2) + "0100");
            } else {
                addressInfo.setCityCode(split[1]);
            }
            addressInfo.setAreaCode(split[2]);
        }
        //营业资质信息
        LicenseInfo licenseInfo = new LicenseInfo();
        if (StringUtil.isNotEmpty(merchantBaseInfo.getMerchantType())) {
            if ("1".equals(merchantBaseInfo.getMerchantType())) {
                licenseInfo.setLicenseFullName(merchantBaseInfo.getMerchantName());
            } else {
                licenseInfo.setLicenseFullName(merchantBaseInfo.getBussAuthName());
            }
        }
        if (StringUtil.isNotEmpty(merchantBaseInfo.getBussAuthNum())) {
            licenseInfo.setLicense(merchantBaseInfo.getBussAuthNum());
        }
        licenseInfo.setLicenseAddress(merchantBaseInfo.getBussAuthAddress());
        licenseInfo.setLicenseStart(merchantBaseInfo.getBussAuthStartTime());
        licenseInfo.setLicenseEnd(merchantBaseInfo.getBussAuthEndTime());
        //licenseInfo.setLicense(queryUserApp.getBussAuthNum());
        //	licenseInfo.setLicenseAddress(queryUserApp.getMerchantDetailAddress());
        //营业执照结束时间
        //licenseInfo.setLicenseEnd(queryUserApp.getBussAuthPeriod());
        //licenseInfo.setLicenseStart(queryUserApp.getLicenseStart());
        //licenseInfo.setLicensePic(businessLicensePic);
        //结算信息
        AccountInfo accountInfo = new AccountInfo();
        if (StringUtil.isEmpty(bankCard.getAccountType())) {
            accountInfo.setType("1");
        } else {
            accountInfo.setType(bankCard.getAccountType());
        }
        accountInfo.setLegalFlag("1");
        accountInfo.setBankCardNo(bankCard.getBankCardNo());
//		accountInfo.setBranch(merchantBankCard.getBranchName());
        accountInfo.setHolder(bankCard.getBankHolder());
        accountInfo.setUnionpay(bankCard.getContactLine());
        accountInfo.setIdCardNo(idCard.getCertNo());
        //accountInfo.setUnionpay(unionpay);
        //门店信息
        ShopInfo shopInfo = new ShopInfo();
        merchantRegisterDTO.setAccountInfo(accountInfo);
        merchantRegisterDTO.setAddressInfo(addressInfo);
        merchantRegisterDTO.setBaseInfo(baseInfo);
        merchantRegisterDTO.setContactInfo(contactInfo);
        merchantRegisterDTO.setLegalPerson(legalPerson);
        if (!"1".equals(merchantBaseInfo.getMerchantType())) {
            // 小微商户不能传营业执照信息
            merchantRegisterDTO.setLicenseInfo(licenseInfo);
        }
        merchantRegisterDTO.setShopInfo(shopInfo);
//		 if(StringUtil.isEmpty(businessLicensePic)) {
//			 return ResultGenerator.genFailResult("请上传营业执照");
//		 }
        for (MerchantImage merchantImage : list) {
            if ("01".equals(merchantImage.getPhotoType())) {//身份证正面
                legalPerson.setIdcardFrontPic(merchantImage.getPicUrl());
            }
            if ("02".equals(merchantImage.getPhotoType())) {//身份证反面
                legalPerson.setIdcardBackPic(merchantImage.getPicUrl());
            }
            if ("03".equals(merchantImage.getPhotoType())) {//营业执照
				if (!"1".equals(merchantBaseInfo.getMerchantType())) {
					licenseInfo.setLicensePic(merchantImage.getPicUrl());
				}
			}
            if ("06".equals(merchantImage.getPhotoType())) {//门头照
                shopInfo.setDoorPic(merchantImage.getPicUrl());
            }
            if ("08".equals(merchantImage.getPhotoType())) {//收银台照片
                shopInfo.setCashierDeskPic(merchantImage.getPicUrl());
            }
            if ("09".equals(merchantImage.getPhotoType())) {//门店内景照片
                shopInfo.setInsidePic(merchantImage.getPicUrl());
            }
            if ("98".equals(merchantImage.getPhotoType())) {//银行卡正面
                accountInfo.setBankCardFrontPic(merchantImage.getPicUrl());
            }
        }
        if (StringUtil.isEmpty(accountInfo.getBankCardFrontPic())) {
            throw new MsgException("请上传银行卡照片");
        }
        if (StringUtil.isEmpty(shopInfo.getCashierDeskPic())) {
            throw new MsgException("请上传收银台照片");
        }
        if (StringUtil.isEmpty(shopInfo.getDoorPic())) {
            throw new MsgException("请上传店铺门头照图片");
        }
        if (StringUtil.isEmpty(shopInfo.getInsidePic())) {
            throw new MsgException("请上传店内环境照片");
        }

         if(StringUtil.isEmpty(licenseInfo.getLicensePic()) && !"1".equals(merchantBaseInfo.getMerchantType())) {
        	 throw new  MsgException("请上传营业执照");
         }

        if (StringUtil.isEmpty(baseInfo.getMccCode())) {
            throw new MsgException("请选择商户行业类别");
        }
        if (StringUtil.isEmpty(baseInfo.getMerchantName())) {
            throw new MsgException("请传入商户名称");
        }
        if (StringUtil.isEmpty(legalPerson.getIdcardBackPic()) || StringUtil.isEmpty(legalPerson.getIdCardEnd()) || StringUtil.isEmpty(legalPerson.getIdcardFrontPic())
                || StringUtil.isEmpty(legalPerson.getIdCardNo()) || StringUtil.isEmpty(legalPerson.getIdCardStart())) {
            throw new MsgException("请先进行身份认证");
        }
        if (StringUtil.isEmpty(legalPerson.getLegalName())) {
            throw new MsgException("请传入法人姓名");
        }
        if (StringUtil.isEmpty(contactInfo.getMobile())) {
            throw new MsgException("请传入手机号码");
        }
        if (StringUtil.isEmpty(contactInfo.getName())) {
            throw new MsgException("请传入联系人姓名");
        }
        if (StringUtil.isEmpty(addressInfo.getAddress())) {
            throw new MsgException("请输入店铺详细地址");
        }

        if (StringUtil.isEmpty(licenseInfo.getLicenseFullName())) {
            throw new MsgException("请输入营业执照名称，执照如无名称，可填“个体户XXX”（执照上姓名）；小微商户填“商户_XXX”（身份证姓名）");
        }

        if (StringUtil.isEmpty(accountInfo.getBankCardFrontPic())) {
            throw new MsgException("请输入银行卡正面");
        }
        if (StringUtil.isEmpty(accountInfo.getBankCardNo())) {
            throw new MsgException("请输入银行卡号");
        }

        if (StringUtil.isEmpty(accountInfo.getHolder())) {
            throw new MsgException("请输入开户名；法人结算：与法人姓名姓名一致；企业账户：与营业执照注册名称一致");
        }
        if (StringUtil.isEmpty(accountInfo.getIdCardNo())) {
            throw new MsgException("请输入结算人身份证号码");
        }

        return merchantRegisterDTO;
    }
}

