package com.hfkj.service.gas.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.dao.BsGasOrderMapper;
import com.hfkj.entity.BsGasOrder;
import com.hfkj.entity.BsGasOrderExample;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.service.order.BsOrderService;
import com.hfkj.service.order.OrderPaySuccessService;
import com.hfkj.sysenum.order.OrderPayTypeEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @className: HighGasOrderServiceImpl
 * @author: HuRui
 * @date: 2022/9/6
 **/
@Service("gasOrderService")
public class BsGasOrderServiceImpl implements BsGasOrderService {

    @Resource
    private BsGasOrderMapper gasOrderMapper;
    @Resource
    private BsOrderService orderService;
    @Resource
    private OrderPaySuccessService paySuccessService;
    @Override
    public void addGasOrder(BsGasOrder gasOrder) {
        gasOrder.setCreateTime(new Date());
        gasOrderMapper.insert(gasOrder);
    }

    @Override
    public void updateGasOrder(BsGasOrder gasOrder) {
        gasOrderMapper.updateByPrimaryKeySelective(gasOrder);
    }

    @Override
    public BsGasOrder getDetailByOrderChildNo(String orderChildNo) {
        BsGasOrderExample example = new BsGasOrderExample();
        example.createCriteria().andOrderChildNoEqualTo(orderChildNo);
        List<BsGasOrder> list = gasOrderMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsGasOrder getDetailByOrderNo(String orderNo) {
        BsGasOrderExample example = new BsGasOrderExample();
        example.createCriteria().andOrderNoEqualTo(orderNo);
        List<BsGasOrder> list = gasOrderMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsGasOrder repairOrder(String orderNo) throws Exception {
        // 加油订单
        BsGasOrder gasOrder = getDetailByOrderNo(orderNo);
        BsGasOrder result  = null;
        if (!gasOrder.getPayType().equals(OrderPayTypeEnum.type4.getCode())) {
            // 补推
            result = paySuccessService.oilHandle(orderService.getDetail(orderNo));
            if (result.getAbnormal().equals(true)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "补单失败！原因：" + result.getAbnormalContent());
            }
        } else {
            // 补推
            result = paySuccessService.oilApiHandle(gasOrder);
            if (result.getAbnormal().equals(true)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "补单失败！原因：" + result.getAbnormalContent());
            }
        }
        return result;
    }

    @Override
    public BsGasOrder getDetailByPaySerialNo(String paySerialNo) {
        BsGasOrderExample example = new BsGasOrderExample();
        example.createCriteria().andPaySerialNoEqualTo(paySerialNo);
        List<BsGasOrder> list = gasOrderMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsGasOrder getDetailByPayChannelOrderNo(String payChannelOrderNo) {
        BsGasOrderExample example = new BsGasOrderExample();
        example.createCriteria().andPayChannelOrderNoEqualTo(payChannelOrderNo);
        List<BsGasOrder> list = gasOrderMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsGasOrder getDetailByChannelOrderNo(String channelOrderNo) {
        BsGasOrderExample example = new BsGasOrderExample();
        example.createCriteria().andChannelOrderNoEqualTo(channelOrderNo);
        List<BsGasOrder> list = gasOrderMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Map<String, Object> getGasOrderCount(Map<String, Object> param) {
        if (MapUtils.getLong(param, "createTimeS") != null) {
            param.put("createTimeS", DateUtil.date2String(new Date(MapUtils.getLong(param, "createTimeS")), DateUtil.Y_M_D_HMS));
        }
        if (MapUtils.getLong(param, "createTimeE") != null) {
            param.put("createTimeE", DateUtil.date2String(new Date(MapUtils.getLong(param, "createTimeE")), DateUtil.Y_M_D_HMS));
        }

        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));
        }

        if (MapUtils.getLong(param, "refundTimeS") != null) {
            param.put("refundTimeS", DateUtil.date2String(new Date(MapUtils.getLong(param, "refundTimeS")), DateUtil.Y_M_D_HMS));
        }
        if (MapUtils.getLong(param, "refundTimeE") != null) {
            param.put("refundTimeE", DateUtil.date2String(new Date(MapUtils.getLong(param, "refundTimeE")), DateUtil.Y_M_D_HMS));
        }
        return gasOrderMapper.selectGasOrderCount(param);
    }

    @Override
    public List<BsGasOrder> getGasOrderList(Map<String, Object> param) {
        BsGasOrderExample example = new BsGasOrderExample();
        BsGasOrderExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(param, "merChainBrandId") != null) {
            criteria.andMerChainBrandIdEqualTo(MapUtils.getLong(param, "merChainBrandId"));
        }

        if (MapUtils.getLong(param, "merId") != null) {
            criteria.andMerIdEqualTo(MapUtils.getLong(param, "merId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merNo"))) {
            criteria.andMerNoEqualTo(MapUtils.getString(param, "merNo"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merName"))) {
            criteria.andMerNameLike("%"+MapUtils.getString(param, "merName")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "orderNo"))) {
            criteria.andOrderNoLike("%"+MapUtils.getString(param, "orderNo")+"%");
        }

        if (MapUtils.getInteger(param, "channelType") != null) {
            criteria.andChannelTypeEqualTo(MapUtils.getInteger(param, "channelType"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "orderChildNo"))) {
            criteria.andOrderChildNoLike("%"+MapUtils.getString(param, "orderChildNo")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "channelOrderNo"))) {
            criteria.andChannelOrderNoLike("%"+MapUtils.getString(param, "channelOrderNo")+"%");
        }

        if (MapUtils.getLong(param, "userId") != null) {
            criteria.andUserIdEqualTo(MapUtils.getLong(param, "userId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "userPhone"))) {
            criteria.andUserPhoneLike("%"+MapUtils.getString(param, "userPhone")+"%");
        }

        if (MapUtils.getInteger(param, "gasOilType") != null) {
            criteria.andGasOilTypeEqualTo(MapUtils.getInteger(param, "gasOilType"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "paySerialNo"))) {
            criteria.andPaySerialNoLike("%"+MapUtils.getString(param, "paySerialNo")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "gasStaffName"))) {
            criteria.andGasStaffNameLike("%"+MapUtils.getString(param, "gasStaffName")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "gasOilNo"))) {
            criteria.andGasOilNoEqualTo(MapUtils.getString(param, "gasOilNo"));
        }

        if (MapUtils.getInteger(param, "payType") != null) {
            criteria.andPayTypeEqualTo(MapUtils.getInteger(param, "payType"));
        }

        if (MapUtils.getLong(param, "gasClassGroupId") != null) {
            criteria.andGasClassGroupIdEqualTo(MapUtils.getLong(param, "gasClassGroupId"));
        }

        if (MapUtils.getLong(param, "gasClassGroupTaskId") != null) {
            criteria.andGasClassGroupTaskIdEqualTo(MapUtils.getLong(param, "gasClassGroupTaskId"));
        }

        if (MapUtils.getLong(param, "agentId") != null && MapUtils.getLong(param, "agentId") == 0L) {
            criteria.andAgentIdIsNull();
        }

        if (MapUtils.getLong(param, "agentId") != null && MapUtils.getLong(param, "agentId") != 0L) {
            criteria.andAgentIdEqualTo(MapUtils.getLong(param, "agentId"));
        }

        if (MapUtils.getLong(param, "agentStaffId") != null) {
            criteria.andAgentStaffIdEqualTo(MapUtils.getLong(param, "agentStaffId"));
        }

        if (MapUtils.getLong(param, "gasStaffId") != null) {
            criteria.andGasStaffIdEqualTo(MapUtils.getLong(param, "gasStaffId"));
        }

        if (MapUtils.getLong(param, "gasClassGroupTaskId") != null) {
            criteria.andGasClassGroupTaskIdEqualTo(MapUtils.getLong(param, "gasClassGroupTaskId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "receiptStatus"))) {
            List<Integer> receiptStatus = Arrays.stream(MapUtils.getString(param, "receiptStatus").split(","))
                    .flatMapToInt(num -> IntStream.of(Integer.parseInt(num))).boxed()
                    .collect(Collectors.toList());
            criteria.andReceiptStatusIn(receiptStatus);
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "status"))) {
            List<Integer> status = Arrays.stream(MapUtils.getString(param, "status").split(","))
                    .flatMapToInt(num -> IntStream.of(Integer.parseInt(num))).boxed()
                    .collect(Collectors.toList());
            criteria.andStatusIn(status);
        }

        if (MapUtils.getLong(param, "createTimeS") != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "createTimeS")));
        }

        if (MapUtils.getLong(param, "createTimeE") != null) {
            criteria.andCreateTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "createTimeE")));
        }

        if (MapUtils.getLong(param, "payTimeS") != null) {
            criteria.andPayTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "payTimeS")));
        }

        if (MapUtils.getLong(param, "payTimeE") != null) {
            criteria.andPayTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "payTimeE")));
        }

        if (MapUtils.getLong(param, "refundTimeS") != null) {
            criteria.andRefundTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "refundTimeS")));
        }

        if (MapUtils.getLong(param, "refundTimeE") != null) {
            criteria.andRefundTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "refundTimeE")));
        }

        if (MapUtils.getBoolean(param, "abnormal") != null) {
            criteria.andAbnormalEqualTo(MapUtils.getBoolean(param,"abnormal"));
        }

        example.setOrderByClause("create_time desc");
        return gasOrderMapper.selectByExample(example);
    }

    @Override
    public List<HashMap<String, Object>> getOilOrderSpread(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 gasOrderMapper.queryOilOrderSpread(param);
    }

    @Override
    public int countTodayOrder(String userPhone) {
        return gasOrderMapper.countTodayOrder(userPhone);
    }
}
