package com.ruoyi.project.asset.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.asset.domain.TLeakagePoints;
import com.ruoyi.project.asset.domain.TLeakagePointsPatrol;
import com.ruoyi.project.asset.domain.TLeakagePointsRecord;
import com.ruoyi.project.asset.service.ITLeakagePointsPatrolService;
import com.ruoyi.project.asset.service.ITLeakagePointsRecordService;
import com.ruoyi.project.asset.service.ITLeakagePointsService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 漏点巡检Controller
 *
 * @author ruoyi
 * @date 2024-04-01
 */
@RestController
@RequestMapping("/asset/pointPatrol")
public class TLeakagePointsPatrolController extends BaseController {
    @Autowired
    private ITLeakagePointsPatrolService tLeakagePointsPatrolService;

    @Autowired
    private ITLeakagePointsRecordService tLeakagePointsRecordService;

    @Autowired
    private ITLeakagePointsService tLeakagePointsService;

    /**
     * 查询漏点巡检列表
     */
    @PreAuthorize("@ss.hasPermi('asset:pointPatrol:list')")
    @GetMapping("/list")
    public TableDataInfo list(TLeakagePointsPatrol tLeakagePointsPatrol) {
        startPage();
        if (tLeakagePointsPatrol.getCheckDateM() == null) {
            tLeakagePointsPatrol.setCheckDateM(new Date());
        }
        List<TLeakagePointsPatrol> list = tLeakagePointsPatrolService.selectTLeakagePointsPatrolList(tLeakagePointsPatrol);
        if (CollectionUtils.isNotEmpty(list)) {
            TLeakagePointsRecord record = new TLeakagePointsRecord();
            record.setPatrolId(list.get(0).getId());
            list.get(0).setRecords(tLeakagePointsRecordService.selectTLeakagePointsRecordList(record));
        }
        return getDataTable(list);
    }

    /**
     * 导出漏点巡检列表
     */
    @PreAuthorize("@ss.hasPermi('asset:pointPatrol:export')")
    @Log(title = "漏点巡检", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TLeakagePointsPatrol tLeakagePointsPatrol) {
        List<TLeakagePointsPatrol> list = tLeakagePointsPatrolService.selectTLeakagePointsPatrolList(tLeakagePointsPatrol);
        ExcelUtil<TLeakagePointsPatrol> util = new ExcelUtil<TLeakagePointsPatrol>(TLeakagePointsPatrol.class);
        util.exportExcel(response, list, "漏点巡检数据");
    }

    /**
     * 获取漏点巡检详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:pointPatrol:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tLeakagePointsPatrolService.selectTLeakagePointsPatrolById(id));
    }

    /**
     * 新增漏点巡检
     */
    @PreAuthorize("@ss.hasPermi('asset:pointPatrol:add')")
    @Log(title = "漏点巡检", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TLeakagePointsPatrol tLeakagePointsPatrol) {
        tLeakagePointsPatrol.setCheckDateN(tLeakagePointsPatrol.getCheckDateM());
        TLeakagePointsPatrol patrol = new TLeakagePointsPatrol();
        patrol.setCheckDateM(tLeakagePointsPatrol.getCheckDateM());
        List<TLeakagePointsPatrol> list = tLeakagePointsPatrolService.selectTLeakagePointsPatrolList(patrol);
        if (CollectionUtils.isNotEmpty(list)) {
            return AjaxResult.warn("当前日期已存在巡检记录！");
        }
        tLeakagePointsPatrolService.insertTLeakagePointsPatrol(tLeakagePointsPatrol);
        TLeakagePoints tLeakagePoints = new TLeakagePoints();
        tLeakagePoints.setLeakageEliminationResult("1,2");
        for (TLeakagePoints leakagePoints : tLeakagePointsService.selectTLeakagePointsList(tLeakagePoints)) {
            TLeakagePointsRecord record = new TLeakagePointsRecord();
            record.setPatrolId(tLeakagePointsPatrol.getId());
            record.setPointNo(leakagePoints.getPointNo());
            record.setLeakagePosition(leakagePoints.getLeakagePosition());
            record.setLeakageMedium(leakagePoints.getLeakageMedium());
            record.setCreatedate(new Date());
            record.setCreaterCode(getUserId().toString());
            tLeakagePointsRecordService.insertTLeakagePointsRecord(record);
        }
        return toAjax(1);
    }

    /**
     * 修改漏点巡检
     */
    @PreAuthorize("@ss.hasPermi('asset:pointPatrol:edit')")
    @Log(title = "漏点巡检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TLeakagePointsPatrol tLeakagePointsPatrol) {
        if (CollectionUtils.isNotEmpty(tLeakagePointsPatrol.getRecords())) {
            tLeakagePointsPatrol.getRecords().forEach(item -> {
                item.setUpdatedate(new Date());
                item.setUpdaterCode(getUserId().toString());
                tLeakagePointsRecordService.updateTLeakagePointsRecord(item);
            });
        }
        return toAjax(tLeakagePointsPatrolService.updateTLeakagePointsPatrol(tLeakagePointsPatrol));
    }

    /**
     * 删除漏点巡检
     */
    @PreAuthorize("@ss.hasPermi('asset:pointPatrol:remove')")
    @Log(title = "漏点巡检", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tLeakagePointsPatrolService.deleteTLeakagePointsPatrolByIds(ids));
    }
}
