package com.dgtly.goods.controller;

import java.util.ArrayList;
import java.util.List;

import com.dgtly.common.constant.UserConstants;
import com.dgtly.common.core.domain.Ztree;
import com.dgtly.common.exception.BusinessException;
import com.dgtly.common.utils.StringUtils;
import com.dgtly.framework.util.ShiroUtils;
import com.dgtly.goods.domain.GoodsInfo;
import com.dgtly.goods.service.IGoodsInfoService;
import com.dgtly.system.domain.SysDept;
import com.dgtly.system.domain.SysMenu;
import com.dgtly.system.domain.SysUser;
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.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;
import com.dgtly.common.annotation.Log;
import com.dgtly.common.enums.BusinessType;
import com.dgtly.goods.domain.GoodsType;
import com.dgtly.goods.service.IGoodsTypeService;
import com.dgtly.common.core.controller.BaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.utils.poi.ExcelUtil;
import com.dgtly.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 商品分类列Controller
 * 
 * @author dgtly
 * @date 2020-02-13
 */
@Controller
@RequestMapping("/goods/type")
public class GoodsTypeController extends BaseController
{
    private String prefix = "goods/type";

    @Autowired
    private IGoodsTypeService goodsTypeService;

    @Autowired
    private IGoodsInfoService goodsInfoService;



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

    /**
     * 查询商品分类列列表
     */
    @RequiresPermissions("goods:type:list")
    @PostMapping("/list")
    @ResponseBody
    public Object list(GoodsType goodsType)
    {
        List<GoodsType> list = goodsTypeService.selectGoodsTypeList(goodsType);
        return list;
    }
    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(GoodsType goodsType)
    {
        List<Ztree> ztrees = goodsTypeService.selectGoodsTypeTree(goodsType);
        return ztrees;
    }

    /**
     * 选择分类树
     */
    @GetMapping("/selectTypeTree/{typeId}")
    public String selectTypeTree(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        GoodsType gt= goodsTypeService.selectGoodsTypeById(typeId);
        if(gt==null){
            gt = new GoodsType();
            gt = goodsTypeService.selectGoodsTypeList(gt).get(0);
        }
        mmap.put("type",gt );
        return prefix + "/tree";
    }



    /**
     * 新增商品分类
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId,ModelMap mmap)
    {
        GoodsType type = null;
        if (0L != parentId)
        {
            type = goodsTypeService.selectGoodsTypeById(parentId);
        }
        else
        {
            type = new GoodsType();
            type.setId(0L);
            type.setName("根目录");
        }
        mmap.put("type", type);
        return prefix + "/add";
    }

    /**
     * 新增保存商品分类列
     */
    @RequiresPermissions("goods:type:add")
    @Log(title = "商品分类列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsType goodsType)
    {
        goodsType.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(goodsTypeService.insertGoodsType(goodsType));
    }

    /**
     * 修改商品分类列
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GoodsType goodsType = goodsTypeService.selectGoodsTypeById(id);
        mmap.put("goodsType", goodsType);
        if (goodsInfoService.selectGoodsInfoCountByTypeId(id) > 0)
        {
            mmap.put("hasGoodsInfo", true);
        }else{
            mmap.put("hasGoodsInfo", false);
        }

        return prefix + "/edit";
    }

    /**
     * 修改商品分类列
     */
    @PostMapping("/getInfo/{id}")
    @ResponseBody
    public Object getInfo(@PathVariable("id") Long id)
    {
        GoodsType goodsType = goodsTypeService.selectGoodsTypeById(id);
        return AjaxResult.success(goodsType);
    }

    /**
     * 修改保存商品分类列
     */
    @RequiresPermissions("goods:type:edit")
    @Log(title = "商品分类列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsType goodsType)
    {
        return toAjax(goodsTypeService.updateGoodsType(goodsType));
    }

    /**
     * 删除商品分类列
     */
    @RequiresPermissions("goods:type:remove")
    @Log(title = "商品分类列", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id")Long id)
    {
        GoodsType paramGoodsTyep = new GoodsType();
        paramGoodsTyep.setAncestors(id.toString());
        if (goodsTypeService.selectGoodsTypeCount(paramGoodsTyep) > 1)
        {
            throw new BusinessException("分类有子菜单，不允许删除");
        }

        if (goodsInfoService.selectGoodsInfoCountByTypeId(id) > 0)
        {
            throw new BusinessException("分类已有商品,不允许删除");
        }
        return toAjax(goodsTypeService.deleteGoodsTypeById(id));
    }


}
