package com.xhpc.charging.station.service;

import com.xhpc.charging.station.mapper.XhpcRequirementMapper;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.domain.Requirement;
import com.xhpc.common.domain.RequirementTask;
import com.xhpc.common.domain.RequirementTime;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yuyang
 * @Date 2025-07-11 16:00
 */
@Service
public class XhpcRequirementServiceImpl implements IXhpcRequirementService{

    @Resource
    XhpcRequirementMapper xhpcRequirementMapper;
    @Resource
    TokenService tokenService;




    @Override
    public List<Map<String, Object>> getLsit(Map<String, Object> params) {
        return xhpcRequirementMapper.getLsit(params);
    }

    @Override
    public Map<String, Object> getRequirementById(Long requirementId) {
        Map<String, Object> map = xhpcRequirementMapper.getRequirementById(requirementId);
        List<Map<String, Object>> list = xhpcRequirementMapper.getRequirementTimeList(requirementId, null);
        if(list !=null && list.size()>0){
            map.put("timeList",list);
        }else{
            map.put("timeList",new ArrayList<>());
        }
        return map;
    }

    @Override
    public Map<String, Object> getRequirementTaskById(Long requirementTaskId) {
        Map<String, Object> map = xhpcRequirementMapper.getRequirementTaskById(requirementTaskId);
        List<Map<String, Object>> list = xhpcRequirementMapper.getRequirementTimeList(null, requirementTaskId);
        if(list !=null && list.size()>0){
            map.put("timeList",list);
        }else{
            map.put("timeList",new ArrayList<>());
        }
        return map;
    }

    @Override
    public R updateTaskById(Long requirementId,Integer status) {

        int i = xhpcRequirementMapper.updateTaskById(requirementId, status);
        if(i>0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public List<Map<String, Object>> getTaskList(Map<String, Object> params) {
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        if (!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())) {

            if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())) {
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                params.put("type", 1);
                params.put("logOperatorId", logOperatorId);
                return xhpcRequirementMapper.getTaskList(params);
            } else {
                //查询赋值的场站
                params.put("type", 2);
                params.put("logOperatorId", sysUserId);
                return xhpcRequirementMapper.getTaskList(params);
            }
        } else {
            params.put("type", 0);
            params.put("logOperatorId", sysUserId);
            return xhpcRequirementMapper.getTaskList(params);
        }
    }

    @Override
    public int addRequirement(Requirement requirement) {
        return xhpcRequirementMapper.addRequirement(requirement);
    }

    @Override
    public int addRequirementTime(RequirementTime requirementTime) {
        return xhpcRequirementMapper.addRequirementTime(requirementTime);
    }

    @Override
    public int addRequirementTask(RequirementTask requirementTask) {
        return xhpcRequirementMapper.addRequirementTask(requirementTask);
    }


}
