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

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.sems.domain.TSpecdevCc;
import com.ruoyi.project.sems.mapper.TSpecdevCcMapper;
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.TSpechiCc;
import com.ruoyi.project.sems.his.service.ITSpechiCcService;
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("tSpechiCcController")
@RequestMapping("/his/cc")
public class TSpechiCcController extends BaseController
{
    @Autowired
    private ITSpechiCcService tSpechiCcService;
    @Resource
    private TSpecdevCcMapper tSpecdevCcMapper;

    /**
     * 查询特种设备叉车台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specCc:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSpechiCc tSpechiCc)
    {
        startPage();
        List<TSpechiCc> list = tSpechiCcService.selectTSpechiCcList(tSpechiCc);
        return getDataTable(list);
    }

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

    /**
     * 导出特种设备叉车台账历史列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specCc:export')")
    @Log(title = "特种设备叉车台账历史", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSpechiCc tSpechiCc)
    {
        List<TSpechiCc> list = tSpechiCcService.selectTSpechiCcList(tSpechiCc);
        ExcelUtil<TSpechiCc> util = new ExcelUtil<TSpechiCc>(TSpechiCc.class);
        return util.exportExcel(list, "cc");
    }

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

    /**
     * 新增特种设备叉车台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specCc:add')")
    @Log(title = "特种设备叉车台账历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSpechiCc tSpechiCc)
    {
        tSpechiCcService.deleteTSpechiCcByDevId(tSpechiCc.getId());
        tSpechiCc.setDevId(tSpechiCc.getId());
        tSpechiCc.setHiType((long)1);
        tSpechiCcService.insertTSpechiCc(tSpechiCc);
        return AjaxResult.success("id" , tSpechiCc.getId());
    }

    public void addOperLog(Object[] ids,String userName, Date operTime , String operType){
        for (Object id: ids
             ) {
            TSpecdevCc cc = tSpecdevCcMapper.selectTSpecdevCcById(Long.parseLong(id.toString()));
            cc.setCreateBy(userName);
            cc.setCreateTime(operTime);
            cc.setRemark(operType);
            cc.setHiType(2l);
            tSpechiCcService.insertTSpechiCcWithDev(cc);
        }
    }

    public void addOperLog(String id ,String userName, Date operTime , String operType){
        TSpecdevCc cc = tSpecdevCcMapper.selectTSpecdevCcById(Long.parseLong(id));
            cc.setCreateBy(userName);
            cc.setCreateTime(operTime);
            cc.setRemark(operType);
            cc.setHiType(2l);
            tSpechiCcService.insertTSpechiCcWithDev(cc);
    }

    /**
     * 修改特种设备叉车台账历史
     */
    @PreAuthorize("@ss.hasPermi('sems:specCc:edit')")
    @Log(title = "特种设备叉车台账历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSpechiCc tSpechiCc)
    {
        return toAjax(tSpechiCcService.updateTSpechiCc(tSpechiCc));
    }

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