package com.ruoyi.project.inspection.controller;

import java.util.Date;

import com.google.common.collect.Maps;

import java.util.List;

import com.ruoyi.project.inspection.domain.TInspectionRecord;
import com.ruoyi.project.inspection.service.ITInspectionRecordService;
import com.ruoyi.project.intact.domain.TIntactGyl;
import com.ruoyi.project.intact.domain.TIntactYsj;
import com.ruoyi.project.intact.service.ITIntactGylService;
import com.ruoyi.project.intact.service.ITIntactYsjService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.inspection.domain.TInspectionRecordRegion;
import com.ruoyi.project.inspection.service.ITInspectionRecordRegionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 区域巡检记录Controller
 *
 * @author ruoyi
 * @date 2022-06-23
 */
@RestController
@RequestMapping("/inspection/region")
public class TInspectionRecordRegionController extends BaseController {
    @Autowired
    private ITInspectionRecordRegionService tInspectionRecordRegionService;

    @Autowired
    private ITIntactGylService tIntactGylService;

    @Autowired
    private ITIntactYsjService tIntactYsjService;

    @Autowired
    private ITInspectionRecordService recordService;

    /**
     * 查询区域巡检记录列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(TInspectionRecordRegion tInspectionRecordRegion) {
        startPage();
        List<TInspectionRecordRegion> list = tInspectionRecordRegionService.selectTInspectionRecordRegionList(tInspectionRecordRegion);
        return getDataTable(list);
    }

    /**
     * 导出区域巡检记录列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:region:export')")
    @Log(title = "区域巡检记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TInspectionRecordRegion tInspectionRecordRegion) {
        List<TInspectionRecordRegion> list = tInspectionRecordRegionService.selectTInspectionRecordRegionList(tInspectionRecordRegion);
        ExcelUtil<TInspectionRecordRegion> util = new ExcelUtil<TInspectionRecordRegion>(TInspectionRecordRegion.class);
        return util.exportExcel(list, "region");
    }

    /**
     * 获取区域巡检记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('inspection:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tInspectionRecordRegionService.selectTInspectionRecordRegionById(id));
    }

    /**
     * 新增区域巡检记录
     */
    @PreAuthorize("@ss.hasPermi('inspection:region:add')")
    @Log(title = "区域巡检记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TInspectionRecordRegion tInspectionRecordRegion) {
        //获取操作人员ID
        Long userId = getUserId();
        tInspectionRecordRegion.setCreaterCode(userId.toString());
        tInspectionRecordRegion.setCreatedate(new Date());
        // 新增区域巡检记录
        int i = tInspectionRecordRegionService.insertTInspectionRecordRegion(tInspectionRecordRegion);

        // 新增设备巡检记录
        TInspectionRecord record = new TInspectionRecord();
        record.setRegion(tInspectionRecordRegion.getRegion());
        record.setReason(tInspectionRecordRegion.getReason());
        record.setInspectionTime(tInspectionRecordRegion.getInspectionTime());
        record.setPastTime(tInspectionRecordRegion.getPastTime());
        record.setPasterCode(tInspectionRecordRegion.getPasterCode());
        record.setTreatment(tInspectionRecordRegion.getTreatment());
        record.setDeptId(tInspectionRecordRegion.getDeptId());
        record.setCreaterCode(userId.toString());
        record.setCreatedate(new Date());
        record.setRemarks(tInspectionRecordRegion.getRemarks());
        record.setDeptName(tInspectionRecordRegion.getDeptName());
        record.setRegionId(tInspectionRecordRegion.getId());

        // 根据区域查询该区域下的所有工业炉设备
        TIntactGyl gyl = new TIntactGyl();
        gyl.setPosition(tInspectionRecordRegion.getRegion());
        List<TIntactGyl> gyls = tIntactGylService.selectTIntactGylList(gyl);
        gyls.forEach(item -> {
            record.setDevname(item.getDevname());
            record.setDevno(item.getDevno());
            // 为该设备添加巡查记录
            recordService.insertTInspectionRecord(record);
        });

        // 根据区域查询该区域下的所有压缩机设备
        TIntactYsj ysj = new TIntactYsj();
        ysj.setPosition(tInspectionRecordRegion.getRegion());
        List<TIntactYsj> ysjs = tIntactYsjService.selectTIntactYsjList(ysj);
        ysjs.forEach(item -> {
            record.setDevname(item.getDevname());
            record.setDevno(item.getDevno());
            // 为该设备添加巡查记录
            recordService.insertTInspectionRecord(record);
        });

        return toAjax(i);
    }

    /**
     * 修改区域巡检记录
     */
    @PreAuthorize("@ss.hasPermi('inspection:region:edit')")
    @Log(title = "区域巡检记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TInspectionRecordRegion tInspectionRecordRegion) {
        //获取操作人员ID
        Long userId = getUserId();
        // 根据区域id查询该区域下的所有已巡查的设备
        List<TInspectionRecord> records = recordService.selectByRegionId(tInspectionRecordRegion.getId());
        for (TInspectionRecord record : records) {
            record.setUpdatedate(new Date());
            record.setUpdaterCode(userId);
            record.setReason(tInspectionRecordRegion.getReason());
            record.setInspectionTime(tInspectionRecordRegion.getInspectionTime());
            record.setPastTime(tInspectionRecordRegion.getPastTime());
            record.setPasterCode(tInspectionRecordRegion.getPasterCode());
            record.setTreatment(tInspectionRecordRegion.getTreatment());
            record.setDeptId(tInspectionRecordRegion.getDeptId());
            record.setRemarks(tInspectionRecordRegion.getRemarks());
            // 修改该区域下的设备巡查信息
            recordService.updateTInspectionRecord(record);
        }
        return toAjax(tInspectionRecordRegionService.updateTInspectionRecordRegion(tInspectionRecordRegion));
    }

    /**
     * 删除区域巡检记录
     */
    @PreAuthorize("@ss.hasPermi('inspection:region:remove')")
    @Log(title = "区域巡检记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        // 删除该区域下所有设备的巡查记录
        recordService.deleteTInspectionRecordByRegionIds(ids);
        // 删除该条记录
        return toAjax(tInspectionRecordRegionService.deleteTInspectionRecordRegionByIds(ids));
    }
}
