package com.org.hktshop.controller.product;

import com.org.hktshop.Interface.product.IHktGoodsCategoryAttrService;
import com.org.hktshop.Interface.product.IHktGoodsCategoryService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.admin.HktAdmin;
import com.org.hktshop.entity.product.HktGoodsCategory;
import com.org.hktshop.entity.product.HktGoodsCategoryAttr;
import com.org.hktshop.entity.vo.HktGoodsCategoryVo;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.Ret;
import com.org.hktshop.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/***
 * 商品分类接口
 *
 */
@RestController
@Api(description = "商品分类接口")
public class HktGoodsCategoryController extends BaseController {
    @Autowired
    IHktGoodsCategoryService categoryService;
    @Autowired
    IHktGoodsCategoryAttrService categoryAttrService;

    /****
     * 新增/修改 分类
     * @param category
     * @param request
     * @return
     */
    @ApiOperation(value = "新增/修改 分类", notes = "新增/修改 分类", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "HktGoodsCategory", value = "分类对象")
    })
    @PostMapping("/async/goodsCategory/updateCategory")
    public Object updateGoodsCategory(@RequestBody HktGoodsCategory category, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = categoryService.updateCategory(admin, category);
            if (ret.isFail()) return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @ApiOperation(value = "删除分类", notes = "删除分类", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类id")
    })
    @PostMapping("/async/goodsCategory/deleteCategory")
    public Object deleteGoodsCategory(@RequestBody HktGoodsCategory category, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = categoryService.deleteCategory(admin, category);
            if (ret.isFail()) return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    /****
     * 查询 产品分类列表/分页 内容
     * @param category
     * @param request
     * @return
     */
    @ApiOperation(value = "查询商品分类列表", notes = "查询商品分类列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "查询类别")
    })
    @PostMapping("/async/goodsCategory/queryList")
    public Object queryList(@RequestBody HktGoodsCategory category, HttpServletRequest request) {
        try {
            if (!StringUtils.isNull(category.getLevel()) && category.getLevel() > 0) {

                Page<HktGoodsCategory> categoryPage = categoryService.queryPage(category);
                return BaseResult.successWithData(categoryPage);
            } else {
                List<HktGoodsCategoryVo> voList = categoryService.queryCategoryVo(category);
                return BaseResult.successWithData(voList);
            }
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    /****
     * 更新商品分类的属性
     * @param categoryAttrs
     * @param request
     * @return
     */
    @ApiOperation(value = "更新商品分类的属性", notes = "更新商品分类的属性", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryAttrs", value = "商品分类属性")
    })
    @PostMapping("/async/goodsCategoryAttr/updateAttr")
    public Object updateGoodsCategoryAttr(@RequestBody HktGoodsCategoryAttr categoryAttrs, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = categoryAttrService.updateCategoryAttr(admin, categoryAttrs);
            if (ret.isFail()) return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            logger.error("心中商品分類屬性錯誤 error={}", ex.toString());
            ex.printStackTrace();
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    /****
     * 更新商品分类的属性
     * @param categoryAttrs
     * @param request
     * @return
     */
    @ApiOperation(value = "删除属性", notes = "删除属性", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "需要删除的属性id")
    })
    @PostMapping("/async/goodsCategoryAttr/deleteAttr")
    public Object deleteGoodsCategoryAttr(@RequestBody HktGoodsCategoryAttr categoryAttrs, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = categoryAttrService.deleteCategoryAttr(admin, categoryAttrs);
            if (ret.isFail()) return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    /****
     * 查询分类属性列表
     * @param categoryAttr
     * @param request
     * @return
     */
    @ApiOperation(value = "参数列表查询", notes = "参数列表查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "HktGoodsCategoryAttr", value = "参数列表对象")
    })
    @PostMapping("/async/goodsCategoryAttr/queryAttr")
    public Object queryGoodsCategoryAttr(@RequestBody HktGoodsCategoryAttr categoryAttr, HttpServletRequest request) {
        try {
            List<HktGoodsCategoryAttr> ret = categoryAttrService.queryLists(categoryAttr);
            return BaseResult.successWithData(ret);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }



    /****
     * 修改分類排序
     * @param category
     * @param request
     * @return
     */
    @ApiOperation(value = "修改分類排序", notes = "修改分類排序", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "HktGoodsCategory", value = "修改分類排序")
    })
    @PostMapping("/async/goodsCategory/updateSort")
    public Object updateSort(@RequestBody HktGoodsCategory category) {
        try {
            Ret ret = categoryService.updateSort(category);
            if (ret.isFail()) return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

}
