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.AccountDto;
import com.sptg.permissions.api.dto.AccountRoleDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.AccountRole;
import com.sptg.permissions.api.feigns.AccountRoleFeign;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * title: RoleBindAccountController
 * package: com.sptg.gateway.controller.permissions
 * description: 角色绑定用户-控制器
 *
 *                  
 *                 
 */
@RestController
@RequestMapping(value = "/permissions/role/account")
@AllArgsConstructor
public class RoleBindAccountController {

    private final AccountRoleFeign accountRoleFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;

    /**
     * 功能描述: 分页查询角色下绑定的账号列表
     * @param dto 查询条件 { roleId：必填 }
     * @return ResultBean<Page<AccountRole>>
     *                  
     *                       
     */
    @GetMapping(value = "/pagingQuery")
    @Security(token = PermissionsSecurityToken.ROLE_DISTRIBUTION,format = DataFormatEnum.JSON)
    public ResultBean<Page> pagingQuery(AccountRoleDto dto,PageBean<AccountRole,AccountRoleDto> pageBean){
        if (null == dto || null == dto.getRoleId()){
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOnLineUser(onLineUser);
        pageBean.setDto(dto);
        return this.accountRoleFeign.pagingQuery(pageBean);
    }

    /**
     * 功能描述: 分页获取账号列表,账号不含有指定的角色
     * @param dto 查询条件，默认查询当前用户所属机构下的用户列表
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     *                  
     *                       
     */
    @GetMapping(value = "/pagingQueryAccountListByNotHaveRole")
    @Security(token = PermissionsSecurityToken.ROLE_DISTRIBUTION,format = DataFormatEnum.JSON)
    public ResultBean<Page> pagingQueryAccountListByNotHaveRole(AccountDto dto,PageBean<Account,AccountDto> pageBean){
        if (null == dto || null == dto.getRoleId()){
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOnLineUser(onLineUser);
        pageBean.setDto(dto);
        return this.accountRoleFeign.pagingQueryAccountListByNotHaveRole(pageBean);
    }


    /**
     * 功能描述: 绑定用户
     * @param dto { accountId:必填,roleId:必填 }
     * @return ResultBean<Boolean>
     *                  
     *                       
     */
    @PostMapping(value = "/bind")
    @Security(token = PermissionsSecurityToken.ROLE_DISTRIBUTION,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> bindUser(AccountRoleDto dto){
        //  参数校验
        if (null == dto || null == dto.getAccountId() || null == dto.getRoleId()){
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOnLineUser(onLineUser);
        ResultBean<Boolean> resultBean=this.accountRoleFeign.bindUser(dto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ROLE_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),"绑定用户id("+dto.getAccountId()+")",dto,onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 解除用户绑定
     * @param dto { accountId:必填,accountIds:必填 }
     * @return ResultBean<Boolean>
     *                  
     *                       
     */
    @PostMapping(value = "/untie")
    @Security(token = PermissionsSecurityToken.ROLE_DISTRIBUTION,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> untieUser(AccountRoleDto dto){
        //  参数校验
        if (null == dto || StringUtils.isEmpty(dto.getAccountIds()) || null == dto.getRoleId()){
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOnLineUser(onLineUser);
        ResultBean<Boolean> resultBean=this.accountRoleFeign.untieUser(dto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.ROLE_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),"解除用户id("+dto.getAccountIds()+")绑定",dto,onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

}
