package com.yfd.platform.modules.patroltask.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.modules.basedata.domain.SubstationBay;
import com.yfd.platform.modules.basedata.domain.SubstationComponent;
import com.yfd.platform.modules.basedata.domain.SubstationDevice;
import com.yfd.platform.modules.basedata.domain.SubstationMaindevice;
import com.yfd.platform.modules.basedata.mapper.*;
import com.yfd.platform.modules.patroltask.domain.ExaminePlan;
import com.yfd.platform.modules.patroltask.mapper.ExaminePlanMapper;
import com.yfd.platform.modules.patroltask.service.IExaminePlanService;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 检修计划表 服务实现类
 * </p>
 *
 *
 * @since 2023-04-08
 */
@Service
public class ExaminePlanServiceImpl extends ServiceImpl<ExaminePlanMapper, ExaminePlan> implements IExaminePlanService {

    @Resource
    private SubstationMapper substationMapper;

    @Resource
    private SubstationBayMapper substationBayMapper;

    @Resource
    private SubstationMaindeviceMapper substationMaindeviceMapper;

    @Resource
    private SubstationComponentMapper substationComponentMapper;

    @Resource
    private SubstationDeviceMapper substationDeviceMapper;
    @Resource
    private ExaminePlanMapper examinePlanMapper;

    /**********************************
     * 用途说明: 根据Id查询关联信息
     * 参数说明 id
     * 参数说明 deviceLevel
     * 返回值说明: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     ***********************************/
    @Override
    public List<Map<String, Object>> getBindInfoById(String id, String deviceLevel) {
        ExaminePlan examinePlan = this.getById(id);
        String deviceList = examinePlan.getDeviceList();
//        String[] split = deviceList.split(",");
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (StrUtil.isBlank(deviceList)) {
            return mapList;
        }
        switch (deviceLevel) {
            case "1":

                //                for (String bayId : split) {
                //                    SubstationBay substationBay = substationBayMapper.selectById(bayId);
                //                    Map<String, Object> map = BeanUtil.beanToMap(substationBay);
                //                    mapList.add(map);
                //                }
                mapList.addAll(substationBayMapper.selectMaps(new LambdaQueryWrapper<SubstationBay>().in(SubstationBay::getBayId, StrUtil.split(deviceList, ","))));
                break;
            case "2":
                mapList.addAll(substationMaindeviceMapper.selectMaps(new LambdaQueryWrapper<SubstationMaindevice>().in(SubstationMaindevice::getMainDeviceId, StrUtil.split(deviceList, ","))));

                //                for (String mainId : split) {
                //                    SubstationMaindevice substationMaindevice = substationMaindeviceMapper
                //                    .selectById(mainId);
                //                    Map<String, Object> map = BeanUtil.beanToMap(substationMaindevice);
                //                    mapList.add(map);
                //                }
                break;
            case "3":
//                for (String deviceId : split) {
//                    SubstationDevice substationDevice = substationDeviceMapper.selectById(deviceId);
//                    Map<String, Object> map = BeanUtil.beanToMap(substationDevice);
//                    mapList.add(map);
//                }
                mapList.addAll(substationDeviceMapper.selectMaps(new LambdaQueryWrapper<SubstationDevice>().in(SubstationDevice::getDeviceId, StrUtil.split(deviceList, ","))));
                break;
            case "4":
//                for (String componentId : split) {
//                    SubstationComponent substationComponent = substationComponentMapper.selectById(componentId);
//                    Map<String, Object> map = BeanUtil.beanToMap(substationComponent);
//                    mapList.add(map);
//                }
                mapList.addAll(substationComponentMapper.selectMaps(new LambdaQueryWrapper<SubstationComponent>().in(SubstationComponent::getComponentId, StrUtil.split(deviceList, ","))));
                break;
        }
        return mapList;
    }

    /**********************************
     * 用途说明: 根据Id查询检修计划绑定点位
     * 参数说明 id 检修计划id
     * 返回值说明: java.util.List<com.yfd.platform.modules.basedata.domain.SubstationDevice>
     ***********************************/
    @Override
    public List<SubstationDevice> getBindExamineDevice(String id) {
        ExaminePlan examinePlan = this.getById(id);
        String deviceStr = examinePlan.getDeviceList();
        if (StrUtil.isBlank(deviceStr)) {
            return null;
        }
        List<String> ids = Arrays.asList(deviceStr.split(","));
        LambdaQueryWrapper<SubstationDevice> queryWrapper = new LambdaQueryWrapper<>();
        if (1 == examinePlan.getDeviceLevel()) {
            queryWrapper.in(SubstationDevice::getBayId, ids);
        }
        if (2==examinePlan.getDeviceLevel()) {
            queryWrapper.in(SubstationDevice::getMainDeviceId, ids);
        }
        if (3==examinePlan.getDeviceLevel()) {
            queryWrapper.in(SubstationDevice::getDeviceId, ids);
        }
        if (4==examinePlan.getDeviceLevel()) {
            queryWrapper.in(SubstationDevice::getComponentId, ids);
        }
        List<SubstationDevice> deviceList = substationDeviceMapper.selectList(queryWrapper);
        return deviceList;
    }

    /**********************************
     * 用途说明: 分页查询检修计划
     * 参数说明 page 分页参数
     * 参数说明 stationId 变电站id
     * 参数说明 configName 检修计划名称
     * 参数说明 bindStatus 绑定状态
     * 返回值说明: com.baomidou.mybatisplus.extension.plugins.pagination.Page<java.util.Map<java.lang.String,java.lang.Object>>
     ***********************************/
    @Override
    public Page<Map<String, Object>> getExaminePlanPage(Page<Map<String, Object>> page, String stationId,
                                                        String configName, String bindStatus) {
        return examinePlanMapper.getExaminePlanPage(page, stationId, configName, bindStatus);
    }


}
