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.commons.lang.util.StringUtil;
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 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.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;

    /***
     * @author zhangfuxin
     * @Description:  商品列表
     * @date 2022/1/4 下午2:46
     */
    @GetMapping("goodsList")
    @ApiOperation(value = "商品列表")
    @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);
        return AjaxResult.success(list);
    }
    
    /***
     * @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("update")
    @ApiOperation(value = "精选商品修改")
    @ResponseBody
    public AjaxResult update( @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());
    }





}
