package com.sckw.slope.detection.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.constant.NumberConstant;

import com.sckw.core.model.page.PageRes;

import com.sckw.core.utils.IdWorker;
import com.sckw.core.web.response.HttpResult;
import com.sckw.slope.detection.dao.mysql.*;
import com.sckw.slope.detection.model.dos.mysql.*;
import com.sckw.slope.detection.model.dto.HeaderData;
import com.sckw.slope.detection.model.param.*;
import com.sckw.slope.detection.model.vo.*;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author sky
 * @description 告警信息service
 * @date 2023-11-10 15:11:29
 */
@Slf4j
@Service
public class KwsAlarmInfoService {

    @Autowired
    CommonService commonService;

    @Autowired
    KwsAlarmInfoMapper kwsAlarmInfoMapper;


    public PageRes select(AlarmInfoQuery query, HttpServletRequest request) {
        PageHelper.startPage(query.getPage(), query.getPageSize());
        HeaderData headerData = commonService.getHeaderData(request);
        query.setMountainId(headerData.getMountainId());
        List<DeviceVo> list = kwsAlarmInfoMapper.selectByDeviceParam(query);
        PageInfo<DeviceVo> pageInfo = new PageInfo<>(list);
        if (CollectionUtils.isEmpty(list)) {
            return PageRes.build(pageInfo, list);
        }
        return PageRes.build(pageInfo, list);
    }
    @Transactional
    public HttpResult update(AlarmInfoAdd infoAdd, HttpServletRequest request) {
        HeaderData headerData = commonService.getHeaderData(request);
        LocalDateTime now = LocalDateTime.now();
        kwsAlarmInfoMapper.update(null, new LambdaUpdateWrapper<KwsAlarmInfo>()
                .eq(KwsAlarmInfo::getMountainId, headerData.getMountainId())
                .eq(KwsAlarmInfo::getCompanyId, headerData.getMountainId())
                .set(KwsAlarmInfo::getUpdateTime, now)
                .set(KwsAlarmInfo::getDelFlag, NumberConstant.ONE));
        JSONArray array = JSON.parseArray(infoAdd.getDesc());
        KwsAlarmInfo alarmInfo = new KwsAlarmInfo();
        for (Object object : array) {
            JSONObject obj = (JSONObject) object;
            KwsAlarmInfo deviceHas = kwsAlarmInfoMapper.selectOne(new LambdaQueryWrapper<KwsAlarmInfo>()
                    .eq(KwsAlarmInfo::getLevel, obj.get("level"))
                    .eq(KwsAlarmInfo::getType, obj.get("type"))
                    .eq(KwsAlarmInfo::getValueDesc, obj.get("value_desc"))
                    .eq(KwsAlarmInfo::getMountainId, headerData.getMountainId())
                    .eq(KwsAlarmInfo::getCompanyId, headerData.getMountainId())
                    .eq(KwsAlarmInfo::getDelFlag, NumberConstant.ZERO)
            );
            if(Objects.isNull(deviceHas)){
                long interId = new IdWorker(NumberConstant.ONE).nextId();
                alarmInfo.setId(interId);
                alarmInfo.setMountainId(headerData.getMountainId());
                alarmInfo.setCompanyId(headerData.getMountainId());
                alarmInfo.setCreateTime(now);
                alarmInfo.setLevel(Integer.parseInt(obj.get("level").toString()));
                alarmInfo.setValueDesc(obj.get("value_desc").toString());
                alarmInfo.setAlias(obj.get("alias").toString());
                alarmInfo.setType(Integer.parseInt(obj.get("type").toString()));
                alarmInfo.setCreateTime(now);
                alarmInfo.setUpdateTime(now);
                kwsAlarmInfoMapper.insert(alarmInfo);
            }

        }
        return HttpResult.ok();


    }

}
