package com.xhpc.order.service.impl;

import com.xhpc.common.core.constant.HttpStatus;
import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.order.domain.XhpcHistoryOrder;
import com.xhpc.order.domain.XhpcHistoryOrderReconciliationStatus;
import com.xhpc.order.mapper.XhpcHistoryOrderMapper;
import com.xhpc.order.mapper.XhpcHistoryOrderReconciliationStatusMapper;
import com.xhpc.order.service.IXhpcHistoryOrderReconciliationStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 历史订单对账状态信息 服务层
 *
 * @author ruoyi
 */
@Service
public class XhpcHistoryOrderReconciliationStatusServiceImpl extends BaseService implements IXhpcHistoryOrderReconciliationStatusService {

    @Autowired
    private XhpcHistoryOrderReconciliationStatusMapper xhpcHistoryOrderReconciliationStatusMapper;

    @Autowired
    private XhpcHistoryOrderMapper xhpcHistoryOrderMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 新增  历史订单对账状态
     *
     * @param xhpcHistoryOrderReconciliationStatus 历史订单对账状态
     */
    @Override
    @Transactional
    public int insert(XhpcHistoryOrderReconciliationStatus xhpcHistoryOrderReconciliationStatus) {
        return xhpcHistoryOrderReconciliationStatusMapper.insert(xhpcHistoryOrderReconciliationStatus);
    }

    /**
     * 修改对账状态 历史订单对账状态
     *
     * @param historyOrderIds 历史订单id集合
     * @param status          状态
     * @param remark          备注
     * @return
     */
    @Override
    @Transactional
    public AjaxResult status(String historyOrderIds, Integer status, String remark) {
        for (String historyOrderId : historyOrderIds.split(",")) {
            XhpcHistoryOrder xhpcHistoryOrder = xhpcHistoryOrderMapper.info(Long.parseLong(historyOrderId));
            if (StatusConstants.RECONCILIATION_STATUS_WAIT == status) {
                if (StatusConstants.RECONCILIATION_STATUS_EXAMINE != xhpcHistoryOrder.getReconciliationStatus()) {
                    return AjaxResult.error(HttpStatus.ERROR_STATUS, "状态异常");
                }
            } else if (StatusConstants.RECONCILIATION_STATUS_CONFIRMED == status) {
                if (StatusConstants.RECONCILIATION_STATUS_WAIT != xhpcHistoryOrder.getReconciliationStatus()) {
                    return AjaxResult.error(HttpStatus.ERROR_STATUS, "状态异常");
                }
            } else if (StatusConstants.RECONCILIATION_STATUS_SUBMITTRD == status) {
                if (StatusConstants.RECONCILIATION_STATUS_WAIT != xhpcHistoryOrder.getReconciliationStatus() && StatusConstants.RECONCILIATION_STATUS_EXAMINE != xhpcHistoryOrder.getReconciliationStatus()) {
                    return AjaxResult.error(HttpStatus.ERROR_STATUS, "状态异常");
                }
            } else if (StatusConstants.RECONCILIATION_STATUS_EXAMINE == status) {
                if (StatusConstants.RECONCILIATION_STATUS_SUBMITTRD != xhpcHistoryOrder.getReconciliationStatus()) {
                    return AjaxResult.error(HttpStatus.ERROR_STATUS, "状态异常");
                }
            }
            XhpcHistoryOrderReconciliationStatus xhpcHistoryOrderReconciliationStatus = new XhpcHistoryOrderReconciliationStatus();
            xhpcHistoryOrderReconciliationStatus.setHistoryOrderId(Long.parseLong(historyOrderId));
            xhpcHistoryOrderReconciliationStatus.setStatus(status);
            xhpcHistoryOrderReconciliationStatus.setRemark(remark);
            xhpcHistoryOrderReconciliationStatusMapper.insert(xhpcHistoryOrderReconciliationStatus);
        }
        xhpcHistoryOrderMapper.updateReconciliation(historyOrderIds.split(","), StringUtils.valueOf(status));
        return AjaxResult.success();
    }

    /**
     * 流量用户信息
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public AjaxResult internetUser(Long userId) {
        Map<String, Object> map = redisService.getCacheMap("privilege:" + userId);
        if (StringUtils.isNotNull(map)) {
            String privilege = StringUtils.valueOf(map.get("privilege"));
            if (StringUtils.isNotEmpty(privilege)) {
                if (StatusConstants.OPERATION_POWER_TYPE == Integer.parseInt(privilege)) {
                    String operatorId = StringUtils.valueOf(map.get("operatorId"));
                    return AjaxResult.success(xhpcHistoryOrderReconciliationStatusMapper.internetUser(userId, operatorId, null));
                } else if (StatusConstants.SON_POWER_TYPE == Integer.parseInt(privilege)) {
                    return AjaxResult.success(xhpcHistoryOrderReconciliationStatusMapper.internetUser(userId, null, "privilege"));
                }
            }
        }
        return AjaxResult.success(xhpcHistoryOrderReconciliationStatusMapper.internetUser(userId, null, null));
    }


    /**
     * 流量用户对账分页列表
     *
     * @param userId              流量用户id
     * @param serialNumber        订单编号
     * @param createTimeStart     创建开始时间
     * @param createTimeEnd       创建结束时间
     * @param chargingStationName 电站名称
     * @param status              状态
     * @return
     */
    @Override
    public List<Map<String, Object>> page(Long userId, String serialNumber, String createTimeStart, String createTimeEnd, String chargingStationName, String status) {
        Map<String, Object> map = redisService.getCacheMap("privilege:" + userId);
        if (StringUtils.isNotNull(map)) {
            String privilege = StringUtils.valueOf(map.get("privilege"));
            if (StringUtils.isNotEmpty(privilege)) {
                startPage();
                if (StatusConstants.OPERATION_POWER_TYPE == Integer.parseInt(privilege)) {
                    String operatorId = StringUtils.valueOf(map.get("operatorId"));
                    return xhpcHistoryOrderReconciliationStatusMapper.page(userId, serialNumber, createTimeStart, createTimeEnd, chargingStationName, status, operatorId, null);
                } else if (StatusConstants.SON_POWER_TYPE == Integer.parseInt(privilege)) {
                    return xhpcHistoryOrderReconciliationStatusMapper.page(userId, serialNumber, createTimeStart, createTimeEnd, chargingStationName, status, null, "privilege");
                }
            }
        }
        startPage();
        return xhpcHistoryOrderReconciliationStatusMapper.page(userId, serialNumber, createTimeStart, createTimeEnd, chargingStationName, status, null, null);
    }

    /**
     * 清分详情
     *
     * @param historyOrderId 历史订单id
     * @return
     */
    @Override
    public AjaxResult info(Long historyOrderId) {
        return AjaxResult.success(xhpcHistoryOrderReconciliationStatusMapper.info(historyOrderId));
    }

}
