package com.prolog.rdc.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.google.common.collect.Lists;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.BmsConstant;
import com.prolog.rdc.tms.constant.enums.LogTypeEnum;
import com.prolog.rdc.tms.dao.mapper.BmsBillPayFeeDetailMapper;
import com.prolog.rdc.tms.dao.mapper.BmsBillPayFeeHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.ShipBillHeadMapper;
import com.prolog.rdc.tms.model.dto.SySParameterDTO;
import com.prolog.rdc.tms.model.dto.loadingbill.BmsLoadingBillDTO;
import com.prolog.rdc.tms.model.dto.pay.BmsBillPayFeeDTO;
import com.prolog.rdc.tms.model.dto.pay.BmsBillPayFeeDetailDTO;
import com.prolog.rdc.tms.model.dto.pay.BmsLoadingBillPayFeeDTO;
import com.prolog.rdc.tms.model.dto.pay.BmsLoadingBillPayFeeInfoDTO;
import com.prolog.rdc.tms.model.dto.pay.BmsShipBillPayFeeDTO;
import com.prolog.rdc.tms.model.dto.pay.BmsShipBillPayFeeInfoDTO;
import com.prolog.rdc.tms.model.dto.shipbill.BmsLogOperationDTO;
import com.prolog.rdc.tms.model.dto.shipbill.BmsShipBillDTO;
import com.prolog.rdc.tms.model.dto.shipbill.BmsShipBillDetailDTO;
import com.prolog.rdc.tms.model.dto.shipbill.BmsShipBillPackageDTO;
import com.prolog.rdc.tms.model.entity.BmsBillPayFeeDetailModel;
import com.prolog.rdc.tms.model.entity.BmsBillPayFeeHeaderModel;
import com.prolog.rdc.tms.model.entity.BmsShipBillHeaderModel;
import com.prolog.rdc.tms.model.entity.SysRefdatadModel;
import com.prolog.rdc.tms.model.vo.pay.BmsPayDetailAddVO;
import com.prolog.rdc.tms.model.vo.pay.BmsPayFeeAddVO;
import com.prolog.rdc.tms.model.vo.pay.BmsPayQueryVO;
import com.prolog.rdc.tms.proxy.MainDataProxy;
import com.prolog.rdc.tms.service.BmsBillLogService;
import com.prolog.rdc.tms.service.BmsBillPayFeeService;
import com.prolog.rdc.tms.service.BmsLoadingBillService;
import com.prolog.rdc.tms.service.BmsShipBillService;
import com.prolog.rdc.tms.util.ConvertorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hongbo.pan
 * @date 2023/4/24
 */
@Service
public class BmsBillPayFeeServiceImpl implements BmsBillPayFeeService {

    @Autowired
    private BmsShipBillService shipBillService;

    @Autowired
    private BmsLoadingBillService loadingBillService;

    @Autowired
    private BmsBillLogService logService;

    @Autowired
    private BmsBillPayFeeHeaderMapper payFeeHeaderMapper;

    @Autowired
    private BmsBillPayFeeDetailMapper payFeeDetailMapper;

    @Autowired
    private MainDataProxy mainDataProxy;

    @Autowired
    private ConvertorUtil convertorUtil;

    @Autowired
    private ShipBillHeadMapper shipBillHeadMapper;

    @Override
    public Page<BmsShipBillPayFeeDTO> findShipBillFee(BmsPayQueryVO vo) {
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<BmsShipBillPayFeeDTO> dtoList = payFeeHeaderMapper.findShipBillFee(vo);
        if (CollectionUtil.isNotEmpty(dtoList)) {
            List<SysRefdatadModel> dicData = getShipBillDic();
            UserEntity user = PlgFxUsrUtils.getUserEntity();
            Criteria criteria = Criteria.forClass(BmsBillPayFeeDetailModel.class);
            Restriction r1 = Restrictions.in(BmsBillPayFeeDetailModel::getBillNo, dtoList.stream().map(BmsShipBillPayFeeDTO::getBillNo).collect(Collectors.toList()));
            Restriction r2 = Restrictions.eq(BmsBillPayFeeDetailModel::getEnterpriseId, user.getEnterpriseId());
            criteria.setRestriction(Restrictions.and(r1, r2));
            List<BmsBillPayFeeDetailModel> payFeeDetailList = payFeeDetailMapper.findByCriteria(criteria);
            Map<String, List<BmsBillPayFeeDetailModel>> payFeeDetailMap = payFeeDetailList.stream().collect(Collectors.groupingBy(BmsBillPayFeeDetailModel::getBillNo));
            dtoList.forEach(dto -> {
                dto.setTransportFee(BigDecimal.ZERO);
                dto.setDeliveryFee(BigDecimal.ZERO);
                dto.setInsuranceFee(BigDecimal.ZERO);
                List<BmsBillPayFeeDetailModel> feeDetailList = payFeeDetailMap.get(dto.getBillNo());
                if (CollectionUtil.isNotEmpty(feeDetailList)) {
                    dto.setTransportFee(feeDetailList.stream().filter(feeDetail ->
                            "支出运费".equals(feeDetail.getExpenseName())).map(BmsBillPayFeeDetailModel::getTaxAmt).reduce(
                                    BigDecimal.ZERO, NumberUtil::add));
                    dto.setDeliveryFee(feeDetailList.stream().filter(feeDetail ->
                            "配送费".equals(feeDetail.getExpenseName())).map(BmsBillPayFeeDetailModel::getTaxAmt).reduce(
                            BigDecimal.ZERO, NumberUtil::add));
                    dto.setInsuranceFee(feeDetailList.stream().filter(feeDetail ->
                            "保价费".equals(feeDetail.getExpenseName())).map(BmsBillPayFeeDetailModel::getTaxAmt).reduce(
                            BigDecimal.ZERO, NumberUtil::add));
                }
                dto.setOrderTypeName(ConvertorUtil.getName(dicData, BmsConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
                dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, BmsConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderBusinessType())));
                dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, BmsConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
                dto.setShipperTypeName(ConvertorUtil.getName(dicData, BmsConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
                dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, BmsConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
                dto.setStatusName(ConvertorUtil.getName(dicData, BmsConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
                dto.setSourceName(ConvertorUtil.getName(dicData, BmsConstant.SOURCE, String.valueOf(dto.getSource())));
                dto.setShipBillStatusName(ConvertorUtil.getName(dicData, BmsConstant.SHIPBILL_STATUS, String.valueOf(dto.getShipBillStatus())));
                dto.setSynchronousStatusName(ConvertorUtil.getName(dicData, BmsConstant.SYNCHRONOUS_STATUS, String.valueOf(dto.getSynchronousStatus())));
                dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, BmsConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
                dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, BmsConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
                dto.setTransportTypeName(ConvertorUtil.getName(dicData, BmsConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
                dto.setFeeCltTypeName(ConvertorUtil.getName(dicData, BmsConstant.FEE_CLT_TYPE, String.valueOf(dto.getFeeCltType())));
                dto.setPayFeeCltTypeName(ConvertorUtil.getName(dicData, BmsConstant.FEE_CLT_TYPE, String.valueOf(dto.getPayFeeCltType())));
                dto.setCostStatusName(ConvertorUtil.getName(dicData, BmsConstant.COST_STATUS, String.valueOf(dto.getCostStatus())));
                dto.setSettlementStatusName(ConvertorUtil.getName(dicData, BmsConstant.SETTLEMENT_STATUS, String.valueOf(dto.getSettlementStatus())));
                dto.setCheckStatusName(ConvertorUtil.getName(dicData, BmsConstant.CHECK_STATUS, String.valueOf(dto.getCheckStatus())));
            });
        }
        return PageUtils.getPage(dtoList);
    }

    @Override
    public Page<BmsLoadingBillPayFeeDTO> findLoadingBillFee(BmsPayQueryVO vo) {
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<BmsLoadingBillPayFeeDTO> dtoList = payFeeHeaderMapper.findLoadingBillFee(vo);
        if (CollectionUtil.isNotEmpty(dtoList)) {
            List<SysRefdatadModel> dicData = getLoadingDic();
            dtoList.forEach(dto -> {
                dto.setStatusName(ConvertorUtil.getName(dicData, BmsConstant.LOADING_BILL_STATUS, String.valueOf(dto.getStatus())));
                dto.setSourceName(ConvertorUtil.getName(dicData, BmsConstant.SOURCE, String.valueOf(dto.getSource())));
                dto.setSynchronousStatusName(ConvertorUtil.getName(dicData, BmsConstant.SYNCHRONOUS_STATUS, String.valueOf(dto.getSynchronousStatus())));
                dto.setFeeCltTypeName(ConvertorUtil.getName(dicData, BmsConstant.FEE_CLT_TYPE, String.valueOf(dto.getFeeCltType())));
                dto.setCostStatusName(ConvertorUtil.getName(dicData, BmsConstant.COST_STATUS, String.valueOf(dto.getCostStatus())));
                dto.setSettlementStatusName(ConvertorUtil.getName(dicData, BmsConstant.SETTLEMENT_STATUS, String.valueOf(dto.getSettlementStatus())));
                dto.setCarrierTypeName(ConvertorUtil.getName(dicData, BmsConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierType())));
                dto.setCheckStatusName(ConvertorUtil.getName(dicData, BmsConstant.CHECK_STATUS, String.valueOf(dto.getCheckStatus())));
            });
        }
        return PageUtils.getPage(dtoList);
    }

    @Override
    public BmsShipBillPayFeeInfoDTO detailShipBillById(Long id) {
        BmsBillPayFeeHeaderModel headerModel = payFeeHeaderMapper.findById(id, BmsBillPayFeeHeaderModel.class);
        BmsBillPayFeeDTO feeDTO = BeanUtil.copyProperties(headerModel, BmsBillPayFeeDTO.class);
        feeDTO.setDetailDTOList(getDetail(id));
        BmsShipBillDTO shipBillDTO = shipBillService.detailById(headerModel.getBillId());
        BmsShipBillPayFeeInfoDTO shipBillFeeDTO = BeanUtil.copyProperties(shipBillDTO, BmsShipBillPayFeeInfoDTO.class);
        shipBillFeeDTO.setDetailList(shipBillDTO.getDetailList());
        shipBillFeeDTO.setPackageList(shipBillDTO.getPackageList());
        shipBillFeeDTO.setIncomeFeeDTO(feeDTO);
        return shipBillFeeDTO;
    }

    @Override
    public BmsLoadingBillPayFeeInfoDTO detailLoadingBillById(Long id) {
        BmsBillPayFeeHeaderModel headerModel = payFeeHeaderMapper.findById(id, BmsBillPayFeeHeaderModel.class);
        BmsBillPayFeeDTO feeDTO = BeanUtil.copyProperties(headerModel, BmsBillPayFeeDTO.class);
        feeDTO.setDetailDTOList(getDetail(id));
        BmsLoadingBillDTO loadingBillDTO = loadingBillService.detailById(headerModel.getBillId());
        BmsLoadingBillPayFeeInfoDTO loadingBillFeeDTO = BeanUtil.copyProperties(loadingBillDTO, BmsLoadingBillPayFeeInfoDTO.class);
        loadingBillFeeDTO.setShipBillList(loadingBillDTO.getShipBillList());
        loadingBillFeeDTO.setIncomeFeeDTO(feeDTO);
        return loadingBillFeeDTO;
    }

    @Override
    public List<BmsBillPayFeeDetailDTO> getDetail(Long id) {
        BmsBillPayFeeHeaderModel headerModel = payFeeHeaderMapper.findById(id, BmsBillPayFeeHeaderModel.class);
        List<BmsBillPayFeeDetailDTO> dtoList = Lists.newArrayList();
        List<BmsBillPayFeeDetailModel> modelList = findDetailModel(headerModel);
        if (CollectionUtil.isNotEmpty(modelList)) {
            List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[]{
                    BmsConstant.EXPENSE_STATUS, // 费用状态
                    BmsConstant.BILLING_STATUS // 计费状态
            });
            dtoList = modelList.stream().map(dto -> {
                BmsBillPayFeeDetailDTO detailDTO = BeanUtil.copyProperties(dto, BmsBillPayFeeDetailDTO.class);
                detailDTO.setExpenseStatusName(ConvertorUtil.getName(dicData, BmsConstant.EXPENSE_STATUS, String.valueOf(detailDTO.getExpenseStatus())));
                detailDTO.setBillingSourceName(detailDTO.getBillingSource() == 1 ? "价格表生成" : "手工录入");
                detailDTO.setBillingTypeName(detailDTO.getBillingType());
                detailDTO.setBillingStatusName(ConvertorUtil.getName(dicData, BmsConstant.BILLING_STATUS, String.valueOf(detailDTO.getBillingStatus())));
                return detailDTO;
            }).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public List<BmsLogOperationDTO> getLog(Long id) {
        return logService.getLog(LogTypeEnum.PAY_FEE, id.toString());
    }

    @Override
    public void addFee(BmsPayFeeAddVO vo) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        SySParameterDTO parameterData = mainDataProxy.queryParameter();
        Integer costPrecision = parameterData.getCostPrecision();
        Integer costInteger = parameterData.getCostInteger();
        RoundingMode roundingMode = null;
        if (costInteger == 1) {
            roundingMode = RoundingMode.HALF_UP;
        } else if (costInteger == 2) {
            roundingMode = RoundingMode.UP;
        } else if (costInteger == 3){
            roundingMode = RoundingMode.DOWN;
        }
        List<BmsBillPayFeeHeaderModel> headerModelList = payFeeHeaderMapper.findByIds(BmsBillPayFeeHeaderModel.class, vo.getIdList().toArray(), null);
        RoundingMode finalRoundingMode = roundingMode;
        vo.getDetailAddVOList().forEach(detailAddVO -> {
            BigDecimal taxAmt = detailAddVO.getTaxAmt();
            BigDecimal noTaxAmt = NumberUtil.div(taxAmt, NumberUtil.add(BigDecimal.ONE, NumberUtil.div(new BigDecimal(detailAddVO.getTaxPoint()), new BigDecimal("100"))), costPrecision, finalRoundingMode);
            BigDecimal avgTaxAmt = taxAmt;
            BigDecimal avgNoTaxAmt = noTaxAmt;
            if (detailAddVO.getIsShare() == 1) {
                avgTaxAmt = NumberUtil.div(taxAmt, headerModelList.size(), costPrecision, finalRoundingMode);
                avgNoTaxAmt = NumberUtil.div(noTaxAmt, headerModelList.size(), costPrecision, finalRoundingMode);
            }
            BigDecimal tax = NumberUtil.sub(avgTaxAmt, avgNoTaxAmt);
            detailAddVO.setTaxAmt(avgTaxAmt);
            detailAddVO.setTax(tax);
            detailAddVO.setNoTaxAmt(avgNoTaxAmt);
            for (int i = 0; i < headerModelList.size(); i++) {
                if (detailAddVO.getIsShare() == 1 && i == headerModelList.size() - 1) {
                    detailAddVO.setTaxAmt(NumberUtil.sub(taxAmt, NumberUtil.mul(avgTaxAmt, (headerModelList.size() - 1))));
                    detailAddVO.setNoTaxAmt(NumberUtil.sub(noTaxAmt, NumberUtil.mul(avgNoTaxAmt, (headerModelList.size() - 1))));
                    detailAddVO.setTax(NumberUtil.sub(detailAddVO.getTaxAmt(), detailAddVO.getNoTaxAmt()));
                }
                addDetail(headerModelList.get(i), detailAddVO);
            }
        });
        headerModelList.forEach(headerModel -> {
            List<BmsBillPayFeeDetailModel> detailModelList = findDetailModel(headerModel);
            if (CollectionUtil.isNotEmpty(detailModelList)) {
                headerModel.setContractPayAmount(detailModelList.stream().filter(bmsBillPayFeeDetailModel -> bmsBillPayFeeDetailModel.getBillingSource() == 1).map(BmsBillPayFeeDetailModel::getTaxAmt).reduce(BigDecimal.ZERO, NumberUtil::add));
                headerModel.setPayAmount(detailModelList.stream().map(BmsBillPayFeeDetailModel::getTaxAmt).reduce(BigDecimal.ZERO, NumberUtil::add));
            } else {
                headerModel.setContractPayAmount(BigDecimal.ZERO);
                headerModel.setPayAmount(BigDecimal.ZERO);
            }
            headerModel.setUpdateperson(user.getNickname());
            headerModel.setUpdatetime(DateUtil.date());
            payFeeHeaderMapper.update(headerModel);
        });
    }

    @Override
    public List<BmsShipBillDTO> getShipBill(Long id) {
        BmsBillPayFeeHeaderModel headerModel = payFeeHeaderMapper.findById(id, BmsBillPayFeeHeaderModel.class);
        return loadingBillService.getShipBill(headerModel.getBillId());
    }

    @Override
    public List<BmsShipBillDetailDTO> getItem(Long id) {
        List<BmsShipBillDetailDTO> list = Lists.newArrayList();
        List<Long> shipBillIdList = getShipBillIdList(id);
        if (CollectionUtil.isNotEmpty(shipBillIdList)) {
            list = shipBillIdList.stream().flatMap(shipBillId -> shipBillService.getItem(shipBillId).stream()).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public List<BmsShipBillPackageDTO> getPackage(Long id) {
        List<BmsShipBillPackageDTO> list = Lists.newArrayList();
        List<Long> shipBillIdList = getShipBillIdList(id);
        if (CollectionUtil.isNotEmpty(shipBillIdList)) {
            list = shipBillIdList.stream().flatMap(shipBillId -> shipBillService.getPackage(shipBillId).stream()).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public List<Object> getAudit(Long id) {
        return Lists.newArrayList();
    }

    @Override
    public void submit(List<Long> idList) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        List<BmsBillPayFeeDetailModel> detailModelList = payFeeDetailMapper.findByIds(BmsBillPayFeeDetailModel.class, idList.toArray(), null);
        detailModelList.forEach(detailModel -> {
            if (BmsConstant.EXPENSE_STATUS_0.equals(detailModel.getExpenseStatus())) {
                detailModel.setExpenseStatus(BmsConstant.EXPENSE_STATUS_2);
                detailModel.setUpdateperson(user.getNickname());
                detailModel.setUpdatetime(DateUtil.date());
                payFeeDetailMapper.update(detailModel);
            }
        });
    }

    /**
     * getShipBillIdList
     * @param id
     * @return
     */
    private List<Long> getShipBillIdList(Long id) {
        List<Long> shipBillIdList = Lists.newArrayList();
        BmsBillPayFeeHeaderModel headerModel = payFeeHeaderMapper.findById(id, BmsBillPayFeeHeaderModel.class);
        if (headerModel.getBillType() == 10) {
            shipBillIdList.add(headerModel.getBillId());
        } else {
            Criteria criteria = Criteria.forClass(BmsShipBillHeaderModel.class);
            Restriction r1 = Restrictions.eq(BmsShipBillHeaderModel::getLoadingId, headerModel.getBillId());
            Restriction r2 = Restrictions.eq(BmsShipBillHeaderModel::getEnterpriseId, headerModel.getEnterpriseId());
            criteria.setRestriction(Restrictions.and(r1, r2));
            List<BmsShipBillHeaderModel> modelList = shipBillHeadMapper.findByCriteria(criteria);
            if (CollectionUtil.isNotEmpty(modelList)) {
                shipBillIdList = modelList.stream().map(BmsShipBillHeaderModel::getId).collect(Collectors.toList());
            }
        }
        return shipBillIdList;
    }

    /**
     * findDetailModel
     * @param headerModel
     * @return
     */
    @Override
    public List<BmsBillPayFeeDetailModel> findDetailModel(BmsBillPayFeeHeaderModel headerModel) {
        Criteria criteria = Criteria.forClass(BmsBillPayFeeDetailModel.class);
        Restriction r1 = Restrictions.eq(BmsBillPayFeeDetailModel::getBillId, headerModel.getBillId());
        Restriction r2 = Restrictions.eq(BmsBillPayFeeDetailModel::getBillNo, headerModel.getBillNo());
        Restriction r3 = Restrictions.eq(BmsBillPayFeeDetailModel::getEnterpriseId, headerModel.getEnterpriseId());
        criteria.setRestriction(Restrictions.and(r1, r2, r3));
        return payFeeDetailMapper.findByCriteria(criteria);
    }

    /**
     * addDetail
     * @param headerModel
     * @param detailAddVO
     * @return
     */
    private BmsBillPayFeeDetailModel addDetail(BmsBillPayFeeHeaderModel headerModel, BmsPayDetailAddVO detailAddVO) {
        String expenseNo = mainDataProxy.getCodeNumberByCode(BmsConstant.SHIP_BILL_EXPENSE_NO);
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        BmsBillPayFeeDetailModel detailModel = new BmsBillPayFeeDetailModel();
        detailModel.setBillNo(headerModel.getBillNo());
        detailModel.setBillId(headerModel.getBillId());
        detailModel.setPayFeeType(headerModel.getPayFeeType());
        detailModel.setExpenseCode(detailAddVO.getExpenseCode());
        detailModel.setExpenseName(detailAddVO.getExpenseName());
        detailModel.setExpenseType(detailAddVO.getExpenseType());
        detailModel.setExpenseTypeName(detailAddVO.getExpenseTypeName());
        detailModel.setExpenseAccountCode(detailAddVO.getExpenseAccountCode());
        detailModel.setExpenseAccountName(detailAddVO.getExpenseAccountName());
        detailModel.setAmount(detailAddVO.getTaxAmt());
        detailModel.setTaxPoint(detailAddVO.getTaxPoint());
        detailModel.setTaxAmt(detailAddVO.getTaxAmt());
        detailModel.setIsPreTax(detailAddVO.getIsPreTax());
        detailModel.setNoTaxAmt(detailAddVO.getNoTaxAmt());
        detailModel.setTax(detailAddVO.getTax());
        detailModel.setIsShare(detailAddVO.getIsShare().toString());
        detailModel.setMemo(detailAddVO.getMemo());
        detailModel.setExpenseNo(expenseNo);
        detailModel.setExpenseStatus(0);
        detailModel.setCheckStatus(BmsConstant.CHECK_STATUS_0);
        detailModel.setBillingSource(2);
        detailModel.setBillingStatus(1);
        detailModel.setBillingDate(DateUtil.date());
//        detailModel.setBillingContract();
//        detailModel.setBillingPrice();
//        detailModel.setBillingType();
//        detailModel.setBillingFailReason();
        detailModel.setCreateperson(user.getNickname());
        detailModel.setCreatetime(DateUtil.date());
        detailModel.setUpdateperson(user.getNickname());
        detailModel.setUpdatetime(DateUtil.date());
        detailModel.setEnterpriseId(user.getEnterpriseId());
        detailModel.setEnterpriseName(user.getEnterpriseName());
        payFeeDetailMapper.save(detailModel);
        return detailModel;
    }

    /**
     * getDic
     * @return
     */
    private List<SysRefdatadModel> getShipBillDic() {
        return convertorUtil.getDictionary(new String[] {
                BmsConstant.SHIPBILL_STATUS, // 运单状态
                BmsConstant.SHIPPER_TYPE, // 运输状态
                BmsConstant.ABNORMAL_STATUS, // 异常状态
                BmsConstant.ORDER_TYPE, // 订单类型
                BmsConstant.CONSIGNEE_TYPE, // 收货方类型
                BmsConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
                BmsConstant.SETTLEMENT_STATUS, // 核算状态
                BmsConstant.SOURCE, // 来源
                BmsConstant.COST_STATUS, // 计费状态
                BmsConstant.SYNCHRONOUS_STATUS, // 同步状态
                BmsConstant.TEMPERATURE_REQUIREMENT,// 温度要求
                BmsConstant.LINE_CARRIER_TYPE,// 承运方类型
                BmsConstant.DELIVERY_METHOD_TYPE,// 配送方式
                BmsConstant.TRANSPORT_TYPE,// 运输模式
                // 费用计算类型
                BmsConstant.FEE_CLT_TYPE,
                BmsConstant.CHECK_STATUS
        });
    }

    /**
     * getDic
     * @return
     */
    private List<SysRefdatadModel> getLoadingDic() {
        return convertorUtil.getDictionary(new String[] {
                // 核算状态
                BmsConstant.SETTLEMENT_STATUS,
                // 计费状态
                BmsConstant.COST_STATUS,
                // 同步状态
                BmsConstant.SYNCHRONOUS_STATUS,
                // 承运方类型
                BmsConstant.LINE_CARRIER_TYPE,
                // 车辆类别
                BmsConstant.VEHICLE_TYPE_CATEGORY,
                // 费用计算类型
                BmsConstant.FEE_CLT_TYPE,
                // 来源
                BmsConstant.SOURCE,
                BmsConstant.LOADING_BILL_STATUS,
                BmsConstant.CHECK_STATUS
        });
    }
}
