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

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

import com.ruoyi.project.sems.domain.TSpecdevYlgd;
import com.ruoyi.project.sems.domain.TSpecdevYlrq;
import com.ruoyi.project.sems.mapper.TSpecdevYlrqMapper;
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.TSpechiYlrq;
import com.ruoyi.project.sems.his.service.ITSpechiYlrqService;
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("tSpechiYlrqController")
@RequestMapping("/his/ylrq")
public class TSpechiYlrqController extends BaseController
{
    @Autowired
    private ITSpechiYlrqService tSpechiYlrqService;
    @Resource
    private TSpecdevYlrqMapper tSpecdevYlrqMapper;
    /**
     * 查询特种设备压力容器台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlrq:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSpechiYlrq tSpechiYlrq)
    {
        startPage();
        List<TSpechiYlrq> list = tSpechiYlrqService.selectTSpechiYlrqList(tSpechiYlrq);
        return getDataTable(list);
    }

    /**
     * 导出特种设备压力容器台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlrq:export')")
    @Log(title = "特种设备压力容器台账历史", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSpechiYlrq tSpechiYlrq)
    {
        List<TSpechiYlrq> list = tSpechiYlrqService.selectTSpechiYlrqList(tSpechiYlrq);
        ExcelUtil<TSpechiYlrq> util = new ExcelUtil<TSpechiYlrq>(TSpechiYlrq.class);
        return util.exportExcel(list, "ylrq");
    }

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

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


    /**
     * 新增特种设备压力容器台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlrq:add')")
    @Log(title = "特种设备压力容器台账历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSpechiYlrq tSpechiYlrq)
    {
        tSpechiYlrqService.deleteTSpechiYlrqByDevId(tSpechiYlrq.getId());
        tSpechiYlrq.setDevId(tSpechiYlrq.getId());
        tSpechiYlrq.setHiType((long)1);
        tSpechiYlrqService.insertTSpechiYlrq(tSpechiYlrq);
        return AjaxResult.success("id" , tSpechiYlrq.getId());
    }

    /**
     * 修改特种设备压力容器台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specYlrq:edit')")
    @Log(title = "特种设备压力容器台账历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSpechiYlrq tSpechiYlrq)
    {
        return toAjax(tSpechiYlrqService.updateTSpechiYlrq(tSpechiYlrq));
    }

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

    public void addOperLog(Object[] ids, String userName, Date operTime , String operType){
        for (Object id: ids
        ) {
            TSpecdevYlrq dev = tSpecdevYlrqMapper.selectTSpecdevYlrqById(Long.parseLong(id.toString()));
            dev.setCreateBy(userName);
            dev.setCreateTime(operTime);
            dev.setRemark(operType);
            dev.setHiType(2l);
            tSpechiYlrqService.insertTSpechiYlrqWithDev(dev);
        }
    }

    public void addOperLog(String id ,String userName, Date operTime , String operType){
        TSpecdevYlrq dev = tSpecdevYlrqMapper.selectTSpecdevYlrqById(Long.parseLong(id));
        dev.setCreateBy(userName);
        dev.setCreateTime(operTime);
        dev.setRemark(operType);
        dev.setHiType(2l);
        tSpechiYlrqService.insertTSpechiYlrqWithDev(dev);
    }


}
