using Sugar.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
using Utils;
public class usersManager : DbContext<users>
{

    //当前类已经继承了 DbContext增、删、查、改的方法

    //这里面写的代码不会给覆盖,如果要重新生成请删除 usersManager.cs
    /// <summary>
    /// 根据条件查询用户信息列表
    /// </summary>
    /// <param name="DeptCode">部门代码</param>
    /// <param name="staffcode">用户唯一标识码</param>
    /// <param name="username">用户名</param>
    /// <param name="station">岗位代码</param>
    /// <param name="issystem">是否系统管理员</param>
    /// <param name="islock">用户是否锁定</param>
    /// <returns></returns>
    public List<dynamic> GetUsersList(string DeptCode, string staffcode, string username, string station, string issystem, string islock)
    {
        String sqlCon = "";
        if (!String.IsNullOrEmpty(DeptCode))
        {
            sqlCon += " AND (a.DeptCode='" + DeptCode + "')  ";
        }

        if (!String.IsNullOrEmpty(username))
        {

            sqlCon += " AND a.UserName LIKE '" + username + "%' ";
        }
        if (!String.IsNullOrEmpty(station))
        {

            sqlCon += " AND a.StationCode LIKE '" + station + "%' ";
        }

        if (!String.IsNullOrEmpty(islock))
        {

            sqlCon += " AND a.islock='" + islock + "' ";
        }
        var sql = " SELECT  a.status,a.SysID, a.StaffCode, a.UserID, a.UserName, a.DeptCode,a.OrgCode, a.StationCode, a.Mobile, a.Tel, a.Fax, a.Education, a.PolityStatus," +
            " a.IDCard, a.Homeplace, a.Nation, a.Email, a.SEX, a.Birthday, a.Addr, a.IsSystem, a.BeginDate, a.EndDate, a.IsLock, a.LastUpDate," +
            " b.DeptName,case IsSystem when 1 then '是' else '否'end AS System, case IsLock when 1 then '停用' else '启用'end AS Lock,c.NationName," +
            " d.EduName,e.StatusName,f.ItemName ,a.creline " +
            " FROM Users a  " +
            " INNER JOIN Depts b on a.DeptCode=b.DeptCode " +
            " LEFT  JOIN NationInfo c on a.Nation=c.NationCode " +
            " LEFT  JOIN EducationInfo d on a.Education=d.EduCode " +
            " LEFT  JOIN PolityStatusInfo e on a.PolityStatus=e.StatusCode " +
            " LEFT  JOIN ItemDictionary f on a.StationCode=f.ItemID " +
        "  WHERE 1=1 "+sqlCon;
        if (issystem != "1")
        {
            sql = " SELECT  a.status,a.SysID, a.StaffCode, a.UserID, a.UserName, a.DeptCode,a.OrgCode, a.StationCode, a.Mobile, a.Tel, a.Fax, a.Education, a.PolityStatus," +
            " a.IDCard, a.Homeplace, a.Nation, a.Email, a.SEX, a.Birthday, a.Addr, a.IsSystem, a.BeginDate, a.EndDate, a.IsLock, a.LastUpDate," +
            " b.DeptName,case IsSystem when 1 then '是' else '否'end AS System, case IsLock when 1 then '停用' else '启用'end AS Lock,c.NationName," +
            " d.EduName,e.StatusName,f.ItemName ,a.creline " +
            " FROM Users a  " +
            " INNER JOIN Depts b on a.DeptCode=b.DeptCode " +
            " INNER JOIN usersindepts b1  on b1.deptcode=b.deptcode " +
            " LEFT  JOIN NationInfo c on a.Nation=c.NationCode " +
            " LEFT  JOIN EducationInfo d on a.Education=d.EduCode " +
            " LEFT  JOIN PolityStatusInfo e on a.PolityStatus=e.StatusCode " +
            " LEFT  JOIN ItemDictionary f on a.StationCode=f.ItemID " +
            "  WHERE 1=1 and b1.staffcode='" + staffcode + "' " + sqlCon;

        }
        var list = Db.Ado.SqlQuery<dynamic>(sql);
        return list;
    }

    /// <summary>
    /// 获取员工工号最大值
    /// </summary>
    /// <returns></returns>
    public string GetMaxStaffCode()
    {
        var sql = $"SELECT Max( isnull(staffcode,1001)) as staffcode FROM Users";
        var staffcode = Db.Ado.GetString(sql);
        return staffcode.IsNullOrEmpty() ? "1001" : staffcode;
    }

    /// <summary>
    /// 设置员工状态
    /// </summary>
    /// <param name="staffcode">工号</param>
    /// <param name="status">状态：在岗，待岗，休班</param>
    /// <returns></returns>
    public int SetStaffStatus(string staffcode, string status)
    {
        var sql = $"update users set status='{status}' where staffcode='{staffcode}'";
        var rows = Db.Ado.ExecuteCommand(sql);
        return rows;
    }



}
