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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.domain.sale.ext.SaOrderChargeLogExt;
import com.hzs.common.domain.sale.order.SaOrderChargeLog;
import com.hzs.sale.order.mapper.SaOrderChargeLogMapper;
import com.hzs.sale.order.param.SaOrderChargeLogParam;
import com.hzs.sale.order.service.ISaOrderChargeLogService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单修改记录 服务实现类
 *
 * @author hzs
 * @since 2023-02-01
 */
@Service
public class SaOrderChargeLogServiceImpl extends ServiceImpl<SaOrderChargeLogMapper, SaOrderChargeLog> implements ISaOrderChargeLogService {

    @Override
    public Map<Long, Integer> queryOrderAndLogCount(List<Long> pkOrderList, Integer pkCountry) {
        Map<Long, Integer> resultMap = new HashMap<>(pkOrderList.size());

        List<SaOrderChargeLogExt> list = baseMapper.queryOrderAndLogCount(pkOrderList, pkCountry);
        for (Long pkOrder : pkOrderList) {
            // 保证map中存在所有值，给一个默认为0的值
            resultMap.put(pkOrder, 0);

            for (SaOrderChargeLogExt saOrderChargeLog : list) {
                if (pkOrder.equals(saOrderChargeLog.getPkOrder())) {
                    // 当查询出来的数据中存在指定订单，则进行赋值
                    resultMap.put(pkOrder, saOrderChargeLog.getLogCount());
                    break;
                }
            }
        }

        return resultMap;
    }

    @Override
    public List<SaOrderChargeLogExt> queryDataByTypeAndCode(Integer chargeType, String businessCode, Integer pkCountry) {
        return baseMapper.queryDataByTypeAndCode(chargeType, businessCode, pkCountry);
    }

    @Override
    public int checkOrderApprovalExist(List<Integer> typeList, List<String> orderCodeList, Integer pkCountry, String... businessCode) {
        if (businessCode != null && businessCode.length > 0) {
            return baseMapper.checkOrderApprovalExistRemoveBusinessCode(typeList, orderCodeList, pkCountry, businessCode[0]);
        } else {
            return baseMapper.checkOrderApprovalExist(typeList, orderCodeList, pkCountry);
        }
    }

    /**
     * 撤单列表
     *
     * @param orderChargeLogParam
     * @return
     */
    @Override
    public List<SaOrderChargeLogExt> cancellationList(SaOrderChargeLogParam orderChargeLogParam) {
        return baseMapper.cancellationList(orderChargeLogParam);
    }

}
