package com.cweb.controller.group;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsStoreProductCategory;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.group.BsStoreGroupService;
import com.hfkj.service.group.BsStoreProductCategoryService;
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.HashMap;
import java.util.Map;

/**
 * @className: BsStoreProductCategoryController
 * @author: HuRui
 * @date: 2023/11/28
 **/
@Controller
@Api(value = "门店产品分类")
@RequestMapping(value = "/storeProductCategory")
public class BsStoreProductCategoryController {

    private static Logger log = LoggerFactory.getLogger(BsStoreProductCategoryController.class);

    @Resource
    private BsStoreProductCategoryService storeProductCategoryService;
    @Resource
    private BsStoreGroupService storeGroupService;
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/editCategory",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑分类")
    public ResponseData editCategory(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("storeId") == null || StringUtils.isBlank(body.getString("name"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            BsStoreProductCategory category = null;
            if (body.getLong("id") != null) {
                // 查询分类
                category = storeProductCategoryService.getCategory(body.getLong("id"));
                category.setName(body.getString("name"));
            } else {
                category = new BsStoreProductCategory();
                category.setStoreId(body.getLong("storeId"));
                category.setName(body.getString("name"));
                category.setSort(storeProductCategoryService.getMaxSort(category.getStoreId()) + 1);
                category.setOpId(userInfoModel.getSecUser().getId());
                category.setOpName(userInfoModel.getSecUser().getUserName());
                category.setStatus(1);
            }
            storeProductCategoryService.editData(category);

            // 刷新缓存
            storeProductCategoryService.getStoreCategoryCache(category.getStoreId(), true);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/delCategory",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除分类")
    public ResponseData delCategory(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("id") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            storeProductCategoryService.delCategory(body.getLong("id"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/sortUp",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "向上排序")
    public ResponseData sortUp(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("id") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            storeProductCategoryService.sortUp(body.getLong("id"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/sortDown",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "向下排序")
    public ResponseData sortDown(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("id") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            storeProductCategoryService.sortDown(body.getLong("id"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getCategory",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询分类详情")
    public ResponseData getCategory(@RequestParam(value = "id" , required = true) Long id) {
        try {

            return ResponseMsgUtil.success( storeProductCategoryService.getCategory(id));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getCategoryList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询分类列表")
    public ResponseData getCategoryList(@RequestParam(value = "storeId" , required = true) Long storeId,
                                        @RequestParam(value = "name" , required = false) String name,
                                        @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                        @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {

            Map<String,Object> param = new HashMap<>();
            param.put("storeId", storeId);
            param.put("name", name);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(storeProductCategoryService.getCategoryList(param)));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getALLCategory",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询所有分类")
    public ResponseData getALLCategory(@RequestParam(value = "storeId" , required = true) Long storeId,
                                       @RequestParam(value = "name" , required = false) String name) {
        try {

            Map<String,Object> param = new HashMap<>();
            param.put("storeId", storeId);
            param.put("name", name);

            return ResponseMsgUtil.success(storeProductCategoryService.getCategoryList(param));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getStoreProduct",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "【菜单】查询门店分类及产品")
    public ResponseData getStoreProduct(@RequestParam(value = "storeId" , required = true) Long storeId) {
        try {

            return ResponseMsgUtil.success(storeProductCategoryService.getStoreCategoryCache(storeId,false));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/search",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "【菜单】搜索")
    public ResponseData search(@RequestParam(value = "storeId" , required = true) Long storeId,
                               @RequestParam(value = "name" , required = true) String name,
                               @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                               @RequestParam(value = "pageSize" , required = true) Integer pageSize) {

        try {

            Map<String,Object> param = new HashMap<>();
            param.put("storeId", storeId);
            param.put("name", name);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(storeGroupService.getStoreProductByList(param)));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
