package com.schisandra.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.schisandra.auth.application.convert.SchisandraAuthPermissionDTOConvert;
import com.schisandra.auth.application.dto.SchisandraAuthPermissionDTO;
import com.schisandra.auth.common.entity.Result;
import com.schisandra.auth.domain.bo.SchisandraAuthPermissionBO;
import com.schisandra.auth.domain.service.SchisandraAuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/authPermission/")
@Slf4j
public class SchisandraAuthPermissionController {
    @Resource
    private SchisandraAuthPermissionDomainService  schisandraAuthPermissionDomainService;

   /**
    * @description: 更新权限信息
    * @param: [schisandraAuthPermissionDTO]
    * @return: com.schisandra.auth.common.bo.Result
    * @author zhang
    * @date: 2024/4/17 16:44
    */
    @PostMapping("update")
    public Result update(@RequestBody SchisandraAuthPermissionDTO schisandraAuthPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("authPermissionController.update.dto:{}", JSON.toJSONString(schisandraAuthPermissionDTO));
            }
//            checkRoleInfo(schisandraAuthPermissionDTO);
            SchisandraAuthPermissionBO schisandraAuthPermissionBO = SchisandraAuthPermissionDTOConvert.INSTANCE.convertDTOToBO(schisandraAuthPermissionDTO);
            return Result.ok(schisandraAuthPermissionDomainService.update(schisandraAuthPermissionBO));
        } catch (Exception e) {
            log.error("authPermissionController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新权限信息失败");
        }
    }
    /**
     * @description:    添加权限信息
     * @param: [schisandraAuthPermissionDTO]
     * @return: com.schisandra.auth.common.bo.Result
     * @author zhang
     * @date: 2024/4/17 16:55
     */
    @PostMapping("insert")
    public Result insert(@RequestBody SchisandraAuthPermissionDTO schisandraAuthPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("authPermissionController.insert.dto:{}", JSON.toJSONString(schisandraAuthPermissionDTO));
            }
//            checkRoleInfo(schisandraAuthPermissionDTO);
            SchisandraAuthPermissionBO schisandraAuthPermissionBO = SchisandraAuthPermissionDTOConvert.INSTANCE.convertDTOToBO(schisandraAuthPermissionDTO);
            return Result.ok(schisandraAuthPermissionDomainService.insert(schisandraAuthPermissionBO));
        } catch (Exception e) {
            log.error("authPermissionController.insert.error:{}", e.getMessage(), e);
            return Result.fail("插入权限信息失败");
        }
    }
    /**
     * @description: 查询权限信息
     * @param: [schisandraAuthPermissionDTO]
     * @return: com.schisandra.auth.common.bo.Result
     * @author zlg
     * @date: 2024/4/17 16:54
     */
    @PostMapping("select")
    public Result select(@RequestBody SchisandraAuthPermissionDTO schisandraAuthPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("authPermissionController.update.dto:{}", JSON.toJSONString(schisandraAuthPermissionDTO));
            }

//            checkRoleInfo(schisandraAuthPermissionDTO);
            if (schisandraAuthPermissionDTO.getId()==null){
                return Result.fail("id不能为空");
            }

            SchisandraAuthPermissionBO schisandraAuthPermissionBO = SchisandraAuthPermissionDTOConvert.INSTANCE.convertDTOToBO(schisandraAuthPermissionDTO);
            System.out.println("bo:"+schisandraAuthPermissionBO.getId());
            return Result.ok(schisandraAuthPermissionDomainService.select(schisandraAuthPermissionBO));
        } catch (Exception e) {
            log.error("authPermissionController.select.error:{}", e.getMessage(), e);
            return Result.fail("查询权限信息失败");
        }
    }
    /**
     * @description: 删除权限信息
     * @param: [schisandraAuthPermissionDTO]
     * @return: com.schisandra.auth.common.bo.Result
     * @author zhang
     * @date: 2024/4/17 16:54
     */
    @PostMapping("delete")
    public Result delete(@RequestBody SchisandraAuthPermissionDTO schisandraAuthPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("authPermissionController.update.dto:{}", JSON.toJSONString(schisandraAuthPermissionDTO));
            }
//            checkRoleInfo(schisandraAuthPermissionDTO);
            SchisandraAuthPermissionBO schisandraAuthPermissionBO = SchisandraAuthPermissionDTOConvert.INSTANCE.convertDTOToBO(schisandraAuthPermissionDTO);
            return Result.ok(schisandraAuthPermissionDomainService.delete(schisandraAuthPermissionBO));
        } catch (Exception e) {
            log.error("authPermissionController.select.error:{}", e.getMessage(), e);
            return Result.fail("删除权限信息失败");
        }
    }
    private void checkRoleInfo(@RequestBody SchisandraAuthPermissionDTO schisandraAuthPermissionDTO) {
//        Preconditions.checkArgument(!Utils.isBlank(schisandraAuthPermissionDTO.getId()), "角色名不能为空");
//        Preconditions.checkArgument(!StringUtils.isBlank(schisandraAuthPermissionDTO.getName()), "角色key值不能为空");
    }

}
