package com.sptg.permissions.biz.controller;

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.CommonConstant;
import com.sptg.common.core.util.CommonUtil;
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.entity.Role;
import com.sptg.permissions.api.feigns.AccountRoleFeign;
import com.sptg.permissions.biz.service.AccountRoleServiceInterface;
import com.sptg.permissions.biz.service.AccountServiceInterface;
import com.sptg.permissions.biz.service.RoleServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * title: AccountRoleController
 * package: com.sptg.permissions.biz.controller
 * description: 账号角色-控制器
 *
 *
 *
 */
@RestController
@RequiredArgsConstructor
public class AccountRoleController implements AccountRoleFeign {

    private final AccountRoleServiceInterface accountRoleServiceInterface;
    private final RoleServiceInterface roleServiceInterface;
    private final AccountServiceInterface accountServiceInterface;

    @Override
    @PostMapping(value = "/account/role/pagingQuery")
    public ResultBean<Page> pagingQuery(@RequestBody PageBean<AccountRole,AccountRoleDto> pageBean) {
        AccountRoleDto dto = pageBean.getDto();
        //  获取角色信息
        Role role = this.roleServiceInterface.selectById(dto.getRoleId());
        Assert.notNull(role,"角色Id不存在");
        //  校验权限
        if (!dto.getOnLineUser().getOrgId().equals(role.getOrgId())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        //  分页查询
        Page<AccountRole> page = this.accountRoleServiceInterface.pagingQuery(dto,pageBean);
        //  获取账号信息
        List<AccountRole> accountRoleList = page.getRecords();
        accountRoleList.forEach(item -> {
            Account account = this.accountServiceInterface.selectById(item.getAccountId());
            if(account != null){
                account.setLoginPwd(CommonConstant.HIDE);
                item.setAccount(account);
            }
        });
        page.setRecords(accountRoleList);
        return new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @Override
    @PostMapping(value = "/account/role/pagingQueryAccountListByNotHaveRole")
    public ResultBean<Page> pagingQueryAccountListByNotHaveRole(@RequestBody PageBean<Account,AccountDto> pageBean) {
        Page<Account> accountList = this.accountServiceInterface.pagingQueryByNotHaveRole(pageBean);
        return new ResultBean<>(ResultEnum.SUCCESS,accountList);
    }

    @Override
    @PostMapping(value = "/account/role/bind")
    public ResultBean<Boolean> bindUser(@RequestBody AccountRoleDto dto) {
        //  校验权限
        Account onLineUser = dto.getOnLineUser();
        //  获取角色信息
        Role role = this.roleServiceInterface.selectById(dto.getRoleId());
        Assert.notNull(role,"角色Id不存在");
        if (!role.getOrgId().equals(onLineUser.getOrgId())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        //  获取账号信息
        Account account = this.accountServiceInterface.selectById(dto.getAccountId());
        Assert.notNull(role,"账号Id不存在");
        //  权限校验
        if (!account.getOrgPosition().startsWith(onLineUser.getOrgPosition())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        Boolean result = this.accountRoleServiceInterface.bindUser(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/account/role/untie")
    public ResultBean<Boolean> untieUser(@RequestBody AccountRoleDto dto) {
        Account onLineUser = dto.getOnLineUser();
        //  获取角色信息
        Role role = this.roleServiceInterface.selectById(dto.getRoleId());
        Assert.notNull(role,"角色Id不存在");
        if (!role.getOrgId().equals(onLineUser.getOrgId())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        List<Long> list = CommonUtil.stringToLongList(dto.getAccountIds());
        //  可操作的账号列表
        List<Long> accountIdList = new ArrayList<>(list.size());
        //  校验权限
        list.forEach(item -> {
            Account account = this.accountServiceInterface.selectById(item);
            //  权限校验
            if (account.getOrgPosition().startsWith(onLineUser.getOrgPosition())){
                accountIdList.add(item);
            }
        });
        if (accountIdList.size() == 0){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        dto.setAccountIdList(accountIdList);
        Boolean result = this.accountRoleServiceInterface.untieUser(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

}
