package com.xhpc.activity.service.impl;

import com.xhpc.activity.mapper.XhpcChargingStationMapper;
import com.xhpc.activity.service.StationLogService;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class StationLogServiceImpl extends BaseService implements StationLogService {

    @Resource
    XhpcChargingStationMapper stationMapper;
    @Resource
    private TokenService tokenService;

    @Override
    public List<Map<String, Object>> getStationPage(HttpServletRequest request, Map<String, Object> params){
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //桩的统计、该时段金额
        List<Map<String, Object>> list =new ArrayList<>();
        if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
            //运维管理人员
            startPage();
            params.put("type",2);
            params.put("operatorId",sysUserId);
            list = stationMapper.selectXhpcChargingStationList(params);
        }else{
            if(UserTypeUtil.SYS_SUPER_TYPE_ONE == loginUser.getSuperType()){
                params.put("tenantId",loginUser.getTenantId());
                return  stationMapper.selectXhpcChargingStationList(params);
            }
            if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                Long logOperatorId = sysUser.getOperatorId();
                startPage();
                if ("01".equals(sysUser.getUserType())) {
                    params.put("type",1);
                    params.put("operatorId",logOperatorId);
                    //运营商看自己的场站
                    list = stationMapper.selectXhpcChargingStationList(params);
                }else{
                    startPage();
                    params.put("type",2);
                    params.put("operatorId",sysUserId);
                    list = stationMapper.selectXhpcChargingStationList(params);
                }
            }else{
                startPage();
                params.put("type",0);
                list = stationMapper.selectXhpcChargingStationList(params);
            }
        }
        return list;
    }


    @Override
    public List<Map<String, Object>> getStationRatePage(Long stationId){
        return stationMapper.selectRateListByStationId(stationId);
    }


    @Override
    public Map<String, Object> getRateInfo(int rateId){
        Map<String, Object> resData = new HashMap<>();
        resData.put("base", stationMapper.selectBaseRateTimeListByRateId(rateId));
        resData.put("current", stationMapper.selectRateTimeListByRateId(rateId));

        return resData;
    }
}
