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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.basedata.domain.SubstationDevice;
import com.yfd.platform.modules.basedata.service.ISubstationDeviceService;
import com.yfd.platform.modules.patroltask.domain.DefectDevice;
import com.yfd.platform.modules.patroltask.mapper.DefectDeviceMapper;
import com.yfd.platform.modules.patroltask.service.IDefectDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhengsl
 * @since 2024-03-28
 */
@Service
public class DefectDeviceServiceImpl extends ServiceImpl<DefectDeviceMapper, DefectDevice> implements IDefectDeviceService {

    @Resource
    private ISubstationDeviceService substationDeviceService;
    @Resource
    private DefectDeviceMapper defectDeviceMapper;

    /**********************************
     * 用途说明: 查询缺陷数据
     * 参数说明 page
     * 参数说明 stationId 变电站id
     * 参数说明 areaId 区域id
     * 参数说明 bayId 间隔id
     * 参数说明 taskName 任务名称
     * 参数说明 deviceName 点位名称
     * 参数说明 patroldeviceName 设备名称
     * 参数说明 componentName 部件名称
     * 参数说明 recognitionType 识别类型
     * 参数说明 isDefectFlag 是否归入
     * 参数说明 startDate 开始时间
     * 参数说明 endDate 结束时间
     * 返回值说明: com.yfd.platform.config.ResponseResult
     ***********************************/
    @Override
    public ResponseResult getDefectDeviceList(Page<Map<String, Object>> page, String stationCode, String areaId,
                                              String bayId, String taskName, String deviceName,
                                              String patroldeviceName, String componentName, String recognitionType,
                                              String isDefectFlag, String startDate, String endDate) {
        String startFormat = "";
        String endFormat = "";
        if (StrUtil.isNotBlank(startDate) && StrUtil.isNotBlank(endDate)) {

            if (StrUtil.isNotBlank(startDate)) {
                Date parseStart = DateUtil.parse(startDate);
                //一天的开始
                Date beginOfDay = DateUtil.beginOfDay(parseStart);
                startFormat = DateUtil.format(beginOfDay, "yyyy-MM-dd HH:mm:ss");
            }
            if (StrUtil.isNotBlank(startDate)) {
                Date parseEnd = DateUtil.parse(endDate);
                //一天的结束
                Date endOfDay = DateUtil.endOfDay(parseEnd);
                endFormat = DateUtil.format(endOfDay, "yyyy-MM-dd HH:mm:ss");
            }

        }
        Page<Map<String, Object>> mapPage = defectDeviceMapper.getDefectDeviceList(page, stationCode, areaId, bayId,
                taskName, deviceName, patroldeviceName, componentName, recognitionType, isDefectFlag, startFormat,
                endFormat);
                List<Map<String, Object>> records = mapPage.getRecords();
                records.forEach(m -> {
                    String flag = m.get("flag").toString();
                    String valid = ObjectUtil.isNotEmpty(m.get("valid1")) ? m.get("valid1").toString() : "";
                    if ("4".equals(flag)) {
                        m.put("analysisResult", "识别失败");
                    } else if ("6".equals(flag)) {
                        m.put("analysisResult", "采集失败");
                    } else if ("2".equals(valid)) {
                        m.put("analysisResult", "异常");
                    }
                });
                mapPage.setRecords(records);
        return ResponseResult.successData(mapPage);
    }

    /**********************************
     * 用途说明: 缺陷信息归入
     * 参数说明 ids id集合
     * 返回值说明: boolean
     ***********************************/
    @Override
    public boolean setDefectDevice(String ids) {
        if (StrUtil.isBlank(ids)) {
            return false;
        }
        List<String> idList = StrUtil.split(ids, ",");
        LambdaUpdateWrapper<DefectDevice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(DefectDevice::getDefectId, idList).ne(DefectDevice::getIsDefectFlag, "1").set(DefectDevice::getIsDefectFlag, "1");
        return this.update(updateWrapper);
    }

    /**********************************
     * 用途说明: 根据id获取缺陷信息
     * 参数说明 id id
     * 返回值说明: com.yfd.platform.config.ResponseResult
     ***********************************/
    @Override
    public ResponseResult getDefectDeviceById(String id) {
        DefectDevice defectDevice = this.getById(id);
        return ResponseResult.successData(defectDevice);
    }

    /**********************************
     * 用途说明: 缺陷信息删除
     * 参数说明 ids id集合
     * 返回值说明: boolean
     ***********************************/
    @Override
    public boolean deleteDefectDevice(String ids) {
        if (StrUtil.isBlank(ids)) {
            return false;
        }
        List<String> idList = StrUtil.split(ids, ",");
        return this.removeByIds(idList);
    }
}
