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

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.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.goods.example.GoodsBindRankExample;
import com.slodon.b2b2c.goods.example.GoodsRankCategoryExample;
import com.slodon.b2b2c.goods.example.GoodsRankExample;
import com.slodon.b2b2c.goods.pojo.Goods;
import com.slodon.b2b2c.goods.pojo.GoodsBindRank;
import com.slodon.b2b2c.goods.pojo.GoodsRank;
import com.slodon.b2b2c.goods.pojo.GoodsRankCategory;
import com.slodon.b2b2c.model.goods.GoodsBindRankModel;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.model.goods.GoodsRankCategoryModel;
import com.slodon.b2b2c.model.goods.GoodsRankModel;
import com.slodon.b2b2c.vo.goods.FrontGoodsRankListVO;
import com.slodon.b2b2c.vo.goods.FrontGoodsRankVO;
import com.slodon.b2b2c.vo.goods.GoodsRankCategoryVO;
import com.slodon.b2b2c.vo.goods.RankRecommendVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
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.List;

@Api(tags = "front-商品榜单")
@RestController
@Slf4j
@RequestMapping("v3/goods/front/goods/rank")
public class FrontGoodsRankController extends BaseController {

    @Resource
    private GoodsRankModel goodsRankModel;
    @Resource
    private GoodsBindRankModel goodsBindRankModel;
    @Resource
    private GoodsRankCategoryModel goodsRankCategoryModel;
    @Resource
    private GoodsModel goodsModel;

    @ApiOperation("榜单分类列表")
    @GetMapping("categoryList")
    public JsonResult<List<GoodsRankCategoryVO>> categoryList(HttpServletRequest request) {
        GoodsRankCategoryExample example = new GoodsRankCategoryExample();
        example.setState(GoodsRankConst.STATE_1);
        example.setInUse("notNull");
        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("榜单聚合页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "分类id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "typeId", value = "榜单类型id", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<FrontGoodsRankListVO> list(HttpServletRequest request, Integer categoryId, Integer typeId) {
        AssertUtil.notNullOrZero(categoryId, "分类不能为空，请重试！");
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        List<RankRecommendVO> rankVOS = new ArrayList<>();
        List<FrontGoodsRankListVO.RankTypeVO> typeList = new ArrayList<>();

        //查询属性
        String fields = "rank_type";
        //查询该分类下的榜单类型
        GoodsRankExample example = new GoodsRankExample();
        example.setCategoryId(categoryId);
        example.setState(GoodsRankConst.STATE_1);
        example.setGroupBy(fields);
        example.setOrderBy("rank_type ASC");
        List<GoodsRank> list = goodsRankModel.getGoodsRankListFields(fields, example);
        if (CollectionUtils.isEmpty(list)) {
            return SldResponse.success(new FrontGoodsRankListVO(rankVOS, typeList, pager));
        } else {
            list.forEach(goodsRank -> {
                FrontGoodsRankListVO.RankTypeVO typeVO = new FrontGoodsRankListVO.RankTypeVO();
                typeVO.setTypeId(goodsRank.getRankType());
                typeVO.setTypeName(getRankType(goodsRank.getRankType()));
                typeList.add(typeVO);
            });
        }
        //查询该分类下的榜单
        if (!StringUtil.isNullOrZero(typeId)) {
            example.setRankType(typeId);
        } else {
            example.setRankType(typeList.get(0).getTypeId());
        }
        example.setGroupBy(null);
        example.setOrderBy("sort asc, create_time desc");
        List<GoodsRank> rankList = goodsRankModel.getGoodsRankList(example, pager);
        if (!CollectionUtils.isEmpty(rankList)) {
            PagerInfo pagerInfo = new PagerInfo(3, 1);
            rankList.forEach(goodsRank -> {
                RankRecommendVO vo = new RankRecommendVO(goodsRank);
                //查询榜单绑定商品
                GoodsBindRankExample goodsBindRankExample = new GoodsBindRankExample();
                goodsBindRankExample.setRankId(goodsRank.getRankId());
                goodsBindRankExample.setOrderBy("goods_rank ASC");
                List<GoodsBindRank> goodsBindRankList = goodsBindRankModel.getGoodsBindRankList(goodsBindRankExample, pagerInfo);
                List<RankRecommendVO.RankRecommendGoodsVO> goodsList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(goodsBindRankList)) {
                    goodsBindRankList.forEach(goodsBindRank -> {
                        Goods goods = goodsModel.getGoodsByGoodsId(goodsBindRank.getGoodsId());
                        AssertUtil.notNull(goods, "商品不存在");
                        RankRecommendVO.RankRecommendGoodsVO goodsVO = new RankRecommendVO.RankRecommendGoodsVO(goods);
                        goodsList.add(goodsVO);
                    });
                }
                vo.setGoodsList(goodsList);
                rankVOS.add(vo);
            });
        }
        return SldResponse.success(new FrontGoodsRankListVO(rankVOS, typeList, pager));
    }

    @ApiOperation("榜单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rankId", value = "榜单id", required = true, paramType = "query")
    })
    @GetMapping("detail")
    public JsonResult<FrontGoodsRankVO> detail(HttpServletRequest request, Integer rankId) {
        //查询榜单信息
        GoodsRank goodsRank = goodsRankModel.getGoodsRankByRankId(rankId);
        AssertUtil.notNull(goodsRank, "榜单不存在");
        //榜单未启用直接返回
        if (goodsRank.getState() == GoodsRankConst.STATE_0) {
            return SldResponse.success();
        }

        FrontGoodsRankVO vo = new FrontGoodsRankVO(goodsRank);
        //查询榜单绑定商品
        GoodsBindRankExample example = new GoodsBindRankExample();
        example.setRankId(rankId);
        example.setOrderBy("goods_rank ASC");
        List<GoodsBindRank> list = goodsBindRankModel.getGoodsBindRankList(example, null);
        List<FrontGoodsRankVO.FrontRankGoodsVO> goodsList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(goodsBindRank -> {
                Goods goods = goodsModel.getGoodsByGoodsId(goodsBindRank.getGoodsId());
                AssertUtil.notNull(goods, "商品不存在");
                FrontGoodsRankVO.FrontRankGoodsVO goodsVO = new FrontGoodsRankVO.FrontRankGoodsVO(goods);
                goodsVO.setRankReason(goodsBindRank.getRankReason());
                goodsList.add(goodsVO);
            });
        }
        vo.setGoodsList(goodsList);
        return SldResponse.success(vo);
    }

    @ApiOperation("更多榜单推荐")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rankId", value = "榜单id", required = true, paramType = "query")
    })
    @GetMapping("moreRankRecommend")
    public JsonResult<List<RankRecommendVO>> moreRankRecommend(HttpServletRequest request, Integer rankId) {
        //查询榜单信息
        GoodsRank goodsRankDb = goodsRankModel.getGoodsRankByRankId(rankId);
        AssertUtil.notNull(goodsRankDb, "榜单不存在");

        List<RankRecommendVO> vos = new ArrayList<>();
        PagerInfo pager = new PagerInfo(3, 1);
        GoodsRankExample example = new GoodsRankExample();
        example.setCategoryId(goodsRankDb.getCategoryId());
        example.setRankIdNotEquals(rankId);
        example.setState(GoodsRankConst.STATE_1);
        example.setOrderBy("sort asc, create_time desc");
        List<GoodsRank> list = goodsRankModel.getGoodsRankList(example, pager);
        if (CollectionUtils.isEmpty(list)) {
            return SldResponse.success(vos);
        }

        list.forEach(goodsRank -> {
            RankRecommendVO vo = new RankRecommendVO(goodsRank);
            //查询榜单绑定商品
            GoodsBindRankExample goodsBindRankExample = new GoodsBindRankExample();
            goodsBindRankExample.setRankId(goodsRank.getRankId());
            goodsBindRankExample.setOrderBy("goods_rank ASC");
            List<GoodsBindRank> goodsBindRankList = goodsBindRankModel.getGoodsBindRankList(goodsBindRankExample, pager);
            List<RankRecommendVO.RankRecommendGoodsVO> goodsList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(goodsBindRankList)) {
                goodsBindRankList.forEach(goodsBindRank -> {
                    Goods goods = goodsModel.getGoodsByGoodsId(goodsBindRank.getGoodsId());
                    AssertUtil.notNull(goods, "商品不存在");
                    RankRecommendVO.RankRecommendGoodsVO goodsVO = new RankRecommendVO.RankRecommendGoodsVO(goods);
                    goodsList.add(goodsVO);
                });
            }
            vo.setGoodsList(goodsList);
            vos.add(vo);
        });
        return SldResponse.success(vos);
    }

    /**
     * 根据类型获取对应名称
     *
     * @param rankType
     * @return
     */
    private String getRankType(Integer rankType) {
        String value = "好物榜";
        if (StringUtils.isEmpty(rankType)) {
            return value;
        }
        switch (rankType) {
            case GoodsRankConst.RANK_TYPE_1:
                value = "畅销榜";
                break;
            case GoodsRankConst.RANK_TYPE_2:
                value = "好评榜";
                break;
            case GoodsRankConst.RANK_TYPE_3:
                value = "新品榜";
                break;
            case GoodsRankConst.RANK_TYPE_4:
                value = "好物榜";
                break;
            default:
                break;
        }
        return value;
    }
}
