package com.xhpc.charging.station.service;

import com.xhpc.charging.station.mapper.XhpcPileEditionMapper;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.domain.XhpcPileEdition;
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.stereotype.Service;

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

/**
 * @author yuyang
 * @date 2022/8/30 14:10
 */
@Service
public class XhpcPileEditionServiceImpl  extends BaseService implements IXhpcPileEditionService{

    @Resource
    private XhpcPileEditionMapper xhpcPileEditionMapper;
    @Resource
    private TokenService tokenService;

    /**
     * 查询版本列表
     *
     * @param request
     * @param chargingStationId
     * @param chargingPileIds
     * @return
     */
    @Override
    public List<Map<String, Object>> list(HttpServletRequest request, Long chargingStationId, String chargingPileIds) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        String tenantId =loginUser.getTenantId();
        //判断登录人的权限
        List listPileIds = new ArrayList();
        if(chargingPileIds !=null && !"".equals(chargingPileIds)){
            String[] split = chargingPileIds.split(",");
            listPileIds = Arrays.asList(split);
        }
        List<Map<String, Object>> list =new ArrayList<>();
        if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
            //运维人员权限
            list = xhpcPileEditionMapper.list(chargingStationId,listPileIds,sysUserId,3,tenantId);
        }else{
            if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
               // startPage();
                if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                    Long logOperatorId = sysUser.getOperatorId();
                    //运营商看自己的场站
                    list = xhpcPileEditionMapper.list(chargingStationId,listPileIds,logOperatorId,1,tenantId);
                }else{
                    //查询赋值的场站
                    list = xhpcPileEditionMapper.list(chargingStationId,listPileIds,sysUserId,2,tenantId);
                }
            }else{
               // startPage();
                list = xhpcPileEditionMapper.list(chargingStationId,listPileIds,sysUserId,0,tenantId);
            }
        }
        return list;
    }

    @Override
    public AjaxResult addPileEdition(HttpServletRequest request, XhpcPileEdition xhpcPileEdition) {

        //判断是否已添加该桩
        String[] split = xhpcPileEdition.getChargingPileIds().split(",");
        if(split ==null || split.length==0){
            return AjaxResult.error(500, "请选择桩");
        }
        List list = Arrays.asList(split);
        int xhpcPileEditions = xhpcPileEditionMapper.getXhpcPileEditions(xhpcPileEdition.getPileEditionId(), list,1);

        if(xhpcPileEditions >0){
            return AjaxResult.error(500, "有重复桩");
        }
        if(xhpcPileEdition.getPileEditionId() ==null){
            LoginUser loginUser = tokenService.getLoginUser(request);
            String tenantId =loginUser.getTenantId();
            xhpcPileEdition.setTenantId(tenantId);
            xhpcPileEditionMapper.insertXhpcPileEdition(xhpcPileEdition);
        }else{
            xhpcPileEditionMapper.updatePileEdition(xhpcPileEdition);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult status(Long pileEditionId,Integer status) {

        XhpcPileEdition xhpcPileEdition = xhpcPileEditionMapper.getXhpcPileEditionById(pileEditionId);
        if(xhpcPileEdition==null){
            return AjaxResult.error(500, "数据错误,请刷新页面在操作");
        }
        String[] split = xhpcPileEdition.getChargingPileIds().split(",");
        if(split ==null || split.length==0){
            return AjaxResult.error(500, "请选择桩");
        }
        List list = Arrays.asList(split);
        xhpcPileEdition.setPileEditionId(pileEditionId);
        xhpcPileEdition.setStatus(status);
        if(status==0){
            int xhpcPileEditions = xhpcPileEditionMapper.getXhpcPileEditions(xhpcPileEdition.getPileEditionId(),list,1);
            if(xhpcPileEditions >0){
                return AjaxResult.error(500, "有重复桩");
            }
        }
        xhpcPileEditionMapper.updatePileEdition(xhpcPileEdition);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getChargingPileList(Long chargingStationId) {
        return AjaxResult.success(xhpcPileEditionMapper.getChargingPileList(chargingStationId));
    }


}
