package com.xunhong.erp.turbo.biz.infrastructure.convert;

import com.xunhong.erp.turbo.api.biz.dto.cmd.DealerCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.DealerUpdateCmd;
import com.xunhong.erp.turbo.biz.domain.entity.Dealer;
import com.xunhong.erp.turbo.biz.infrastructure.entity.DealerDO;
import com.xunhong.erp.turbo.biz.infrastructure.entity.DealerSettingDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValueCheckStrategy;

/**
 * @author shenyifei
 */
@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
public interface DealerConvert {

	@Mapping(target = "strawMatCostFlag", source = "setting.strawMatCostFlag")
	@Mapping(target = "shareRatio", source = "setting.shareRatio")
	@Mapping(target = "shareAdjusted", source = "setting.shareAdjusted")
	@Mapping(target = "lossAmount", source = "setting.lossAmount")
	@Mapping(target = "includePackingFlag", source = "setting.includePackingFlag")
	@Mapping(target = "includeFreightFlag", source = "setting.includeFreightFlag")
	@Mapping(target = "freightCostFlag", source = "setting.freightCostFlag")
	@Mapping(target = "enableShare", source = "setting.enableShare")
	@Mapping(target = "enableLoss", source = "setting.enableLoss")
	@Mapping(target = "enableCompanyRebate", source = "setting.enableCompanyRebate")
	@Mapping(target = "enableAccrualTax", source = "setting.enableAccrualTax")
	@Mapping(target = "companyRebateRatio", source = "setting.companyRebateRatio")
	@Mapping(target = "accrualTaxRatio", source = "setting.accrualTaxRatio")
	@Mapping(target = "dealerPaymentAccountList", source = "dealerPaymentAccountDOList")
	Dealer toDealer(DealerDO dealerDO);

	@Mapping(target = "setting", expression = "java(this.toSettingDO(dealerCreateCmd))")
	@Mapping(target = "sort", ignore = true)
	@Mapping(target = "deliveryTemplate", ignore = true)
	@Mapping(target = "dealerPaymentAccountDOList", ignore = true)
	@Mapping(target = "version", ignore = true)
	@Mapping(target = "updatedAt", ignore = true)
	@Mapping(target = "isDelete", ignore = true)
	@Mapping(target = "createdAt", ignore = true)
	DealerDO toDealerDO(DealerCreateCmd dealerCreateCmd);

	default DealerSettingDO toSettingDO(DealerCreateCmd dealerCreateCmd) {
		DealerSettingDO dealerSettingDO = new DealerSettingDO();
		dealerSettingDO.setEnableShare(dealerCreateCmd.getEnableShare());
		dealerSettingDO.setShareRatio(dealerCreateCmd.getShareRatio());
		dealerSettingDO.setFreightCostFlag(dealerCreateCmd.getFreightCostFlag());
		dealerSettingDO.setStrawMatCostFlag(dealerCreateCmd.getStrawMatCostFlag());
		dealerSettingDO.setIncludePackingFlag(dealerCreateCmd.getIncludePackingFlag());
		dealerSettingDO.setIncludeFreightFlag(dealerCreateCmd.getIncludeFreightFlag());
		dealerSettingDO.setEnableAccrualTax(dealerCreateCmd.getEnableAccrualTax());
		dealerSettingDO.setAccrualTaxRatio(dealerCreateCmd.getAccrualTaxRatio());
		dealerSettingDO.setEnableCompanyRebate(dealerCreateCmd.getEnableCompanyRebate());
		dealerSettingDO.setCompanyRebateRatio(dealerCreateCmd.getCompanyRebateRatio());
		dealerSettingDO.setShareAdjusted(dealerCreateCmd.getShareAdjusted());
		dealerSettingDO.setEnableLoss(dealerCreateCmd.getEnableLoss());
		dealerSettingDO.setLossAmount(dealerCreateCmd.getLossAmount());
		return dealerSettingDO;
	}

	@Mapping(target = "setting", expression = "java(this.toSettingDO(dealerUpdateCmd))")
	@Mapping(target = "sort", ignore = true)
	@Mapping(target = "dealerPaymentAccountDOList", ignore = true)
	@Mapping(target = "version", ignore = true)
	@Mapping(target = "updatedAt", ignore = true)
	@Mapping(target = "isDelete", ignore = true)
	@Mapping(target = "createdAt", ignore = true)
	void toDealerDO(@MappingTarget DealerDO dealerDO, DealerUpdateCmd dealerUpdateCmd);
}
