package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dto.LiveGoodsMapperDto;
import com.ym.mec.biz.dal.entity.LiveGoods;
import com.ym.mec.biz.dal.entity.LiveGoodsMapper;
import com.ym.mec.biz.dal.page.LiveGoodsMapperQueryInfo;
import com.ym.mec.biz.service.LiveGoodsMapperService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "直播商品关联管理")
@RequestMapping("liveGoodsMapper")
@RestController
public class LiveGoodsMapperController extends BaseController {

    @Autowired
    private LiveGoodsMapperService liveGoodsMapperService;

    @ApiOperation("分页查询直播间商品")
    @PostMapping("/page")
    @PreAuthorize("@pcs.hasPermissions('liveGoodsMapper/page')")
    public HttpResponseResult<PageInfo<LiveGoodsMapperDto>> page(LiveGoodsMapperQueryInfo queryInfo) {
        return succeed(liveGoodsMapperService.queryGoodsPage(queryInfo));
    }

    @ApiOperation("新增直播间商品关联")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('liveGoodsMapper/add')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "liveGoodsIds", value = "商品编号列表", required = true, dataType = "String"),
            @ApiImplicitParam(name = "liveId", value = "房间号", required = true,dataType = "String")
    })
    public HttpResponseResult add(String liveId, String liveGoodsIds) {
        liveGoodsMapperService.add(liveId,liveGoodsIds);
        return succeed();
    }

    @ApiOperation("获取直播间商品详情")
    @GetMapping(value = "/getGoodsDetail")
    @PreAuthorize("@pcs.hasPermissions('liveGoodsMapper/getGoodsDetail')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "liveGoodsId", value = "商品编号", required = true, dataType = "long"),
            @ApiImplicitParam(name = "liveId", value = "房间号", required = true,dataType = "String")
    })
    public HttpResponseResult<LiveGoodsMapperDto> getGoodsDetail(Integer liveGoodsId, String liveId) {
        return succeed(liveGoodsMapperService.getLiveGoodsList(liveId,liveGoodsId,null).get(0));
    }

    @ApiOperation("上架、下架直播间商品")
    @PostMapping("/updateStatus")
    @PreAuthorize("@pcs.hasPermissions('liveGoodsMapper/updateStatus')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "liveGoodsId", value = "商品编号", required = true, dataType = "long"),
            @ApiImplicitParam(name = "status", value = "状态：1-上架，0-下架", required = true,dataType = "boolean"),
            @ApiImplicitParam(name = "liveId", value = "房间号", required = true,dataType = "String")
    })
    public HttpResponseResult updateStatus(Integer liveGoodsId, String liveId, Boolean status) {
        if (liveGoodsId == null || status == null || StringUtils.isEmpty(liveId)) {
            throw new BizException("参数不能为空");
        }
        if (status) {
            liveGoodsMapperService.upGoods(liveGoodsId, liveId);
        }else {
            liveGoodsMapperService.downGoods(liveGoodsId, liveId);
        }
        return succeed();
    }
}
