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

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.dto.SqbPerfGoodsBindItemParam;
import com.liquidnet.client.admin.zhengzai.goblin.service.ISqbPerformanceGoodsService;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.dto.vo.GoblinAdminSqbGoodsVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbPerfGoodsVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbPerfListRespVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 演出-收钱吧商品关联管理
 */
@Slf4j
@Controller
@Api(tags = "收钱吧-演出商品关联管理")
@RequestMapping("sqb/performance/goods")
public class SqbPerformanceGoodsController extends BaseController {

    private final String prefix = "zhengzai/goblin/sqbGoods";

    @Autowired
    private ISqbPerformanceGoodsService sqbPerformanceGoodsService;

    /**
     * 关联商品管理页面（iframe 内嵌）
     */
    @GetMapping("page/{performancesId}")
    public String sqbGoodsPage(@PathVariable("performancesId") String performancesId, ModelMap mmap) {
        mmap.put("performancesId", performancesId);
        return prefix + "/index";
    }

    /**
     * 搜索可选商品（skuType=33 的收钱吧商品）
     */
    @GetMapping("search")
    @ResponseBody
    @ApiOperation("搜索收钱吧候选商品")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "query", dataType = "String", name = "keyword", value = "关键词", required = false),
    })
    public AjaxResult searchGoods(@RequestParam(value = "keyword", required = false) String keyword) {
        ResponseDto<List<GoblinAdminSqbGoodsVo>> resp = sqbPerformanceGoodsService.searchGoods(keyword);
        if (resp.isSuccess()) {
            return AjaxResult.success(resp.getData());
        }
        return AjaxResult.error(resp.getMessage());
    }

    /**
     * 批量绑定/保存关联商品配置
     */
    @PostMapping("bind")
    @ResponseBody
    @ApiOperation("批量绑定演出与商品（含换购价、演出级自动下架全局配置）")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "query", required = true, dataType = "String", name = "performancesId", value = "演出ID"),
            @ApiImplicitParam(type = "query", required = false, dataType = "int", name = "autoOffline", value = "演出结束后不在前台列表展示 0-否 1-是（不取消关联、不改商品上下架）"),
    })
    public AjaxResult bind(@RequestParam("performancesId") String performancesId,
                           @RequestParam(value = "autoOffline", required = false, defaultValue = "0") Integer autoOffline,
                           @RequestBody List<SqbPerfGoodsBindItemParam> items) {
        ResponseDto<Boolean> resp = sqbPerformanceGoodsService.bind(performancesId, items, autoOffline);
        if (resp.isSuccess()) {
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.error(resp.getMessage());
    }

    /**
     * 解除关联
     */
    @PostMapping("unbind")
    @ResponseBody
    @ApiOperation("解除演出与商品关联")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "performancesId", value = "演出ID"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "skuId", value = "SKU ID"),
    })
    public AjaxResult unbind(@RequestParam("performancesId") String performancesId,
                             @RequestParam("skuId") String skuId) {
        ResponseDto<Boolean> resp = sqbPerformanceGoodsService.unbind(performancesId, skuId);
        if (resp.isSuccess()) {
            return AjaxResult.success("已取消关联");
        }
        return AjaxResult.error(resp.getMessage());
    }

    /**
     * 查询演出已关联商品列表
     */
    @GetMapping("list")
    @ResponseBody
    @ApiOperation("查询演出关联商品列表（含全局配置）")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "query", required = true, dataType = "String", name = "performancesId", value = "演出ID"),
    })
    public AjaxResult list(@RequestParam("performancesId") String performancesId) {
        ResponseDto<GoblinSqbPerfListRespVo> resp = sqbPerformanceGoodsService.list(performancesId);
        if (resp.isSuccess()) {
            return AjaxResult.success(resp.getData());
        }
        return AjaxResult.error(resp.getMessage());
    }
}
