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.mapper.TSpecdevDtMapper;
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.TSpechiDt;
import com.ruoyi.project.sems.his.service.ITSpechiDtService;
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("tSpechiDtController")
@RequestMapping("/his/dt")
public class TSpechiDtController extends BaseController
{
    @Autowired
    private ITSpechiDtService tSpechiDtService;

    @Resource
    private TSpecdevDtMapper tSpecdevDtMapper;
    /**
     * 查询特种设备电梯台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specDt:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSpechiDt tSpechiDt)
    {
        startPage();
        List<TSpechiDt> list = tSpechiDtService.selectTSpechiDtList(tSpechiDt);
        return getDataTable(list);
    }

    /**
     * 导出特种设备电梯台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specDt:export')")
    @Log(title = "特种设备电梯台账历史", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSpechiDt tSpechiDt)
    {
        List<TSpechiDt> list = tSpechiDtService.selectTSpechiDtList(tSpechiDt);
        ExcelUtil<TSpechiDt> util = new ExcelUtil<TSpechiDt>(TSpechiDt.class);
        return util.exportExcel(list, "dt");
    }

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

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

    /**
     * 新增特种设备电梯台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specDt:add')")
    @Log(title = "特种设备电梯台账历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSpechiDt tSpechiDt)
    {
        tSpechiDtService.deleteTSpechiDtByDevId(tSpechiDt.getId());
        tSpechiDt.setDevId(tSpechiDt.getId());
        tSpechiDt.setHiType((long)1);
        tSpechiDtService.insertTSpechiDt(tSpechiDt);
        return AjaxResult.success("id" , tSpechiDt.getId());
    }

    /**
     * 修改特种设备电梯台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specDt:edit')")
    @Log(title = "特种设备电梯台账历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSpechiDt tSpechiDt)
    {
        return toAjax(tSpechiDtService.updateTSpechiDt(tSpechiDt));
    }

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

    public void addOperLog(Object[] ids, String userName, Date operTime , String operType){
        for (Object id: ids
        ) {
            TSpecdevDt dev = tSpecdevDtMapper.selectTSpecdevDtById(Long.parseLong(id.toString()));
            dev.setCreateBy(userName);
            dev.setCreateTime(operTime);
            dev.setRemark(operType);
            dev.setHiType(2l);
            tSpechiDtService.insertTSpechiDtWithDev(dev);
        }
    }

    public void addOperLog(String id ,String userName, Date operTime , String operType){
        TSpecdevDt dev = tSpecdevDtMapper.selectTSpecdevDtById(Long.parseLong(id));
        dev.setCreateBy(userName);
        dev.setCreateTime(operTime);
        dev.setRemark(operType);
        dev.setHiType(2l);
        tSpechiDtService.insertTSpechiDtWithDev(dev);
    }

}
