package com.kmall.admin.controller;

import com.kmall.admin.dto.CopyCategoryDto;
import com.kmall.admin.dto.StoreDto;
import com.kmall.admin.dto.StoreIdDto;
import com.kmall.admin.entity.CategoryEntity;
import com.kmall.admin.service.CategoryService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.common.constant.JxlsXmlTemplateName;
import com.kmall.common.utils.*;
import com.kmall.common.utils.excel.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author Scott
 * @email
 * @date 2017-08-21 15:32:31
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ExcelUtil excelUtil;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("category:list")
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        ParamUtils.setName(params, "categoryName");
        //查询列表数据
        Query query = new Query(params);

        List<CategoryEntity> categoryList = categoryService.queryList(query);
        int total = categoryService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(categoryList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("category:info")
    public R info(@PathVariable("id") Integer id) {
        CategoryEntity category = categoryService.queryObject(id);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.update(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("category:delete")
    public R delete(@RequestBody Integer[] ids) {
        categoryService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        ParamUtils.setName(params, "categoryName");

        List<CategoryEntity> list = categoryService.queryList(params);
        //添加顶级菜单
//        CategoryEntity root = new CategoryEntity();
//        root.setId(0);
//        root.setName("一级分类");
//        root.setParentId(-1);
//        root.setOpen(true);
//        list.add(root);
        return R.ok().put("list", list);
    }

    /**
     * 查看信息(全部加载页面渲染太慢！)
     */
    @RequestMapping("/getAreaTree")
    public R getAreaTree() {

        Map<String, Object> map = new HashMap<>();
        ParamUtils.setQueryPowerByRoleType(map, "storeId", "merchSn", "thirdPartyMerchCode");

        List<CategoryEntity> list = categoryService.queryList(map);
        for (CategoryEntity sysRegionEntity : list) {
            sysRegionEntity.setValue(sysRegionEntity.getId() + "");
            sysRegionEntity.setLabel(sysRegionEntity.getName());
        }
        List<CategoryEntity> node = TreeUtils.factorTree(list);

        return R.ok().put("node", node);
    }

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping("/getCategorySelect")
    public R getCategorySelect(@RequestParam Map<String, Object> map) {
        map.put("parentId", "0");
        ParamUtils.setQueryPowerByRoleType(map, "storeId", "merchSn", "thirdPartyMerchCode");
        List<CategoryEntity> list = categoryService.queryList(map);
        return R.ok().put("list", list);
    }

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping("/getCategorySelectByParent")
    public R getCategorySelectByParent(@RequestParam Map<String, Object> map) {
        String parentId = map.get("parentId").toString();
        List<CategoryEntity> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(parentId)) {
            ParamUtils.setQueryPowerByRoleType(map, "storeId", "merchSn", "thirdPartyMerchCode");

            list = categoryService.queryList(map);
        }

        return R.ok().put("list", list);
    }

    @RequestMapping("/query")
    public R query(@RequestParam Map<String, Object> params) {

        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");

        params.put("isShow", 1);
        List<CategoryEntity> list = categoryService.queryList(params);
        //添加顶级菜单
        CategoryEntity root = new CategoryEntity();
        root.setId(0);
        root.setName("一级分类");
        root.setParentId(-1);
        root.setOpen(true);
        list.add(root);
        return R.ok().put("list", list);
    }

    @RequestMapping("/getCategory")
    public R getCategory(@RequestParam Map<String, Object> map) {
        ParamUtils.setQueryPowerByRoleType(map, "storeId", "merchSn", "thirdPartyMerchCode");

        List<CategoryEntity> list = categoryService.queryList(map);
        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/saveCopyCategory")
//    @RequiresPermissions("category:saveCopyCategory")
    public R saveCopyCategory(@RequestBody CopyCategoryDto copyCategoryDto) {
        categoryService.saveCopyCategory(copyCategoryDto);
        return R.ok();
    }
}
