package cn.superdesk.uniorder.service.order.receive.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.enums.common.*;
import cn.superdesk.uniorder.common.enums.order.*;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import cn.superdesk.uniorder.common.lock.zookeeper.annotation.DistributedLock;
import cn.superdesk.uniorder.common.numerical.AnCollectionUtils;
import cn.superdesk.uniorder.common.numerical.AnDateHelper;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderCredit;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderExample;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dao.mapper.base.*;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesFlowingParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.score.ScoreOrderCreditReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.score.ScoreOrderGoodsReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.score.ScoreOrderReceiveParam;
import cn.superdesk.uniorder.service.order.base.CommonMergeOrderCreditProcessService;
import cn.superdesk.uniorder.service.order.base.CommonMergeOrderGoodsProcessService;
import cn.superdesk.uniorder.service.order.base.CommonOrderProcessService;
import cn.superdesk.uniorder.service.order.base.CommonOrderTransportProcessService;
import cn.superdesk.uniorder.service.order.receive.ReceiveFlscQchOrderService;
import cn.superdesk.uniorder.service.setting.UnifiedServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Predicate;


@Slf4j
@Service
public class ReceiveFlscQchOrderServiceImpl implements ReceiveFlscQchOrderService {

    @Autowired
    private UnifiedOrderMapper                    unifiedOrderMapper;
    @Autowired
    private UnifiedOrderMapperEx                  unifiedOrderMapperEx;
    @Autowired
    private UnifiedOrderMapperExCustomerDimCredit unifiedOrderMapperExScore;
    @Autowired
    private UnifiedOrderGoodsMapper               unifiedOrderGoodsMapper;
    @Autowired
    private UnifiedOrderGoodsMapperEx             unifiedOrderGoodsMapperEx;
    @Autowired
    private UnifiedOrderCreditMapper              unifiedOrderCreditMapper;

    @Autowired
    private CommonOrderProcessService            commonOrderProcessService;
    @Autowired
    private CommonMergeOrderGoodsProcessService  commonOrderGoodsMergeProcessService;
    @Autowired
    private CommonMergeOrderCreditProcessService commonOrderCreditMergeProcessService;

    @Autowired
    private CommonOrderTransportProcessService commonOrderTransportProcessService;

    @Autowired
    private UnifiedServiceService unifiedServiceService;

    /**
     * 商城 订单 侨城汇(积分订单)
     *
     * @param receiveParam
     * @return
     */
    @Transactional
    @Override
    @DistributedLock(name = "#receiveParam.orderSn")
    public UnifiedOrderIssuesFlowingParam kafkaScoreOrderReceive(ScoreOrderReceiveParam receiveParam) {

        UnifiedOrderIssuesFlowingParam callback = new UnifiedOrderIssuesFlowingParam();
        callback.setUid(receiveParam.getUid());
        callback.setProcessResult(CommonProcessResultEnum.正常.getKey());
        callback.setProcessReport("");

        try {


            if (null != receiveParam.getDeleted() && receiveParam.getDeleted().equals(1)) {

                UnifiedOrderExample example4FixDeleteEq1 = new UnifiedOrderExample();
                example4FixDeleteEq1.createCriteria()
                        .andOrderSnEqualTo(receiveParam.getOrderSn());
                List<UnifiedOrder> unifiedOrders4Check = unifiedOrderMapper.selectByExample(example4FixDeleteEq1);

                if (AnCollectionUtils.isNotEmpty(unifiedOrders4Check)) {

                    UnifiedOrder unifiedOrder4FixDeleteEq1 = new UnifiedOrder();
                    unifiedOrder4FixDeleteEq1.setId(unifiedOrders4Check.get(0).getId());

                    // 20201017 16:34 补个更新支付方式与订单状态
                    unifiedOrder4FixDeleteEq1.setPayType(receiveParam.getPayType());
                    unifiedOrder4FixDeleteEq1.setOrderState(receiveParam.getStatus());
                    unifiedOrder4FixDeleteEq1.setOrderStateName(SuperDeskMallOrderStatusEnum.getDesc(receiveParam.getStatus()));

                    unifiedOrder4FixDeleteEq1.setCompletState(UnifiedOrderCompletStateUtils.convertBySuperDeskMallOrderStatus(receiveParam.getStatus(), receiveParam.getDeleted()));

                    commonOrderProcessService.attachUpdate(unifiedOrder4FixDeleteEq1);
                }
            }

            // TODO 条件有问题 目前是不触发的

            // 如果这个单的价格为 0 为过来还有什么意义呢? // dispatchPrice + grprice = price 所以不能排除 订单金额为0 但有运费
            if (null != receiveParam.getPrice() && receiveParam.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
                // TODO 记录 问题单
                commonOrderProcessService.cleanErrorOrder(receiveParam.getOrderSn());

                callback.setProcessResult(CommonProcessResultEnum.异常_数据本身缺失.getKey());
                callback.setProcessReport("如果这个单的价格为 0 为过来还有什么意义呢? // dispatchPrice + grprice = price 所以不能排除 订单金额为0 但有运费");
                return callback;
            }

            if (null == receiveParam.getGrprice()) {
                // TODO 记录 问题单
                commonOrderProcessService.cleanErrorOrder(receiveParam.getOrderSn());
                callback.setProcessResult(CommonProcessResultEnum.异常_数据本身缺失.getKey());
                callback.setProcessReport("如果这个单的价格为 0 为过来还有什么意义呢? // dispatchPrice + grprice = price 所以不能排除 订单金额为0 但有运费");
                return callback;
            } else if (receiveParam.getGrprice().compareTo(BigDecimal.ZERO) <= 0) {
                commonOrderProcessService.cleanErrorOrder(receiveParam.getOrderSn());
                callback.setProcessResult(CommonProcessResultEnum.异常_数据本身缺失.getKey());
                callback.setProcessReport("如果这个单的价格为 0 为过来还有什么意义呢? // dispatchPrice + grprice = price 所以不能排除 订单金额为0 但有运费");
                return callback;
            }

            // 如果 这个单 没有商品 直接 返回
            if (AnCollectionUtils.isEmpty(receiveParam.getGoods())) {
                // TODO 记录 问题单
                commonOrderProcessService.cleanErrorOrder(receiveParam.getOrderSn());
                callback.setProcessResult(CommonProcessResultEnum.异常_数据本身缺失.getKey());
                callback.setProcessReport("订单商品为空");
                return callback;
            }
            // 如果没有 支付 流水 直接 返回
            if (AnCollectionUtils.isEmpty(receiveParam.getIntegralCredit())) {
                // TODO 记录 问题单
                commonOrderProcessService.cleanErrorOrder(receiveParam.getOrderSn());
                callback.setProcessResult(CommonProcessResultEnum.异常_数据本身缺失.getKey());
                callback.setProcessReport("订单支付流水为空");
                return callback;
            }

            // 处理订单
            UnifiedOrder unifiedOrderEntity = new UnifiedOrder();
            convertScoreOrderReceiveParam2Entity(receiveParam, unifiedOrderEntity);

            // 处理订单 商品
            final BigDecimal[]                costAmount                      = {new BigDecimal(0)};
            List<UnifiedOrderGoods>           processUnifiedOrderGoodsList    = new ArrayList<>();
            List<ScoreOrderGoodsReceiveParam> scoreOrderGoodsReceiveParamList = receiveParam.getGoods();

            // TODO 过滤 goodsId = 0 price = 0 || costPrice = 0 unitPrice = 0 20201013 add by linjinyu
            List<Predicate<ScoreOrderGoodsReceiveParam>> predicates = new ArrayList<>();
            predicates.add(r -> null != r.getPrice() && r.getPrice().compareTo(BigDecimal.ZERO) > 0);
            predicates.add(r -> null != r.getCostPrice() && r.getCostPrice().compareTo(BigDecimal.ZERO) > 0);
            List<ScoreOrderGoodsReceiveParam> scoreOrderGoodsReceiveParamFilterList = AnLamdaUtils.filters(scoreOrderGoodsReceiveParamList, predicates);
            if (AnCollectionUtils.isEmpty(scoreOrderGoodsReceiveParamFilterList)) {
                // TODO 记录error
                callback.setProcessResult(CommonProcessResultEnum.异常_数据本身缺失.getKey());
                callback.setProcessReport("scoreOrderGoodsReceiveParamFilterList is Empty");
                return callback;
            }

            // TODO 过滤 goodsId = 0 price = 0 costPrice = 0 unitPrice = 0 20201013 add by linjinyu

            scoreOrderGoodsReceiveParamFilterList.forEach(scoreOrderGoodsReceiveParam -> {

                UnifiedOrderGoods unifiedOrderGoods = new UnifiedOrderGoods();

                convertScoreOrderGoodsReceiveParam2Entity(scoreOrderGoodsReceiveParam, unifiedOrderGoods);

                unifiedOrderGoods.setOrderSn(unifiedOrderEntity.getOrderSn());

                processUnifiedOrderGoodsList.add(unifiedOrderGoods);

                // 计算成本金额合计
//				costAmount[0] = costAmount[0].add(new BigDecimal(unifiedOrderGoods.getQuantity().doubleValue() * unifiedOrderGoods.getCostPrice().doubleValue()));
            });

//			unifiedOrderEntity.setCostAmount(costAmount[0]);

//			UnifiedServicePartGoodsTransportParam defaultGoodsTransport = unifiedServiceService.getUnifiedServicePartGoodsTransport(unifiedOrderEntity.getServiceNo());
//
//			// 将运费转换为商品 同 (一般订单 add 20190925)
//			if (null != unifiedOrderEntity.getTransportAmount() && unifiedOrderEntity.getTransportAmount().compareTo(BigDecimal.ZERO) > 0) {
//
//				UnifiedOrderGoods unifiedOrderGoods = new UnifiedOrderGoods();
//
//				unifiedOrderGoods.setDirection(1);
//				unifiedOrderGoods.setAccountingStateCustomer(CommonAccountingStateEnum.未对账.getKey());
//				unifiedOrderGoods.setAccountingStateSupplier(CommonAccountingStateEnum.未对账.getKey());
//
//				unifiedOrderGoods.setOrderSn(unifiedOrderEntity.getOrderSn());
//
//				// add by linjinyu 20200723 已经确认过 运费goods_id 必定为 0 如果 goods_id 中 0 为能唯一代表运费,哪么,SQL就会有问题,但这个问题不明显
//				unifiedOrderGoods.setGoodsId(0L);
//				unifiedOrderGoods.setGoodsName("运费");
//				unifiedOrderGoods.setUnit(CommonOrderGoodsUnitEnum.项.getDesc());
//				unifiedOrderGoods.setQuantity(1);
//				unifiedOrderGoods.setPrice(unifiedOrderEntity.getTransportAmount());
//				unifiedOrderGoods.setCostPrice(unifiedOrderEntity.getTransportAmount());
//
//
////				defaultGoodsTransport
//				unifiedOrderGoods.setFiscalCode(defaultGoodsTransport.getGoodsTransportFiscalCode());
//				unifiedOrderGoods.setTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
//				unifiedOrderGoods.setTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());
//
//				// 商户税率
//				unifiedOrderGoods.setMerchTaxId(0L);
//				unifiedOrderGoods.setMerchTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
//				unifiedOrderGoods.setMerchTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());
//
//				// 平台税率
//				unifiedOrderGoods.setOperationTaxId(0L);
//				unifiedOrderGoods.setOperationTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
//				unifiedOrderGoods.setOperationTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());
//
//				unifiedOrderGoods.setCreateTime(DateUtil.date());
//				unifiedOrderGoods.setCreateUserId(1L);
//				unifiedOrderGoods.setUpdateTime(DateUtil.date());
//				unifiedOrderGoods.setUpdateUserId(1L);
//
//				processUnifiedOrderGoodsList.add(unifiedOrderGoods);
//
//			}

            // 处理订单 商品 积分支付流水
            /****************************************** 运费逻辑修改 mark by linjinyu at 20210419 start ******************************************/
            commonOrderTransportProcessService.processTransport(unifiedOrderEntity, processUnifiedOrderGoodsList);
            /****************************************** 运费逻辑修改 mark by linjinyu at 20210419   end ******************************************/

            /****************************************** TODO 应用 再核算 20210419 拆单 子单 订单金额有可能是不包含运费 start ******************************************/
            commonOrderProcessService.processOrderAmount(unifiedOrderEntity, processUnifiedOrderGoodsList, receiveParam.getUid());
            /****************************************** TODO 应用 再核算 20210419 拆单 子单 订单金额有可能是不包含运费   end ******************************************/


            List<UnifiedOrderCredit>           processUnifiedOrderCreditList    = new ArrayList<>();
            List<ScoreOrderCreditReceiveParam> scoreOrderCreditReceiveParamList = receiveParam.getIntegralCredit();
            scoreOrderCreditReceiveParamList.forEach(scoreOrderCreditReceiveParam -> {

                UnifiedOrderCredit scoreOrderCredit = new UnifiedOrderCredit();

                convertScoreOrderCreditReceiveParam2Entity(scoreOrderCreditReceiveParam, scoreOrderCredit);
                scoreOrderCredit.setOrderSn(unifiedOrderEntity.getOrderSn());
                processUnifiedOrderCreditList.add(scoreOrderCredit);

            });


            UnifiedOrderExample example = new UnifiedOrderExample();
            example.createCriteria()
                    .andOrderSnEqualTo(receiveParam.getOrderSn());
            List<UnifiedOrder> unifiedOrders4Check = unifiedOrderMapper.selectByExample(example);

            // 如果这个单不存在
            if (AnCollectionUtils.isEmpty(unifiedOrders4Check)) {

                // 没有商品 没有流水 不保存订单数据
                if (AnCollectionUtils.isNotEmpty(processUnifiedOrderGoodsList) && AnCollectionUtils.isNotEmpty(processUnifiedOrderCreditList)) {

                    unifiedOrderMapper.insertSelective(unifiedOrderEntity);

                    processUnifiedOrderGoodsList.forEach(scoreOrderGoods -> {
                        unifiedOrderGoodsMapper.insert(scoreOrderGoods);
                    });

                    processUnifiedOrderCreditList.forEach(scoreOrderCredit -> {
                        unifiedOrderCreditMapper.insert(scoreOrderCredit);
                    });
                }

            } else {

                // TODO 未处理 大于 1 的情况
                UnifiedOrder scoreOrderEntitySearch = unifiedOrders4Check.get(0);

                unifiedOrderEntity.setId(scoreOrderEntitySearch.getId());
                unifiedOrderEntity.setUpdateTime(DateUtil.date());

                // rewrite 对账状态
                unifiedOrderEntity.setAccountingStateCustomer(scoreOrderEntitySearch.getAccountingStateCustomer());
                unifiedOrderEntity.setAccountingStateSupplier(scoreOrderEntitySearch.getAccountingStateSupplier());

                commonOrderProcessService.attachUpdate(unifiedOrderEntity);

                // 重构 by linjinyu 20200915 start
                commonOrderGoodsMergeProcessService.processUnifiedOrderGoodsBykafkaPushOrderReceive(unifiedOrderEntity, processUnifiedOrderGoodsList, 1);
                // 重构 by linjinyu 20200915   end

                // 重构 by linjinyu 20200915 start
                commonOrderCreditMergeProcessService.processUnifiedOrderCreditBykafkaPushOrderReceive(unifiedOrderEntity, processUnifiedOrderCreditList, 1);
                // 重构 by linjinyu 20200915   end

            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("商城 同步 统一订单 (侨城汇-积分订单)-UnifiedOrder 失败:{}", ExceptionUtils.getMessage(e));
        } finally {
            log.info("##################### kafkaScoreOrderReceive end #####################");

            return callback;
        }
    }

    /**
     * 测试正常 202006111715
     *
     * @param scoreOrderReceiveParam
     * @param unifiedOrderEntity
     */
    public void convertScoreOrderReceiveParam2Entity(ScoreOrderReceiveParam scoreOrderReceiveParam, UnifiedOrder unifiedOrderEntity) {

        unifiedOrderEntity.setOrderType(CommonOrderTypeEnum.订单.getKey());

        unifiedOrderEntity.setOrderSn(scoreOrderReceiveParam.getOrderSn());
        unifiedOrderEntity.setOrderSnOutTrade(scoreOrderReceiveParam.getOrderSn());

        unifiedOrderEntity.setServiceNo(CommonServiceNoEnum.getServiceNoByServiceId(scoreOrderReceiveParam.getOrderType()));
        unifiedOrderEntity.setServiceName(CommonServiceNoEnum.getServiceNameByServiceId(scoreOrderReceiveParam.getOrderType()));

        unifiedOrderEntity.setProjectId(scoreOrderReceiveParam.getOrganizationId());
        unifiedOrderEntity.setProjectName(scoreOrderReceiveParam.getOrganizationName());

        unifiedOrderEntity.setPurchaseVirtId(scoreOrderReceiveParam.getEnterpriseId());
        unifiedOrderEntity.setPurchaseVirtName(scoreOrderReceiveParam.getEnterpriseName());

        unifiedOrderEntity.setPurchaseDepartmentName(scoreOrderReceiveParam.getDepartmentName());

        unifiedOrderEntity.setPurchaseUserId(scoreOrderReceiveParam.getRealnameId());
        unifiedOrderEntity.setPurchaseUserName(scoreOrderReceiveParam.getRealname());
        unifiedOrderEntity.setPurchasePhone(scoreOrderReceiveParam.getMobile());

        unifiedOrderEntity.setSaleId(scoreOrderReceiveParam.getMerchId());
        unifiedOrderEntity.setSaleName(scoreOrderReceiveParam.getMerchName());

        // 商城 dispatchPrice + grprice = price scoreOrderReceiveParam.getPrice()
        unifiedOrderEntity.setAmount(scoreOrderReceiveParam.getPrice());
        unifiedOrderEntity.setCostAmount(new BigDecimal("0"));
        unifiedOrderEntity.setTransportAmount(scoreOrderReceiveParam.getDispatchPrice());
        unifiedOrderEntity.setTransportMerchantAssumeAmount(scoreOrderReceiveParam.getDispatchPriceJd());

        unifiedOrderEntity.setPayType(scoreOrderReceiveParam.getPayType());
        // TODO 写死
        unifiedOrderEntity.setPayState(2);
        unifiedOrderEntity.setOrderState(scoreOrderReceiveParam.getStatus());
        unifiedOrderEntity.setOrderStateName(SuperDeskMallOrderStatusEnum.getDesc(scoreOrderReceiveParam.getStatus()));

        // TODO 通过业务方的 订单状态 归一成 统一订单 订单完成 状态 (已完成 未完成 已取消)
        unifiedOrderEntity.setCompletState(UnifiedOrderCompletStateUtils.convertBySuperDeskMallOrderStatus(scoreOrderReceiveParam.getStatus(), scoreOrderReceiveParam.getDeleted()));

        unifiedOrderEntity.setOrderLockingCustomer(CommonAccountingLockingEnum.正常.getKey());
        unifiedOrderEntity.setOrderLockingSupplier(CommonAccountingLockingEnum.正常.getKey());

        unifiedOrderEntity.setAccountingStateCustomer(CommonAccountingStateEnum.未对账.getKey());
        unifiedOrderEntity.setAccountingStateSupplier(CommonAccountingStateEnum.未对账.getKey());



        // TODO 写死
        unifiedOrderEntity.setSettlementType(2);
        // TODO 写死
        unifiedOrderEntity.setAppraiseState(2);
        // TODO 写死
        unifiedOrderEntity.setSourceChannel(1);

        Map<String, String> otherParams = new HashMap<>();
        otherParams.put("sendTime", AnDateHelper.parseDateToStrFormat(scoreOrderReceiveParam.getSendTime(), AnDateHelper.DATE_TIME_STRING));
        unifiedOrderEntity.setOtherParams(AnJsonUtils.toJson(otherParams));

        // 这个是可以用于记录 谁创建的 20200611
        unifiedOrderEntity.setCreateUserId(1L);
        unifiedOrderEntity.setCreateTime(scoreOrderReceiveParam.getCreateTime());

        unifiedOrderEntity.setPayTime(scoreOrderReceiveParam.getPayTime());
        unifiedOrderEntity.setSendTime(scoreOrderReceiveParam.getSendTime());
        unifiedOrderEntity.setFinishTime(scoreOrderReceiveParam.getFinishTime());

        // TODO 结算时间逻辑 添加 mark by linjinyu at 20211112
        // |---- 1. kafka 正向通道,触发点 订单已完成 产生 结算时间(订单完成时间填充) right
        // |---- 2. kafka 负向通道,解发点 退单已完成 解锁正负单时 产生结算时间(退单完成时间填充)
        // |---- 3. 供应商结算 未对账商品 list 逻辑 不变(没说明) 看样子是 加了个 结算时间 筛选逻辑
        if (UnifiedOrderCompletStateEnum.已完成.getKey().equals(unifiedOrderEntity.getCompletState())
                && ObjectUtil.isNotNull(unifiedOrderEntity.getFinishTime())) {
            unifiedOrderEntity.setAccountingTime(unifiedOrderEntity.getFinishTime());
        }


        // TODO 写死
        unifiedOrderEntity.setUserType("2");
        // TODO 写死
        unifiedOrderEntity.setUserName("系统");

        // 这个为什么用完成时间来填充
        unifiedOrderEntity.setUpdateTime(scoreOrderReceiveParam.getFinishTime());
        // TODO 写死
        unifiedOrderEntity.setUpdateUserId(1L);

        // 原本代码也是注释了
//		unifiedOrderEntity.setRecUserId();
        unifiedOrderEntity.setRecUserName(scoreOrderReceiveParam.getAddressRealname());
        unifiedOrderEntity.setRecUserPhone(scoreOrderReceiveParam.getAddressMobile());
        unifiedOrderEntity.setRecAddress(scoreOrderReceiveParam.getAddressAddress());


        /************************************** update by linjinjy 20200727 20200723_林志鹏_税率,退款格式.docx **************************************/
        unifiedOrderEntity.setExpress(scoreOrderReceiveParam.getExpress());
        unifiedOrderEntity.setExpressCompany(scoreOrderReceiveParam.getExpressCompany());
        unifiedOrderEntity.setExpressSn(scoreOrderReceiveParam.getExpressSn());
        /************************************** update by linjinjy 20200723 20200723_林志鹏_税率,退款格式.docx **************************************/


        unifiedOrderEntity.setRemarks(scoreOrderReceiveParam.getRemark());


        // 这个是老的,以前是在商城哪里开票的,功能是 雨与豪做的
        // TODO 开票资料保存 待完成 早前也是没有的 start
//		unifiedOrderEntity.setInvState();
//		unifiedOrderEntity.setInvNumber();
//		unifiedOrderEntity.setInvSn();
        // TODO 开票资料保存 待完成 早前也是没有的   end

    }

    /**
     * 测试正常 202006112100
     *
     * @param scoreOrderGoodsReceiveParam
     * @param scoreOrderGoodsEntity
     */
    public void convertScoreOrderGoodsReceiveParam2Entity(ScoreOrderGoodsReceiveParam scoreOrderGoodsReceiveParam, UnifiedOrderGoods scoreOrderGoodsEntity) {


        scoreOrderGoodsEntity.setGoodsType(CommonOrderGoodsTypeEnum.商品.getKey());
        scoreOrderGoodsEntity.setDirection(CommonDirectionEnum.正向.getKey());

        scoreOrderGoodsEntity.setGoodsLockingCustomer(CommonAccountingLockingEnum.正常.getKey());
        scoreOrderGoodsEntity.setGoodsLockingSupplier(CommonAccountingLockingEnum.正常.getKey());

        scoreOrderGoodsEntity.setAccountingStateCustomer(CommonAccountingStateEnum.未对账.getKey());
        scoreOrderGoodsEntity.setAccountingStateSupplier(CommonAccountingStateEnum.未对账.getKey());

        scoreOrderGoodsEntity.setOutSnapshotOrderGoodsId(scoreOrderGoodsReceiveParam.getOutSnapshotOrderGoodsId());

//		scoreOrderGoodsEntity.setOrderSn();
        scoreOrderGoodsEntity.setGoodsId(scoreOrderGoodsReceiveParam.getGoodsId());
        scoreOrderGoodsEntity.setGoodsName(scoreOrderGoodsReceiveParam.getTitle());
        scoreOrderGoodsEntity.setQuantity(scoreOrderGoodsReceiveParam.getTotal());
        scoreOrderGoodsEntity.setUnit(StrUtil.isBlank(scoreOrderGoodsReceiveParam.getUnit()) ? CommonOrderGoodsUnitEnum.项.getDesc() : scoreOrderGoodsReceiveParam.getUnit());
        scoreOrderGoodsEntity.setPrice(scoreOrderGoodsReceiveParam.getUnitPrice());
        // add costprice 成本价
        scoreOrderGoodsEntity.setCostPrice(scoreOrderGoodsReceiveParam.getCostPrice());

        Map<String, String> goodsParams = new HashMap<>();
        goodsParams.put("taxCode", scoreOrderGoodsReceiveParam.getTaxCode());
        goodsParams.put("fiscalCode", scoreOrderGoodsReceiveParam.getFiscalCode());
        scoreOrderGoodsEntity.setGoodsParams(AnJsonUtils.toJson(goodsParams));

        scoreOrderGoodsEntity.setSelfOption(scoreOrderGoodsReceiveParam.getSelfOption() == null ? "" : scoreOrderGoodsReceiveParam.getSelfOption());

        /************************************** update by linjinjy 20200727 20200723_林志鹏_税率,退款格式.docx 将要弃用 20210413 mark by linjinyu **************************************/

        scoreOrderGoodsEntity.setFiscalCode(scoreOrderGoodsReceiveParam.getFiscalCode());
        scoreOrderGoodsEntity.setTaxCode(scoreOrderGoodsReceiveParam.getTaxCode());
        scoreOrderGoodsEntity.setTaxRate(scoreOrderGoodsReceiveParam.getTaxRate());

        /************************************** update by linjinjy 20200727 20200723_林志鹏_税率,退款格式.docx 将要弃用 20210413 mark by linjinyu **************************************/


        /****************************************** 税率修改 20210413 mark by linjinyu start ******************************************/

        // 商户税率
        scoreOrderGoodsEntity.setMerchTaxId(scoreOrderGoodsReceiveParam.getMerchTaxId());
        scoreOrderGoodsEntity.setMerchTaxCode(scoreOrderGoodsReceiveParam.getMerchTaxCode());
        scoreOrderGoodsEntity.setMerchTaxRate(scoreOrderGoodsReceiveParam.getMerchTaxRate());

        // 平台税率
        scoreOrderGoodsEntity.setOperationTaxId(scoreOrderGoodsReceiveParam.getOperationTaxId());
        scoreOrderGoodsEntity.setOperationTaxCode(scoreOrderGoodsReceiveParam.getOperationTaxCode());
        scoreOrderGoodsEntity.setOperationTaxRate(scoreOrderGoodsReceiveParam.getOperationTaxRate());

        /****************************************** 税率修改 20210413 mark by linjinyu   end ******************************************/


        // 弃 20200904 by linjinyu
        /************************************** update by linjinjy 20200810 20200729_林志鹏_税率,退款格式.docx **************************************/

//		scoreOrderGoodsEntity.setCateName(scoreOrderGoodsReceiveParam.getCateName());

        /************************************** update by linjinjy 20200810 20200729_林志鹏_税率,退款格式.docx **************************************/


        /****************************************** 三级分类 20200904 by linjinyu 20200904_林志鹏_三极分类 ******************************************/

        scoreOrderGoodsEntity.setCateIdLv1(scoreOrderGoodsReceiveParam.getCateIdLv1());
        scoreOrderGoodsEntity.setCateIdLv2(scoreOrderGoodsReceiveParam.getCateIdLv2());
        scoreOrderGoodsEntity.setCateIdLv3(scoreOrderGoodsReceiveParam.getCateIdLv3());
        scoreOrderGoodsEntity.setCateNameLv1(scoreOrderGoodsReceiveParam.getCateNameLv1());
        scoreOrderGoodsEntity.setCateNameLv2(scoreOrderGoodsReceiveParam.getCateNameLv2());
        scoreOrderGoodsEntity.setCateNameLv3(scoreOrderGoodsReceiveParam.getCateNameLv3());

        /****************************************** 三级分类 20200904 by linjinyu 20200904_林志鹏_三极分类 ******************************************/


        scoreOrderGoodsEntity.setCreateTime(DateUtil.date());
        // TODO 写死
        scoreOrderGoodsEntity.setCreateUserId(1L);
        scoreOrderGoodsEntity.setUpdateTime(DateUtil.date());
        // TODO 写死
        scoreOrderGoodsEntity.setUpdateUserId(1L);


    }

    /**
     * 测试正常 202006112200
     *
     * @param scoreOrderCreditReceiveParam
     * @param unifiedOrderCreditEntity
     */
    public void convertScoreOrderCreditReceiveParam2Entity(ScoreOrderCreditReceiveParam scoreOrderCreditReceiveParam, UnifiedOrderCredit unifiedOrderCreditEntity) {

//		scoreOrderCreditEntity.setOrderSn();

        unifiedOrderCreditEntity.setDirection(CommonDirectionEnum.正向.getKey());

        unifiedOrderCreditEntity.setOutSnapshotOrderCreditId(scoreOrderCreditReceiveParam.getOrderCreditId());

        unifiedOrderCreditEntity.setOrderId(scoreOrderCreditReceiveParam.getOrderId());
        unifiedOrderCreditEntity.setGoodsId(scoreOrderCreditReceiveParam.getGoodsId());
        unifiedOrderCreditEntity.setCreditType(scoreOrderCreditReceiveParam.getCreditType());

        if (scoreOrderCreditReceiveParam.getCreditType().equals(0) && scoreOrderCreditReceiveParam.getCreditName() == null) {
            unifiedOrderCreditEntity.setCreditName("微信支付");
        } else {
            unifiedOrderCreditEntity.setCreditName(scoreOrderCreditReceiveParam.getCreditName());
        }

        /* 积分变动类型(1充值 2消费 3退款) */
        unifiedOrderCreditEntity.setDailyAccountType(scoreOrderCreditReceiveParam.getDailyAccountType());
        unifiedOrderCreditEntity.setBatchs(scoreOrderCreditReceiveParam.getBatchs());
        unifiedOrderCreditEntity.setBalance(scoreOrderCreditReceiveParam.getBalance());

        // TODO 要检查
        unifiedOrderCreditEntity.setEnterpriseId(scoreOrderCreditReceiveParam.getEnterpriseId());
        unifiedOrderCreditEntity.setMemberId(scoreOrderCreditReceiveParam.getMemberId());

        unifiedOrderCreditEntity.setPayStatus(scoreOrderCreditReceiveParam.getPayStatus());
        unifiedOrderCreditEntity.setPayType(scoreOrderCreditReceiveParam.getPayType());

        unifiedOrderCreditEntity.setCreditLockingCustomer(CommonAccountingLockingEnum.正常.getKey());
        unifiedOrderCreditEntity.setCreditLockingSupplier(CommonAccountingLockingEnum.正常.getKey());

        unifiedOrderCreditEntity.setAccountingStateCustomer(CommonAccountingStateEnum.未对账.getKey());
        unifiedOrderCreditEntity.setAccountingStateSupplier(CommonAccountingStateEnum.未对账.getKey());

        unifiedOrderCreditEntity.setInvoiceApplyStatus(UnifiedOrderCreditInvoiceApplyStatusEnum.待申请.getKey());
        unifiedOrderCreditEntity.setInvoiceMakeStatus(UnifiedOrderCreditInvoiceMakeStatusEnum.待开票.getKey());
        unifiedOrderCreditEntity.setBackStatus(UnifiedOrderCreditBackAccountStatusEnum.未回款.getKey());


        unifiedOrderCreditEntity.setCreateTime(new Date(scoreOrderCreditReceiveParam.getCreateTime()));
        unifiedOrderCreditEntity.setCreateUserId(1L);
        unifiedOrderCreditEntity.setUpdateTime(DateUtil.date());
        unifiedOrderCreditEntity.setUpdateUserId(1L);

    }
}
