package com.ruoyi.web.controller.baseinfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.baseinfo.domain.BaseDeviceInfo;
import com.ruoyi.baseinfo.domain.BaseShiftInfo;
import com.ruoyi.baseinfo.service.IBaseDeviceInfoService;
import com.ruoyi.baseinfo.service.IBasePlandetailInfoService;
import com.ruoyi.baseinfo.service.IBaseShiftInfoService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
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.baseinfo.domain.BasePlanInfo;
import com.ruoyi.baseinfo.service.IBasePlanInfoService;
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;

/**
 * 计划信息Controller
 * 
 * @author CaesarBao
 * @date 2020-10-23
 */
@Controller
@RequestMapping("/baseinfo/planinfo")
public class BasePlanInfoController extends BaseController
{
    private String prefix = "baseinfo/planinfo";

    @Autowired
    private IBasePlanInfoService basePlanInfoService;
    @Autowired
    private IBaseDeviceInfoService baseDeviceInfoService;
    @Autowired
    private IBaseShiftInfoService baseShiftInfoService;
    @Autowired
    private IBasePlandetailInfoService basePlandetailInfoService;

    @RequiresPermissions("baseinfo:planinfo:view")
    @GetMapping()
    public String planinfo(ModelMap mmap)
    {
        BaseDeviceInfo baseDeviceInfo = new BaseDeviceInfo();
        BaseShiftInfo baseShiftInfo = new BaseShiftInfo();
        mmap.put("cbdeviceInfo",baseDeviceInfoService.selectBaseDeviceInfoList(baseDeviceInfo));
        mmap.put("cbshitfInfo",baseShiftInfoService.selectBaseShiftInfoList(baseShiftInfo));
        return prefix + "/planinfo";
    }

    /**
     * 查询计划信息列表
     */
    @RequiresPermissions("baseinfo:planinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasePlanInfo basePlanInfo)
    {
        startPage();
        List<BasePlanInfo> list = basePlanInfoService.selectBasePlanInfoList(basePlanInfo);
        return getDataTable(list);
    }

    /**
     * 导出计划信息列表
     */
    @RequiresPermissions("baseinfo:planinfo:export")
    @Log(title = "计划信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasePlanInfo basePlanInfo)
    {
        List<BasePlanInfo> list = basePlanInfoService.selectBasePlanInfoList(basePlanInfo);
        ExcelUtil<BasePlanInfo> util = new ExcelUtil<BasePlanInfo>(BasePlanInfo.class);
        return util.exportExcel(list, "planinfo");
    }

    /**
     * 新增计划信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        BaseDeviceInfo baseDeviceInfo = new BaseDeviceInfo();
        BaseShiftInfo baseShiftInfo = new BaseShiftInfo();
        mmap.put("cbdeviceInfo",baseDeviceInfoService.selectBaseDeviceInfoList(baseDeviceInfo));
        mmap.put("cbshitfInfo",baseShiftInfoService.selectBaseShiftInfoList(baseShiftInfo));
        return prefix + "/add";
    }

    /**
     * 新增保存计划信息
     */
    @RequiresPermissions("baseinfo:planinfo:add")
    @Log(title = "计划信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasePlanInfo basePlanInfo)
    {
        Random rand = new Random();
        String code = new SimpleDateFormat("yyMMddHHmmss").format(new Date()) + (rand.nextInt(9));
        basePlanInfo.setPlanid(code);
        basePlanInfo.setCreateBy(ShiroUtils.getLoginName());
        basePlanInfo.setCreateTime(DateUtils.getNowDate());
        return toAjax(basePlanInfoService.insertBasePlanInfo(basePlanInfo));
    }

    /**
     * 修改计划信息
     */
    @GetMapping("/edit/{planid}")
    public String edit(@PathVariable("planid") String planid, ModelMap mmap)
    {
        BasePlanInfo basePlanInfo = basePlanInfoService.selectBasePlanInfoById(planid);
        mmap.put("cbdeviceInfo",baseDeviceInfoService.selectEditBaseDeviceInfoList(basePlanInfo.getDeviceid()));
        mmap.put("cbshitfInfo",baseShiftInfoService.selectEditBaseShiftInfoList(basePlanInfo.getPlanShiftId()));
        mmap.put("basePlanInfo", basePlanInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存计划信息
     */
    @RequiresPermissions("baseinfo:planinfo:edit")
    @Log(title = "计划信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasePlanInfo basePlanInfo)
    {
        return toAjax(basePlanInfoService.updateBasePlanInfo(basePlanInfo));
    }

    /**
     * 删除计划信息
     */
    @RequiresPermissions("baseinfo:planinfo:remove")
    @Log(title = "计划信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basePlanInfoService.deleteBasePlanInfoByIds(ids));
    }

}
