package com.energy.manage.service.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.energy.manage.common.po.system.SysPermissionPO;
import com.energy.manage.common.reponse.RespMsg;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.config.annotations.UserLoginToken;
import com.energy.manage.service.domain.dto.system.PermissionDelDto;
import com.energy.manage.service.domain.dto.system.PermissionDto;
import com.energy.manage.service.domain.dto.system.PermissionUpdateDto;
import com.energy.manage.service.domain.dto.system.SystemPermissionListDto;
import com.energy.manage.service.domain.vo.system.PermissionVo;
import com.energy.manage.service.domain.vo.system.SystemPermissionVo;
import com.energy.manage.service.service.system.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author sy
 * @menu: 资源表
 * @date 2021-10-18 13:32:05
 */
@RestController
@RequestMapping("syspermission")
@CrossOrigin
@Api(value = "SysPermissionController", tags = "系统权限管理")
public class SysPermissionController {

    @Autowired
    private SysPermissionService sysPermissionService;

    /**
     * 增加权限
     * @return
     */
    @UserLoginToken
    @ApiOperation(value = "增加权限")
    @PostMapping(value = "add")
    public ResultResp addPermission(@RequestBody PermissionDto permissionDto) {
        if (StringUtils.isEmpty(permissionDto.getPname()) || StringUtils.isEmpty(permissionDto.getPcode())) {
            return ResultResp.NOTNULLPARAM();
        }
        SysPermissionPO po = new SysPermissionPO();
        po.setParentId(permissionDto.getParentId());
        po.setPermissionName(permissionDto.getPname());
        po.setPermissionUrl(permissionDto.getPurl());
        po.setPermissionCode(permissionDto.getPcode());
        po.setPermissionIconUrl(permissionDto.getPiconUrl());
        po.setPermissionState(permissionDto.getPstat());
        po.setPermissionType(permissionDto.getPtype());
        po.setPermissionSort(permissionDto.getPsort());
        po.setPermissionShow(permissionDto.getPermissionShow());
        boolean flg = sysPermissionService.addPermission(po);
        if (flg) {
            return ResultResp.SUCCESS();
        }
        return ResultResp.FAIL();
    }

    /**
     * 删除权限
     *
     * @return
     */
    @UserLoginToken
    @ApiOperation(value = "删除权限")
    @PostMapping(value = "delete")
    public ResultResp deletePermission(@RequestBody PermissionDelDto permissionDelDto) {
        if (!Optional.ofNullable(permissionDelDto.getPermissionId()).isPresent()) {
            return ResultResp.NOTNULLPARAM();
        }
        SysPermissionPO po = new SysPermissionPO();
        po.setPermissionId(permissionDelDto.getPermissionId());
        boolean flag = sysPermissionService.delPermission(po);
        if (flag) {
            return ResultResp.SUCCESS();
        }
        return ResultResp.FAIL();
    }

    /**
     * 权限更新
     * @return
     */
    @UserLoginToken
    @ApiOperation(value = "更新权限")
    @PostMapping(value = "update")
    public ResultResp updatePermission(@RequestBody PermissionUpdateDto permissionUpdateDto) {

        if (!Optional.ofNullable(permissionUpdateDto.getPermissionId()).isPresent()) {
            return ResultResp.NOTNULLPARAM();
        }
        SysPermissionPO po = new SysPermissionPO();
        po.setPermissionId(permissionUpdateDto.getPermissionId());
        po.setPermissionName(permissionUpdateDto.getPname());
        po.setPermissionUrl(permissionUpdateDto.getPurl());
        po.setPermissionCode(permissionUpdateDto.getPcode());
        po.setPermissionIconUrl(permissionUpdateDto.getPiconUrl());
        po.setPermissionState(permissionUpdateDto.getPstat());
        po.setPermissionType(permissionUpdateDto.getPtype());
        po.setPermissionSort(permissionUpdateDto.getPsort());
        po.setPermissionShow(permissionUpdateDto.getPermissionShow());
        boolean flag = sysPermissionService.updatePermission(po);
        if (flag) {
            return ResultResp.SUCCESS();
        }
        return ResultResp.FAIL();
    }

    /**
     * 获取全部权限
     *
     * @return
     */
    @UserLoginToken
    @ApiOperation(value = "获取全部权限")
    @PostMapping(value = "getAll")
    public ResultResp<List<SystemPermissionVo>> getAll(@RequestBody SystemPermissionListDto systemPermissionListDto) {
        List<SystemPermissionVo> list = sysPermissionService.getAllPermission(systemPermissionListDto);
        return ResultResp.SUCCESS(list);
    }
}
