package com.sptg.gateway.controller.permissions;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.SysLogConstant;
import com.sptg.common.core.util.WebUtils;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.constant.PermissionsSecurityToken;
import com.sptg.permissions.api.dto.RoleDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Role;
import com.sptg.permissions.api.feigns.RoleFeign;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * title: RoleManageController
 * package: com.sptg.gateway.controller.permissions
 * description: 角色管理-控制器
 *
 *                  
 *                 
 */
@RestController
@RequestMapping(value = "/permissions/role")
@RequiredArgsConstructor
public class RoleManageController {

    /* -------------------------------------------------- 接口注入 -------------------------------------------------- */

    private final RoleFeign roleFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;

    /* --------------------------------------------------- 控制器 --------------------------------------------------- */

    /**
     * 功能描述: 分页查询角色列表
     * @param dto 查询条件
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     *                  
     *                       
     */
    @GetMapping(value = "/pagingQuery")
    @Security(token = PermissionsSecurityToken.ROLE_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page> pagingQuery(RoleDto dto, PageBean<Role,RoleDto> pageBean){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOnLineUser(onLineUser);
        pageBean.setDto(dto);
        return this.roleFeign.pagingQuery(pageBean);
    }

    /**
     * 功能描述: 创建角色
     * @param dto 角色信息
     * @return ResultBean<Boolean> { true：成功,false：失败 }
     *                  
     *                       
     */
    @PostMapping(value = "/add")
    @Security(token = PermissionsSecurityToken.ROLE_INSERT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addEntity(RoleDto dto){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOnLineUser(onLineUser);
        ResultBean<Boolean> resultBean=this.roleFeign.addEntity(dto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            Role role = new Role();
            BeanUtils.copyProperties(dto,role);
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ROLE_MANAGE, SysLogConstant.ADD_EVENT,
                    WebUtils.getIp(),dto,onLineUser,role);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 根据Id获取角色信息
     * @param id 角色Id
     * @return ResultBean<Role>
     *                  
     *                       
     */
    @GetMapping(value = "/get/{id}")
    @Security(token = PermissionsSecurityToken.ROLE_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Role> getById(@PathVariable(value = "id") Long id){
        Role role = this.roleFeign.getById(id);
        if (null == role){
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        if (!role.getOrgId().equals(onLineUser.getOrgId())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,role);
    }

    /**
     * 功能描述: 修改角色信息
     * @param dto 角色信息
     * @return ResultBean<Boolean> { true：成功,false：失败 }
     *                  
     *                       
     */
    @PostMapping(value = "/update")
    @Security(token = PermissionsSecurityToken.ROLE_UPDATE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> updateEntity(RoleDto dto){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        Role oldObj = this.roleFeign.getById(dto.getId());
        dto.setOnLineUser(onLineUser);
        ResultBean<Boolean> resultBean=this.roleFeign.updateEntity(dto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            Role newObj = this.roleFeign.getById(dto.getId());
            oldObj.setMenuList(null);
            newObj.setMenuList(null);
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ROLE_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),dto,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 删除角色
     * @param id 角色Id
     * @return ResultBean<Boolean> { true：成功,false：失败 }
     *                  
     *                       
     */
    @GetMapping(value = "/delete/{id}")
    @Security(token = PermissionsSecurityToken.ROLE_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteEntity(@PathVariable(value = "id") Long id){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        RoleDto dto = new RoleDto();
        dto.setId(id);
        dto.setOnLineUser(onLineUser);
        Role oldObj = this.roleFeign.getById(dto.getId());
        ResultBean<Boolean> resultBean=this.roleFeign.deleteEntity(dto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            oldObj.setMenuList(null);
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ROLE_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(),dto,onLineUser,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

}
