You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/MvcShipping/DAL/MsCodeEmployee/MsCodeEmployeeDAL.cs

379 lines
17 KiB
C#

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.MsCodeEmployee;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
using DSWeb.Areas.MvcShipping.Helper;
using DSWeb.Common.DB;
using System.Linq;
using DSWeb.SoftMng.Model;
using System.Data.Entity.Migrations;
namespace DSWeb.MvcShipping.DAL.MsCodeEmployee
{
public class MsCodeEmployeeDAL
{
#region Inquery DataList
static public List<CodeEmployee> GetDataList(string strCondition, string companyid, string sort = null)
{
var rangstr =" CORPID='"+companyid+"'";
if (!string.IsNullOrEmpty(rangstr))
{
if (!string.IsNullOrEmpty(strCondition))
{
strCondition = strCondition + " and " + rangstr;
}
else
{
strCondition = rangstr;
}
}
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("[GID],USERCODE,[USERNAME],[SEX],[ENROLLTIME],[BIRTHDAY],[OFFICEPHONE],[HOMEPHONE],[MOBILE],[EMAIL1],[EMAIL2],[HOMEADDRESS]");
strSql.Append(",[DEPTNAME],[STATUS],[LEAVEOFFICETIME],OFFICEADDR,[CREATEUSER],[CREATETIME],[MODIFIEDUSER],[MODIFIEDTIME],[REMARK],[CORPID]");
strSql.Append(" from code_employee ");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" where " + strCondition);
}
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else {
strSql.Append(" order by DEPTNAME,USERNAME");
}
return SetData(strSql);
}
static public CodeEmployee GetData(string condition, string companyid)
{
CodeEmployee data = null;
var list = GetDataList(condition,companyid);
if (list.Count > 0)
data = list[0];
if (data == null)
{
data = new CodeEmployee();
data.CORPID = companyid;
}
return data;
}
private static List<CodeEmployee> SetData(StringBuilder strSql)
{
var headList = new List<CodeEmployee>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
CodeEmployee data = new CodeEmployee();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
data.USERNAME = Convert.ToString(reader["USERNAME"]);
data.USERCODE = Convert.ToString(reader["USERCODE"]);
data.SEX = Convert.ToString(reader["SEX"]);
data.CORPID = Convert.ToString(reader["CORPID"]);
if (reader["ENROLLTIME"] != DBNull.Value)
data.ENROLLTIME = Convert.ToDateTime(reader["ENROLLTIME"]).ToString("yyyy-MM-dd");
if (reader["BIRTHDAY"] != DBNull.Value)
data.BIRTHDAY = Convert.ToDateTime(reader["BIRTHDAY"]).ToString("yyyy-MM-dd");
data.OFFICEPHONE = Convert.ToString(reader["OFFICEPHONE"]);
data.HOMEPHONE = Convert.ToString(reader["HOMEPHONE"]);
data.MOBILE = Convert.ToString(reader["MOBILE"]);
data.EMAIL1 = Convert.ToString(reader["EMAIL1"]);
data.EMAIL2 = Convert.ToString(reader["EMAIL2"]);
data.HOMEADDRESS = Convert.ToString(reader["HOMEADDRESS"]);
data.DEPTNAME = Convert.ToString(reader["DEPTNAME"]);
data.STATUS = Convert.ToString(reader["STATUS"]);
data.OFFICEADDR = Convert.ToString(reader["OFFICEADDR"]);
if (reader["LEAVEOFFICETIME"] != DBNull.Value)
data.LEAVEOFFICETIME = Convert.ToDateTime(reader["LEAVEOFFICETIME"]).ToString("yyyy-MM-dd");
data.CREATEUSER = Convert.ToString(reader["CREATEUSER"]);
if (reader["CREATETIME"] != DBNull.Value)
data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]).ToString("yyyy-MM-dd HH:mm:ss");
data.MODIFIEDUSER = Convert.ToString(reader["MODIFIEDUSER"]);
if (reader["MODIFIEDTIME"] != DBNull.Value)
data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]).ToString("yyyy-MM-dd HH:mm:ss");
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
public static DBResult SaveDetail(List<CodeEmployee> bodyList, string companyid,string userid)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var cmdInsert =
db.GetSqlStringCommand(
@"insert into code_employee (GID,USERCODE,USERNAME,SEX,ENROLLTIME,BIRTHDAY,OFFICEPHONE,HOMEPHONE,MOBILE,EMAIL1
,EMAIL2,HOMEADDRESS,DEPTNAME,STATUS,LEAVEOFFICETIME,OFFICEADDR,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME
,REMARK,CORPID)
values (@GID,@USERCODE,@USERNAME,@SEX,@ENROLLTIME,@BIRTHDAY,@OFFICEPHONE,@HOMEPHONE,@MOBILE,@EMAIL1
,@EMAIL2,@HOMEADDRESS,@DEPTNAME,@STATUS,@LEAVEOFFICETIME,@OFFICEADDR,@CREATEUSER,@CREATETIME,@MODIFIEDUSER,@MODIFIEDTIME
,@REMARK,@CORPID) ");
var cmdUpdate =
db.GetSqlStringCommand(
@"update code_employee set USERCODE=@USERCODE,USERNAME=@USERNAME,SEX=@SEX,ENROLLTIME=@ENROLLTIME,BIRTHDAY=@BIRTHDAY,OFFICEPHONE=@OFFICEPHONE
,HOMEPHONE=@HOMEPHONE,MOBILE=@MOBILE,EMAIL1=@EMAIL1,EMAIL2=@EMAIL2,HOMEADDRESS=@HOMEADDRESS,DEPTNAME=@DEPTNAME,STATUS=@STATUS
,LEAVEOFFICETIME=@LEAVEOFFICETIME,OFFICEADDR=@OFFICEADDR,MODIFIEDUSER=@MODIFIEDUSER,MODIFIEDTIME=@MODIFIEDTIME,REMARK=@REMARK
where GID=@GID ");
if (bodyList != null)
{
foreach (var enumValue in bodyList)
{
if (enumValue.ENROLLTIME == "") enumValue.ENROLLTIME = null;
else
{
enumValue.ENROLLTIME = Convert.ToDateTime(enumValue.ENROLLTIME.Replace("GMT+0800 (中国标准时间)", "")).ToString("yyyy-MM-dd");
}
if (enumValue.BIRTHDAY == "") enumValue.BIRTHDAY = null;
else
{
enumValue.BIRTHDAY = Convert.ToDateTime(enumValue.BIRTHDAY.Replace("GMT+0800 (中国标准时间)", "")).ToString("yyyy-MM-dd");
}
if (enumValue.LEAVEOFFICETIME == "") enumValue.LEAVEOFFICETIME = null;
else
{
enumValue.LEAVEOFFICETIME = Convert.ToDateTime(enumValue.LEAVEOFFICETIME.Replace("GMT+0800 (中国标准时间)", "")).ToString("yyyy-MM-dd");
}
if (enumValue.GID == "*" || enumValue.GID == "")
{
cmdInsert.Parameters.Clear();
db.AddInParameter(cmdInsert, "@GID", DbType.String, Guid.NewGuid().ToString());
db.AddInParameter(cmdInsert, "@CORPID", DbType.String, companyid);
db.AddInParameter(cmdInsert, "@USERCODE", DbType.String, enumValue.USERCODE);
db.AddInParameter(cmdInsert, "@USERNAME", DbType.String, enumValue.USERNAME);
db.AddInParameter(cmdInsert, "@SEX", DbType.String, enumValue.SEX);
db.AddInParameter(cmdInsert, "@ENROLLTIME", DbType.String, enumValue.ENROLLTIME);
db.AddInParameter(cmdInsert, "@BIRTHDAY", DbType.String, enumValue.BIRTHDAY);
db.AddInParameter(cmdInsert, "@OFFICEPHONE", DbType.String, enumValue.OFFICEPHONE);
db.AddInParameter(cmdInsert, "@HOMEPHONE", DbType.String, enumValue.HOMEPHONE);
db.AddInParameter(cmdInsert, "@MOBILE", DbType.String, enumValue.MOBILE);
db.AddInParameter(cmdInsert, "@EMAIL1", DbType.String, enumValue.EMAIL1);
db.AddInParameter(cmdInsert, "@EMAIL2", DbType.String, enumValue.EMAIL2);
db.AddInParameter(cmdInsert, "@HOMEADDRESS", DbType.String, enumValue.HOMEADDRESS);
db.AddInParameter(cmdInsert, "@DEPTNAME", DbType.String, enumValue.DEPTNAME);
db.AddInParameter(cmdInsert, "@STATUS", DbType.String, enumValue.STATUS);
db.AddInParameter(cmdInsert, "@LEAVEOFFICETIME", DbType.String, enumValue.LEAVEOFFICETIME);
db.AddInParameter(cmdInsert, "@OFFICEADDR", DbType.String, enumValue.OFFICEADDR);
db.AddInParameter(cmdInsert, "@MODIFIEDUSER", DbType.String, userid);
db.AddInParameter(cmdInsert, "@MODIFIEDTIME", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
db.AddInParameter(cmdInsert, "@CREATEUSER", DbType.String, userid);
db.AddInParameter(cmdInsert, "@CREATETIME", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
db.AddInParameter(cmdInsert, "@REMARK", DbType.String, enumValue.REMARK);
db.ExecuteNonQuery(cmdInsert, tran);
}
else
{
cmdUpdate.Parameters.Clear();
db.AddInParameter(cmdUpdate, "@GID", DbType.String, enumValue.GID);
db.AddInParameter(cmdUpdate, "@USERCODE", DbType.String, enumValue.USERCODE);
db.AddInParameter(cmdUpdate, "@USERNAME", DbType.String, enumValue.USERNAME);
db.AddInParameter(cmdUpdate, "@SEX", DbType.String, enumValue.SEX);
db.AddInParameter(cmdUpdate, "@ENROLLTIME", DbType.String, enumValue.ENROLLTIME);
db.AddInParameter(cmdUpdate, "@BIRTHDAY", DbType.String, enumValue.BIRTHDAY);
db.AddInParameter(cmdUpdate, "@OFFICEPHONE", DbType.String, enumValue.OFFICEPHONE);
db.AddInParameter(cmdUpdate, "@HOMEPHONE", DbType.String, enumValue.HOMEPHONE);
db.AddInParameter(cmdUpdate, "@MOBILE", DbType.String, enumValue.MOBILE);
db.AddInParameter(cmdUpdate, "@EMAIL1", DbType.String, enumValue.EMAIL1);
db.AddInParameter(cmdUpdate, "@EMAIL2", DbType.String, enumValue.EMAIL2);
db.AddInParameter(cmdUpdate, "@HOMEADDRESS", DbType.String, enumValue.HOMEADDRESS);
db.AddInParameter(cmdUpdate, "@DEPTNAME", DbType.String, enumValue.DEPTNAME);
db.AddInParameter(cmdUpdate, "@STATUS", DbType.String, enumValue.STATUS);
db.AddInParameter(cmdUpdate, "@LEAVEOFFICETIME", DbType.String, enumValue.LEAVEOFFICETIME);
db.AddInParameter(cmdUpdate, "@OFFICEADDR", DbType.String, enumValue.OFFICEADDR);
db.AddInParameter(cmdUpdate, "@MODIFIEDUSER", DbType.String, userid);
db.AddInParameter(cmdUpdate, "@MODIFIEDTIME", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
db.AddInParameter(cmdUpdate, "@REMARK", DbType.String, enumValue.REMARK);
db.ExecuteNonQuery(cmdUpdate, tran);
}
}
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "保存出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "保存成功" + result.Message;
return result;
}
public static DBResult DeleteDetail(List<CodeEmployee> headData)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
if (headData != null)
{
foreach (var enumValue in headData)
{
var cmdDelete = db.GetSqlStringCommand("delete from code_employee where GID='" + enumValue.GID + "'");
db.ExecuteNonQuery(cmdDelete, tran);
}
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "删除出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "删除成功";
return result;
}
public static DBResult GetDingTalkUserList()
{
var result = new DBResult();
try
{
var userlist = DingTalkHelper.GetUserList();
DingTalk_.deptlist = DingTalkHelper.GetDeptList();
var cdc = new CommonDataContext();
var codeemployeeList = cdc.code_employee.ToList();
var updList = new List<DingTalk_>();
var insertList = new List<DingTalk_>();
var _updList = new List<code_employee_md>();
var _insertList = new List<code_employee_md>();
foreach (var item in userlist)
{
if (codeemployeeList.Exists(x => x.GID == item.userid))
{
//var curruser = codeemployeeList.First(x => x.GID == item.userid);
//curruser.USERNAME = item.name;
//curruser.MOBILE = item.mobile;
//curruser.MODIFIEDTIME = DateTime.Now;
//curruser.OFFICEPHONE = item.telephone;
updList.Add(item);
_updList.Add(codeemployeeList.First(x => x.GID == item.userid));
}
else
{
insertList.Add(item);
}
}
if (updList.Count > 0)
{
foreach (var item in updList)
{
var newupdrec = item.getCodeEmployee();
var updrec = _updList.First(x => x.GID == item.userid);
updrec.USERNAME = newupdrec.USERNAME;
updrec.MOBILE = newupdrec.MOBILE;
updrec.MODIFIEDTIME = newupdrec.MODIFIEDTIME;
updrec.OFFICEPHONE = newupdrec.OFFICEPHONE;
updrec.CORPID= newupdrec.CORPID;
updrec.DEPTNAME=newupdrec.DEPTNAME;
updrec.OFFICEADDR=newupdrec.OFFICEADDR;
cdc.code_employee.AddOrUpdate(updrec);
}
}
if (insertList.Count > 0)
{
foreach (var item in insertList)
{
var newinsertrec = item.getCodeEmployee();
cdc.code_employee.AddOrUpdate(newinsertrec);
}
}
cdc.SaveChanges();
result.OK("同步完成");
}
catch(Exception e){
result.SetErrorInfo(e.Message);
}
return result;
}
}
}