package com.ghy.web.controller.goods;

import com.ghy.common.annotation.Log;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.ghy.common.enums.BusinessType;
import com.ghy.common.utils.ShiroUtils;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.goods.domain.DeptGoodsCategory;
import com.ghy.goods.domain.Goods;
import com.ghy.goods.service.DeptGoodsCategoryService;
import com.ghy.goods.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

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

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

    @Resource
    private GoodsService goodsService;
    @Resource
    private DeptGoodsCategoryService deptGoodsCategoryService;

    @RequiresPermissions("goods:goods:view")
    @GetMapping()
    public String goods() {
        return PREFIX + "/goods";
    }

//    @RequiresPermissions("goods:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Goods goods) {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("goods:goods:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Goods goods) {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        return util.exportExcel(list, "商品数据");
    }

    @RequiresPermissions("goods:goods:remove")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{ids}")
    @ResponseBody
    public AjaxResult remove(@PathVariable String ids) {
        try {
            return toAjax(goodsService.deleteGoodsByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        Long parentId = ShiroUtils.getSysUser().getParentId();
        DeptGoodsCategory category = new DeptGoodsCategory();
        category.setDeptId(parentId);
        mmap.put("deptGoodsCategories", deptGoodsCategoryService.list(category));
        return PREFIX + "/add";
    }

    /**
     * 新增保存商品
     */
    @RequiresPermissions("goods:goods:add")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Goods goods) {
        goods.setCreateBy(getLoginName());
        goods.setDeptId(getSysUser().getDeptId());
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("goods:goods:edit")
    @GetMapping("/edit/{goodsId}")
    public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap) {
        Long parentId = ShiroUtils.getSysUser().getParentId();
        DeptGoodsCategory category = new DeptGoodsCategory();
        category.setDeptId(parentId);
        mmap.put("deptGoodsCategories", deptGoodsCategoryService.list(category));
        mmap.put("goods", goodsService.selectById(goodsId));
        return PREFIX + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("goods:goods:edit")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Goods goods) {
        goods.setUpdateBy(getLoginName());
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 校验商品名称
     */
    @PostMapping("/checkGoodsNameUnique")
    @ResponseBody
    public String checkGoodsNameUnique(Goods goods) {
        return goodsService.checkGoodsNameUnique(goods);
    }

    /**
     * 校验商品编码
     */
    @PostMapping("/checkGoodsCodeUnique")
    @ResponseBody
    public String checkGoodsCodeUnique(Goods goods) {
        return goodsService.checkGoodsCodeUnique(goods);
    }

}
