package com.inspect.alarminfo.controller;

import com.alibaba.fastjson.JSONArray;
import com.inspect.alarmdefect.domain.AlarmDefect;
import com.inspect.alarmdefect.service.IAlarmDefectService;
import com.inspect.alarmdifferent.domain.AlarmDifferent;
import com.inspect.alarmdifferent.service.IAlarmDifferentService;
import com.inspect.alarminfo.domain.AlarmInfo;
import com.inspect.alarminfo.service.IAlarmInfoService;
import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/alarminfo"})
public class AlarmInfoController extends BaseController {
    private final IAlarmInfoService alarmInfoService;
    private final IAlarmDefectService alarmDefectService;
    private final IAlarmDifferentService alarmDifferentService;

    public AlarmInfoController(IAlarmInfoService alarmInfoService, IAlarmDefectService alarmDefectService, IAlarmDifferentService alarmDifferentService) {
        this.alarmInfoService = alarmInfoService;
        this.alarmDefectService = alarmDefectService;
        this.alarmDifferentService = alarmDifferentService;
    }

    @GetMapping({"/list"})
    public TableDataInfo list(AlarmInfo alarmInfo) {
        this.startPage();
        List<AlarmInfo> list = this.alarmInfoService.selectAlarmInfoList(alarmInfo);

        for (AlarmInfo arr : list) {
            AlarmDifferent alarmDifferent = new AlarmDifferent();
            alarmDifferent.setAlarmId(String.valueOf(arr.getLineId()));
            List<AlarmDifferent> differentList = this.alarmDifferentService.selectAlarmDifferentList(alarmDifferent);
            if (!differentList.isEmpty()) {
                alarmInfo.setDifferent(differentList);
            }

            AlarmDefect alarmDefect = new AlarmDefect();
            alarmDefect.setAlarmId(String.valueOf(arr.getLineId()));
            List<AlarmDefect> alarmDefectList = this.alarmDefectService.selectAlarmDefectList(alarmDefect);
            if (!alarmDefectList.isEmpty()) {
                alarmInfo.setDefect(alarmDefectList);
            }
        }

        return this.getDataTable(list);
    }

    @Log(
            title = "告警内容",
            businessType = BizType.EXPORT
    )
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, AlarmInfo alarmInfo) {
        List<AlarmInfo> list = this.alarmInfoService.selectAlarmInfoList(alarmInfo);
        ExcelUtil<AlarmInfo> util = new ExcelUtil<>(AlarmInfo.class);
        util.exportExcel(response, list, "告警内容数据");
    }

    @GetMapping({"/{lineId}"})
    public AjaxResult getInfo(@PathVariable("lineId") Long lineId) {
        AlarmInfo alarmInfo = this.alarmInfoService.selectAlarmInfoByLineId(lineId);
        AlarmDifferent alarmDifferent = new AlarmDifferent();
        alarmDifferent.setAlarmId(lineId + "");
        List<AlarmDifferent> differentList = this.alarmDifferentService.selectAlarmDifferentList(alarmDifferent);
        if (!differentList.isEmpty()) {
            alarmInfo.setDifferent(differentList);
        }

        AlarmDefect alarmDefect = new AlarmDefect();
        alarmDefect.setAlarmId(String.valueOf(lineId));
        List<AlarmDefect> alarmDefectList = this.alarmDefectService.selectAlarmDefectList(alarmDefect);
        if (!alarmDefectList.isEmpty()) {
            alarmInfo.setDefect(alarmDefectList);
        }

        return AjaxResult.success(alarmInfo);
    }

    @Log(
            title = "告警内容",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@RequestBody AlarmInfo alarmInfo) {
        return this.toAjax(this.alarmInfoService.insertAlarmInfo(alarmInfo));
    }

    @Log(
            title = "告警内容",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody AlarmInfo alarmInfo) {
        return this.toAjax(this.alarmInfoService.updateAlarmInfo(alarmInfo));
    }

    @Log(
            title = "告警内容",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{lineIds}"})
    public AjaxResult remove(@PathVariable Long[] lineIds) {
        return this.toAjax(this.alarmInfoService.deleteAlarmInfoByLineIds(lineIds));
    }

    public void analysis(String message) {
        List<AlarmInfo> eqList = JSONArray.parseArray(message, AlarmInfo.class);
        if (!eqList.isEmpty()) {

            for (AlarmInfo arr : eqList) {
                int s = this.alarmInfoService.insertAlarmInfo(arr);
                List<AlarmDifferent> different = arr.getDifferent();

                for (AlarmDifferent crr : different) {
                    crr.setAlarmId(String.valueOf(s));
                    this.alarmDifferentService.insertAlarmDifferent(crr);
                }

                List<AlarmDefect> defect = arr.getDefect();

                for (AlarmDefect drr : defect) {
                    drr.setAlarmId(String.valueOf(s));
                    this.alarmDefectService.insertAlarmDefect(drr);
                }
            }
        }

    }

    public void padTeaAnalysis(String message) {
        List<AlarmInfo> eqList = JSONArray.parseArray(message, AlarmInfo.class);
        if (!eqList.isEmpty()) {

            for (AlarmInfo arr : eqList) {
                int s = this.alarmInfoService.updateAlarmInfo(arr);
                List<AlarmDifferent> different = arr.getDifferent();

                for (AlarmDifferent crr : different) {
                    this.alarmDifferentService.updateAlarmDifferent(crr);
                }

                List<AlarmDefect> defect = arr.getDefect();

                for (AlarmDefect drr : defect) {
                    drr.setAlarmId(String.valueOf(s));
                    this.alarmDefectService.updateAlarmDefect(drr);
                }
            }
        }

    }
}
