package com.slodon.b2b2c.controller.goods.admin;

import com.slodon.b2b2c.aop.OperationLogger;
import com.slodon.b2b2c.core.constant.GoodsRankConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.goods.example.GoodsRankCategoryExample;
import com.slodon.b2b2c.goods.example.GoodsRankExample;
import com.slodon.b2b2c.goods.pojo.GoodsRankCategory;
import com.slodon.b2b2c.model.goods.GoodsRankCategoryModel;
import com.slodon.b2b2c.model.goods.GoodsRankModel;
import com.slodon.b2b2c.system.pojo.Admin;
import com.slodon.b2b2c.vo.goods.GoodsRankCategoryListVO;
import com.slodon.b2b2c.vo.goods.GoodsRankCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "admin-榜单分类")
@RestController
@RequestMapping("v3/goods/admin/goods/rank/category")
public class AdminGoodsRankCategoryController extends BaseController {

    @Resource
    private GoodsRankCategoryModel goodsRankCategoryModel;
    @Resource
    private GoodsRankModel goodsRankModel;

    @ApiOperation("榜单分类列表(全部)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryName", value = "分类名称", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<GoodsRankCategoryListVO>> list(HttpServletRequest request, String categoryName) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        GoodsRankCategoryExample example = new GoodsRankCategoryExample();
        example.setCategoryNameLike(categoryName);
        List<GoodsRankCategory> list = goodsRankCategoryModel.getGoodsRankCategoryList(example, pager);
        List<GoodsRankCategoryListVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(category -> {
                GoodsRankCategoryListVO vo = new GoodsRankCategoryListVO(category);
                //查询关联榜单数量
                GoodsRankExample rankExample = new GoodsRankExample();
                rankExample.setCategoryId(category.getCategoryId());
                vo.setRankNum(goodsRankModel.getGoodsRankCount(rankExample));
                vos.add(vo);
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("榜单分类列表")
    @GetMapping("categoryList")
    public JsonResult<List<GoodsRankCategoryVO>> categoryList(HttpServletRequest request) {
        GoodsRankCategoryExample example = new GoodsRankCategoryExample();
        example.setState(GoodsRankConst.STATE_1);
        example.setOrderBy("sort asc, create_time desc");
        List<GoodsRankCategory> list = goodsRankCategoryModel.getGoodsRankCategoryList(example, null);
        List<GoodsRankCategoryVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(category -> {
                vos.add(new GoodsRankCategoryVO(category));
            });
        }
        return SldResponse.success(vos);
    }

    @ApiOperation("新增榜单分类")
    @OperationLogger(option = "新增榜单分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryName", value = "分类名称", required = true),
            @ApiImplicitParam(name = "sort", value = "排序", required = true)
    })
    @PostMapping("add")
    public JsonResult addGoodsRankCategory(HttpServletRequest request, String categoryName, Integer sort) {
        AssertUtil.sortCheck(sort);

        Admin admin = UserUtil.getUser(request, Admin.class);

        //分类名称不可重复
        GoodsRankCategoryExample example = new GoodsRankCategoryExample();
        example.setCategoryName(categoryName);
        List<GoodsRankCategory> list = goodsRankCategoryModel.getGoodsRankCategoryList(example, null);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(list), "榜单分类名称不可重复");

        GoodsRankCategory category = new GoodsRankCategory();
        category.setCategoryName(categoryName);
        category.setSort(sort);
        category.setCreateAdminId(admin.getAdminId());
        category.setCreateTime(new Date());
        goodsRankCategoryModel.saveGoodsRankCategory(category);
        return SldResponse.success("操作成功");
    }

    @ApiOperation("编辑榜单分类")
    @OperationLogger(option = "编辑榜单分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "分类id", required = true),
            @ApiImplicitParam(name = "categoryName", value = "分类名称", required = true),
            @ApiImplicitParam(name = "sort", value = "排序", required = true)
    })
    @PostMapping("update")
    public JsonResult updateGoodsRankCategory(HttpServletRequest request, Integer categoryId, String categoryName, Integer sort) {
        AssertUtil.sortCheck(sort);

        Admin admin = UserUtil.getUser(request, Admin.class);
        //分类名称不可重复
        GoodsRankCategoryExample example = new GoodsRankCategoryExample();
        example.setCategoryName(categoryName);
        example.setCategoryIdNotEquals(categoryId);
        List<GoodsRankCategory> list = goodsRankCategoryModel.getGoodsRankCategoryList(example, null);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(list), "榜单分类名称不可重复");

        GoodsRankCategory category = new GoodsRankCategory();
        category.setCategoryId(categoryId);
        category.setCategoryName(categoryName);
        category.setSort(sort);
        category.setUpdateAdminId(admin.getAdminId());
        category.setUpdateTime(new Date());
        goodsRankCategoryModel.updateGoodsRankCategory(category);
        return SldResponse.success("操作成功");
    }

    @ApiOperation("是否启用榜单分类")
    @OperationLogger(option = "是否启用榜单分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "分类id", required = true),
            @ApiImplicitParam(name = "isEnable", value = "是否启用，false-不启用，true-启用", required = true)
    })
    @PostMapping("isEnable")
    public JsonResult isEnable(HttpServletRequest request, Integer categoryId, boolean isEnable) {
        Admin admin = UserUtil.getUser(request, Admin.class);

        if (!isEnable) {
            //使用中的分类不可以停用
            GoodsRankExample example = new GoodsRankExample();
            example.setCategoryId(categoryId);
            Integer count = goodsRankModel.getGoodsRankCount(example);
            AssertUtil.isTrue(count > 0, "当前分类已关联了榜单，暂不可停用");
        }

        GoodsRankCategory category = new GoodsRankCategory();
        category.setCategoryId(categoryId);
        if (isEnable) {
            category.setState(GoodsRankConst.STATE_1);
        } else {
            category.setState(GoodsRankConst.STATE_0);
        }
        category.setUpdateAdminId(admin.getAdminId());
        category.setUpdateTime(new Date());
        goodsRankCategoryModel.updateGoodsRankCategory(category);
        return SldResponse.success("操作成功");
    }

    @ApiOperation("删除榜单分类")
    @OperationLogger(option = "删除榜单分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "分类id", required = true)
    })
    @PostMapping("del")
    public JsonResult delSpellLabel(HttpServletRequest request, Integer categoryId) {
        //使用中的分类不可以删除
        GoodsRankExample example = new GoodsRankExample();
        example.setCategoryId(categoryId);
        Integer count = goodsRankModel.getGoodsRankCount(example);
        AssertUtil.isTrue(count > 0, "当前分类已关联了榜单，暂不可删除");

        goodsRankCategoryModel.deleteGoodsRankCategory(categoryId);
        return SldResponse.success("删除成功");
    }
}
