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.domain.Ztree;
import com.ghy.common.core.text.Convert;
import com.ghy.common.enums.BusinessType;
import com.ghy.common.utils.ExceptionUtil;
import com.ghy.common.utils.ShiroUtils;
import com.ghy.goods.domain.DeptGoodsCategory;
import com.ghy.goods.service.DeptGoodsCategoryService;
import com.ghy.web.pojo.vo.AppCategoryRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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/deptcategory")
public class GoodsDeptCategoryController extends BaseController {

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

    @Resource
    DeptGoodsCategoryService deptGoodsCategoryService;

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


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

    @RequiresPermissions("goods:deptcategory:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(String menuIds) {
        Long parentId = ShiroUtils.getSysUser().getDept().getParentId();
        Long[] categoryIds = Convert.toLongArray(menuIds);
        return deptGoodsCategoryService.batchAdd(parentId, categoryIds);
    }

    @PostMapping("/listByStep")
    @ResponseBody
    public AjaxResult listByStep(@RequestBody DeptGoodsCategory deptGoodsCategory) {
        try {
            return AjaxResult.success(deptGoodsCategoryService.listByStep(deptGoodsCategory));
        }catch (Exception e){
            logger.error(e.getMessage());
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }

    }

    @RequiresPermissions("goods:deptcategory:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(id);
        mmap.put("deptGoodsCategory", deptGoodsCategory);
        return PREFIX + "/edit";
    }

    @RequiresPermissions("goods:deptcategory:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(DeptGoodsCategory category) {
        category.setUpdateBy(getLoginName());
        deptGoodsCategoryService.edit(category);
        return AjaxResult.success();
    }

    /**
     * 商品类别表
     */
    @RequiresPermissions("goods:deptcategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<DeptGoodsCategory> list(DeptGoodsCategory category) {
        Long parentId = ShiroUtils.getSysUser().getDept().getParentId();
        category.setDeptId(parentId);
        return deptGoodsCategoryService.list(category);
    }

    @GetMapping("/tree")
    @ResponseBody
    public List<Ztree> goodsDeptCategoryTree() {
        Long parentId = ShiroUtils.getSysUser().getDept().getParentId();
        return deptGoodsCategoryService.tree(parentId);
    }

    @PostMapping("/app/list")
    @ResponseBody
    public AjaxResult appList(@RequestBody DeptGoodsCategory deptGoodsCategory) {
        return AjaxResult.success(deptGoodsCategoryService.appList(deptGoodsCategory.getDeptId()));
    }

    @PostMapping("/app/category")
    @ResponseBody
    public AjaxResult appCategory(@RequestBody AppCategoryRequest request){
        try {

            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除子公司商品类别接口
     */
    @RequiresPermissions("goods:deptcategory:remove")
    @Log(title = "子公司商品类别管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{ids}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("ids") String ids) {
        try {
            return toAjax(deptGoodsCategoryService.delete(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
