package com.bweb.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.CmsCategory;
import com.hfkj.model.ResponseData;
import com.hfkj.service.CmsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "内容分类管理")
@RequestMapping(value = "/cmsCategory")
public class CmsCategoryController {

    private static Logger log = LoggerFactory.getLogger(CmsCategoryController.class);
    @Resource
    private CmsCategoryService cmsCategoryService;

    @RequestMapping(value = "/addCategory", method = RequestMethod.POST)
    @ApiOperation(value = "增加 分类")
    @ResponseBody
    public ResponseData addCategory(@RequestBody JSONObject jsonObject) {
        try {

            CmsCategory cmsCategory = jsonObject.getObject("category", CmsCategory.class);
            JSONArray jsonArray = jsonObject.getJSONArray("roles");
            Object[] roleArray = jsonArray.toArray();

            if (cmsCategory == null || roleArray == null || roleArray.length == 0
                    || StringUtils.isBlank(cmsCategory.getName())
                    || StringUtils.isBlank(cmsCategory.getCode())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<Integer> roleList = new ArrayList<>();
            for (Object object : roleArray) {
                roleList.add(Integer.valueOf(object.toString()));
            }

            if (cmsCategoryService.addCategory(cmsCategory, roleList) > 0) {
                return ResponseMsgUtil.success("添加数据成功");
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ADD_DATA_ERROR, "");
            }

        } catch (Exception e) {
            log.error("CmsCategoryController --> addCategory() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/updateCategory", method = RequestMethod.POST)
    @ApiOperation(value = "修改 内容分类")
    @ResponseBody
    public ResponseData updateCategory(@RequestBody JSONObject jsonObject) {
        try {

            CmsCategory cmsCategory = jsonObject.getObject("category", CmsCategory.class);
            JSONArray jsonArray = jsonObject.getJSONArray("roles");
            Object[] roleArray = jsonArray.toArray();

            if (cmsCategory == null || roleArray == null || roleArray.length == 0
                    || cmsCategory.getId() == null
                    || StringUtils.isBlank(cmsCategory.getName())
                    || StringUtils.isBlank(cmsCategory.getCode())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<Integer> roleList = new ArrayList<>();
            for (Object object : roleArray) {
                roleList.add(Integer.valueOf(object.toString()));
            }

            if (cmsCategoryService.updateCategory(cmsCategory, roleList) > 0) {
                return ResponseMsgUtil.success("修改数据成功");
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.UPDATE_DATA_ERROR, "");
            }

        } catch (Exception e) {
            log.error("CmsCategoryController --> updateCategory() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/delCategory", method = RequestMethod.GET)
    @ApiOperation(value = "删除 内容分类")
    @ResponseBody
    public ResponseData delCategory(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (cmsCategoryService.delCategory(id) > 0) {
                return ResponseMsgUtil.success("删除成功");
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.DELETE_DATA_ERROR, "");
            }
        } catch (Exception e) {
            log.error("CmsCategoryController --> updateCategory() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getCategoryById", method = RequestMethod.GET)
    @ApiOperation(value = "查询 分类详情")
    @ResponseBody
    public ResponseData getCategoryById(@RequestParam(value = "id", required = true) Long id) {
        try {
            return ResponseMsgUtil.success(cmsCategoryService.getCategoryById(id));
        } catch (Exception e) {
            log.error("CmsCategoryController --> getCategoryById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getCategoryTree", method = RequestMethod.GET)
    @ApiOperation(value = "获取分类树")
    @ResponseBody
    public ResponseData getCategoryTree(@RequestParam(value = "roleType", required = false) Integer roleType,
                                        @RequestParam(value = "parentCode", required = false) String parentCode) {
        try {
            Map<String, Object> paramMap = new HashMap<>();
            if (roleType != null) {
                paramMap.put("roleType", roleType);
            }
            if (StringUtils.isNotBlank(parentCode)) {
                paramMap.put("parentCode", parentCode);
            }

            return ResponseMsgUtil.success(cmsCategoryService.getCategoryTree(paramMap));
        } catch (Exception e) {
            log.error("CmsCategoryController --> getCategoryTree() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getOwnCategoryTree", method = RequestMethod.GET)
    @ApiOperation(value = "获取拥有的分类树")
    @ResponseBody
    public ResponseData getOwnCategoryTree(@RequestParam(value = "roleType", required = false) Integer roleType) {
        try {
            Map<String, Object> paramMap = new HashMap<>();
            if (roleType != null) {
                paramMap.put("roleType", roleType);
            }

            return ResponseMsgUtil.success(cmsCategoryService.getOwnCategoryTree(paramMap));
        } catch (Exception e) {
            log.error("CmsCategoryController --> getCategoryTree() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getRolesOfCategory", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询 分类角色列表")
    @ResponseBody
    public ResponseData getRolesOfCategory(@RequestParam(value = "id", required = false) Long id) {
        try {
            List<Integer> roleList = new ArrayList<>();
            if (id != null) {
                roleList = cmsCategoryService.getRolesOfCategory(id);
            } else {
                roleList.add(1);
            }

            return ResponseMsgUtil.success(roleList);
        } catch (Exception e) {
            log.error("CmsCategoryController --> getCategoryById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
