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

import com.slodon.b2b2c.aop.OperationLogger;
import com.slodon.b2b2c.core.constant.VideoConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
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.AuthorModel;
import com.slodon.b2b2c.model.video.VideoSettingModel;
import com.slodon.b2b2c.video.example.AuthorExample;
import com.slodon.b2b2c.video.pojo.Author;
import com.slodon.b2b2c.video.pojo.VideoSetting;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
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.LinkedList;
import java.util.List;
import java.util.Map;

@Api(tags = "admin-视频设置")
@RestController
@RequestMapping("v3/video/admin/video/setting")
public class AdminVideoSettingController extends BaseController {

    @Resource
    private VideoSettingModel videoSettingModel;
    @Resource
    private AuthorModel authorModel;

    @ApiOperation("获取短视频设置信息")
    @GetMapping("getSettingList")
    public JsonResult<List<VideoSetting>> getSettingList() {

        String str = "video_switch,video_audit_switch,member_review_switch,member_bind_goods,member_level,bind_goods_num";

        List<VideoSetting> list = getSetting(str);
        AssertUtil.notNull(list, "请输入正确的参数");
        return SldResponse.success(list);
    }

    @ApiOperation("获取直播设置信息")
    @GetMapping("getLiveSettingList")
    public JsonResult<List<VideoSetting>> getLiveSettingList() {

        String str = "live_switch,live_bind_goods_num,live_notice";

        List<VideoSetting> list = getSetting(str);
        AssertUtil.notNull(list, "请输入正确的参数");
        return SldResponse.success(list);
    }

    @ApiOperation("批量更新后台参数配置")
    @OperationLogger(option = "批量更新后台参数配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "要修改的name名称", paramType = "query"),
            @ApiImplicitParam(name = "value", value = "要修改的value值", paramType = "query")
    })
    @PostMapping("updateSettingList")
    public JsonResult updateSettingList(HttpServletRequest request) {
        Map<String, String> postMap = WebUtil.handlerPostMap(request);
        if (null == postMap) {
            return SldResponse.badArgument();
        }

        for (Map.Entry<String, String> entry : postMap.entrySet()) {
            if (entry.getKey().length() == 0) {
                continue;
            }
            VideoSetting setting = new VideoSetting();
            setting.setName(entry.getKey());
            setting.setValue(entry.getValue());
            videoSettingModel.updateVideoSetting(setting);
            //发布权限审核开关关闭后之前默认的以及待审核的具备发布权限
            if ("member_review_switch".equals(entry.getKey()) && "0".equals(entry.getValue())) {
                AuthorExample example = new AuthorExample();
                example.setPermissionStateIn(VideoConst.PERMISSION_STATE_0 + "," + VideoConst.PERMISSION_STATE_1);
                Author author = new Author();
                author.setPermissionState(VideoConst.PERMISSION_STATE_2);
                author.setUpdateTime(new Date());
                authorModel.updateAuthorByExample(author, example);
            }
        }
        return SldResponse.success("更新成功");
    }

    /**
     * 获取Setting参数
     *
     * @param str 以逗号分割要查询的键值name
     * @return
     */
    private List<VideoSetting> getSetting(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        List<VideoSetting> list = new LinkedList<>();
        String[] split = str.split(",");
        for (String s : split) {
            if (StringUtils.isEmpty(s)) {
                continue;
            }
            VideoSetting setting = videoSettingModel.getVideoSettingBySettingName(s);
            if (setting == null) {
                continue;
            }
            list.add(setting);
        }
        return list;
    }

}
