package com.ruoyi.project.sems.his.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.sems.domain.TSpecdevGl;
import com.ruoyi.project.sems.domain.TSpecdevYlgd;
import com.ruoyi.project.sems.mapper.TSpecdevYlgdMapper;
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.sems.his.domain.TSpechiYlgd;
import com.ruoyi.project.sems.his.service.ITSpechiYlgdService;
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;

import javax.annotation.Resource;

/**
 * 特种设备压力管道台账历史Controller
 *
 * @author ruoyi
 * @date 2021-09-02
 */
@RestController("tSpechiYlgdController")
@RequestMapping("/his/ylgd")
public class TSpechiYlgdController extends BaseController
{
    @Autowired
    private ITSpechiYlgdService tSpechiYlgdService;
    @Resource
    private TSpecdevYlgdMapper tSpecdevYlgdMapper;

    /**
     * 查询特种设备压力管道台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlgd:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSpechiYlgd tSpechiYlgd)
    {
        startPage();
        List<TSpechiYlgd> list = tSpechiYlgdService.selectTSpechiYlgdList(tSpechiYlgd);
        return getDataTable(list);
    }

    /**
     * 获取特种设备压力容器台账历史改造历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlgd:query')")
    @GetMapping(value = "/reform/{devId}")
    public AjaxResult getReform(@PathVariable("devId") Long devId)
    {
        return AjaxResult.success(tSpechiYlgdService.selectTSpechiYlgdByReform(devId));
    }

    /**
     * 导出特种设备压力管道台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlgd:export')")
    @Log(title = "特种设备压力管道台账历史", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSpechiYlgd tSpechiYlgd)
    {
        List<TSpechiYlgd> list = tSpechiYlgdService.selectTSpechiYlgdList(tSpechiYlgd);
        ExcelUtil<TSpechiYlgd> util = new ExcelUtil<TSpechiYlgd>(TSpechiYlgd.class);
        return util.exportExcel(list, "ylgd");
    }

    /**
     * 获取特种设备压力管道台账历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlgd:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tSpechiYlgdService.selectTSpechiYlgdById(id));
    }

    /**
     * 新增特种设备压力管道台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlgd:add')")
    @Log(title = "特种设备压力管道台账历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSpechiYlgd tSpechiYlgd)
    {
        tSpechiYlgdService.deleteTSpechiYlgdByDevId(tSpechiYlgd.getId());
        tSpechiYlgd.setDevId(tSpechiYlgd.getId());
        tSpechiYlgd.setHiType((long)1);
        tSpechiYlgdService.insertTSpechiYlgd(tSpechiYlgd);
        return AjaxResult.success("id" , tSpechiYlgd.getId());
    }

    /**
     * 修改特种设备压力管道台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlgd:add')")
    @Log(title = "特种设备压力管道台账历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSpechiYlgd tSpechiYlgd)
    {
        return toAjax(tSpechiYlgdService.updateTSpechiYlgd(tSpechiYlgd));
    }

    /**
     * 删除特种设备压力管道台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlgd:remove')")
    @Log(title = "特种设备压力管道台账历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSpechiYlgdService.deleteTSpechiYlgdByIds(ids));
    }

    public void addOperLog(Object[] ids, String userName, Date operTime , String operType){
        for (Object id: ids
        ) {
            TSpecdevYlgd dev = tSpecdevYlgdMapper.selectTSpecdevYlgdById(Long.parseLong(id.toString()));
            dev.setCreateBy(userName);
            dev.setCreateTime(operTime);
            dev.setRemark(operType);
            dev.setHiType(2l);
            tSpechiYlgdService.insertTSpechiYlgdWithDev(dev);
        }
    }

    public void addOperLog(String id ,String userName, Date operTime , String operType){
        TSpecdevYlgd dev = tSpecdevYlgdMapper.selectTSpecdevYlgdById(Long.parseLong(id));
        dev.setCreateBy(userName);
        dev.setCreateTime(operTime);
        dev.setRemark(operType);
        dev.setHiType(2l);
        tSpechiYlgdService.insertTSpechiYlgdWithDev(dev);
    }
}
