/**
 * 用户管理
 * 作者:lxm
 * 包括 登录 改密码 获取权限密码
 */

import { ERRORENUM } from "../config/errorEnum";
import { LIMITSLIST } from "../config/systemConfig";
import * as userData from "../data/admin/user";
import { BizError } from "../util/bizError";
import { getToken } from "../util/tools";


export async function login(loginId:string, pwd:number) {
    let userInfo = await userData.findUserByLoginId(loginId);
    if (!userInfo) throw new BizError(ERRORENUM.登录失败, '用户不存在');

    if (userInfo.pwd != pwd) throw new BizError(ERRORENUM.密码错误, `密码错误 ${pwd}`);

    let token = getToken(userInfo.loginId);

    await userData.updateData({token}, {loginId});

    let limitsList = [LIMITSLIST.工时填报];

    if (userInfo.departmentId == 7 || userInfo.departmentId == 8 || userInfo.departmentId == 6  ) {
        limitsList.push(LIMITSLIST.工时管理);
    }
    if (userInfo.userId == 4) {
        for (let i = 2; i <=7; i++) {
            limitsList.push(i);
        }
    }

    let result = { token, 
        userId:userInfo.userId, 
        roleId:userInfo.roleId, 
        departmentId:userInfo.departmentId, 
        name:userInfo.name,
        headSculpture:`http://192.168.0.105:7088/1.png`,
        limitsList
    };

    

    return result;
}


export async function changePwd() {
    
}
