using Foresight.Data;
using IronIntel.Contractor.Users;
using IronIntel.Services.Business.Admin;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IronIntel.Contractor.Security
{
    public class JobsiteLimitManagement
    {
        public static UserInfo[] GetSubscribeContacts(Int64 jlid)
        {
            const string SQL = "select u.USERIID,u.USERNAME,u.CONTACTTYPE from JOBSITELIMITSUBSCRIBE js left join USERS u on js.CONTACTID=u.USERIID where js.JOBSITELIMITID={0} and u.USERIID is not null";

            List<UserInfo> list = new List<UserInfo>();
            DataTable dt = SystemParams.GetDbInstance().GetDataTableBySQL(SQL, jlid);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    UserInfo ci = new UserInfo();
                    ci.IID = FIDbAccess.GetFieldString(dr["USERIID"], string.Empty);
                    ci.DisplayName = FIDbAccess.GetFieldString(dr["USERNAME"], string.Empty);
                    ci.ContactType = (ContactTypes)FIDbAccess.GetFieldInt(dr["CONTACTTYPE"], 100);
                    list.Add(ci);
                }
            }
            return list.ToArray();
        }
        public static void SaveSubscribeContacts(string jlid, string[] contactids)
        {
            const string SQL_D = "delete from JOBSITELIMITSUBSCRIBE where JOBSITELIMITID={0}";
            const string SQL = @"insert into JOBSITELIMITSUBSCRIBE(JOBSITELIMITID,CONTACTID) values({0},{1})";

            FISqlConnection db = SystemParams.GetDbInstance();
            db.ExecSQL(SQL_D, jlid);

            foreach (var cid in contactids)
            {
                db.ExecSQL(SQL, jlid, cid);
            }
        }
    }
}
