package com.schisandra.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.schisandra.auth.application.convert.SchisandraAuthRoleDTOConverter;
import com.schisandra.auth.application.dto.SchisandraAuthRoleDTO;
import com.schisandra.auth.common.entity.Result;
import com.schisandra.auth.domain.bo.SchisandraAuthRoleBO;
import com.schisandra.auth.domain.service.SchisandraAuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * ClassName:SchisandraAuthRoleController
 * Package:com.schisandra.auth.application.controller
 * Description:AuthRoleController层
 *
 * @Author:fanyang
 * @Create:2024/4/4 - 20:47
 * @Version: v1.0
 */
@RestController
@RequestMapping("/role/")
@Slf4j
public class SchisandraAuthRoleController {
    @Resource
    private SchisandraAuthRoleDomainService schisandraAuthRoleDomainService;

    /**
     * @description 更新角色信息
     * @param schisandraAuthRoleDTO
     * @return
     * @author fanyang
     * @date 2024/4/3 22:05
     */
    @PostMapping("update")
    public Object update(@RequestBody SchisandraAuthRoleDTO schisandraAuthRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.update.dto:{}", JSON.toJSONString(schisandraAuthRoleDTO));
            }
            checkRoleInfo(schisandraAuthRoleDTO);
            SchisandraAuthRoleBO authRoleBO = SchisandraAuthRoleDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthRoleDTO);
            return Result.ok(schisandraAuthRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新角色信息失败");
        }
    }
    /**
     * @description 查询角色信息
     * @param schisandraAuthRoleDTO
     * @return
     * @author fanyang
     * @date 2024/4/3 22:05
     */
    @PostMapping("query")
    public Result query(@RequestBody SchisandraAuthRoleDTO schisandraAuthRoleDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("RoleController.select.dto:{}", JSON.toJSONString(schisandraAuthRoleDTO));
            }
            checkRoleInfo(schisandraAuthRoleDTO);
            SchisandraAuthRoleBO authRoleBO = SchisandraAuthRoleDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthRoleDTO);
            return Result.ok(schisandraAuthRoleDomainService.select(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.select.error:{}", e.getMessage(), e);
            return Result.fail("查询角色信息失败");
        }
    }
    /**
     * @description 添加角色信息
     * @param schisandraAuthRoleDTO
     * @return
     * @author fanyang
     * @date 2024/4/3 22:12
     */
    @PostMapping("insert")
    public Result insert(@RequestBody SchisandraAuthRoleDTO schisandraAuthRoleDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("RoleController.insert.dto:{}", JSON.toJSONString(schisandraAuthRoleDTO));
            }
            checkRoleInfo(schisandraAuthRoleDTO);
            SchisandraAuthRoleBO authRoleBO = SchisandraAuthRoleDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthRoleDTO);
            return Result.ok(schisandraAuthRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.insert.error:{}", e.getMessage(), e);
            return Result.fail("添加角色信息失败");
        }
    }
    /**
     * @description 删除角色信息
     * @param id
     * @return
     * @author fanyang
     * @date 2024/4/3 22:12
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Long id){
        try{
            if(log.isInfoEnabled()){
                log.info("RoleController.insert.dto:{}",id);
            }
            return Result.ok(schisandraAuthRoleDomainService.delete(id));
        } catch (Exception e) {
            log.error("RoleController.insert.error:{}", e.getMessage(), e);
            return Result.fail("删除角色信息失败");
        }
    }
    private void checkRoleInfo(@RequestBody SchisandraAuthRoleDTO schisandraAuthRoleDTO) {
//        Preconditions.checkArgument(!StringUtils.isBlank(schisandraAuthRoleDTO.getRoleName()), "角色名不能为空");
//        Preconditions.checkArgument(!StringUtils.isBlank(schisandraAuthRoleDTO.getRoleKey()), "角色key值不能为空");
    }
}
