package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.UserService;
import org.jeecg.modules.workorderPermission.entity.WorkorderPermission;
import org.jeecg.modules.workorderPermission.service.IWorkorderPermissionService;
import org.jeecg.modules.workorderPermissionRole.entity.WorkorderPermissionRole;
import org.jeecg.modules.workorderPermissionRole.service.IWorkorderPermissionRoleService;
import org.jeecg.modules.workorderRole.entity.WorkorderRole;
import org.jeecg.modules.workorderRole.service.IWorkorderRoleService;
import org.jeecg.modules.workorderUser.entity.WorkorderUser;
import org.jeecg.modules.workorderUser.service.IWorkorderUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    //个人信息
    @Resource
    private IWorkorderUserService workorderUserService;

    //角色信息
    @Resource
    private IWorkorderRoleService workorderRoleService;

    //权限信息
    @Resource
    private IWorkorderPermissionService workorderPermissionService;

    //角色权限信息
    @Resource
    private IWorkorderPermissionRoleService workorderPermissionRoleService;
    /*************************************************************************************/

    //查询用户列表
    @Override
    public Result<?> queryUserList(PageBean pageBean) {
        Page<WorkorderUser> page = new Page<WorkorderUser>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<WorkorderUser> pageList = workorderUserService
                .lambdaQuery()
                .page(page);

        return Result.OK("用户信息列表", pageList);
    }

    //用户信息-根据id查询用户信息
    @Override
    public Result<?> queryUserById(String userId) {
        //查询公告信息详情
        WorkorderUser one = workorderUserService
                .lambdaQuery()
                .eq(WorkorderUser::getId, userId)
                .one();

        return Result.OK("用户信息详情", one);

    }

    //登录
    @Override
    public Result<?> login(String userName, String password) {
        //查询用户信息详情
        WorkorderUser user = workorderUserService
                .lambdaQuery()
                .eq(WorkorderUser::getName, userName)
                .eq(WorkorderUser::getPassword, password)
                .one();

        if(null != user){
            //获取用户角色信息
            WorkorderRole role = workorderRoleService
                    .lambdaQuery()
                    .eq(WorkorderRole::getId, user.getRoleId())
                    .one();
            user.setRoleName(role.getName());

            //获取角色权限信息
            //1、获取权限信息
            List<WorkorderPermission> permissionList = workorderPermissionService
                    .lambdaQuery()
                    .list();
            //2、获取角色关联权限
            List<WorkorderPermissionRole> permissionRoleList = workorderPermissionRoleService
                    .lambdaQuery()
                    .eq(WorkorderPermissionRole::getRoleId, role.getId())
                    .list();

            //添加权限名称
            for (WorkorderPermissionRole workorderPermissionRole : permissionRoleList) {
                for (WorkorderPermission permission : permissionList) {
                    if(workorderPermissionRole.getPermissionId().equals(permission.getId())){
                        workorderPermissionRole.setPermissionName(permission.getName());
                    }
                }
            }
            user.setPermissionRoleList(permissionRoleList);

            return Result.OK("用户登录成功", user);
        }else {
            return Result.error("该用户不存在", user);
        }

    }

    //注册
    @Override
    public Result<?> register(WorkorderUser workorderUser) {

        //查询是否有同名用户(不能注册同名用户)
        WorkorderUser one = workorderUserService
                .lambdaQuery()
                .eq(WorkorderUser::getName, workorderUser.getName())
                .one();

        if(null != one){
            return Result.error("用户名已存在！");
        }

        boolean flag = workorderUserService.save(workorderUser);
        if(flag){
            return Result.OK("注册成功！");
        }else {
            return Result.error("注册失败！");
        }
    }

    //修改用户信息
    @Override
    public Result<?> updateUser(WorkorderUser workorderUser) {
        boolean flag = workorderUserService.updateById(workorderUser);
        if(flag){
            return Result.OK("修改用户信息成功！");
        }else {
            return Result.error("修改用户信息失败！");
        }
    }




}
