package com.yunzhupaas.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import com.yunzhupaas.base.controller.SuperController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.service.ModuleDataAuthorizeSchemeService;
import com.yunzhupaas.base.vo.ListVO;
import com.yunzhupaas.base.entity.ModuleDataAuthorizeSchemeEntity;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.exception.DataException;
import com.yunzhupaas.base.model.moduledataauthorizescheme.DataAuthorizeSchemeCrForm;
import com.yunzhupaas.base.model.moduledataauthorizescheme.DataAuthorizeSchemeInfoVO;
import com.yunzhupaas.base.model.moduledataauthorizescheme.DataAuthorizeSchemeListVO;
import com.yunzhupaas.base.model.moduledataauthorizescheme.DataAuthorizeSchemeUpForm;
import com.yunzhupaas.permission.service.AuthorizeService;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.JsonUtilEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据权限方案
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date 2023/09/27
 */
@Tag(name = "数据权限方案", description = "ModuleDataAuthorizeScheme")
@RestController
@RequestMapping("/api/system/ModuleDataAuthorizeScheme")
public class ModuleDataAuthorizeSchemeController extends SuperController<ModuleDataAuthorizeSchemeService, ModuleDataAuthorizeSchemeEntity> {

    @Autowired
    private ModuleDataAuthorizeSchemeService schemeService;

    @Autowired
    private AuthorizeService authorizeService;

    /**
     * 列表
     *
     * @param moduleId 功能主键
     * @return ignore
     */
    @Operation(summary = "方案列表")
    @Parameters({
            @Parameter(name = "moduleId", description = "功能主键", required = true)
    })
    @GetMapping("/{moduleId}/List")
    public ActionResult<ListVO<DataAuthorizeSchemeListVO>> list(@PathVariable("moduleId") String moduleId) {
        List<ModuleDataAuthorizeSchemeEntity> data = schemeService.getList(moduleId);
        List<DataAuthorizeSchemeListVO> list = JsonUtil.getJsonToList(data, DataAuthorizeSchemeListVO.class);
        ListVO<DataAuthorizeSchemeListVO> vo = new ListVO<>();
        vo.setList(list);
        return ActionResult.success(vo);
    }

    /**
     * 信息
     *
     * @param id 主键值
     * @return ignore
     * @throws DataException ignore
     */
    @Operation(summary = "获取方案信息")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @SaCheckPermission("system.menu")
    @GetMapping("/{id}")
    public ActionResult<DataAuthorizeSchemeInfoVO> info(@PathVariable("id") String id) throws DataException {
        ModuleDataAuthorizeSchemeEntity entity = schemeService.getInfo(id);
        DataAuthorizeSchemeInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, DataAuthorizeSchemeInfoVO.class);
        return ActionResult.success(vo);
    }

    /**
     * 新建
     *
     * @param dataAuthorizeSchemeCrForm 实体对象
     * @return ignore
     */
    @Operation(summary = "新建方案")
    @Parameters({
            @Parameter(name = "dataAuthorizeSchemeCrForm", description = "实体对象", required = true)
    })
    @SaCheckPermission("system.menu")
    @PostMapping
    public ActionResult create(@RequestBody @Valid DataAuthorizeSchemeCrForm dataAuthorizeSchemeCrForm) {
        ModuleDataAuthorizeSchemeEntity entity = JsonUtil.getJsonToBean(dataAuthorizeSchemeCrForm, ModuleDataAuthorizeSchemeEntity.class);
        // 判断fullName是否重复
        if (schemeService.isExistByFullName(entity.getId(), entity.getFullName(), entity.getModuleId())) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        // 判断encode是否重复
        if (schemeService.isExistByEnCode(entity.getId(), entity.getEnCode(), entity.getModuleId())) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        schemeService.create(entity);
        return ActionResult.success(MsgCode.SU001.get());
    }

    /**
     * 更新
     *
     * @param id                        主键值
     * @param dataAuthorizeSchemeUpForm 实体对象
     * @return ignore
     */
    @Operation(summary = "更新方案")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true),
            @Parameter(name = "dataAuthorizeSchemeUpForm", description = "实体对象", required = true)
    })
    @SaCheckPermission("system.menu")
    @PutMapping("/{id}")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid DataAuthorizeSchemeUpForm dataAuthorizeSchemeUpForm) {
        ModuleDataAuthorizeSchemeEntity entity = JsonUtil.getJsonToBean(dataAuthorizeSchemeUpForm, ModuleDataAuthorizeSchemeEntity.class);
        // 判断encode是否重复
        if ("1".equals(String.valueOf(entity.getAllData()))) {
            return ActionResult.fail(MsgCode.SYS021.get());
        }
        // 判断fullName是否重复
        if (schemeService.isExistByFullName(id, entity.getFullName(), entity.getModuleId())) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        // 判断encode是否重复
        if (schemeService.isExistByEnCode(id, entity.getEnCode(), entity.getModuleId())) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        boolean flag = schemeService.update(id, entity);
        if (!flag) {
            return ActionResult.success(MsgCode.FA002.get());
        }
        authorizeService.removeAuthByUserOrMenu(null, new ArrayList(){{add(dataAuthorizeSchemeUpForm.getModuleId());}});
        return ActionResult.success(MsgCode.SU004.get());
    }

    /**
     * 删除
     *
     * @param id 主键值
     * @return ignore
     */
    @Operation(summary = "删除方案")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @SaCheckPermission("system.menu")
    @DeleteMapping("/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        ModuleDataAuthorizeSchemeEntity entity = schemeService.getInfo(id);
        if (entity != null) {
            schemeService.delete(entity);
            return ActionResult.success(MsgCode.SU003.get());
        }
        return ActionResult.fail(MsgCode.FA003.get());
    }

}

