package com.jeequan.jeepay.converter;

import cn.hutool.core.util.DesensitizedUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.MchApplyment;
import com.jeequan.jeepay.core.entity.MchStore;
import com.jeequan.jeepay.core.entity.*;
import com.jeequan.jeepay.core.model.applyment.MchModifyApplymentModel;
import com.jeequan.jeepay.core.model.applyment.PaywayFee;
import com.jeequan.jeepay.db.entity.*;
import com.jeequan.jeepay.model.RateConfigSimple;
import org.mapstruct.*;

@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface MchInfoConverter {

    @BeanMapping(builder = @Builder(disableBuilder = true))
    MchAppEntity toDbEntity(MchApp mchApp);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    com.jeequan.jeepay.db.entity.MchStore toDbEntity(MchStore mchStore);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    MchApp toModel(MchAppEntity entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    MchApplyment toModel(com.jeequan.jeepay.db.entity.MchApplyment entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    com.jeequan.jeepay.db.entity.MchApplyment toDbEntity(MchApplyment entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    MchModifyApplyment toModel(MchModifyApplymentEntity entity);


    @BeanMapping(builder = @Builder(disableBuilder = true))
    MchStore toModel(com.jeequan.jeepay.db.entity.MchStore entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    @Mapping(target = "unionSearchId", ignore = true)
    @Mapping(target = "modifyModel", ignore = true)
    @Mapping(target = "merchantType", ignore = true)
    @Mapping(target = "mchFullName", ignore = true)
    @Mapping(target = "lastDate", ignore = true)
    @Mapping(target = "firstDate", ignore = true)
    @Mapping(target = "epUserId", ignore = true)
    @Mapping(target = "agentNo", ignore = true)
    MchModifyApplymentEntity toDbEntity(MchModifyApplyment entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    void cp(SettleInfo entity, @MappingTarget SettleInfo target);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    void cp(com.jeequan.jeepay.core.entity.MchApplyment srcData, @MappingTarget com.jeequan.jeepay.db.entity.MchApplyment target);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    void cp(com.jeequan.jeepay.db.entity.MchApplyment srcData, @MappingTarget com.jeequan.jeepay.core.entity.MchApplyment target);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    MchSubInfoEntity toDbEntity(MchSubInfo entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    MchSubInfo toModel(MchSubInfoEntity entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    MchSubInfo clone(MchSubInfo entity);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    @Mapping(target = "altId", ignore = true)
    @Mapping(target = "id", ignore = true)
    PayInterfaceConfig toConfig(MchPayInterfaceConfig alternative);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    @Mapping(target = "sort", ignore = true)
    @Mapping(target = "alias", ignore = true)
    @Mapping(target = "inUseFlag", ignore = true)
    @Mapping(target = "id", ignore = true)
    MchPayInterfaceConfig toMchConfig(PayInterfaceConfig config);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    void cp(RedPacketRule entity, @MappingTarget RedPacketRule target);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    @Mapping(target = "areaCode", ignore = true)
    @Mapping(target = "alipayShopCreateId", ignore = true)
    @Mapping(target = "createdAt", ignore = true)
    @Mapping(target = "updatedAt", ignore = true)
    @Mapping(target = "remark", ignore = true)
    void cp(com.jeequan.jeepay.db.entity.MchApplyment entity, @MappingTarget MchStore target);

    @Mapping(target = "alipayShopStatus", ignore = true)
    @Mapping(target = "alipayShopId", ignore = true)
    @BeanMapping(builder = @Builder(disableBuilder = true))
    @Mapping(target = "areaCode", ignore = true)
    @Mapping(target = "alipayShopCreateId", ignore = true)
    @Mapping(target = "createdAt", ignore = true)
    @Mapping(target = "updatedAt", ignore = true)
    @Mapping(target = "remark", ignore = true)
    void cp(MchApplyment entity, @MappingTarget MchStore target);

    @Mapping(target = "ext", ignore = true)
    @Mapping(target = "dateRange", ignore = true)
    @Mapping(target = "updatedAt", ignore = true)
    @Mapping(target = "range", ignore = true)
    @Mapping(target = "paywayFeeDetail", ignore = true)
    @Mapping(target = "mccCode", ignore = true)
    @Mapping(target = "isvNo", ignore = true)
    @Mapping(target = "infoType", ignore = true)
    @Mapping(target = "infoId", ignore = true)
    @Mapping(target = "ifCode", ignore = true)
    @Mapping(target = "createdAt", ignore = true)
    @BeanMapping(builder = @Builder(disableBuilder = true))
    RateConfig toRateConfig(PaywayFee paywayFee);

    @BeanMapping(builder = @Builder(disableBuilder = true))
    @Mapping(target = "infoId", ignore = true)
    void cp(RateConfigSimple rateConfigSimple, @MappingTarget RateConfig rateConfig);

    default void desensitized(MchModifyApplyment applyment) {
        if (applyment == null) {
            return;
        }

        /* 解析进件JSON信息 对关键信息进行脱敏 */
        JSONObject applymentJson = JSON.parseObject(applyment.getApplyDetailInfo());

        /* 公共进件参数 */
        MchModifyApplymentModel model = applymentJson.toJavaObject(MchModifyApplymentModel.class);

        applymentJson.put("settAccountNo", DesensitizedUtil.bankCard(model.getSettAccountNo()));
        applymentJson.put("settAccountName", DesensitizedUtil.chineseName(model.getSettAccountName()));
        applymentJson.put("settAccountIdcardNo", DesensitizedUtil.idCardNum(model.getSettAccountIdcardNo(),3, 4));

        applyment.setApplyDetailInfo(applymentJson.toJSONString());
    }

    default void desensitized(MchModifyApplymentEntity applyment) {
        if (applyment == null) {
            return;
        }

        /* 解析进件JSON信息 对关键信息进行脱敏 */
        JSONObject applymentJson = JSON.parseObject(applyment.getApplyDetailInfo());

        /* 公共进件参数 */
        MchModifyApplymentModel model = applymentJson.toJavaObject(MchModifyApplymentModel.class);

        applymentJson.put("settAccountNo", DesensitizedUtil.bankCard(model.getSettAccountNo()));
        applymentJson.put("settAccountName", DesensitizedUtil.chineseName(model.getSettAccountName()));
        applymentJson.put("settAccountIdcardNo", DesensitizedUtil.idCardNum(model.getSettAccountIdcardNo(),3, 4));

        applyment.setApplyDetailInfo(applymentJson.toJSONString());
    }
}
