package com.prolog.cs.biz.outbound.convert;

import com.prolog.cs.biz.outbound.model.OmsDoBillHDO;
import com.prolog.cs.biz.outbound.model.dto.BillPackageDTO;
import com.prolog.cs.biz.outbound.model.entity.OmsDoBillDDO;
import com.prolog.cs.biz.outbound.model.tms.TmsShipBill;
import com.prolog.cs.biz.outbound.model.tms.TmsShipBillItem;
import com.prolog.cs.biz.outbound.model.tms.TmsShipBillPackage;
import com.prolog.cs.biz.outbound.model.tms.TmsShipBillPackageItem;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface TmsConvert {

    @Mapping(target = "branchCode", source = "h.branchcode")
    @Mapping(target = "branchName", source = "h.branchname")
    @Mapping(target = "cargoOwnerCode", source = "h.consignorcode")
    @Mapping(target = "cargoOwnerName", source = "h.consignorname")
    @Mapping(target = "orderType", expression = "java(\"2\".equals(h.getPriorityLevel())?1:2)")
    @Mapping(target = "departmentCode", source = "h.enquiryDepartment")
    @Mapping(target = "departmentName", source = "h.enquiryDepartment")
    @Mapping(target = "orderId", source = "h.billid")
    @Mapping(target = "orderNo", source = "h.billno")
    @Mapping(target = "orderDate", source = "h.billDate", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "carriersCode", source = "h.carrierCode")
    @Mapping(target = "carriersName", source = "h.carrierName")
    @Mapping(target = "orderBusinessType", source = "h.carrierType")
    @Mapping(target = "transportType", constant = "1")
    @Mapping(target = "deliveryMethodType", expression = "java(\"5\".equals(h.getArrivalMode())?2:1)")
    @Mapping(target = "shipperType", constant = "2")
    @Mapping(target = "shipperUnitCode", source = "h.branchcode")
    @Mapping(target = "shipperUnitName", source = "h.branchname")
    @Mapping(target = "shipperCode", source = "h.branchcode")
    @Mapping(target = "shipperName", source = "h.branchname")
    @Mapping(target = "consigneeType", expression = "java(\"2\".equals(h.getShipperType())?1:2)")
    @Mapping(target = "consigneeUnitCode", source = "h.shipperCode")
    @Mapping(target = "consigneeUnitName", source = "h.shippername")
    @Mapping(target = "consigneeCode", source = "h.shipperCode")
    @Mapping(target = "consigneeName", source = "h.shippername")
    @Mapping(target = "totalPcs", source = "h.totPcs")
    @Mapping(target = "totalWeight", source = "h.totWeight")
    @Mapping(target = "totalVolume", source = "h.totVolume")
    TmsShipBill parse2TmsShipBill(OmsDoBillHDO h);

    @Mapping(target = "cargoOwnerCode", source = "h.consignorcode")
    @Mapping(target = "cargoOwnerName", source = "h.consignorname")
    @Mapping(target = "itemCode", source = "d.itemcode")
    @Mapping(target = "itemName", source = "d.itemname")
    @Mapping(target = "pcs", expression = "java(d.getItemQty().divide(d.getPackTypeNum(),0,java.math.BigDecimal.ROUND_DOWN).intValue())")
    @Mapping(target = "packType", constant = "1")
    TmsShipBillItem parse2TmsShipBillItem(OmsDoBillDDO d, OmsDoBillHDO h);

    @Mapping(target = "orderId", source = "dto.billId")
    @Mapping(target = "orderNo", source = "dto.billNo")
    @Mapping(target = "enterpriseId", source = "dto.enterpriseId")
    @Mapping(target = "enterpriseName", source = "dto.enterpriseName")
    TmsShipBillPackage parse2TmsShipBillPackage(BillPackageDTO dto);

    @Mapping(target = "shipbillId", source = "bpd.shipBillNo")
    @Mapping(target = "shipbillNo", source = "bpd.shipBillNo")
    @Mapping(target = "cargoOwnerId", source = "bpd.consignorId")
    @Mapping(target = "cargoOwnerCode", source = "bpd.consignorCode")
    @Mapping(target = "cargoOwnerName", source = "bpd.consignorName")
    @Mapping(target = "seqno", source = "i")
    @Mapping(target = "childParentType", constant = "1")
    @Mapping(target = "packageCode", source = "bpd.barcode")
    @Mapping(target = "packageType", expression = "java(bpd.getLclNo()!=null?2:1)")
    @Mapping(target = "pcs", constant = "1")
    @Mapping(target = "volume", source = "bpd.volume")
    @Mapping(target = "weight", source = "bpd.weight")
    @Mapping(target = "heigth", source = "bpd.height")
    @Mapping(target = "status", constant = "10")
    @Mapping(target = "enterpriseId", source = "bpd.enterpriseId")
    @Mapping(target = "enterpriseName", source = "bpd.enterpriseName")
    @Mapping(target = "createtime", source = "bpd.checkTime2", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "createperson", source = "bpd.checkerName")
    @Mapping(target = "orderId", source = "bpd.billId")
    @Mapping(target = "orderNo", source = "bpd.billNo")
    TmsShipBillPackageItem parse2TmsShipBillPackageItem(BillPackageDTO bpd, int i);

}