package com.liquidnet.client.admin.web.controller.zhengzai.goblin;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.zhengzai.goblin.service.impl.GoblinFrontSelectGoodsServiceImpl;
import com.liquidnet.common.cache.redis.util.RedisGoblinUtil;
import com.liquidnet.commons.lang.util.StringUtil;
import com.liquidnet.service.goblin.constant.GoblinRedisConst;
import com.liquidnet.service.goblin.entity.GoblinFrontSelectGoods;
import com.liquidnet.service.goblin.entity.GoblinGoods;
import com.liquidnet.service.goblin.mapper.GoblinGoodsMapper;
import com.liquidnet.service.goblin.param.GoblinFrontSelectGoodsParam;
import com.liquidnet.service.goblin.param.GoblinGoodsParamz;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 精选商品
 前端控制器
 * </p>
 *
 * @author liquidnet
 * @since 2022-01-04
 */
@Api(tags = "精选商品")
@RestController
@RequestMapping("/selectGoods")
public class GoblinFrontSelectGoodsController extends BaseController {

    @Autowired
    private GoblinGoodsMapper goblinGoodsMapper;
    @Autowired
    private GoblinFrontSelectGoodsServiceImpl goblinFrontSelectGoodsService;
    @Autowired
    private RedisGoblinUtil redisGoblinUtil;

    /***
     * @author zhangfuxin
     * @Description:  商品列表
     * @date 2022/1/4 下午2:46
     */
    @GetMapping("goodsList")
    @ApiOperation(value = "商品列表，type（1、上架时间2、销量3、价格高到低4、价格低到高）")
    @ResponseBody
    public AjaxResult goodsList(@RequestParam(name = "type", required = false) String type) {
        LambdaQueryWrapper<GoblinGoods> queryWrapper = Wrappers.lambdaQuery(GoblinGoods.class);
        queryWrapper.eq(GoblinGoods::getShelvesStatus,3);
        queryWrapper.eq(GoblinGoods::getDelFlg,0);
        //1、上架时间2、销量3、价格高到低4、价格低到高
        if(!StringUtil.isNotBlank(type)||type.equals("1")){
            queryWrapper.orderByDesc(GoblinGoods::getCreatedAt);
        }else if(type.equals("2")){
            //销量 暂时不考虑

        }else if(type.equals("3")){
            //价格从高到低
            queryWrapper.orderByDesc(GoblinGoods::getPriceGe);
        }else{
            queryWrapper.orderByAsc(GoblinGoods::getPriceGe);
        }
        List<GoblinGoods> list=goblinGoodsMapper.selectList(queryWrapper);
        List<GoblinGoodsParamz> list1=new ArrayList<>();
        if(null!=list&&list.size()>0){
            for(GoblinGoods goblins:list){
                GoblinGoodsParamz goblinGoodsParamz=GoblinGoodsParamz.getNew();
                BeanUtils.copyProperties(goblins,goblinGoodsParamz);
                //Integer counts=red.getSpuSaleCount(goblinGoodsInfoVo.getSpuId());
                Object obj =redisGoblinUtil.get(GoblinRedisConst.REDIS_GOBLIN_SALE_SPU_COUNT.concat(goblins.getSpuId()));
                if(null==obj){
                    goblinGoodsParamz.setSaleCount(0);
                }else{
                    goblinGoodsParamz.setSaleCount((int)obj);
                }
                list1.add(goblinGoodsParamz);
            }
            if(type.equals("2")){
                Collections.sort(list1, new Comparator<GoblinGoodsParamz>() {
                    public int compare(GoblinGoodsParamz arg0, GoblinGoodsParamz arg1) {
                        return -(arg0.getSaleCount().compareTo(arg1.getSaleCount()));
                    }});
            }
        }
        return AjaxResult.success(list1);
    }
    
    /***
     * @author zhangfuxin
     * @Description:精选商品保存
     * @date 2022/1/4 下午2:49
     */
    @PostMapping("create")
    @ApiOperation(value = "精选商品保存")
    @ResponseBody
    public AjaxResult create(@RequestBody GoblinFrontSelectGoodsParam goblinFrontSelectGoodsParam) {
        GoblinFrontSelectGoods goblinFrontSelectGoods =new GoblinFrontSelectGoods();
        BeanUtils.copyProperties(goblinFrontSelectGoodsParam,goblinFrontSelectGoods);
        logger.info("精选商品保存{}", JSON.toJSONString(goblinFrontSelectGoods));
        boolean result=goblinFrontSelectGoodsService.create(goblinFrontSelectGoods);
        if (result ) {
            return success("操作成功");
        } else {
            return error("操作失败");
        }
    }
    
    /**
     * @author zhangfuxin
     * @Description:精选商品修改
     * @date 2022/1/4 下午2:49
     */
    @PostMapping("createOrUpdate")
    @ApiOperation(value = "精选商品修改或者增加")
    @ResponseBody
    public AjaxResult createOrUpdate( @RequestBody GoblinFrontSelectGoodsParam goblinFrontSelectGoodsParam) {
        GoblinFrontSelectGoods goblinFrontSelectGoods =new GoblinFrontSelectGoods();
        BeanUtils.copyProperties(goblinFrontSelectGoodsParam,goblinFrontSelectGoods);
        logger.info("精选商品修改{}", JSON.toJSONString(goblinFrontSelectGoods));
        boolean result=goblinFrontSelectGoodsService.update(goblinFrontSelectGoods);
        if (result ) {
            return success("操作成功");
        } else {
            return error("操作失败");
        }
    }


    
    /**
     * @author zhangfuxin
     * @Description: 精选商品list
     * @date 2022/1/4 下午2:49
     */
    @PostMapping("getList")
    @ApiOperation(value = "精选商品list")
    @ResponseBody
    public  AjaxResult getList(){
        return AjaxResult.success(goblinFrontSelectGoodsService.getList());
    }





}
