using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using dccdc.Models; using Dapper; namespace dccdc.DAL { public class MedicalAppointmentDal { public object getYYRS() { string sql = "select convert(varchar(10), ADate, 121) as date,sum(status) as come,count(id) as count from MedicalAppointment" + " where ADate= convert(varchar(10), dateadd(d, -7, GETDATE()), 121)" + " group by convert(varchar(10), ADate, 121)" + " order by date"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql).ToList(); } } public object getYYLX() { string sql = "select convert(varchar(10), ADate, 121) as date,YYType,count(id) as count from MedicalAppointment" + " where ADate= convert(varchar(10), GETDATE(), 121)" + " group by convert(varchar(10), ADate, 121),YYType" + " order by date"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql).ToList(); } } public string Add(MedicalAppointment model) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { model.ATime = DateTime.Now; string sql = @"INSERT INTO [MedicalAppointment] ([openid] ,[xingMing] ,[IDCard] ,[ADate] ,[ATime] ,[Mobile] ,[MType] ,[CYFA] ,[CYFAName] ,[QYXX] ,[YHYS] ,[ZGZT] ,[QYID] ,[YYCode] ,[ZFCode] ,[ZFZT] ,[YYUserID] ) VALUES (@openid , @xingMing , @IDCard , @ADate , @ATime , @Mobile , @MType , @CYFA , @CYFAName , @QYXX , @YHYS , @ZGZT , @QYID , @YYCode , @ZFCode , @ZFZT , @YYUserID)Select SCOPE_IDENTITY()"; return conn.Query(sql, model).FirstOrDefault(); } } /// /// 微信预约登记 /// /// /// public MedicalAppointment WXYY(MedicalAppointment ma, int YYType) { //throw new NotImplementedException(); using (IDbConnection conn = CommHelper.GetSqlConnection()) { ma.ATime = DateTime.Now; string sql = "select top 1 YYCode from MedicalAppointment where YYCode like '" + ma.ATime.ToString("yyyyMMdd") + "%' order by YYCode desc"; var LastYYCode = conn.Query(sql).FirstOrDefault(); if (string.IsNullOrEmpty(LastYYCode)) { ma.YYCode = ma.ATime.ToString("yyyyMMdd") + "001"; } else { int c = int.Parse(LastYYCode.Substring(8)); c++; ma.YYCode = ma.ATime.ToString("yyyyMMdd") + c.ToString("000"); } sql = @"INSERT INTO [MedicalAppointment] ([openid] ,[xingMing] ,[IDCard] ,[ADate] ,[ATime] ,[Mobile] ,[dwmc] ,[zj] ,[MType] ,[CYFA] ,[QYXX] ,[YYCode] ,[ZFCode] ,[ZFZT],SFJE,CYFAName,YYType,sjdid,jgid) VALUES (@openid , @xingMing , @IDCard , @ADate , @ATime , @Mobile , @dwmc , @zj , 1 , @CYFA , @QYXX , @YYCode , @ZFCode , @ZFZT,@SFJE,@CYFAName," + YYType + ",@sjdid,@jgid)Select SCOPE_IDENTITY()"; ma.id = conn.ExecuteScalar(sql, ma); return ma; } } /// /// 根据日期获取不可预约日期列表 /// /// /// /// /// public object getBKYYList(int page, int pagesize, string key) { string sql = "SELECT *,row_number() over(order by id desc) as rownum" + " FROM [dbo].[bkyyrq] where 1=1 "; if (!string.IsNullOrEmpty(key)) { sql += " and rq=@rq"; } DateTime rq; if (!DateTime.TryParse(key, out rq)) { rq = DateTime.Now; } sql = "select * from (" + sql + ") t where t.rownum>(" + page + "-1)*" + pagesize + " and rownum<=" + page + "*" + pagesize; using (var conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { rq }).ToList(); } } public object getYYListDT(string xm, string yyrq, string yylx, string jgid) { string sql = "select a.xingMing 姓名 ,a.idcard 身份证 ,a.adate 预约时间,isnull(b.bumenname,'德城区疾控中心') jgmc, row_number() over(order by a.id desc) as rownum from MedicalAppointment a left join OA..erpbumen b on a.jgid=b.id where 1=1 "; if (!string.IsNullOrEmpty(xm)) { sql += " and a.xingMing like @xm"; } if (!string.IsNullOrEmpty(yyrq)) { DateTime dt; if (DateTime.TryParse(yyrq, out dt)) { sql += " and a.ADate=@yyrq"; } } if (!string.IsNullOrEmpty(yylx) && yylx != "0") { sql += " and a.YYType=@yylx"; } using (var conn = CommHelper.GetSqlConnection()) { //return conn.(sql, new { xm = "%" + xm + "%", yyrq = yyrq, yylx = yylx }).ToList(); //System.Data.SqlClient.SqlDataAdapter sda=new System.Data.SqlClient.SqlDataAdapter () return conn.Query(sql, new { xm = "%" + xm + "%", yyrq = yyrq, yylx = yylx }).ToList(); } } public List getYYQKTJ(string b, string e) { //throw new NotImplementedException(); string sql = @"select CONVERT(varchar(10),Adate,121) rq, sum(case cyfa when 1 then 1 else 0 end) gg, sum(case cyfa when 2 then 1 else 0 end) sp, sum(case cyfa when 3 then 1 else 0 end) yp, sum(case YYType when 1 then 1 else 0 end) wx, sum(case YYType when 2 then 1 else 0 end) zzj, COUNT(1) hj from MedicalAppointment where 1=1 "; DateTime tb = DateTime.Now.Date; DateTime te = DateTime.Now.Date; if (!string.IsNullOrEmpty(b)) { if (DateTime.TryParse(b, out tb)) { sql += " and Adate>=@b"; } } if (!string.IsNullOrEmpty(e)) { if (DateTime.TryParse(e, out te)) { sql += " and Adate<@e"; } } sql += @" group by CONVERT(varchar(10),Adate,121) order by CONVERT(varchar(10),Adate,121)"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { b = tb, e = te.AddDays(1) }).ToList(); } } public object saveBKYYRQ(BKYYRQ bkyy) { //throw new NotImplementedException(); string sql = ""; if (bkyy.id == 0) { sql = "insert into bkyyrq values(@rq)"; } else { sql = "update bkyyrq set rq=@rq where id=@id"; } using (var conn = CommHelper.GetSqlConnection()) { try { int c = conn.Execute(sql, bkyy); if (c > 0) { return new { State = 1, Message = "操作成功!" }; } else { return new { State = 0, Message = "操作失败!" }; } } catch (Exception ex) { return new { State = 0, Message = ex.Message }; } } } public int getBKYYCount(string key) { //throw new NotImplementedException(); string sql = "select count(1) from bkyyrq where 1=1"; if (!string.IsNullOrEmpty(key)) { sql += " and rq=@rq"; } DateTime rq; if (!DateTime.TryParse(key, out rq)) { rq = DateTime.Now; } using (var conn = CommHelper.GetSqlConnection()) { return conn.ExecuteScalar(sql, new { rq }); } } public OperationResult canYY(DateTime aDate, string iDCard, int cyfa,int jgid) { //throw new NotImplementedException(); DateTime n = aDate; string sql = "select count(1) from MedicalAppointment where ADate=@ADate and isnull(jgid,49)=@jgid and CYFA=@CYFA"; using (var conn = CommHelper.GetSqlConnection()) { int c = conn.ExecuteScalar(sql, new { ADate = n,jgid=jgid,CYFA=cyfa }); int mryyrs = 0;// int.Parse(new Common().getParm_Value("mryyrs", "100", "每日最高可预约人数")); sql = "select kyyrs from set_jg_lx_kyyrs where cylx=@cylx and jgid=@jgid"; var v = conn.ExecuteReader(sql, new { cylx = cyfa, jgid = jgid }); if(v.Read()) { mryyrs = (int)v["kyyrs"]; } v.Close(); if (c < mryyrs) { sql = "select count(1) from bkyyrq where rq=@rq"; c = conn.ExecuteScalar(sql, new { rq = n }); if (c > 0) { return new OperationResult { State = 0, Message = "当日不能进行体检预约!" }; } sql = "select count(1) from MedicalAppointment where ADate=@ADate and IDCard=@IDCard and CYFA=@cyfa"; c = conn.ExecuteScalar(sql, new { ADate = n, IDCard = iDCard, cyfa = cyfa, }); if (c == 0) { sql = "select count(1) from MedicalAppointment where ADate>getdate() and status=0 and IDCard=@IDCard and CYFA=@cyfa"; c = conn.ExecuteScalar(sql, new { cyfa = cyfa, IDCard = iDCard }); if (c > 0) { return new OperationResult { State = 0, Message = "你有未完成的预约不能进行预约!" }; } else { if (cyfa == 1 || cyfa == 2) { //<>'快' 是不是bug????zyk20230615 string xjkzsql = @"select top 1 register_date from professionalExam_register where card_number='" + iDCard + @"' and result_status='合格' and exam_type='从业人员体检' and medical_scheme_maintain_id=" + cyfa + @" and special_health_certificate <>'快' order by id desc"; var lsjg = conn.Query(xjkzsql).ToList(); if (lsjg.Count > 0) { string tqyydays = new Common().getParm_Value("ktqyyrq", "30", "健康证到期前可提前预约日期"); int day = int.Parse(tqyydays); DateTime dqrq = DateTime.Parse(lsjg[0].register_date); if ((dqrq.AddYears(1) - DateTime.Now).Days > day) { return new OperationResult { State = 0, Message = "健康证到期前" + tqyydays + "天才可进行预约,您的健康证目前还有" + (dqrq.AddYears(1) - DateTime.Now).Days + "天到期!" }; } else { return new OperationResult { State = 1, Message = "可以预约!" }; } } else { return new OperationResult { State = 1, Message = "可以预约!" }; } } else { return new OperationResult { State = 1, Message = "可以预约!" }; } } } else { return new OperationResult { State = 0, Message = "你已经预约不能重复预约!" }; } } else { return new OperationResult { State = 0, Message = "当日预约人数已满!" }; } } } public List canSelected(string date, string enddate,string jgid,string CYFA) { string sql = @" select lb.adate ,(select kyyrs from set_jg_lx_kyyrs where jgid=@jgid and cylx=@CYFA ) as 'kyyrs',(select sum(kyyrs) from set_jg_lx_kyyrs where jgid=@jgid ) as 'kyyzrs', case when la.yyrs is null then 0 else la.yyrs end yyrs ,case when lc.zyyrs is null then 0 else lc.zyyrs end zyyrs from ( (SELECT CONVERT(varchar(10), DateAdd(day,number,@adate),121) adate FROM master..spt_values WHERE type = 'p' AND number <= DateDiff(day,@adate,@adate1) )lb left join (select * from( select convert(varchar(10),adate,121) adate,count(*)as 'yyrs' ,CYFA,isnull(jgid,49) jgid from MedicalAppointment group by convert(varchar(10),adate,121),CYFA,isnull(jgid,49))a where a.ADate > @adate and a.adate<@adate1 and a.CYFA=@CYFA and isnull(a.jgid,49)=@jgid) la on lb.adate=la.adate left join (select * from( select convert(varchar(10),adate,121) adate,count(*)as 'zyyrs' ,isnull(jgid,49) jgid from MedicalAppointment group by convert(varchar(10),adate,121),isnull(jgid,49))a where a.ADate > @adate and a.adate<@adate1 and isnull(a.jgid,49)=@jgid) lc on lc.adate=la.adate) "; using (var conn = CommHelper.GetSqlConnection()) { string ksrq = new Common().getParm_Value("bzxyyksrq", "2018-05-02", "不限制预约开始日期"); string jsrq = new Common().getParm_Value("bzxyyjsrq", "2018-06-30", "不限制预约结束日期"); DateTime dksrq = DateTime.Parse(ksrq); DateTime djsrq = DateTime.Parse(jsrq); List al = conn.Query(sql, new { adate = date, adate1 = enddate ,CYFA,jgid}).ToList(); List news = new List(); if (al.Any()) { foreach (var m in al) { WeiXinDate model = new WeiXinDate(); switch (Convert.ToDateTime(m.adate).DayOfWeek.ToString()) { case "Saturday1": DateTime a = DateTime.Parse(m.adate); if (a >= dksrq && a <= djsrq) { goto def; } else { model.adate = m.adate; model.yyrs = new Common().getParm_Value("mryyrs", "100", ""); model.zyyrs = new Common().getParm_Value("mryyrs", "100", ""); model.kyyzrs = new Common().getParm_Value("mryyrs", "100", ""); model.kyyrs = new Common().getParm_Value("mryyrs", "100", ""); news.Add(model); } break; case "Sunday1": model.adate = m.adate; model.yyrs = new Common().getParm_Value("mryyrs", "100", ""); model.kyyrs = new Common().getParm_Value("mryyrs", "100", ""); model.zyyrs = new Common().getParm_Value("mryyrs", "100", ""); model.kyyzrs = new Common().getParm_Value("mryyrs", "100", ""); news.Add(model); break; default: def: sql = "select count(1) from bkyyrq where rq=@rq"; int c = conn.ExecuteScalar(sql, new { rq = m.adate }); if (c > 0) { model.adate = m.adate; model.yyrs = new Common().getParm_Value("mryyrs", "100", ""); model.kyyrs = new Common().getParm_Value("mryyrs", "100", ""); model.zyyrs = new Common().getParm_Value("mryyrs", "100", ""); model.kyyzrs = new Common().getParm_Value("mryyrs", "100", ""); news.Add(model); } else { news.Add(m); } break; } } } return news; } } public bool Update(MedicalAppointment model) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = @"UPDATE [MedicalAppointment] SET [openid] = @openid ,[xingMing] = @xingMing ,[IDCard] = @IDCard ,[ADate] = @ADate ,[ATime] =@ATime ,[Mobile] =@Mobile ,[MType] = @MType ,[CYFA] = @CYFA ,[CYFAName] = @CYFAName ,[QYXX] =@QYXX ,[YHYS] =@YHYS ,[ZGZT] = @ZGZT ,[QYID] = @QYID ,[YYCode] =@YYCode ,[ZFZT] = @ZFZT ,[YYUserID] = @YYUserID ,[ZFCode] = @ZFCode WHERE id=@id"; return conn.Execute(sql, model) != 0; } } public MedicalAppointment GetModel(string id) { string sql = @"select a.[id] ,a.[openid] ,a.[xingMing] ,a.[IDCard] ,convert(varchar(10),a.[ADate],121) ADate ,a.[ATime] ,a.[Mobile] ,a.[MType] ,a.[CYFA] ,a.[CYFAName] ,a.[QYXX] ,a.[YHYS] ,a.[ZGZT] ,a.[QYID] ,a.[YYCode] ,a.[ZFCode] ,a.[ZFZT] ,a.[YYUserID] ,a.[SFJE],a.jgid,isnull(b.bumenname,'德城区疾控中心') jgmc from MedicalAppointment a left join OA..erpbumen b on a.jgid=b.id where a.id=@id"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { id = id }).First(); } } public MedicalAppointment GetModelByzfcode(string id) { string sql = @"select [id] ,[openid] ,[xingMing] ,[IDCard] ,convert(varchar(10),[ADate],121) ADate ,[ATime] ,[Mobile] ,[MType] ,[CYFA] ,[CYFAName] ,[QYXX] ,[YHYS] ,[ZGZT] ,[QYID] ,[YYCode] ,[ZFCode] ,[ZFZT] ,[YYUserID] ,[SFJE] from MedicalAppointment where ZFCode=@ZFCode"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { ZFCode = id }).First(); } } public List GetSjd(string yyrq, string jgid) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select sjdid as [key],count(0) as value from MedicalAppointment where adate=@yyrq and jgid=@jgid group by sjdid"; return conn.Query(sql, new { yyrq = yyrq, jgid = jgid }).ToList(); } } public List GetModelByOpenid(string openid) { string sql = "select * from MedicalAppointment where openid=@openid"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { openid = openid }).ToList(); } } public MedicalAppointment getMedicalModel(String openid,String yyrq) { String sql = "select * from MedicalAppointment where openid='"+ openid + "' and ADate='"+ yyrq + "'"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql).FirstOrDefault(); } } /// /// 根据微信主键给缴费字段做处理 /// /// /// public bool ChargePay(string id) { string sql = "update MedicalAppointment set zfzt='1' where id=@id"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Execute(sql, new { id = id }) != 0; } } public int getAllMedicalAppointmentcount(MedicalAppointment model) { string sql = "SELECT count(1) FROM [dbo].[MedicalAppointment] where QYID=@QYID"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { int count = conn.Query(sql, new { @QYID = model.QYID }).FirstOrDefault(); return count; } } public List getAllMedicalAppointment(int page, int pagesize, MedicalAppointment model) { string sql = "SELECT [id],[openid] ,[xingMing] ,[IDCard],[ADate],[ATime],[Mobile],[MType]" + " ,[CYFA],[CYFAName],[YHYS],[ZGZT],[YYCode],[ZFCode],[ZFZT],[YYUserID],[SFJE],row_number() over(order by id) as rownum" + " FROM [dbo].[MedicalAppointment] where QYID=@QYID "; sql = "select * from (" + sql + ") t where t.rownum>(" + page + "-1)*" + pagesize + " and rownum<=" + page + "*" + pagesize; using (IDbConnection conn = CommHelper.GetSqlConnection()) { List list = conn.Query(sql, model).ToList(); foreach (var item in list) { if (item.YHYS != null) { string[] strarry = item.YHYS.Split(','); string sqlitem = "SELECT * FROM harmful_factors_maintain WHERE ID IN("; var count = 0; foreach (var str in strarry) { if (count == 0) { sqlitem += "" + str + ""; } else { sqlitem += "," + str + ""; } } sqlitem += ")"; List itemlist = conn.Query(sqlitem).ToList(); item.YHYS = ""; count = 0; foreach (var itemyhys in itemlist) { if (count == 0) { item.YHYS += itemyhys.harmful_factor_name; } else { item.YHYS += ("," + itemyhys.harmful_factor_name); } } } string sqlitemzgzt = "SELECT * FROM jobs_state_maintain WHERE ID =@id"; JobsStateMaintainModel itemzgzt = conn.Query(sqlitemzgzt, new { @id = item.ZGZT }).FirstOrDefault(); if (itemzgzt != null) { item.QYXX = itemzgzt.jobs_state; } } return list; } } public List getTjztList(string tm) { //throw new NotImplementedException(); string sql = "select * from examination_process where 1=1"; if (!string.IsNullOrEmpty(tm)) { sql += "and person_id in (select id from professionalExam_register where physical_num = '" + tm + "')"; } using (var conn = CommHelper.GetSqlConnection()) { return conn.Query(sql).ToList(); } } public List getYYList(string xm, string yyrq, string yylx, int page, int pagesize,int jgid,int ijgid) { //throw new NotImplementedException(); string sql = "select a.*,isnull(b.bumenname,'德城区疾控中心') jgmc, row_number() over(order by a.id desc) as rownum from MedicalAppointment a left join OA..erpbumen b on a.jgid=b.id where 1=1 "; if (!string.IsNullOrEmpty(xm)) { sql += " and a.xingMing like @xm"; } if (!string.IsNullOrEmpty(yyrq)) { DateTime dt; if (DateTime.TryParse(yyrq, out dt)) { sql += " and a.ADate=@yyrq"; } } if (!string.IsNullOrEmpty(yylx) && yylx != "0") { sql += " and a.YYType=@yylx"; } if(jgid!=49) { sql += " and a.jgid=@jgid"; } if(ijgid!=0) { sql += " and a.jgid=@ijgid"; } using (var conn = CommHelper.GetSqlConnection()) { sql = "select * from (" + sql + ") t where t.rownum>(" + page + "-1)*" + pagesize + " and rownum<=" + page + "*" + pagesize; return conn.Query(sql, new { xm = "%" + xm + "%", yyrq = yyrq, yylx = yylx,jgid,ijgid }).ToList(); } } public List getYYList2(string xm, int page, int pagesize) { string sql = "select *, row_number() over(order by id desc) as rownum from yyqxjl where 1=1 "; if (!string.IsNullOrEmpty(xm)) { sql += " and yyxx like @xm"; } using (var conn = CommHelper.GetSqlConnection()) { sql = "select * from (" + sql + ") t where t.rownum>(" + page + "-1)*" + pagesize + " and rownum<=" + page + "*" + pagesize; return conn.Query(sql, new { xm = "%" + xm + "%" }).ToList(); } } public int getqxyyCount(string xm) { string sql = "select count(1) from yyqxjl where 1=1 "; if (!string.IsNullOrEmpty(xm)) { sql += " and yyxx like @xm"; } using (var conn = CommHelper.GetSqlConnection()) { return conn.ExecuteScalar(sql, new { xm = "%" + xm + "%" }); } } public int getyyCount(string xm, string yyrq, string yylx,int jgid,int ijgid) { string sql = "select count(1) from MedicalAppointment where 1=1"; if (!string.IsNullOrEmpty(xm)) { sql += " and xingMing like @xm"; } if (!string.IsNullOrEmpty(yyrq)) { DateTime dt; if (DateTime.TryParse(yyrq, out dt)) { sql += " and ADate=@yyrq"; } } if (!string.IsNullOrEmpty(yylx) && yylx != "0") { sql += " and YYType=@yylx"; } if(jgid!=49) { sql += " and jgid=@jgid"; } if (ijgid != 0) { sql += " and jgid=@ijgid"; } using (var conn = CommHelper.GetSqlConnection()) { return conn.ExecuteScalar(sql, new { xm = "%" + xm + "%", yyrq = yyrq, yylx = yylx,jgid,ijgid }); } } public int getTjztCount(string tm) { string sql = "select count(1) from examination_process where person_id in (select id from professionalExam_register where physical_num = '" + tm + "')"; using (var conn = CommHelper.GetSqlConnection()) { return conn.ExecuteScalar(sql); } } public ProfessionalExamRegisterModel getTjztXm(string tm) { string sql = "select * from professionalExam_register where physical_num='" + tm + "'"; using (var conn = CommHelper.GetSqlConnection()) { return conn.Query(sql).First(); } } public void qxyy(string id, string openid, string ip, string yyxx) { using (var conn = CommHelper.GetSqlConnection()) { var tran = conn.BeginTransaction(); try { //throw new NotImplementedException(); string sql = "insert into yyqxjl(ip,openid,yyxx,qxsj) values(@ip,@openid,@yyxx,getdate())"; conn.Execute(sql, new { ip, openid, yyxx }, tran); sql = "delete from MedicalAppointment where id=@id"; conn.Execute(sql, new { id }, tran); tran.Commit(); } catch (Exception ex) { tran.Rollback(); throw ex; } } } /// /// 根据id删除不可预约日期 /// /// /// public object del(string id) { string sql = @"delete bkyyrq where id=@id"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { int c = conn.Execute(sql, new { id = id }); if (c > 0) { return new { State = 1, Message = "操作成功!" }; } else { return new { State = 0, Message = "操作失败,请联系管理员!" }; } } catch (Exception ex) { return new { State = 0, Message = ex.Message }; } } } } }