package com.qryord.project.master.model.controller;

import com.qryord.common.enums.EDelete;
import com.qryord.common.utils.DateUtils;
import com.qryord.common.utils.poi.ExcelUtil;
import com.qryord.common.utils.security.ShiroUtils;
import com.qryord.framework.aspectj.lang.annotation.Log;
import com.qryord.framework.aspectj.lang.enums.BusinessType;
import com.qryord.framework.web.controller.BaseController;
import com.qryord.framework.web.domain.AjaxResult;
import com.qryord.framework.web.page.TableDataInfo;
import com.qryord.project.master.model.domain.Model;
import com.qryord.project.master.model.service.IModelService;
import com.qryord.project.master.modelKind.domain.ModelKind;
import com.qryord.project.master.modelKind.service.IModelKindService;
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.*;

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

/**
 * 商品型号Controller
 * 
 * @author ruoyi
 * @date 2022-08-06
 */
@Controller
@RequestMapping("/master/model")
public class ModelController extends BaseController
{
    private String prefix = "master/model";

    @Autowired
    private IModelKindService modelKindService;
    @Autowired
    private IModelService modelService;

    @RequiresPermissions("master:model:view")
    @GetMapping()
    public String model(ModelMap mmap)
    {
        ModelKind cond = new ModelKind();
        cond.setDelFlag(EDelete.FALSE.getVal());
        List<ModelKind> modelKindList = modelKindService.selectModelKindList(cond);
        mmap.put("modelKindList", modelKindList);

        return prefix + "/model";
    }

    /**
     * 查询商品型号列表
     */
    @RequiresPermissions("master:model:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Model model)
    {
        model.setDelFlag(EDelete.FALSE.getVal());
        startPage();
        List<Model> list = modelService.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 导出商品型号列表
     */
    @RequiresPermissions("master:model:export")
    @Log(title = "商品型号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Model model)
    {
        List<Model> list = modelService.selectModelList(model);
        ExcelUtil<Model> util = new ExcelUtil<Model>(Model.class);
        return util.exportExcel(list, "商品型号数据");
    }

    /**
     * 新增商品型号
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        ModelKind cond = new ModelKind();
        cond.setDelFlag(EDelete.FALSE.getVal());
        List<ModelKind> modelKindList = modelKindService.selectModelKindList(cond);
        mmap.put("modelKindList", modelKindList);

        return prefix + "/add";
    }

    /**
     * 新增保存商品型号
     */
    @RequiresPermissions("master:model:add")
    @Log(title = "商品型号", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Model model)
    {
        Date nowDate = DateUtils.getNowDate();
        model.setDelFlag(EDelete.FALSE.getVal());
        model.setCreateBy(ShiroUtils.getLoginName());
        model.setCreateTime(nowDate);
        model.setUpdateBy(ShiroUtils.getLoginName());
        model.setUpdateTime(nowDate);
        return toAjax(modelService.insertModel(model));
    }

    /**
     * 修改商品型号
     */
    @RequiresPermissions("master:model:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Model model = modelService.selectModelById(id);
        mmap.put("model", model);
        ModelKind cond = new ModelKind();
        cond.setDelFlag(EDelete.FALSE.getVal());
        List<ModelKind> modelKindList = modelKindService.selectModelKindList(cond);
        mmap.put("modelKindList", modelKindList);

        return prefix + "/edit";
    }

    /**
     * 修改保存商品型号
     */
    @RequiresPermissions("master:model:edit")
    @Log(title = "商品型号", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Model model)
    {
        model.setUpdateBy(ShiroUtils.getLoginName());
        model.setUpdateTime(DateUtils.getNowDate());
        return toAjax(modelService.updateModel(model));
    }

    /**
     * 删除商品型号
     */
    @RequiresPermissions("master:model:remove")
    @Log(title = "商品型号", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(modelService.deleteModelByIds(ids));
    }
}
