package com.platform.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.annotation.IgnoreAuth;
import com.platform.entity.ApiTmLiveRoomInfoVo;
import com.platform.entity.LiveRoomInfoVo;
import com.platform.entity.Result;
import com.platform.entity.WxLiveRoomGoodsVo;
import com.platform.service.custom.ApiLiveRoomInfoService;
import com.platform.utils.CommonDataUtils;
import com.platform.utils.ResourceUtil;
import com.platform.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author abnerhou
 * @date 2020/9/15 18:17
 * @desciption
 */
@Api(tags = "小程序获取直播列表")
@RestController
@RequestMapping("/api/liveShow")
public class ApiTmLiveShowController {


    @Autowired
    private ApiLiveRoomInfoService apiLiveRoomInfoService;


    @ApiOperation(value = "获取直播列表")
    @IgnoreAuth
    @GetMapping("getRoomList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "key", value = "搜索关键字名称", paramType = "query", required = true, dataType = "String")
    })
    public Result getLiveRoomList(String key , @RequestParam(name = "pageNum") long pageNum ,
                                  @RequestParam(name = "pageSize") long pageSize){

        IPage<ApiTmLiveRoomInfoVo> resultPage = new Page<>();
        resultPage  = apiLiveRoomInfoService.getOrQuery(key, pageNum , pageSize);
        if(resultPage != null){
            List<ApiTmLiveRoomInfoVo> liveRoomInfoVoList = resultPage.getRecords();
            List<LiveRoomInfoVo> resultLiveRoomList = new ArrayList<>();
            WxLiveRoomGoodsVo wxLiveRoomGoodsVo = new WxLiveRoomGoodsVo();
            wxLiveRoomGoodsVo.setCoverImg(ResourceUtil.getConfigByName("goods.cover.image"));
            wxLiveRoomGoodsVo.setName(ResourceUtil.getConfigByName("goods.name"));
            wxLiveRoomGoodsVo.setPrice(new BigDecimal(ResourceUtil.getConfigByName("goods.price")));
            wxLiveRoomGoodsVo.setUrl(ResourceUtil.getConfigByName("goods.buy.url"));
            wxLiveRoomGoodsVo.setGoodsId(new Integer(ResourceUtil.getConfigByName("goods.id")));
            List<WxLiveRoomGoodsVo> wxLiveRoomGoodsVoList = new ArrayList<>();
            wxLiveRoomGoodsVoList.add(wxLiveRoomGoodsVo);
            if(!CollectionUtils.isEmpty(liveRoomInfoVoList)){
                for(ApiTmLiveRoomInfoVo liveRoomInfoVo : liveRoomInfoVoList){
                    LiveRoomInfoVo newLiveRoomInfo = new LiveRoomInfoVo();
                    BeanUtils.copyProperties(liveRoomInfoVo , newLiveRoomInfo);
                    newLiveRoomInfo.setLiveRoomGoodsVos(wxLiveRoomGoodsVoList);
                    resultLiveRoomList.add(newLiveRoomInfo);
                }
            }
            return Result.success(CommonDataUtils.assembleResultCustom(resultPage.getTotal() , resultPage.getPages() ,
                    resultPage.getCurrent() ,pageSize , resultLiveRoomList));
        }
        return Result.success();
    }

    public static  void  main(String[] args){
        System.out.println(ResourceUtil.getConfigByName("goods.price"));
        System.out.println(new BigDecimal(0));
    }

    @ApiOperation(value = "获取直播间详情")
    @IgnoreAuth
    @GetMapping("getRoomDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "liveId", value = "直播间流水Id", paramType = "query", required = true, dataType = "String"),
    })
    public Result getRoomDetail(@RequestParam(name = "liveId") String liveId){

        if(StringUtils.isNullOrEmpty(liveId)){
            return Result.failure("直播间流水Id不能为空");
        }
        ApiTmLiveRoomInfoVo liveRoomInfoVo = apiLiveRoomInfoService.getRoomById(liveId);
        if(null != liveRoomInfoVo){
            WxLiveRoomGoodsVo wxLiveRoomGoodsVo = new WxLiveRoomGoodsVo();
            wxLiveRoomGoodsVo.setCoverImg("https://4dkk.4dage.com/shop/huafa/20200915/17145240908b6e.jpg");
            wxLiveRoomGoodsVo.setName("钻石世家 18K金钻石戒指");
            wxLiveRoomGoodsVo.setPrice(new BigDecimal(4290.00));
            wxLiveRoomGoodsVo.setUrl("https://item.jd.com/32579640875.html");
            wxLiveRoomGoodsVo.setGoodsId(1181354);
            List<WxLiveRoomGoodsVo> wxLiveRoomGoodsVoList = new ArrayList<>();
            wxLiveRoomGoodsVoList.add(wxLiveRoomGoodsVo);
            LiveRoomInfoVo newLiveRoomInfo = new LiveRoomInfoVo();
            BeanUtils.copyProperties(liveRoomInfoVo , newLiveRoomInfo);
            newLiveRoomInfo.setLiveRoomGoodsVos(wxLiveRoomGoodsVoList);
        }
        return Result.success(liveRoomInfoVo);
    }

}
