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.AccountOauthDto;
import com.sptg.permissions.api.dto.AccountOauthDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.AccountOauth;
import com.sptg.permissions.api.entity.Role;
import com.sptg.permissions.api.feigns.AccountOauthFeign;
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/oauth")
@RequiredArgsConstructor
public class OauthManageController {

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

    private final AccountOauthFeign oauthFeign;
    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(AccountOauthDto dto, PageBean<AccountOauth,AccountOauthDto> pageBean){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOnLineUser(onLineUser);
        pageBean.setDto(dto);
        return this.oauthFeign.pagingQuery(pageBean);
    }

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

}
