package com.ruoyi.web.controller.nanjing;

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

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.nanjing.domain.TBdProductinfo;
import com.ruoyi.nanjing.service.ITBdProductinfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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.TRpProductchangeinfo;
import com.ruoyi.nanjing.service.ITRpProductchangeinfoService;
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-22
 */
@Controller
@RequestMapping("/nanjing/ProductChangeInfo")
public class TRpProductchangeinfoController extends BaseController
{
    private String prefix = "nanjing/ProductChangeInfo";

    @Autowired
    private ITBdProductinfoService tBdProductinfoService;
    @Autowired
    private ITRpProductchangeinfoService tRpProductchangeinfoService;

    @RequiresPermissions("nanjing:ProductChangeInfo:view")
    @GetMapping()
    public String ProductChangeInfo(ModelMap map)
    {
        List<TBdProductinfo> tBdProductinfoList = tBdProductinfoService.selectTBdProductinfoList(new TBdProductinfo());
        map.addAttribute("list",tBdProductinfoList);
        return prefix + "/ProductChangeInfo";
    }

    /**
     * 查询机种切换管理列表
     */
    @RequiresPermissions("nanjing:ProductChangeInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TRpProductchangeinfo tRpProductchangeinfo)
    {
        startPage();
        List<TRpProductchangeinfo> list = tRpProductchangeinfoService.selectTRpProductchangeinfoList(tRpProductchangeinfo);
        return getDataTable(list);
    }

    /**
     * 导出机种切换管理列表
     */
    @RequiresPermissions("nanjing:ProductChangeInfo:export")
    @Log(title = "机种切换管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TRpProductchangeinfo tRpProductchangeinfo)
    {
        List<TRpProductchangeinfo> list = tRpProductchangeinfoService.selectTRpProductchangeinfoList(tRpProductchangeinfo);
        ExcelUtil<TRpProductchangeinfo> util = new ExcelUtil<TRpProductchangeinfo>(TRpProductchangeinfo.class);
        return util.exportExcel(list, "ProductChangeInfo");
    }

    /**
     * 新增机种切换管理
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        List<TBdProductinfo> tBdProductinfoList = tBdProductinfoService.selectTBdProductinfoList(new TBdProductinfo());
        map.addAttribute("list",tBdProductinfoList);
        return prefix + "/add";
    }

    /**
     * 新增保存机种切换管理
     */
    @RequiresPermissions("nanjing:ProductChangeInfo:add")
    @Log(title = "机种切换管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TRpProductchangeinfo tRpProductchangeinfo)
    {
        tRpProductchangeinfo.setInsertTime(new Date());
        return toAjax(tRpProductchangeinfoService.insertTRpProductchangeinfo(tRpProductchangeinfo));
    }

    /**
     * 修改机种切换管理
     */
    @GetMapping("/edit/{ID}")
    public String edit(@PathVariable("ID") Long ID, ModelMap mmap)
    {
        List<TBdProductinfo> tBdProductinfoList = tBdProductinfoService.selectTBdProductinfoList(new TBdProductinfo());
        mmap.addAttribute("list",tBdProductinfoList);
        TRpProductchangeinfo tRpProductchangeinfo = tRpProductchangeinfoService.selectTRpProductchangeinfoById(ID);
        mmap.put("tRpProductchangeinfo", tRpProductchangeinfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存机种切换管理
     */
    @RequiresPermissions("nanjing:ProductChangeInfo:edit")
    @Log(title = "机种切换管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TRpProductchangeinfo tRpProductchangeinfo)
    {
        return toAjax(tRpProductchangeinfoService.updateTRpProductchangeinfo(tRpProductchangeinfo));
    }

    @RequiresPermissions("nanjing:ProductChangeInfo:start")
    @PostMapping("/start")
    @ResponseBody
    public AjaxResult start(HttpServletRequest request){
        String ids = request.getParameter("id");
        TRpProductchangeinfo tRpProductchangeinfo = tRpProductchangeinfoService.selectTRpProductchangeinfoById(Convert.toLong(ids));
        if(tRpProductchangeinfo.getBeginTime()==null){
            tRpProductchangeinfo.setBeginTime(new Date());
            return toAjax(tRpProductchangeinfoService.updateTRpProductchangeinfo(tRpProductchangeinfo));
        }else{
            return toAjax(0);
        }
    }

    /**
     * 删除机种切换管理
     */
    @RequiresPermissions("nanjing:ProductChangeInfo:remove")
    @Log(title = "机种切换管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tRpProductchangeinfoService.deleteTRpProductchangeinfoByIds(ids));
    }
}
