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.XhpcHistoryOrderSortingStatus;
import com.xhpc.order.mapper.XhpcHistoryOrderMapper;
import com.xhpc.order.mapper.XhpcHistoryOrderSortingStatusMapper;
import com.xhpc.order.service.IXhpcHistoryOrderSortingStatusService;
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 XhpcHistoryOrderSortingStatusServiceImpl extends BaseService implements IXhpcHistoryOrderSortingStatusService {

    @Autowired
    private XhpcHistoryOrderSortingStatusMapper xhpcHistoryOrderSortingStatusMapper;

    @Autowired
    private XhpcHistoryOrderMapper xhpcHistoryOrderMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 新增  历史订单清分状态
     *
     * @param xhpcHistoryOrderSortingStatus 历史订单清分状态
     */
    @Override
    @Transactional
    public int insert(XhpcHistoryOrderSortingStatus xhpcHistoryOrderSortingStatus) {
        return xhpcHistoryOrderSortingStatusMapper.insert(xhpcHistoryOrderSortingStatus);
    }

    /**
     * 修改清分状态
     *
     * @param historyOrderIds
     * @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.SORTING_STATUS_CONFIRMED == status) {
                if (StatusConstants.SORTING_STATUS_EXAMINE != xhpcHistoryOrder.getSortingStatus()) {
                    return AjaxResult.error(HttpStatus.ERROR_STATUS, "状态异常");
                }
            } else if (StatusConstants.SORTING_STATUS_WITHDRAWAL == status) {
                if (StatusConstants.SORTING_STATUS_CONFIRMED != xhpcHistoryOrder.getSortingStatus()) {
                    return AjaxResult.error(HttpStatus.ERROR_STATUS, "状态异常");
                }
            } else if (StatusConstants.SORTING_STATUS_SUBMITTRD == status) {
                if (StatusConstants.SORTING_STATUS_CONFIRMED != xhpcHistoryOrder.getSortingStatus() && StatusConstants.SORTING_STATUS_EXAMINE != xhpcHistoryOrder.getSortingStatus()) {
                    return AjaxResult.error(HttpStatus.ERROR_STATUS, "状态异常");
                }
            } else if (StatusConstants.SORTING_STATUS_EXAMINE == status) {
                if (StatusConstants.SORTING_STATUS_SUBMITTRD != xhpcHistoryOrder.getSortingStatus()) {
                    return AjaxResult.error(HttpStatus.ERROR_STATUS, "状态异常");
                }
            }
            XhpcHistoryOrderSortingStatus xhpcHistoryOrderSortingStatus = new XhpcHistoryOrderSortingStatus();
            xhpcHistoryOrderSortingStatus.setHistoryOrderId(Long.parseLong(historyOrderId));
            xhpcHistoryOrderSortingStatus.setStatus(status);
            xhpcHistoryOrderSortingStatus.setRemark(remark);
            xhpcHistoryOrderSortingStatusMapper.insert(xhpcHistoryOrderSortingStatus);
        }
        xhpcHistoryOrderMapper.updateSorting(historyOrderIds.split(","), StringUtils.valueOf(status));
        return AjaxResult.success();
    }

    /**
     * 运营商用户信息
     *
     * @param userId
     * @return 结果
     */
    @Override
    public AjaxResult operatorUser(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(xhpcHistoryOrderSortingStatusMapper.operatorUser(userId, operatorId, null));
                } else if (StatusConstants.SON_POWER_TYPE == Integer.parseInt(privilege)) {
                    return AjaxResult.success(xhpcHistoryOrderSortingStatusMapper.operatorUser(userId, null, "privilege"));
                }
            }
        }
        return AjaxResult.success(xhpcHistoryOrderSortingStatusMapper.operatorUser(userId, null, null));
    }


    /**
     * 运营商清分分页列表
     *
     * @param userId              用户id
     * @param serialNumber        订单编号
     * @param createTimeStart     创建开始时间
     * @param createTimeEnd       创建结束时间
     * @param chargingStationName 电站名称
     * @param status              状态
     * @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 xhpcHistoryOrderSortingStatusMapper.page(userId, serialNumber, createTimeStart, createTimeEnd, chargingStationName, status, operatorId, null);
                } else if (StatusConstants.SON_POWER_TYPE == Integer.parseInt(privilege)) {
                    return xhpcHistoryOrderSortingStatusMapper.page(userId, serialNumber, createTimeStart, createTimeEnd, chargingStationName, status, null, "privilege");
                }
            }
        }
        startPage();
        return xhpcHistoryOrderSortingStatusMapper.page(userId, serialNumber, createTimeStart, createTimeEnd, chargingStationName, status, null, null);
    }

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