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

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

import com.ruoyi.project.sems.domain.TSpecdevDt;
import com.ruoyi.project.sems.domain.TSpecdevDzsb;
import com.ruoyi.project.sems.mapper.TSpecdevDzsbMapper;
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.TSpechiDzsb;
import com.ruoyi.project.sems.his.service.ITSpechiDzsbService;
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("tSpechiDzsbController")
@RequestMapping("/his/dzsb")
public class TSpechiDzsbController extends BaseController
{
    @Autowired
    private ITSpechiDzsbService tSpechiDzsbService;
    @Resource
    TSpecdevDzsbMapper tSpecdevDzsbMapper;

    /**
     * 查询特种设备吊装设备台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specDzsb:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSpechiDzsb tSpechiDzsb)
    {
        startPage();
        List<TSpechiDzsb> list = tSpechiDzsbService.selectTSpechiDzsbList(tSpechiDzsb);
        return getDataTable(list);
    }

    /**
     * 导出特种设备吊装设备台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specDzsb:export')")
    @Log(title = "特种设备吊装设备台账历史", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSpechiDzsb tSpechiDzsb)
    {
        List<TSpechiDzsb> list = tSpechiDzsbService.selectTSpechiDzsbList(tSpechiDzsb);
        ExcelUtil<TSpechiDzsb> util = new ExcelUtil<TSpechiDzsb>(TSpechiDzsb.class);
        return util.exportExcel(list, "dzsb");
    }

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

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

    /**
     * 新增特种设备吊装设备台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specDzsb:add')")
    @Log(title = "特种设备吊装设备台账历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSpechiDzsb tSpechiDzsb)
    {
        tSpechiDzsbService.deleteTSpechiDzsbByDevId(tSpechiDzsb.getId());
        tSpechiDzsb.setDevId(tSpechiDzsb.getId());
        tSpechiDzsb.setHiType((long)1);
        tSpechiDzsbService.insertTSpechiDzsb(tSpechiDzsb);
        return AjaxResult.success("id" , tSpechiDzsb.getId());
    }

    /**
     * 修改特种设备吊装设备台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specDzsb:edit')")
    @Log(title = "特种设备吊装设备台账历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSpechiDzsb tSpechiDzsb)
    {
        return toAjax(tSpechiDzsbService.updateTSpechiDzsb(tSpechiDzsb));
    }

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

    public void addOperLog(Object[] ids, String userName, Date operTime , String operType){
        for (Object id: ids
        ) {
            TSpecdevDzsb dev = tSpecdevDzsbMapper.selectTSpecdevDzsbById(Long.parseLong(id.toString()));
            dev.setCreateBy(userName);
            dev.setCreateTime(operTime);
            dev.setRemark(operType);
            dev.setHiType(2l);
            tSpechiDzsbService.insertTSpechiDzsbWithDev(dev);
        }
    }

    public void addOperLog(String id ,String userName, Date operTime , String operType){
        TSpecdevDzsb dev = tSpecdevDzsbMapper.selectTSpecdevDzsbById(Long.parseLong(id));
        dev.setCreateBy(userName);
        dev.setCreateTime(operTime);
        dev.setRemark(operType);
        dev.setHiType(2l);
        tSpechiDzsbService.insertTSpechiDzsbWithDev(dev);
    }
}
