package com.hfkj.service.gas.impl;

import com.hfkj.common.utils.DateUtil;
import com.hfkj.dao.BsGasOrderSpreadMapper;
import com.hfkj.entity.*;
import com.hfkj.service.gas.BsGasOrderSpreadService;
import com.hfkj.service.spread.BsUserSpreadAccountService;
import com.hfkj.service.spread.BsUserSpreadOilChannelService;
import com.hfkj.service.spread.BsUserSpreadRelService;
import com.hfkj.sysenum.spread.UserSpreadAccountRecordSourceTypeEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: BsGasOrderSpreadServiceImpl
 * @author: HuRui
 * @date: 2025/4/7
 **/
@Service("gasOrderSpreadService")
public class BsGasOrderSpreadServiceImpl implements BsGasOrderSpreadService {
    @Resource
    private BsGasOrderSpreadMapper gasOrderSpreadMapper;
    @Resource
    private BsUserSpreadRelService userSpreadRelService;
    @Resource
    private BsUserSpreadOilChannelService userSpreadOilChannelService;
    @Resource
    private BsUserSpreadAccountService userSpreadAccountService;
    @Override
    public void editData(BsGasOrderSpread data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setStatus(1);
            data.setCreateTime(new Date());
            gasOrderSpreadMapper.insert(data);
        } else {
            gasOrderSpreadMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public void createOrderSpread(BsGasOrder order) {
        // 油站渠道
        BsUserSpreadOilChannel channel = userSpreadOilChannelService.getDetailByChannel(order.getChannelType());
        if (channel != null && order.getUserId() != null) {
            // 查询用户直属上级
            BsUserSpreadRel userSpreadRel = userSpreadRelService.getDetailByUser(order.getUserId());
            if (userSpreadRel != null) {
                BsGasOrderSpread gasOrderSpread = new BsGasOrderSpread();
                gasOrderSpread.setOrderNo(order.getOrderNo());
                gasOrderSpread.setGainsRebateUserId(userSpreadRel.getParentUserId());
                gasOrderSpread.setGainsRebateRate(channel.getRebate());
                gasOrderSpread.setGainsRebateAmount(order.getPayablePrice().multiply(channel.getRebate().divide(new BigDecimal("100"))).setScale(2, BigDecimal.ROUND_HALF_DOWN));

                // 查询二级
                BsUserSpreadRel userSpreadOneRel = userSpreadRelService.getDetailByUser(userSpreadRel.getParentUserId());
                if (userSpreadOneRel != null) {
                    gasOrderSpread.setGainsRebateOneUserId(userSpreadOneRel.getParentUserId());
                    gasOrderSpread.setGainsRebateOneRate(channel.getRebateOne());
                    gasOrderSpread.setGainsRebateOneAmount(order.getPayablePrice().multiply(channel.getRebateOne().divide(new BigDecimal("100"))).setScale(2, BigDecimal.ROUND_HALF_DOWN));

                    /*// 查询二级
                    BsUserSpreadRel userSpreadTwoRel = userSpreadRelService.getDetailByUser(userSpreadOneRel.getParentUserId());
                    if (userSpreadTwoRel != null) {
                        gasOrderSpread.setGainsRebateTwoUserId(userSpreadTwoRel.getParentUserId());
                        gasOrderSpread.setGainsRebateTwoRate(channel.getRebateTwo());
                        gasOrderSpread.setGainsRebateTwoAmount(order.getPayablePrice().multiply(channel.getRebateTwo().divide(new BigDecimal("100"))).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                    }*/
                }
                editData(gasOrderSpread);
            }
        }
    }

    @Override
    public BsGasOrderSpread getDetailByOrderNo(String orderNo) {
        BsGasOrderSpreadExample example = new BsGasOrderSpreadExample();
        example.createCriteria().andOrderNoEqualTo(orderNo).andStatusEqualTo(1);
        List<BsGasOrderSpread> list = gasOrderSpreadMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void settleOrder(Long userId) throws Exception {
        // 获取结算订单数据
        Map<String,Object> dataParam = new HashMap<>();
        dataParam.put("userId", userId);
        Map<String,Object> settleData = gasOrderSpreadMapper.querySettleOrderList(dataParam);
        // 佣金大于0充值
        BigDecimal settleAmount = new BigDecimal(MapUtils.getString(settleData, "settleAmount"));
        if (settleAmount.compareTo(new BigDecimal("0")) >= 1) {
            Map<String,Object> otherParam = new HashMap<>();
            otherParam.put("sourceContent", MapUtils.getString(settleData, "settleDate")+"佣金");
            userSpreadAccountService.increase(userId, settleAmount, UserSpreadAccountRecordSourceTypeEnum.type1,otherParam);
        }
    }

    @Override
    public BigDecimal countSettleOrderPrice(Map<String, Object> param) {
        if (StringUtils.isBlank(MapUtils.getString(param, "status"))) {
            // 设置查询默认状态数据
            param.put("status", "2,4");
        }
        if (MapUtils.getLong(param, "payTimeS") != null) {
            param.put("payTimeS", DateUtil.date2String(new Date(MapUtils.getLong(param, "payTimeS")),DateUtil.Y_M_D_HMS));
        }
        if (MapUtils.getLong(param, "payTimeE") != null) {
            param.put("payTimeE", DateUtil.date2String(new Date(MapUtils.getLong(param, "payTimeE")),DateUtil.Y_M_D_HMS));
        }
        return gasOrderSpreadMapper.countSettleOrderPrice(param);
    }

    @Override
    public HashMap<String, Object> countCommission() {
        return gasOrderSpreadMapper.countCommission();
    }

    @Override
    public BigDecimal getNotCashOutAmount(Long userId) {
        return gasOrderSpreadMapper.queryNotCashOutAmount(userId);
    }
}
