package com.ruoyi.web.controller.nanjing;

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

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.nanjing.domain.TRpWorktrayrealtimeinfo;
import com.ruoyi.nanjing.service.ITRpWorktrayrealtimeinfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 托盘管理Controller
 * 
 * @author limy
 * @date 2021-01-28
 */
@Controller
@RequestMapping("/nanjing/WorkTrayRealTimeInfo")
public class TRpWorktrayrealtimeinfoController extends BaseController
{
    private String prefix = "nanjing/WorkTrayRealTimeInfo";

    @Autowired
    private ITRpWorktrayrealtimeinfoService tRpWorktrayrealtimeinfoService;

    @RequiresPermissions("nanjing:WorkTrayRealTimeInfo:view")
    @GetMapping()
    public String WorkTrayRealTimeInfo()
    {
        return prefix + "/WorkTrayRealTimeInfo";
    }

    /**
     * 查询托盘管理列表
     */
    @RequiresPermissions("nanjing:WorkTrayRealTimeInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TRpWorktrayrealtimeinfo tRpWorktrayrealtimeinfo)
    {
        startPage();
        List<TRpWorktrayrealtimeinfo> list = tRpWorktrayrealtimeinfoService.selectTRpWorktrayrealtimeinfoList(tRpWorktrayrealtimeinfo);
        return getDataTable(list);
    }

    /**
     * 导出托盘管理列表
     */
    @RequiresPermissions("nanjing:WorkTrayRealTimeInfo:export")
    @Log(title = "托盘管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TRpWorktrayrealtimeinfo tRpWorktrayrealtimeinfo)
    {
        List<TRpWorktrayrealtimeinfo> list = tRpWorktrayrealtimeinfoService.selectTRpWorktrayrealtimeinfoList(tRpWorktrayrealtimeinfo);
        ExcelUtil<TRpWorktrayrealtimeinfo> util = new ExcelUtil<TRpWorktrayrealtimeinfo>(TRpWorktrayrealtimeinfo.class);
        return util.exportExcel(list, "WorkTrayRealTimeInfo");
    }

    /**
     * 新增托盘管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存托盘管理
     */
    @RequiresPermissions("nanjing:WorkTrayRealTimeInfo:add")
    @Log(title = "托盘管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TRpWorktrayrealtimeinfo tRpWorktrayrealtimeinfo)
    {
        tRpWorktrayrealtimeinfo.setInsertTime(new Date());
        tRpWorktrayrealtimeinfo.setCurrentStation("0");
        tRpWorktrayrealtimeinfo.setState(0L);
        tRpWorktrayrealtimeinfo.setUsedFlag(1L);
        return toAjax(tRpWorktrayrealtimeinfoService.insertTRpWorktrayrealtimeinfo(tRpWorktrayrealtimeinfo));
    }

    /**
     * 修改托盘管理
     */
    @GetMapping("/edit/{rfidNum}")
    public String edit(@PathVariable("rfidNum") String rfidNum, ModelMap mmap)
    {
        TRpWorktrayrealtimeinfo tRpWorktrayrealtimeinfo = tRpWorktrayrealtimeinfoService.selectTRpWorktrayrealtimeinfoById(rfidNum);
        mmap.put("tRpWorktrayrealtimeinfo", tRpWorktrayrealtimeinfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存托盘管理
     */
    @RequiresPermissions("nanjing:WorkTrayRealTimeInfo:edit")
    @Log(title = "托盘管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TRpWorktrayrealtimeinfo tRpWorktrayrealtimeinfo)
    {
        return toAjax(tRpWorktrayrealtimeinfoService.updateTRpWorktrayrealtimeinfo(tRpWorktrayrealtimeinfo));
    }

    /**
     * 删除托盘管理
     */
    @RequiresPermissions("nanjing:WorkTrayRealTimeInfo:remove")
    @Log(title = "托盘管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tRpWorktrayrealtimeinfoService.deleteTRpWorktrayrealtimeinfoByIds(ids));
    }

    /**
     *启用托盘
     */
    @PostMapping("/start")
    @ResponseBody
    public AjaxResult startPallet(HttpServletRequest request){
        String rfidNum = request.getParameter("rfidNum");
        String msg = "";
        TRpWorktrayrealtimeinfo tRpWorktrayrealtimeinfo = tRpWorktrayrealtimeinfoService.selectTRpWorktrayrealtimeinfoById(rfidNum);
        if(tRpWorktrayrealtimeinfo.getUsedFlag()==0){
            tRpWorktrayrealtimeinfo.setUsedFlag(1L);
            tRpWorktrayrealtimeinfo.setUpdateTime(new Date());
            int res = tRpWorktrayrealtimeinfoService.updateTRpWorktrayrealtimeinfo(tRpWorktrayrealtimeinfo);
            if(res>0){
                msg = "已启用!";
                return AjaxResult.success(msg);
            }
        }
        else
        {
            msg="已是启用状态！";
        }
        return AjaxResult.error(msg);
    }
    @PostMapping("/end")
    @ResponseBody
    public AjaxResult endPallet(HttpServletRequest request){
        String rfidNum = request.getParameter("rfidNum");
        String msg = "";
        TRpWorktrayrealtimeinfo tRpWorktrayrealtimeinfo = tRpWorktrayrealtimeinfoService.selectTRpWorktrayrealtimeinfoById(rfidNum);
        if(tRpWorktrayrealtimeinfo.getUsedFlag()==1){
            tRpWorktrayrealtimeinfo.setUsedFlag(0L);
            tRpWorktrayrealtimeinfo.setUpdateTime(new Date());
            tRpWorktrayrealtimeinfo.setDeleteTime(new Date());
            int res = tRpWorktrayrealtimeinfoService.updateTRpWorktrayrealtimeinfo(tRpWorktrayrealtimeinfo);
            if(res>0){
                msg = "已停用!";
                return AjaxResult.success(msg);
            }
        }
        else
        {
            msg="已是停用状态！";
        }
        return AjaxResult.error(msg);
    }
}
