package com.op.mes.controller;

import com.op.common.core.utils.poi.ExcelUtil;
import com.op.common.core.web.controller.BaseController;
import com.op.common.core.web.domain.AjaxResult;
import com.op.common.core.web.page.TableDataInfo;
import com.op.common.log.annotation.Log;
import com.op.common.log.enums.BusinessType;
import com.op.common.security.annotation.RequiresPermissions;
import com.op.mes.domain.ProRecipe;
import com.op.mes.service.IProRecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 配药配方Controller
 *
 * @author yinjinlu
 * @date 2022-05-11
 */
@RestController
@RequestMapping("/pro/recipe")
public class ProRecipeController extends BaseController {
    @Autowired
    private IProRecipeService proRecipeService;

    /**
     * 查询配药配方列表
     */
    @RequiresPermissions("mes:pro:recipe:list")
    @GetMapping("/list")
    public TableDataInfo list(ProRecipe proRecipe) {
        startPage();
        List<ProRecipe> list = proRecipeService.selectProRecipeList(proRecipe);
        return getDataTable(list);
    }

    /**
     * 查询所有可用配方的清单
     *
     * @return
     */
    @RequiresPermissions("mes:pro:recipe:list")
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        ProRecipe recipe = new ProRecipe();
        recipe.setEnableFlag("Y");
        List<ProRecipe> list = proRecipeService.selectProRecipeList(recipe);
        return AjaxResult.success(list);
    }

    /**
     * 导出配药配方列表
     */
    @RequiresPermissions("@ss.hasPermi('mes:pro:recipe:export')")
    @Log(title = "配药配方", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProRecipe proRecipe) {
        List<ProRecipe> list = proRecipeService.selectProRecipeList(proRecipe);
        ExcelUtil<ProRecipe> util = new ExcelUtil<ProRecipe>(ProRecipe.class);
        util.exportExcel(response, list, "配药配方数据");
    }

    /**
     * 获取配药配方详细信息
     */
    @RequiresPermissions("mes:pro:recipe:query")
    @GetMapping(value = "/{recipeId}")
    public AjaxResult getInfo(@PathVariable("recipeId") String recipeId) {
        return AjaxResult.success(proRecipeService.selectProRecipeByRecipeId(recipeId));
    }

    /**
     * 新增配药配方
     */
    @RequiresPermissions("mes:pro:recipe:add")
    @Log(title = "配药配方", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProRecipe proRecipe) {
        if (!proRecipeService.checkRecipeCodeUnique(proRecipe)) {
            return AjaxResult.error("配方编码已存在！");
        }
        if (!proRecipeService.checkRecipeNameUnique(proRecipe)) {
            return AjaxResult.error("配方名称已存在！");
        }
        return toAjax(proRecipeService.insertProRecipe(proRecipe));
    }

    /**
     * 修改配药配方
     */
    @RequiresPermissions("mes:pro:recipe:edit")
    @Log(title = "配药配方", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProRecipe proRecipe) {
        if (!proRecipeService.checkRecipeCodeUnique(proRecipe)) {
            return AjaxResult.error("配方编码已存在！");
        }
        if (!proRecipeService.checkRecipeNameUnique(proRecipe)) {
            return AjaxResult.error("配方名称已存在！");
        }
        return toAjax(proRecipeService.updateProRecipe(proRecipe));
    }

    /**
     * 删除配药配方
     */
    @RequiresPermissions("mes:pro:recipe:remove")
    @Log(title = "配药配方", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recipeIds}")
    public AjaxResult remove(@PathVariable String[] recipeIds) {
        return toAjax(proRecipeService.deleteProRecipeByRecipeIds(recipeIds));
    }
}
