package com.ghy.web.controller.goods;

import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.ghy.goods.domain.Goods;
import com.ghy.goods.domain.GoodsStandard;
import com.ghy.goods.service.GoodsStandardService;
import com.ghy.order.domain.OrderTemplate;
import com.ghy.order.service.OrderTemplateService;
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 javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("goods/standard")
public class GoodsStandardController extends BaseController {

    private static final String PREFIX = "goods/standard";

    @Resource
    private GoodsStandardService goodsStandardService;

    @Autowired
    private OrderTemplateService orderTemplateService;

    @GetMapping("/addTemplate")
    public String addTemplate(ModelMap modelMap){
        return PREFIX + "/addTemplate";
    }

    @GetMapping("getByTemplateId/{id}")
    public String getByTemplateId(ModelMap modelMap, @PathVariable("id") Long templateId) {
        OrderTemplate orderTemplate = orderTemplateService.selectById(templateId);
        modelMap.put("template", orderTemplate);
        return PREFIX + "/addTemplate";
    }
    @RequiresPermissions("goods:standard:view")
    @GetMapping
    public String goodsStandard(Long goodsId, ModelMap mmap) {
        List<GoodsStandard> goodsStandardList = goodsStandardService.selectByGoodsId(goodsId);
        mmap.put("goodsStandardList", goodsStandardList);
        return PREFIX + "/goodsStandard";
    }

    @ResponseBody
    @RequestMapping("list")
    public TableDataInfo list(Long goodsId) {
        startPage();
        List<GoodsStandard> list = goodsStandardService.selectByGoodsId(goodsId);
        return getDataTable(list);
    }

    /**
     * 全量保存
     */
    @ResponseBody
    @PostMapping("save")
    public AjaxResult save(@RequestBody List<GoodsStandard> goodsStandardList) {
        return toAjax(goodsStandardService.save(goodsStandardList));
    }

    /**
     * 根据部门商品分类ID获取商品列表
     */
    @ResponseBody
    @GetMapping("/goods/by-dept-category/{deptGoodsCategoryId}")
    public AjaxResult getGoodsByDeptCategoryId(@PathVariable("deptGoodsCategoryId") Long deptGoodsCategoryId) {
        List<Goods> goodsList = goodsStandardService.selectGoodsByDeptGoodsCategoryId(deptGoodsCategoryId);
        return AjaxResult.success(goodsList);
    }

}
