package com.hzs.ground.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.ESettleType;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.ground.order.SaOrderSettle;
import com.hzs.common.domain.ground.order.ext.SaOrderSettleExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.ext.SaOrderItemsExt;
import com.hzs.common.domain.scm.postage.ext.ThSupplierPostageExt;
import com.hzs.ground.order.mapper.SaOrderSettleMapper;
import com.hzs.ground.order.param.OrderQueryParam;
import com.hzs.ground.order.service.ISaOrderSettleService;
import com.hzs.ground.postage.service.IThSupplierPostageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 销售订单-三方结算表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2024-04-15
 */
@Service
public class SaOrderSettleServiceImpl extends ServiceImpl<SaOrderSettleMapper, SaOrderSettle> implements ISaOrderSettleService {

    @Autowired
    private IThSupplierPostageService supplierPostageService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrderSettleByOrder(OrderQueryParam orderQueryParam, List<SaOrderExt> orderList) {
        List<SaOrderSettle> saOrderSettleList = new ArrayList<>();
        List<SaOrderItemsExt> orderItemsExtList = new ArrayList<>();
        for (SaOrderExt saOrderExt : orderList) {
            for (SaOrderItemsExt saOrderItems : saOrderExt.getOrderItemsExtList()) {
                SaOrderSettle saOrderSettle = SaOrderSettle.builder()
                        .pkOrder(saOrderExt.getPkId())
                        .pkOrderItems(saOrderItems.getPkId())
                        .settleStatus(EYesNo.NO.getIntValue())
                        .price(saOrderItems.getSupplyPrice())
                        .quantity(saOrderItems.getQuantity())
                        .pkSupplier(orderQueryParam.getPkSupplier())
                        .postage(BigDecimal.ZERO)
                        .totalPrice(ComputeUtil.computeMultiply(saOrderItems.getSupplyPrice(), saOrderItems.getQuantity()))
                        .build();
                saOrderSettle.setPkCountry(orderQueryParam.getPkCountry());
                saOrderSettle.setPkCreator(orderQueryParam.getPkCreator());
                saOrderSettleList.add(saOrderSettle);
                saOrderItems.setPkSupplier(orderQueryParam.getPkSupplier());
                orderItemsExtList.add(saOrderItems);
            }
        }
        List<ThSupplierPostageExt> thSupplierPostageExtList = supplierPostageService.querySupplierPostageByOrder(orderItemsExtList);
        if (saOrderSettleList.size() > 0) {
            Map<Long, ThSupplierPostageExt> supplierPostageExtMap = new HashMap<>();
            if(thSupplierPostageExtList.size()>0){
                thSupplierPostageExtList.forEach(thSupplierPostageExt -> supplierPostageExtMap.put(thSupplierPostageExt.getPkOrder(), thSupplierPostageExt));
            }
            for (SaOrderSettle saOrderSettle : saOrderSettleList) {
                if(supplierPostageExtMap.containsKey(saOrderSettle.getPkOrder())){
                    saOrderSettle.setPostage(supplierPostageExtMap.get(saOrderSettle.getPkOrder()).getPostage());
                    saOrderSettle.setTotalPrice(ComputeUtil.computeAdd(saOrderSettle.getTotalPrice(), saOrderSettle.getPostage()));
                    supplierPostageExtMap.remove(saOrderSettle.getPkOrder());
                }
            }
            saveBatch(saOrderSettleList);
        }
    }

    @Override
    public List<SaOrderSettleExt> querySaOrderSettle(OrderQueryParam queryParam) {
        if (queryParam.getEndTime() != null) {
            queryParam.setEndTime(DateUtils.afterDate(1, ChronoUnit.DAYS, queryParam.getEndTime()));
        }
        return baseMapper.querySaOrderSettle(queryParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSaOrderSettle(List<SaOrderSettleExt> orderSettleExtList) {
        // 分为线上线下2部分
        List<SaOrderSettleExt> onlineSaOrderSettleList = new ArrayList<>();
        for (SaOrderSettleExt saOrderSettleExt : orderSettleExtList) {
            if(ESettleType.ONLINE.getValue() == saOrderSettleExt.getSettleType()){
                onlineSaOrderSettleList.add(saOrderSettleExt);
            }
        }
        // 批量更新结算状态
        baseMapper.updateSaOrderSettle(orderSettleExtList);
        // 为账号发放结算金额，这个可能会发2份，待确认
    }
}
