package com.slodon.b2b2c.controller.video.admin;

import com.slodon.b2b2c.aop.OperationLogger;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.model.video.SvideoLabelModel;
import com.slodon.b2b2c.model.video.SvideoThemeModel;
import com.slodon.b2b2c.model.video.SvideoVideoModel;
import com.slodon.b2b2c.video.example.SvideoLabelExample;
import com.slodon.b2b2c.video.example.SvideoThemeExample;
import com.slodon.b2b2c.video.example.SvideoVideoExample;
import com.slodon.b2b2c.video.pojo.SvideoLabel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Api(tags = "admin-短视频标签管理")
@RestController
@RequestMapping("v3/video/admin/video/label")
public class AdminLabelController extends BaseController {

    @Resource
    private SvideoLabelModel svideoLabelModel;
    @Resource
    private SvideoThemeModel svideoThemeModel;
    @Resource
    private SvideoVideoModel svideoVideoModel;

    @ApiOperation("标签列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelName", value = "标签名称", paramType = "query"),
            @ApiImplicitParam(name = "isShow", value = "是否显示，1-显示；0-不显示", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<SvideoLabel>> list(HttpServletRequest request, String labelName, Integer isShow) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoLabelExample example = new SvideoLabelExample();
        example.setLabelNameLike(labelName);
        example.setIsShow(isShow);
        List<SvideoLabel> list = svideoLabelModel.getSvideoLabelList(example, pager);
        return SldResponse.success(new PageVO<>(list, pager));
    }

    @ApiOperation("新增标签")
    @OperationLogger(option = "新增标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelName", value = "标签名称", required = true),
            @ApiImplicitParam(name = "sort", value = "排序", required = true)
    })
    @PostMapping("add")
    public JsonResult add(HttpServletRequest request, String labelName, Integer sort) {
        AssertUtil.sortCheck(sort);

        //标签名称不可重复
        SvideoLabelExample example = new SvideoLabelExample();
        example.setLabelName(labelName);
        List<SvideoLabel> list = svideoLabelModel.getSvideoLabelList(example, null);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(list), "标签名称不可重复");

        SvideoLabel label = new SvideoLabel();
        label.setLabelName(labelName);
        label.setSort(sort);
        label.setCreateTime(new Date());
        svideoLabelModel.saveSvideoLabel(label);
        return SldResponse.success("新增成功");
    }

    @ApiOperation("修改标签")
    @OperationLogger(option = "修改标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId", value = "标签id", required = true),
            @ApiImplicitParam(name = "labelName", value = "标签名称", required = true),
            @ApiImplicitParam(name = "sort", value = "排序", required = true)
    })
    @PostMapping("update")
    public JsonResult update(HttpServletRequest request, Integer labelId, String labelName, Integer sort) {
        AssertUtil.sortCheck(sort);

        //标签名称不可重复
        SvideoLabelExample example = new SvideoLabelExample();
        example.setLabelName(labelName);
        example.setLabelIdNotEquals(labelId);
        List<SvideoLabel> list = svideoLabelModel.getSvideoLabelList(example, null);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(list), "标签名称不可重复");

        SvideoLabel label = new SvideoLabel();
        label.setLabelId(labelId);
        label.setLabelName(labelName);
        label.setSort(sort);
        label.setUpdateTime(new Date());
        svideoLabelModel.updateSvideoLabel(label);
        return SldResponse.success("修改成功");
    }

    @ApiOperation("删除标签")
    @OperationLogger(option = "删除标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId", value = "标签id", required = true)
    })
    @PostMapping("del")
    public JsonResult del(HttpServletRequest request, Integer labelId) {
        //查询推荐主题否正在使用
        SvideoThemeExample example = new SvideoThemeExample();
        example.setLabelId(labelId);
        Integer count = svideoThemeModel.getSvideoThemeCount(example);
        AssertUtil.isTrue(count > 0, "该标签正在使用，不能删除");
        //查询短视频是否正在使用
        SvideoVideoExample videoExample = new SvideoVideoExample();
        videoExample.setLabelId(labelId);
        count = svideoVideoModel.getSvideoVideoCount(videoExample);
        AssertUtil.isTrue(count > 0, "该标签正在使用，不能删除");

        svideoLabelModel.deleteSvideoLabel(labelId);
        return SldResponse.success("删除成功");
    }

    @ApiOperation("是否显示标签")
    @OperationLogger(option = "是否显示标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId", value = "标签id", required = true),
            @ApiImplicitParam(name = "isShow", value = "是否显示，0为不显示，1为显示", required = true)
    })
    @PostMapping("isShow")
    public JsonResult isShow(HttpServletRequest request, Integer labelId, Integer isShow) {
        SvideoLabel label = new SvideoLabel();
        label.setLabelId(labelId);
        label.setIsShow(isShow);
        label.setUpdateTime(new Date());
        svideoLabelModel.updateSvideoLabel(label);
        return SldResponse.success("修改成功");
    }
}
