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

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

import com.ruoyi.project.sems.domain.TSpecdevDzsb;
import com.ruoyi.project.sems.domain.TSpecdevGl;
import com.ruoyi.project.sems.mapper.TSpecdevGlMapper;
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.TSpechiGl;
import com.ruoyi.project.sems.his.service.ITSpechiGlService;
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("tSpechiGlController")
@RequestMapping("/his/gl")
public class TSpechiGlController extends BaseController
{
    @Autowired
    private ITSpechiGlService tSpechiGlService;
    @Resource
    private TSpecdevGlMapper tSpecdevGlMapper;

    /**
     * 查询特种设备锅炉台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specGl:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSpechiGl tSpechiGl)
    {
        startPage();
        List<TSpechiGl> list = tSpechiGlService.selectTSpechiGlList(tSpechiGl);
        return getDataTable(list);
    }

    /**
     * 获取特种设备锅炉台账历史改造历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specGl:query')")
    @GetMapping(value = "/reform/{devId}")
    public AjaxResult getReform(@PathVariable("devId") Long devId)
    {
        return AjaxResult.success(tSpechiGlService.selectTSpechiGlByReform(devId));
    }

    /**
     * 导出特种设备锅炉台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specGl:export')")
    @Log(title = "特种设备锅炉台账历史", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSpechiGl tSpechiGl)
    {
        List<TSpechiGl> list = tSpechiGlService.selectTSpechiGlList(tSpechiGl);
        ExcelUtil<TSpechiGl> util = new ExcelUtil<TSpechiGl>(TSpechiGl.class);
        return util.exportExcel(list, "gl");
    }

    /**
     * 获取特种设备锅炉台账历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('sems:specGl:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tSpechiGlService.selectTSpechiGlById(id));
    }

    /**
     * 新增特种设备锅炉台账历史
     * 设置并返回历史表id
     */
    @PreAuthorize("@ss.hasPermi('sems:specGl:add')")
    @Log(title = "特种设备锅炉台账历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSpechiGl tSpechiGl)
    {
        tSpechiGlService.deleteTSpechiGlByDevId(tSpechiGl.getId());
        tSpechiGl.setDevId(tSpechiGl.getId());
        tSpechiGl.setHiType((long)1);
        tSpechiGlService.insertTSpechiGl(tSpechiGl);
        return AjaxResult.success("id",tSpechiGl.getId());
    }

    /**
     * 修改特种设备锅炉台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specGl:edit')")
    @Log(title = "特种设备锅炉台账历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSpechiGl tSpechiGl)
    {
        return toAjax(tSpechiGlService.updateTSpechiGl(tSpechiGl));
    }

    /**
     * 删除特种设备锅炉台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specGl:remove')")
    @Log(title = "特种设备锅炉台账历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSpechiGlService.deleteTSpechiGlByIds(ids));
    }

    public void addOperLog(Object[] ids, String userName, Date operTime , String operType){
        for (Object id: ids
        ) {
            TSpecdevGl dev = tSpecdevGlMapper.selectTSpecdevGlById(Long.parseLong(id.toString()));
            dev.setCreateBy(userName);
            dev.setCreateTime(operTime);
            dev.setRemark(operType);
            dev.setHiType(2l);
            tSpechiGlService.insertTSpechiGlWithDev(dev);
        }
    }

    public void addOperLog(String id ,String userName, Date operTime , String operType){
        TSpecdevGl dev = tSpecdevGlMapper.selectTSpecdevGlById(Long.parseLong(id));
        dev.setCreateBy(userName);
        dev.setCreateTime(operTime);
        dev.setRemark(operType);
        dev.setHiType(2l);
        tSpechiGlService.insertTSpechiGlWithDev(dev);
    }
}
