package com.dgtly.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.dgtly.common.annotation.ApiNoCheckSign;
import com.dgtly.common.annotation.ApiPassToken;
import com.dgtly.common.core.controller.ApiBaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.core.domain.ParameterObject;
import com.dgtly.common.core.domain.ResultType;
import com.dgtly.common.exception.api.LessParamException;
import com.dgtly.goods.domain.GoodsInfo;
import com.dgtly.goods.service.IGoodsInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Example;
import io.swagger.annotations.ExampleProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/goods/info")
@ApiPassToken/* 不用验证token  方法上可用*/
//@ApiNoCheckSign/* 不用验证 签名 方法上可用*/
public class GoodsInfoController extends ApiBaseController {

    @Autowired
    private IGoodsInfoService goodsInfoService;


    @ApiOperation(value = "商品列表分页")
    @ApiImplicitParam(name = "params" , paramType = "body")
    @PostMapping("/page")
    public Object getPasgeList(){
        ParameterObject obj =  getParameterObject();
        GoodsInfo gi = obj.parseBean(GoodsInfo.class);
        if(gi==null){
            gi = new GoodsInfo();
        }
        if(obj.contaonsKey("orderByColumn")){
            if(!obj.contaonsKey("isAsc")){
                throw new LessParamException("orderByColumn字段和isAsc字段必须同时存在");
            }
        }
        startPage(obj);/*向分页传递 分页参数*/
        List<GoodsInfo> goodsInfos =  goodsInfoService.selectGoodsInfoListInWeb(gi);
        return AjaxResult.success(getDataTable(goodsInfos));
    }


    @ApiOperation(value = "热门商品列表")
    @ApiImplicitParam(name = "params" , paramType = "body")
    @PostMapping("/hotlist")
    public Object getHotList(){
        List<GoodsInfo> goodsInfos =  goodsInfoService.selectGoodsInfoHotList();
        return AjaxResult.success().putKV("list",goodsInfos);
    }

    @ApiOperation(value = "商品详情")
    @ApiImplicitParam(name = "params" , paramType = "body")
    @PostMapping("/details")
    public Object detail(){
        ParameterObject obj =  getParameterObject();
        obj.checkParameterNotNull("id");
        Long id = obj.getLong("id");
        GoodsInfo goodsInfo =  goodsInfoService.selectGoodsInfoById(id);
        if(goodsInfo!=null){
            return AjaxResult.success(goodsInfo);
        }else{
            return AjaxResult.error(ResultType.NUll);
        }
    }


    /**
     * @descption: 统计平台商品入驻数
     * @param:
     * @return:
     * @auther: LiuLingChao
     * @date: 2020-03-11 11:18
     */
    @PostMapping("/countGoodsInfo")
    public Object countGoodsInfo(){
        int goodsNum = goodsInfoService.countGoodsInfo();
        return AjaxResult.success().putKV("goodsNum",goodsNum);
    }
}
