package com.ruoyi.traceability.controller;

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

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.traceability.domain.ProMqUpload;
import com.ruoyi.traceability.service.IProMqUploadService;
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.xml.namespace.QName;

/**
 * 模切数据上传Controller
 * 
 * @author wenjy
 * @date 2022-11-18
 */
@Controller
@RequestMapping("/traceability/mqupload")
public class ProMqUploadController extends BaseController
{
    private String prefix = "traceability/mqupload";

    @Autowired
    private IProMqUploadService proMqUploadService;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

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

    /**
     * 查询模切数据上传列表
     */
    @RequiresPermissions("traceability:mqupload:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProMqUpload proMqUpload)
    {
        startPage();
        List<ProMqUpload> list = proMqUploadService.selectProMqUploadList(proMqUpload);
        return getDataTable(list);
    }

    /**
     * 导出模切数据上传列表
     */
    @RequiresPermissions("traceability:mqupload:export")
    @Log(title = "模切数据上传", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProMqUpload proMqUpload)
    {
        List<ProMqUpload> list = proMqUploadService.selectProMqUploadList(proMqUpload);
        ExcelUtil<ProMqUpload> util = new ExcelUtil<ProMqUpload>(ProMqUpload.class);
        return util.exportExcel(list, "模切数据上传数据");
    }

    /**
     * 新增模切数据上传
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存模切数据上传
     */
    @RequiresPermissions("traceability:mqupload:add")
    @Log(title = "模切数据上传", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProMqUpload proMqUpload)
    {
        proMqUpload.setRecordTime(simpleDateFormat.format(new Date()));
        return toAjax(proMqUploadService.insertProMqUpload(proMqUpload));
    }

    /**
     * 修改模切数据上传
     */
    @RequiresPermissions("traceability:mqupload:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ProMqUpload proMqUpload = proMqUploadService.selectProMqUploadById(id);
        mmap.put("proMqUpload", proMqUpload);
        return prefix + "/edit";
    }

    /**
     * 修改保存模切数据上传
     */
    @RequiresPermissions("traceability:mqupload:edit")
    @Log(title = "模切数据上传", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProMqUpload proMqUpload)
    {
        return toAjax(proMqUploadService.updateProMqUpload(proMqUpload));
    }

    /**
     * 上传重量特征数据
     * @param ids
     * @return
     */
    @PostMapping( "/dataUpload")
    @ResponseBody
    public AjaxResult dataUpload(Long ids)
    {
        //根据id获取上传的数据信息
        ProMqUpload proMqUpload = proMqUploadService.selectProMqUploadById(ids);

        if(proMqUpload != null){
            return toAjax(1);
        }
        return toAjax(0);
    }

    /**
     * 删除模切数据上传
     */
    @RequiresPermissions("traceability:mqupload:remove")
    @Log(title = "模切数据上传", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(proMqUploadService.deleteProMqUploadByIds(ids));
    }
}