package com.zhongshu.card.server.core.controller.scene;

import com.github.microservice.auth.client.constant.AuthConstant;
import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.scene.ProjectBindSceneParam;
import com.zhongshu.card.client.model.scene.ProjectSceneInfoModel;
import com.zhongshu.card.client.model.scene.ProjectSceneInfoSearch;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.service.scene.ProjectSceneInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author TRX
 * @date 2024/6/5
 */
@RestController
@RequestMapping("/projectSceneInfo")
@Tag(name = "场景--项目场景管理")
public class ProjectSceneInfoController {

    @Autowired
    private ProjectSceneInfoService projectSceneInfoService;

    @ResourceAuth(value = "projectSceneManager", type = AuthType.Project, remark = "项目场景_应用场景管理_168_Menu")
    @ResourceAuth(value = "projectSceneManager", type = AuthType.School, remark = "项目场景_应用场景管理_168_Menu")
    @ResourceAuth(value = "projectSceneManager", type = AuthType.BusinessMain, remark = "项目场景_应用场景管理_168_Menu")
    @ResourceAuth(value = "projectSceneManager", type = AuthType.CommonOrg, remark = "项目场景_应用场景管理_168_Menu")
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.Project, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.School, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.BusinessMain, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.CommonOrg, remark = "")
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "项目场景列表-分页查询", description = "项目场景列表-分页查询")
    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
    public ResultContent<Page<ProjectSceneInfoModel>> page(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) ProjectSceneInfoSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        return projectSceneInfoService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "绑定场景", description = "绑定场景")
    @RequestMapping(value = "bindScene", method = {RequestMethod.POST})
    public ResultContent bindScene(@RequestBody @Valid ProjectBindSceneParam param) {
        return this.projectSceneInfoService.bindScene(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "绑定场景(多个)", description = "绑定场景")
    @RequestMapping(value = "bindScenes", method = {RequestMethod.POST})
    public ResultContent bindScenes(@RequestBody @Valid ProjectBindSceneParam param) {
        return this.projectSceneInfoService.bindScenes(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "根据场景数据ID 场景详情", description = "根据场景数据ID 场景详情")
    @RequestMapping(value = "getDetailById", method = {RequestMethod.POST})
    public ResultContent<ProjectSceneInfoModel> getDetailById(@RequestBody IDParam param) {
        return this.projectSceneInfoService.getDetailById(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除场景", description = "删除场景")
    @RequestMapping(value = "deleteById", method = {RequestMethod.POST})
    public ResultContent deleteById(@RequestBody IDParam param) {
        return this.projectSceneInfoService.deleteById(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "禁用场景", description = "禁用场景")
    @RequestMapping(value = "disableData", method = {RequestMethod.POST})
    public ResultContent disableData(@RequestBody IDParam param) {
        return this.projectSceneInfoService.changeState(param.getId(), DataState.Disable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "启用场景", description = "启用场景")
    @RequestMapping(value = "enableById", method = {RequestMethod.POST})
    public ResultContent enableById(@RequestBody IDParam param) {
        return this.projectSceneInfoService.changeState(param.getId(), DataState.Enable);
    }

}
