人民医院API

DeviceController.cs 86KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345
  1. using RMYY_CallCenter_Api.DB;
  2. using RMYY_CallCenter_Api.Model;
  3. using RMYY_CallCenter_Api.Utility;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. namespace RMYY_CallCenter_Api.Controllers.System
  14. {
  15. public class DeviceController : BaseController
  16. {
  17. BLL.T_Adjust_Device deviceservice = new BLL.T_Adjust_Device();
  18. BLL.T_Adjust_DeviceType typeservice = new BLL.T_Adjust_DeviceType();
  19. BLL.T_Adjust_Log logBll = new BLL.T_Adjust_Log();
  20. private static InternalMessagesController msg = new InternalMessagesController();
  21. BLL.T_Adjust_TpInfo tpinfoBll = new BLL.T_Adjust_TpInfo();
  22. Bll.T_Sys_Department deptbll = new Bll.T_Sys_Department();
  23. /// <summary>
  24. /// 设备信息list Device/GetList
  25. /// </summary>
  26. /// <param name="typeName">设备名称</param>
  27. /// <param name="dicId">设备类型对应的字典的id</param>
  28. /// <param name="assetCode">资产编码</param>
  29. /// <param name="deviceShortCode">设备短码</param>
  30. /// <param name="deptId">所属科室</param>
  31. /// <param name="deviceStatus">int 设备状态 设备调配(1待上架,2闲置,3使用中) </param>
  32. /// <param name="orderBy"> 排序 F_DicId或 F_UpdateTime</param>
  33. /// <param name="page"></param>
  34. /// <param name="pagesize"></param>
  35. /// <param name="orderByRule">倒序还是正序,desc或者 asc</param>
  36. /// <returns></returns>
  37. public ActionResult GetList(string typeName, string assetCode, string code, string deviceTypeId, string deviceShortCode, string orderBy, string deviceStatus, int orderByRule = 0, int page = 1, int pagesize = 10, int isdc = 0, int deptId = 0, int dicId = 0)
  38. {
  39. string sql = " and F_IsDelete=0 and F_Classify=1";
  40. if (!string.IsNullOrEmpty(deviceTypeId))
  41. {
  42. sql += " and F_DeviceTypeId in ( " + deviceTypeId + ")";
  43. }
  44. if (!string.IsNullOrEmpty(code))
  45. {
  46. sql += " and (F_AssetCode='" + code + "' or F_DeviceShortCode='" + code + "')";
  47. }
  48. DataTable dt = new DataTable();
  49. string strorderby = " order by ";
  50. if (string.IsNullOrEmpty(orderBy))
  51. {
  52. strorderby += " F_UpdateTime";
  53. }
  54. else
  55. {
  56. strorderby += orderBy;
  57. }
  58. if (orderByRule == 0)
  59. {
  60. strorderby += " desc";
  61. }
  62. else
  63. {
  64. strorderby += " asc";
  65. }
  66. if (!string.IsNullOrEmpty(typeName))
  67. {
  68. sql += " and F_TypeName like '%" + typeName + "%' ";
  69. }
  70. if (dicId > 0)
  71. {
  72. sql += " and F_DicId='" + dicId + "' ";
  73. }
  74. if (!string.IsNullOrEmpty(assetCode))
  75. {
  76. sql += " and F_AssetCode='" + assetCode + "' ";
  77. }
  78. if (!string.IsNullOrEmpty(deviceShortCode))
  79. {
  80. sql += " and F_DeviceShortCode='" + deviceShortCode + "' ";
  81. }
  82. if (deptId > 0)
  83. {
  84. sql += " and F_BelongDept='" + deptId + "' ";
  85. }
  86. if (!string.IsNullOrEmpty(deviceStatus))
  87. {
  88. //string[] strlist= deviceStatus.Split(',');
  89. //List<int> lis = new List<int>();
  90. //foreach (var item in strlist)
  91. //{
  92. // lis.Add(Convert.ToInt32(item));
  93. //}
  94. sql += " and F_DeviceStatus in (" + deviceStatus + ") ";
  95. }
  96. if (isdc > 0)
  97. {
  98. string dcsql = " and a.F_IsDelete=0 and and a.F_Classify=1";
  99. string dcstrorderby = " order by ";
  100. if (string.IsNullOrEmpty(orderBy))
  101. {
  102. dcstrorderby += " a.F_UpdateTime";
  103. }
  104. else
  105. {
  106. dcstrorderby += orderBy;
  107. }
  108. if (orderByRule == 0)
  109. {
  110. strorderby += " desc";
  111. }
  112. else
  113. {
  114. strorderby += " asc";
  115. }
  116. //if (!string.IsNullOrEmpty(code))
  117. //{
  118. // sql += " and (a.F_AssetCode='" + code + "' or a. F_DeviceShortCode='" + code + "')";
  119. //}
  120. if (!string.IsNullOrEmpty(typeName))
  121. {
  122. dcsql += " and a.F_TypeName like '%" + typeName + "%' ";
  123. }
  124. if (dicId > 0)
  125. {
  126. dcsql += " and a.F_DicId='" + dicId + "' ";
  127. }
  128. if (!string.IsNullOrEmpty(assetCode))
  129. {
  130. dcsql += " and a.F_AssetCode='" + assetCode + "' ";
  131. }
  132. if (!string.IsNullOrEmpty(deviceShortCode))
  133. {
  134. dcsql += " and a.F_DeviceShortCode='" + deviceShortCode + "' ";
  135. }
  136. if (deptId > 0)
  137. {
  138. dcsql += " and a.F_BelongDept='" + deptId + "' ";
  139. }
  140. if (!string.IsNullOrEmpty(deviceStatus))
  141. {
  142. sql += " and a.F_DeviceStatus in (" + deviceStatus + ") ";
  143. }
  144. //if (deviceTypeId > 0)
  145. //{
  146. // dcsql += " and a.F_DeviceTypeId=" + deviceTypeId + "";
  147. //}
  148. if (!string.IsNullOrEmpty(deviceTypeId))
  149. {
  150. dcsql += " and F_DeviceTypeId in ( " + deviceTypeId + " )";
  151. }
  152. var dtdc = DB.DbHelperSQL.Query("SELECT a.F_BigTypeName 设备类型,a.F_TypeName 设备名称,F_AssetCode 资产编码 , F_DeviceShortCode 设备短码,F_Specification 规格型号, F_Code 出厂编码,F_BelongDeptName 归属科室, F_BelongPhone 归属科室电话, (CASE F_DeviceStatus WHEN 1 THEN '待上架' WHEN 2 THEN '闲置' WHEN 3 THEN '使用中' ELSE '' END) 设备状态,CAST(b.F_price AS VARCHAR) + '/' + b.F_ChargingStandard 计费标准 from T_Adjust_Device a LEFT join T_Adjust_DeviceType b on b.F_TypeId = a.F_DeviceTypeId where 1=1 " + dcsql + dcstrorderby).Tables[0];
  153. var msg = new NPOIHelper().ExportToExcel("设备管理", dtdc);
  154. if (msg == "")
  155. {
  156. return Success("导出成功");
  157. }
  158. else
  159. {
  160. return Error("导出失败");
  161. }
  162. }
  163. int recordCount = 0;
  164. dt = Bll.PagerBll.GetListPager(
  165. "(SELECT T_Adjust_Device.*,b.F_Price,F_ChargingStandard from T_Adjust_Device LEFT join T_Adjust_DeviceType b on b.F_TypeId=T_Adjust_Device.F_DeviceTypeId ) T_Adjust_Device",
  166. "F_DeviceId",
  167. "*",
  168. sql,
  169. strorderby,
  170. pagesize,
  171. page,
  172. true,
  173. out recordCount);
  174. dt.Columns.Add("FileUrl", typeof(object));
  175. foreach (DataRow dr in dt.Rows)
  176. {
  177. if (!string.IsNullOrEmpty(fileurlpath) && !string.IsNullOrEmpty(dr["F_File"].ToString()))
  178. {
  179. dr["FileUrl"] = GetFileData(dr["F_File"].ToString(), fileurlpath);
  180. }
  181. }
  182. var obj = new
  183. {
  184. state = "success",
  185. message = "成功",
  186. rows = dt,
  187. total = recordCount
  188. };
  189. return Content(obj.ToJson());
  190. }
  191. /// <summary>
  192. /// 添加或编辑设备信息 Device/AddOrEditDevice
  193. /// </summary>
  194. /// <param name="address">部门地址</param>
  195. /// <param name="deptId">部门id</param>
  196. /// <param name="deptName">部门名</param>
  197. /// <param name="phone">部门电话</param>
  198. /// <param name="assetCode">资产编码</param>
  199. /// <param name="deviceShortCode">设备断码</param>
  200. /// <param name="code">出厂编号</param>
  201. /// <param name="specification">规格型号</param>
  202. /// <param name="deviceTypeId">设备类型表的id</param>
  203. /// <param name="deviceId">设备id</param>
  204. /// <returns></returns>
  205. public ActionResult AddOrEditDevice(string address, int deptId, string deptName
  206. , string phone, string assetCode, string deviceShortCode, string file,
  207. string code, string specification, string bigTypeName, string typeName, int dicId = 0,
  208. int deviceTypeId = 0, int deviceId = 0)
  209. {
  210. int chuancan = deviceTypeId;
  211. if (deviceTypeId == 0)
  212. {
  213. if (string.IsNullOrEmpty(bigTypeName) && string.IsNullOrEmpty(typeName))
  214. {
  215. return Error("请选择或者输入设备类型和设备名称");
  216. }
  217. }
  218. Model.T_Adjust_DeviceType typemodel = new T_Adjust_DeviceType();
  219. if (deviceTypeId > 0)
  220. {
  221. typemodel = typeservice.GetModel(deviceTypeId);
  222. if (typemodel == null)
  223. {
  224. return Error(" 设备类型不存在");
  225. }
  226. }
  227. else
  228. {
  229. //找不到字典 判断字典表是否存在,不存在加一个字典值
  230. int dicaddid = 0;
  231. if (dicId > 0)
  232. {
  233. Model.T_Sys_DictionaryValue dicmodel = new Bll.T_Sys_DictionaryValue().GetModel(dicId);
  234. if (dicmodel != null)
  235. {
  236. dicaddid = dicmodel.F_DictionaryValueId;
  237. }
  238. }
  239. else
  240. {
  241. Model.T_Sys_DictionaryValue dicmodel = new Bll.T_Sys_DictionaryValue().GetModel(bigTypeName);
  242. if (dicmodel != null)
  243. {
  244. dicaddid = dicmodel.F_DictionaryValueId;
  245. }
  246. else
  247. {
  248. dicaddid = new Bll.T_Sys_DictionaryValue().Add(bigTypeName);
  249. }
  250. }
  251. //先添加字典的记录
  252. //添加设备类型记录
  253. // int dicaddid = new Bll.T_Sys_DictionaryValue().Add(bigTypeName);
  254. Model.T_Adjust_DeviceType modeltype = typeservice.GetModel(bigTypeName, typeName, 1);
  255. if (modeltype == null)
  256. {
  257. Model.T_Adjust_DeviceType model = new T_Adjust_DeviceType();
  258. model.F_DeviceNum = 0;
  259. model.F_IsDelete = 0;
  260. model.F_Classification = 1;
  261. model.F_CreateBy = User.F_UserCode;
  262. model.F_BigTypeName = bigTypeName;
  263. model.F_TypeName = typeName;
  264. model.F_DicId = dicaddid;
  265. model.F_CreateTime = DateTime.Now;
  266. int res = typeservice.Add(model);
  267. deviceTypeId = res;
  268. }
  269. else { deviceTypeId = modeltype.F_TypeId; }
  270. }
  271. if (string.IsNullOrEmpty(assetCode))
  272. {
  273. return Error("资产编码不能为空");
  274. }
  275. var existslist = deviceservice.GetModelList(" F_AssetCode ='" + assetCode + "'");
  276. if (deviceId > 0)
  277. {
  278. if (existslist.Count == 0)
  279. { }
  280. else if (existslist.Count == 1 && existslist.First().F_DeviceId == deviceId)
  281. { }
  282. else
  283. {
  284. return Error("资产编码不能重复!");
  285. }
  286. ////设备类型的数量加1
  287. //var mm = typeservice.GetModel(deviceTypeId);
  288. //mm.F_DeviceNum = mm.F_DeviceNum == null ? 1 : mm.F_DeviceNum + 1;
  289. //mm.F_UpdateTime = DateTime.Now;
  290. //typeservice.Update(mm);
  291. if (deviceTypeId != existslist.First().F_DeviceId)
  292. {
  293. //修改了类型说明
  294. var mm = typeservice.GetModel(deviceTypeId);
  295. mm.F_DeviceNum = mm.F_DeviceNum == null ? 1 : mm.F_DeviceNum + 1;
  296. mm.F_UpdateTime = DateTime.Now;
  297. typeservice.Update(mm);
  298. var olddevice = deviceservice.GetModel(existslist.First().F_DeviceId);
  299. var oldtype = typeservice.GetModel(olddevice.F_DeviceTypeId);
  300. oldtype.F_DeviceNum = oldtype.F_DeviceNum - 1;
  301. oldtype.F_UpdateTime = DateTime.Now;
  302. typeservice.Update(oldtype);
  303. }
  304. Model.T_Adjust_Device model = deviceservice.GetModel(deviceId);
  305. //编辑
  306. model.F_BigTypeName = typemodel.F_BigTypeName;
  307. model.F_TypeName = typemodel.F_TypeName;
  308. model.F_DicId = typemodel.F_DicId;
  309. model.F_BelongPhone = phone;
  310. model.F_BelongAddress = address;
  311. model.F_BelongDept = deptId;
  312. model.F_BelongDeptName = deptName;
  313. model.F_DeviceTypeId = deviceTypeId;
  314. model.F_AssetCode = assetCode;
  315. model.F_DeviceShortCode = deviceShortCode;
  316. model.F_Specification = specification;
  317. model.F_Code = code;
  318. model.F_File = file;
  319. model.F_UpdateTime = DateTime.Now;
  320. if (deviceservice.Update(model))
  321. {
  322. return Success("修改成功");
  323. }
  324. else
  325. {
  326. return Error("修改失败");
  327. }
  328. }
  329. else
  330. {
  331. if (existslist.Count > 0)
  332. {
  333. return Error("资产编码不能重复!");
  334. }
  335. Model.T_Adjust_Device model = new T_Adjust_Device();
  336. model.F_BigTypeName = typemodel.F_BigTypeName;
  337. model.F_TypeName = typemodel.F_TypeName;
  338. model.F_DicId = typemodel.F_DicId;
  339. model.F_BelongPhone = phone;
  340. model.F_BelongAddress = address;
  341. model.F_BelongDept = deptId;
  342. model.F_BelongDeptName = deptName;
  343. model.F_DeviceTypeId = deviceTypeId;
  344. model.F_AssetCode = assetCode;
  345. model.F_DeviceShortCode = deviceShortCode;
  346. model.F_Specification = specification;
  347. model.F_Code = code;
  348. model.F_Source = 1;
  349. model.F_CreateBy = User.F_UserCode;
  350. model.F_CreateTime = DateTime.Now;
  351. model.F_UpdateTime = DateTime.Now;
  352. model.F_DeviceStatus = 1;//待上架
  353. model.F_Classify = 1;
  354. model.F_IsDelete = 0;
  355. model.F_File = file;
  356. int res = deviceservice.Add(model);
  357. if (res > 0)
  358. {
  359. //设备类型的数量加1
  360. var mm = typeservice.GetModel(deviceTypeId);
  361. mm.F_DeviceNum = mm.F_DeviceNum == null ? 1 : mm.F_DeviceNum + 1;
  362. mm.F_UpdateTime = DateTime.Now;
  363. typeservice.Update(mm);
  364. return Success("添加成功");
  365. }
  366. else
  367. {
  368. return Error("添加失败");
  369. }
  370. }
  371. }
  372. /// <summary>
  373. /// 删除设备 DelDeviceType
  374. /// </summary>
  375. /// <param name="deviceId"></param>
  376. /// <returns></returns>
  377. public ActionResult DelDevice(int deviceId)
  378. {
  379. if (deviceId <= 0)
  380. {
  381. return Error("请选中要删除的列");
  382. }
  383. string sql = " update T_Adjust_Device set F_IsDelete=1 where F_DeviceId=" + deviceId;
  384. if (DbHelperSQL.ExecuteSql(sql) > 0)
  385. {
  386. //把设备类型表的数量-1
  387. string updatenum = " update T_Adjust_DeviceType set F_DeviceNum=(SELECT Count(1) from T_Adjust_Device WHERE F_IsDelete=0 and F_DeviceTypeId in (SELECT F_DeviceTypeId from T_Adjust_Device WHERE F_IsDelete=0 and F_DeviceId=" + deviceId + ")) WHERE F_TypeId in (SELECT F_DeviceTypeId from T_Adjust_Device WHERE F_DeviceId=" + deviceId + ")";
  388. DbHelperSQL.ExecuteSql(updatenum);
  389. return Success("删除成功");
  390. }
  391. else
  392. {
  393. return Error("删除失败");
  394. }
  395. }
  396. /// <summary>
  397. /// GetTypeName 添加编辑时根据dicid获取设备名称下拉框
  398. /// </summary>
  399. /// <param name="dicId"></param>
  400. /// <returns></returns>
  401. public ActionResult GetTypeName(string bigTypeName, string typeName, int dicId = 0, int classification = 0)
  402. {
  403. string sql = " F_IsDelete=0 ";
  404. if (dicId >= 0)
  405. {
  406. sql += " and F_DicId=" + dicId + " ";
  407. }
  408. if (classification > 0)
  409. {
  410. sql += " and F_Classification=" + classification + " ";
  411. }
  412. if (!string.IsNullOrEmpty(bigTypeName))
  413. {
  414. sql += " and F_BigTypeName='" + bigTypeName + "' ";
  415. }
  416. if (!string.IsNullOrEmpty(typeName))
  417. {
  418. sql += " and F_TypeName='" + typeName + "' ";
  419. }
  420. List<T_Adjust_DeviceType> devices = typeservice.GetModelList(sql);
  421. var obj = new
  422. {
  423. state = "success",
  424. message = "成功",
  425. rows = devices
  426. };
  427. return Content(obj.ToJson());
  428. //return deviceservice.GetModelList(" F_DicId='" + dicId + "' and F_IsDelete=0");
  429. }
  430. /// <summary>
  431. /// BatchUpdate
  432. /// </summary>
  433. /// <param name="ids"></param>
  434. /// <param name="operateType">下架1 上架2 </param>
  435. /// <returns></returns>
  436. public ActionResult BatchUpdate(int[] ids, int operateType)
  437. {
  438. int total = 0;
  439. if (ids != null && ids.Length > 0)
  440. {
  441. foreach (int item in ids)
  442. {
  443. Model.T_Adjust_Device model = deviceservice.GetModel(item);
  444. if (model.F_DeviceStatus == 3)
  445. {
  446. continue;
  447. }
  448. model.F_DeviceStatus = operateType;
  449. if (operateType == 2)
  450. {
  451. model.F_ListingTime = DateTime.Now;
  452. }
  453. else
  454. {
  455. model.F_ListingTime = null;
  456. }
  457. model.F_UpdateTime = DateTime.Now;
  458. bool insertlog = false;
  459. if (model.F_Classify == 2)
  460. {
  461. insertlog = true;
  462. }
  463. if (deviceservice.Update(model))
  464. {
  465. if (insertlog)
  466. {
  467. string desc = "";
  468. if (operateType == 1)
  469. {
  470. desc = "下架";
  471. }
  472. else
  473. {
  474. desc = "上架";
  475. }
  476. InsertLog(model.F_DeviceId, desc);
  477. }
  478. total++;
  479. }
  480. }
  481. if (total > 0)
  482. {
  483. return Success("批量修改成功");
  484. }
  485. else
  486. {
  487. return Error("批量修改失败");
  488. }
  489. }
  490. else
  491. {
  492. return Error("获取参数失败");
  493. }
  494. }
  495. /// <summary>
  496. /// 我的设备列表 Device/GetMyDeviceList
  497. /// </summary>
  498. /// <param name="typeName">设备名称</param>
  499. /// <param name="startTime"></param>
  500. /// <param name="endTime"></param>
  501. /// <param name="dicId">设备类型</param>
  502. /// <param name="deviceStatus">设备状态传int 1 待上架 2 闲置 3 使用中</param>
  503. /// <param name="page"></param>
  504. /// <param name="pagesize"></param>
  505. /// <returns></returns>
  506. public ActionResult GetMyDeviceList(
  507. string typeName, string startTime, string endTime, int dicId = 0, int deviceStatus = 0, int page = 1, int pagesize = 10, int isdc = 0)
  508. {
  509. DataTable dt = new DataTable();
  510. string sql = " and F_IsDelete=0 and F_Classify=2";
  511. int mydeptid = User.F_DeptId;
  512. sql += " and (F_BelongDept= " + mydeptid + " or F_UseDept=" + mydeptid + " )";
  513. if (dicId > 0)
  514. {
  515. sql += " and F_DicId='" + dicId + "' ";
  516. }
  517. if (!string.IsNullOrEmpty(startTime))
  518. {
  519. sql += " and F_ListingTime>='" + startTime + "'";
  520. }
  521. if (!string.IsNullOrEmpty(endTime))
  522. {
  523. sql += " and F_ListingTime<='" + endTime + "'";
  524. }
  525. if (!string.IsNullOrEmpty(typeName))
  526. {
  527. sql += " and F_TypeName like '%" + typeName + "%' ";
  528. }
  529. if (deviceStatus > 0)
  530. {
  531. sql += " and F_DeviceStatus='" + deviceStatus + "' ";
  532. }
  533. if (isdc > 0)
  534. {
  535. var dtdc = DB.DbHelperSQL.Query(" SELECT F_TypeName 设备名称, F_Desc 设备描述,F_AssetCode 资产编码 , F_DeviceShortCode 设备短码,F_Specification 规格型号, F_BelongDeptName 归属科室, F_BelongPhone 归属科室电话,(CASE F_DeviceStatus WHEN 1 THEN '待上架' WHEN 2 THEN '可调拨' WHEN 3 THEN '调拨中' ELSE ''END) 设备状态,F_ListingTime 上架时间 from T_Adjust_Device a WITH(NOLOCK) where 1=1 " + sql + "order by F_DeviceId desc").Tables[0];
  536. var msg = new NPOIHelper().ExportToExcel("我的设备", dtdc);
  537. if (msg == "")
  538. {
  539. return Success("导出成功");
  540. }
  541. else
  542. {
  543. return Error("导出失败");
  544. }
  545. }
  546. int recordCount = 0;
  547. dt = Bll.PagerBll.GetListPager(
  548. "T_Adjust_Device",
  549. "F_DeviceId",
  550. "*",
  551. sql,
  552. " order by F_DeviceId desc",
  553. pagesize,
  554. page,
  555. true,
  556. out recordCount);
  557. dt.Columns.Add("FileUrl", typeof(object));
  558. foreach (DataRow dr in dt.Rows)
  559. {
  560. if (!string.IsNullOrEmpty(fileurlpath) && !string.IsNullOrEmpty(dr["F_File"].ToString()))
  561. {
  562. dr["FileUrl"] = GetFileData(dr["F_File"].ToString(), fileurlpath);
  563. }
  564. }
  565. var obj = new
  566. {
  567. state = "success",
  568. message = "成功",
  569. rows = dt,
  570. total = recordCount
  571. };
  572. return Content(obj.ToJson());
  573. }
  574. /// <summary>
  575. /// 我的设备列表中添加编辑设备 Device/AddOrEditMyDevice
  576. /// </summary>
  577. /// <param name="address">部门地址</param>
  578. /// <param name="phone">部门电话</param>
  579. /// <param name="assetCode">资产编码</param>
  580. /// <param name="file">文件 string</param>
  581. /// <param name="desc">描述</param>
  582. /// <param name="deviceTypeId">设备类型表的主键id,不是dicId</param>
  583. /// <param name="deviceId">设备id,编辑用</param>
  584. /// <returns></returns>
  585. public ActionResult AddOrEditMyDevice(string address
  586. , string phone, string assetCode, string file, string desc, string deviceShortCode, string bigTypeName, string typeName,
  587. string code, string specification, int dicId = 0,
  588. int deviceTypeId = 0, int deviceId = 0)
  589. {
  590. if (deviceTypeId == 0)
  591. {
  592. if (string.IsNullOrEmpty(bigTypeName) && string.IsNullOrEmpty(typeName))
  593. {
  594. return Error("请选择或者输入设备类型和设备名称");
  595. }
  596. if (dicId == 0 && string.IsNullOrEmpty(typeName))
  597. {
  598. return Error("请选择或者输入设备类型和设备名称");
  599. }
  600. }
  601. Model.T_Adjust_DeviceType typemodel = new T_Adjust_DeviceType();
  602. if (deviceTypeId > 0)
  603. {
  604. typemodel = typeservice.GetModel(deviceTypeId);
  605. if (typemodel == null)
  606. {
  607. return Error(" 设备类型不存在");
  608. }
  609. }
  610. else
  611. {
  612. //先添加字典的记录
  613. //添加设备类型记录
  614. //T_Sys_DictionaryValue orderModel = new T_Sys_DictionaryValue();
  615. //orderModel.F_Name = bigTypeName;
  616. //orderModel.F_State = true;
  617. //orderModel.F_DictionaryFlag = "DBSBLX";
  618. //int dicaddid = new Bll.T_Sys_DictionaryValue().Add(bigTypeName);
  619. int dicaddid = 0;
  620. if (dicId > 0)
  621. {
  622. Model.T_Sys_DictionaryValue dicmodel = new Bll.T_Sys_DictionaryValue().GetModel(dicId);
  623. if (dicmodel != null)
  624. {
  625. dicaddid = dicmodel.F_DictionaryValueId;
  626. bigTypeName = dicmodel.F_Name;
  627. }
  628. }
  629. else
  630. {
  631. Model.T_Sys_DictionaryValue dicmodel = new Bll.T_Sys_DictionaryValue().GetModel(bigTypeName);
  632. if (dicmodel != null)
  633. {
  634. dicaddid = dicmodel.F_DictionaryValueId;
  635. }
  636. else
  637. {
  638. dicaddid = new Bll.T_Sys_DictionaryValue().Add(bigTypeName);
  639. }
  640. }
  641. Model.T_Adjust_DeviceType modeltype = typeservice.GetModel(bigTypeName, typeName, 2);
  642. if (modeltype == null)
  643. {
  644. // Model.T_Adjust_DeviceType model = new T_Adjust_DeviceType();
  645. typemodel.F_DeviceNum = 0;
  646. typemodel.F_IsDelete = 0;
  647. typemodel.F_CreateBy = User.F_UserCode;
  648. typemodel.F_BigTypeName = bigTypeName;
  649. typemodel.F_TypeName = typeName;
  650. typemodel.F_Classification = 2;
  651. typemodel.F_DicId = dicaddid;
  652. typemodel.F_CreateTime = DateTime.Now;
  653. typemodel.F_UpdateTime = DateTime.Now;
  654. int res = typeservice.Add(typemodel);
  655. deviceTypeId = res;
  656. }
  657. else
  658. {
  659. typemodel = modeltype;
  660. deviceTypeId = modeltype.F_TypeId;
  661. }
  662. }
  663. var existslist = deviceservice.GetModelList(" F_AssetCode ='" + assetCode + "' and F_AssetCode is not null and F_AssetCode!='' ");
  664. if (deviceId > 0)
  665. {
  666. if (existslist.Count == 0)
  667. { }
  668. else if (existslist.Count == 1 && existslist.First().F_DeviceId == deviceId)
  669. {
  670. }
  671. else
  672. {
  673. return Error("资产编码不能重复!");
  674. }
  675. Model.T_Adjust_Device model = deviceservice.GetModel(deviceId);
  676. int old = model.F_DeviceTypeId;
  677. bool needupdatecount = false;
  678. if (old != deviceTypeId)
  679. {
  680. needupdatecount = true;
  681. }
  682. //编辑
  683. model.F_BigTypeName = typemodel.F_BigTypeName;
  684. model.F_TypeName = typemodel.F_TypeName;
  685. model.F_DicId = typemodel.F_DicId;
  686. model.F_DeviceTypeId = deviceTypeId;
  687. model.F_BelongPhone = phone;
  688. model.F_BelongAddress = address;
  689. model.F_AssetCode = assetCode;
  690. model.F_DeviceShortCode = deviceShortCode;
  691. model.F_Specification = specification;
  692. model.F_Code = code;
  693. model.F_File = file;
  694. model.F_UpdateTime = DateTime.Now;
  695. model.F_DeviceShortCode = deviceShortCode;
  696. model.F_Specification = specification;
  697. model.F_Code = code;
  698. model.F_Desc = desc;
  699. if (deviceservice.Update(model))
  700. {
  701. if (needupdatecount)
  702. {
  703. string uda = " update T_Adjust_DeviceType set F_DeviceNum= isnull(F_DeviceNum,0)+1 where F_TypeId=" + deviceId + "";
  704. string uda2 = " update T_Adjust_DeviceType set F_DeviceNum= isnull(F_DeviceNum,0)-1 where F_TypeId=" + old + "";
  705. DbHelperSQL.ExecuteSql(uda);
  706. DbHelperSQL.ExecuteSql(uda2);
  707. }
  708. return Success("修改成功");
  709. }
  710. else
  711. {
  712. return Error("修改失败");
  713. }
  714. }
  715. else
  716. {
  717. if (!string.IsNullOrEmpty(assetCode) && existslist.Count > 0)
  718. {
  719. return Error("资产编码不能重复!");
  720. }
  721. // Model.T_Sys_Department dept = new Bll.T_Sys_Department().GetModel(User.F_DeptId);
  722. var dept = DbHelperSQL.Query(" SELECT * from T_AllDept WHERE F_deptid = " + User.F_DeptId + "").Tables[0];
  723. Model.T_Adjust_Device model = new T_Adjust_Device();
  724. model.F_BelongDept = User.F_DeptId;
  725. model.F_BelongDeptName = dept.Rows[0]["keyname"].ToString();
  726. model.F_Source = 1;
  727. model.F_DeviceStatus = 1;//待上架
  728. model.F_Classify = 2;
  729. model.F_IsDelete = 0;
  730. model.F_CreateBy = User.F_UserCode;
  731. model.F_CreateTime = DateTime.Now;
  732. model.F_UpdateTime = DateTime.Now;
  733. model.F_DeviceShortCode = deviceShortCode;
  734. model.F_Specification = specification;
  735. model.F_Code = code;
  736. model.F_DeviceTypeId = deviceTypeId;
  737. model.F_BigTypeName = typemodel.F_BigTypeName;
  738. model.F_TypeName = typemodel.F_TypeName;
  739. model.F_DicId = typemodel.F_DicId;
  740. model.F_BelongPhone = phone;
  741. model.F_BelongAddress = address;
  742. model.F_AssetCode = assetCode;
  743. model.F_Desc = desc;
  744. model.F_File = file;
  745. int res = deviceservice.Add(model);
  746. if (res > 0)
  747. {
  748. string uda = " update T_Adjust_DeviceType set F_DeviceNum= isnull(F_DeviceNum,0)+1 where F_TypeId=" + deviceTypeId + "";
  749. DbHelperSQL.ExecuteSql(uda);
  750. InsertLog(res, "添加设备");
  751. return Success("添加成功");
  752. }
  753. else
  754. {
  755. return Error("添加失败");
  756. }
  757. }
  758. }
  759. /// <summary>
  760. /// GetUnusedList 调拨管理中的闲置列表,状态都是闲置
  761. /// </summary>
  762. /// <param name="typeName">设备名称</param>
  763. /// <param name="startTime"></param>
  764. /// <param name="endTime"></param>
  765. /// <param name="dicId">设备类型</param>
  766. /// <param name="page"></param>
  767. /// <param name="pagesize"></param>
  768. /// <returns></returns>
  769. public ActionResult GetUnusedList(
  770. string typeName, string startTime, string endTime, int dicId = 0, int page = 1, int pagesize = 10, int listType = 1, int isdc = 0)
  771. {
  772. DataTable dt = new DataTable();
  773. string sql = " and F_IsDelete=0 and F_Classify=2 ";
  774. if (listType == 1)
  775. {
  776. sql += " and F_DeviceStatus in (2,3)";
  777. sql += " and F_BelongDept !=" + User.F_DeptId + "";
  778. }
  779. else
  780. {
  781. sql += " and F_DeviceStatus=3";
  782. }
  783. if (dicId > 0)
  784. {
  785. sql += " and F_DicId='" + dicId + "' ";
  786. }
  787. if (!string.IsNullOrEmpty(startTime))
  788. {
  789. sql += " and F_ListingTime>='" + startTime + "'";
  790. }
  791. if (!string.IsNullOrEmpty(endTime))
  792. {
  793. sql += " and F_ListingTime<='" + endTime + "'";
  794. }
  795. if (!string.IsNullOrEmpty(typeName))
  796. {
  797. sql += " and F_TypeName like '%" + typeName + "%' ";
  798. }
  799. //if (deviceStatus > 0)
  800. //{
  801. // sql += " and F_DeviceStatus='" + deviceStatus + "' ";
  802. //}
  803. if (isdc > 0)
  804. {
  805. var dtdc = DB.DbHelperSQL.Query(" SELECT F_TypeName 设备名称, F_Desc 设备描述,F_AssetCode 资产编码 , F_DeviceShortCode 设备短码,F_Specification 规格型号, F_BelongDeptName 归属科室, F_BelongPhone 归属科室电话,(CASE F_DeviceStatus WHEN 1 THEN '待上架' WHEN 2 THEN '可调拨' WHEN 3 THEN '调拨中' ELSE ''END) 设备状态,F_ListingTime 上架时间 from T_Adjust_Device a WITH(NOLOCK) where 1=1 " + sql + "order by F_DeviceId desc").Tables[0];
  806. var msg = new NPOIHelper().ExportToExcel("空闲设备", dtdc);
  807. if (msg == "")
  808. {
  809. return Success("导出成功");
  810. }
  811. else
  812. {
  813. return Error("导出失败");
  814. }
  815. }
  816. int recordCount = 0;
  817. dt = Bll.PagerBll.GetListPager(
  818. "T_Adjust_Device",
  819. "F_DeviceId",
  820. "*",
  821. sql,
  822. " order by F_ListingTime desc",
  823. pagesize,
  824. page,
  825. true,
  826. out recordCount);
  827. dt.Columns.Add("FileUrl", typeof(object));
  828. foreach (DataRow dr in dt.Rows)
  829. {
  830. if (!string.IsNullOrEmpty(fileurlpath) && !string.IsNullOrEmpty(dr["F_File"].ToString()))
  831. {
  832. dr["FileUrl"] = GetFileData(dr["F_File"].ToString(), fileurlpath);
  833. }
  834. }
  835. var obj = new
  836. {
  837. state = "success",
  838. message = "成功",
  839. rows = dt,
  840. total = recordCount
  841. };
  842. return Content(obj.ToJson());
  843. }
  844. private string fileurlpath = ConfigHelper.GetValue("fileurlpath");
  845. public DataTable GetFileData(string ids, string prefix)
  846. {
  847. DataTable dt = DbHelperSQL.Query("select * from T_Sys_Accessories WITH(NOLOCK) where F_FileId in (" + ids + ")").Tables[0];
  848. foreach (DataRow dr in dt.Rows)
  849. {
  850. if (FileHelper.Exists(FileHelper.GetPath() + dr["F_FileUrl"].ToString()))
  851. {
  852. dr["F_FileUrl"] = prefix + dr["F_FileUrl"].ToString();
  853. }
  854. else
  855. {
  856. string url = HomeController.Upload(dr["F_FileUrl"].ToString(), dr["F_FileName"].ToString(), "/" + dr["F_FileUrl"].ToString().Replace(dr["F_FileName"].ToString(), ""));
  857. if (url == "1")
  858. dr["F_FileUrl"] = prefix + dr["F_FileUrl"].ToString();
  859. else
  860. dr["F_FileUrl"] = url;
  861. }
  862. }
  863. return dt;
  864. }
  865. /// <summary>
  866. /// AuditDB
  867. /// </summary>
  868. /// <param name="reason">拒绝的时候原因</param>
  869. /// <param name="operate">0 拒绝 1 同意</param>
  870. /// <param name="deviceId"></param>
  871. /// <returns></returns>
  872. public ActionResult AuditDB(string reason, int operate, int deviceId = 0)
  873. {
  874. if (deviceId <= 0)
  875. {
  876. return Error("请选择设备");
  877. }
  878. Model.T_Adjust_Device model = deviceservice.GetModel(deviceId);
  879. string reusercode = model.F_UseCode;
  880. Model.T_Adjust_Device oldmodel = deviceservice.GetModel(deviceId);
  881. // 参考附件图片,同意、拒绝操作需要给对应人发消息(钉钉)
  882. //1.同意:设备状态变更为待上架,归属科室变更为申请科室
  883. //2.拒绝:设备状态变更为可调配,需要输入拒绝原因
  884. if (operate == 1)
  885. {
  886. model.F_DeviceStatus = 1;
  887. model.F_BelongAddress = model.F_UseAddress;
  888. model.F_BelongDept = model.F_UseDept;
  889. model.F_BelongDeptName = model.F_UseDeptName;
  890. model.F_BelongPhone = model.F_UsePhone;
  891. model.F_UseDept = null;
  892. model.F_UseAddress = "";
  893. model.F_UseDeptName = "";
  894. model.F_UsePhone = "";
  895. model.F_UseCode = "";
  896. model.F_ListingTime = null;
  897. model.F_UpdateTime = DateTime.Now;
  898. if (deviceservice.Update(model))
  899. {
  900. //添加操作日志
  901. InsertLog(deviceId, "同意申请," + oldmodel.F_TypeName + "归属科室更改为" + oldmodel.F_UseDeptName);
  902. msg.AddInternalMessagesInfo("调拨审核同意", model.F_TypeName + "同意调拨", reusercode, User.F_UserCode, EnumSmsType.comprehensive
  903. , 0);
  904. return Success("审核成功");
  905. }
  906. else
  907. {
  908. return Error("审核失败");
  909. }
  910. }
  911. if (operate == 0)
  912. {
  913. model.F_DeviceStatus = 2;
  914. model.F_UseDept = null;
  915. model.F_UseAddress = "";
  916. model.F_UseDeptName = "";
  917. model.F_UsePhone = "";
  918. model.F_UseCode = "";
  919. model.F_UpdateTime = DateTime.Now;
  920. if (deviceservice.Update(model))
  921. {
  922. if (!string.IsNullOrEmpty(reason))
  923. {
  924. reason = ",原因:" + reason;
  925. }
  926. InsertLog(deviceId, "拒绝申请" + reason);
  927. msg.AddInternalMessagesInfo("调拨审核拒绝", model.F_TypeName + "拒绝申请", reusercode, User.F_UserCode, EnumSmsType.comprehensive
  928. , 0);
  929. return Success("审核成功");
  930. }
  931. else
  932. {
  933. return Error("审核失败");
  934. }
  935. }
  936. return Error("审核失败");
  937. }
  938. /// <summary>
  939. /// 申请调拨 Adjust
  940. /// </summary>
  941. /// <param name="deviceId"></param>
  942. /// <returns></returns>
  943. public ActionResult Adjust(int deviceId)
  944. {
  945. if (deviceId <= 0)
  946. {
  947. return Error("请选择设备");
  948. }
  949. Model.T_Adjust_Device model = deviceservice.GetModel(deviceId);
  950. if (model.F_DeviceStatus == 3)
  951. {
  952. return Error("调拨中不允许申请");
  953. }
  954. if (model.F_BelongDept == User.F_DeptId)
  955. {
  956. return Error("此设备所属部门是登录人的部门");
  957. }
  958. Model.T_Sys_Department deptmodel = new Bll.T_Sys_Department().GetModel(User.F_DeptId);
  959. var dept = DbHelperSQL.Query(" SELECT * from T_AllDept WHERE F_deptid = " + User.F_DeptId + "").Tables[0];
  960. model.F_UseDept = User.F_DeptId;
  961. model.F_UseAddress = dept.Rows[0]["F_location"].ToString();// dept.F_Location;
  962. model.F_UseDeptName = dept.Rows[0]["keyname"].ToString();
  963. model.F_UsePhone = deptmodel.F_OfficeTelephone;
  964. model.F_UseCode = User.F_UserCode;
  965. model.F_DeviceStatus = 3;
  966. model.F_UpdateTime = DateTime.Now;
  967. if (deviceservice.Update(model))
  968. {
  969. InsertLog(deviceId, "申请设备调拨");
  970. return Success("申请成功");
  971. }
  972. else
  973. {
  974. return Error("申请失败");
  975. }
  976. }
  977. /// <summary>
  978. /// GetDetail
  979. /// </summary>
  980. /// <param name="deviceId"></param>
  981. /// <param name="classify">1 调配 2调拨</param>
  982. /// <returns></returns>
  983. public ActionResult GetDetail(int deviceId, int classify = 1)
  984. {
  985. var dt = deviceservice.GetList(" F_DeviceId=" + deviceId + "").Tables[0];
  986. dt.Columns.Add("FileUrl", typeof(object));
  987. foreach (DataRow dr in dt.Rows)
  988. {
  989. if (!string.IsNullOrEmpty(fileurlpath) && !string.IsNullOrEmpty(dr["F_File"].ToString()))
  990. {
  991. dr["FileUrl"] = GetFileData(dr["F_File"].ToString(), fileurlpath);
  992. }
  993. }
  994. var tpinfo = tpinfoBll.GetModelList(" F_DeviceId=" + deviceId + " order by F_CreateTime desc");
  995. var obj = new
  996. {
  997. state = "success",
  998. message = "成功",
  999. data = dt,
  1000. logs = tpinfo
  1001. };
  1002. return Content(obj.ToJson());
  1003. }
  1004. public ActionResult GetDBInfo(int deviceId = 0, int page = 1, int pagesize = 10)
  1005. {
  1006. if (deviceId == 0)
  1007. {
  1008. return Error("deviceId 不能为空");
  1009. }
  1010. DataTable dt = new DataTable();
  1011. string sql = " and 1 = 1";
  1012. if (deviceId > 0)
  1013. {
  1014. sql += " and F_DeviceId='" + deviceId + "' ";
  1015. }
  1016. int recordCount = 0;
  1017. dt = Bll.PagerBll.GetListPager(
  1018. "T_Adjust_Log",
  1019. "F_LogId",
  1020. "*",
  1021. sql,
  1022. " order by F_CreateTime desc",
  1023. pagesize,
  1024. page,
  1025. true,
  1026. out recordCount);
  1027. var obj = new
  1028. {
  1029. state = "success",
  1030. message = "成功",
  1031. data = dt,
  1032. total = recordCount
  1033. };
  1034. return Content(obj.ToJson());
  1035. }
  1036. public void InsertLog(int deviceId, string log)
  1037. {
  1038. Model.T_Adjust_Log logmodel = new T_Adjust_Log();
  1039. logmodel.F_CreateBy = User.F_UserName + "(" + User.F_UserCode + ")";
  1040. logmodel.F_CreateTime = DateTime.Now;
  1041. logmodel.F_Log = log;
  1042. logmodel.F_DeviceId = deviceId;
  1043. logBll.Add(logmodel);
  1044. }
  1045. //工单相关的接口
  1046. /// <summary>
  1047. /// GetAllDPDevice创建工单时,获取所有的调配设备
  1048. /// </summary>
  1049. /// <returns></returns>
  1050. public ActionResult GetAllDPDevice()
  1051. {
  1052. string sql = "select F_DeviceId,F_TypeName from T_Adjust_Device where F_Classify=1 and F_IsDelete=0 ";
  1053. var list = DbHelperSQL.Query(sql).Tables[0];
  1054. var obj = new
  1055. {
  1056. state = "success",
  1057. message = "成功",
  1058. data = list
  1059. };
  1060. return Content(obj.ToJson());
  1061. }
  1062. //列表默认显示对应借用类型名称下的闲置设备,可使用资产编码或短码搜索;可选择对应设备点击出库进行出库;
  1063. /// <summary>
  1064. /// GetUnuseDevice根据工单编号查找 该工单借用的类型名称.下的闲置设备,可使用资产编码或短码搜索;可选择对应设备点击出库进行出库;
  1065. /// </summary>
  1066. /// <param name="workOrderCode"></param>
  1067. /// <param name="assetCode"></param>
  1068. /// <param name="deviceShortCode"></param>
  1069. /// <param name="page"></param>
  1070. /// <param name="pagesize"></param>
  1071. /// <returns></returns>
  1072. public ActionResult GetUnuseDevice(string assetCode, string deviceShortCode, int deviceTypeId = 0, int page = 1, int pagesize = 10)
  1073. {
  1074. DataTable dt = new DataTable();
  1075. string sql = " and F_IsDelete=0 and F_Classify=1 and F_DeviceStatus=2 ";
  1076. sql += " and F_DeviceTypeId=" + deviceTypeId + "";
  1077. if (!string.IsNullOrEmpty(assetCode))
  1078. {
  1079. sql += " and F_AssetCode='" + assetCode + "' ";
  1080. }
  1081. if (!string.IsNullOrEmpty(deviceShortCode))
  1082. {
  1083. sql += " and F_DeviceShortCode='" + deviceShortCode + "' ";
  1084. }
  1085. int recordCount = 0;
  1086. dt = Bll.PagerBll.GetListPager(
  1087. "T_Adjust_Device",
  1088. "F_DeviceId",
  1089. "*",
  1090. sql,
  1091. " order by F_DeviceId desc",
  1092. pagesize,
  1093. page,
  1094. true,
  1095. out recordCount);
  1096. var obj = new
  1097. {
  1098. state = "success",
  1099. message = "成功",
  1100. rows = dt,
  1101. total = recordCount
  1102. };
  1103. return Content(obj.ToJson());
  1104. }
  1105. //列表默认显示当前用户所在的科室下所有使用中的设备,可使用资产编码或短码搜索其他设备;可选择对应设备点击入库进行入库;
  1106. /// <summary>
  1107. /// GetUseDevice
  1108. /// </summary>
  1109. /// <param name="assetCode">资产编码</param>
  1110. /// <param name="deviceShortCode">短码</param>
  1111. /// <param name="page"></param>
  1112. /// <param name="pagesize"></param>
  1113. /// <returns></returns>
  1114. public ActionResult GetUseDevice(string assetCode, string deviceShortCode, int deviceTypeId = 0, int page = 1, int pagesize = 10)
  1115. {
  1116. DataTable dt = new DataTable();
  1117. string sql = " and F_IsDelete=0 and F_Classify=1 and F_DeviceStatus=3 and F_UseDept='" + User.F_DeptId + "'";
  1118. sql += " and F_DeviceTypeId=" + deviceTypeId + "";
  1119. if (!string.IsNullOrEmpty(assetCode))
  1120. {
  1121. sql += " and F_AssetCode='" + assetCode + "' ";
  1122. }
  1123. if (!string.IsNullOrEmpty(deviceShortCode))
  1124. {
  1125. sql += " and F_DeviceShortCode='" + deviceShortCode + "' ";
  1126. }
  1127. int recordCount = 0;
  1128. dt = Bll.PagerBll.GetListPager(
  1129. "T_Adjust_Device",
  1130. "F_DeviceId",
  1131. "*",
  1132. sql,
  1133. " order by F_DeviceId desc",
  1134. pagesize,
  1135. page,
  1136. true,
  1137. out recordCount);
  1138. var obj = new
  1139. {
  1140. state = "success",
  1141. message = "成功",
  1142. rows = dt,
  1143. total = recordCount
  1144. };
  1145. return Content(obj.ToJson());
  1146. }
  1147. //string address
  1148. // , string phone, string assetCode, string file,string desc, string deviceShortCode,
  1149. // string code, string specification,
  1150. // string bigname,string name ,
  1151. //1 调配 2调拨
  1152. public ActionResult ImportDevice()
  1153. {
  1154. HttpPostedFileBase _upfile = Request.Files["upFile"];
  1155. int headrow = 0;
  1156. string savepath = Server.MapPath("..") + "/Upload/import/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd");
  1157. // 如果不存在就创建file文件夹
  1158. if (!Directory.Exists(savepath))
  1159. {
  1160. if (savepath != null) Directory.CreateDirectory(savepath);
  1161. }
  1162. string realurl = Path.Combine(savepath, _upfile.FileName);
  1163. // 指定保存文件的路径
  1164. // 保存文件到服务器
  1165. _upfile.SaveAs(realurl);
  1166. NPOIHelper npoi = new NPOIHelper();
  1167. DataTable dt = npoi.ExcelToTable(_upfile, headrow);
  1168. try
  1169. {
  1170. bool isValid = true;
  1171. if (dt != null && dt.Columns.Count >= 2)
  1172. {
  1173. string firstColumnName = dt.Columns[7].ColumnName;
  1174. string secondColumnName = dt.Columns[2].ColumnName;
  1175. isValid = (firstColumnName == "出厂编号" && secondColumnName == "科室电话");
  1176. }
  1177. if (isValid == false)
  1178. {
  1179. return Error("导入模版错误");
  1180. }
  1181. }
  1182. catch (Exception)
  1183. {
  1184. return Error("导入模版错误");
  1185. }
  1186. int res = 0;
  1187. string returnerror = "";
  1188. foreach (DataRow dr in dt.Rows)
  1189. {
  1190. int deviceTypeId = 0;
  1191. int dicaddid = 0;
  1192. headrow = headrow + 1;
  1193. if (dr[0].ToString() != "" && dr[1].ToString() != "" && dr[4].ToString() != "")
  1194. {
  1195. var existslist = deviceservice.GetModelList(" F_AssetCode ='" + dr[4].ToString() + "'");
  1196. if (existslist.Count > 0)
  1197. {
  1198. Model.T_Adjust_Device model = existslist.First();
  1199. if (model.F_Classify != 1)
  1200. {
  1201. returnerror += "第" + headrow + "行资产编码已存在,属于调拨类型";
  1202. continue;
  1203. }
  1204. model.F_BigTypeName = dr[0].ToString();// typemodel.F_BigTypeName;
  1205. model.F_TypeName = dr[1].ToString();
  1206. //根据bigtypename去找dic
  1207. Model.T_Sys_DictionaryValue dic = new Bll.T_Sys_DictionaryValue().GetModel(dr[0].ToString());
  1208. if (dic == null)
  1209. {
  1210. //先添加字典的记录
  1211. dicaddid = new Bll.T_Sys_DictionaryValue().Add(dr[0].ToString());
  1212. }
  1213. model.F_DicId = dicaddid == 0 ? dic.F_DictionaryValueId : dicaddid;//.F_DicId;
  1214. Model.T_Adjust_DeviceType modeltype = typeservice.GetModel(dr[0].ToString(), dr[1].ToString(), 1);
  1215. if (modeltype == null)
  1216. {//添加设备类型记录
  1217. Model.T_Adjust_DeviceType addmodel = new T_Adjust_DeviceType();
  1218. addmodel.F_DeviceNum = 0;
  1219. addmodel.F_IsDelete = 0;
  1220. addmodel.F_Classification = 1;
  1221. addmodel.F_CreateBy = User.F_UserCode;
  1222. addmodel.F_BigTypeName = dr[0].ToString();
  1223. addmodel.F_TypeName = dr[1].ToString();
  1224. addmodel.F_DicId = dicaddid;
  1225. addmodel.F_CreateTime = DateTime.Now;
  1226. addmodel.F_UpdateTime = DateTime.Now;
  1227. int rees = typeservice.Add(addmodel);
  1228. deviceTypeId = rees;
  1229. }
  1230. var dept = DbHelperSQL.Query(" SELECT * from T_AllDept WHERE F_deptid = " + User.F_DeptId + "").Tables[0];
  1231. model.F_BelongPhone = dr[2].ToString();
  1232. model.F_BelongAddress = dr[3].ToString();
  1233. model.F_BelongDept = User.F_DeptId;
  1234. // model.F_BelongDeptName = dept.F_DeptName;// eptName;
  1235. model.F_BelongDeptName = dept.Rows[0]["keyname"].ToString();
  1236. model.F_DeviceTypeId = deviceTypeId == 0 ? modeltype.F_TypeId : deviceTypeId;
  1237. model.F_AssetCode = dr[4].ToString();
  1238. model.F_DeviceShortCode = dr[5].ToString();
  1239. model.F_Specification = dr[6].ToString();
  1240. model.F_Code = dr[7].ToString();
  1241. model.F_UpdateTime = DateTime.Now;
  1242. deviceservice.Update(model);
  1243. }
  1244. else
  1245. {
  1246. Model.T_Adjust_Device model = new T_Adjust_Device();
  1247. model.F_BigTypeName = dr[0].ToString();// typemodel.F_BigTypeName;
  1248. model.F_TypeName = dr[1].ToString();
  1249. //根据bigtypename去找dic
  1250. Model.T_Sys_DictionaryValue dic = new Bll.T_Sys_DictionaryValue().GetModel(dr[0].ToString());
  1251. if (dic == null)
  1252. {
  1253. //先添加字典的记录
  1254. dicaddid = new Bll.T_Sys_DictionaryValue().Add(dr[0].ToString());
  1255. }
  1256. model.F_DicId = dicaddid == 0 ? dic.F_DictionaryValueId : dicaddid;//.F_DicId;
  1257. Model.T_Adjust_DeviceType modeltype = typeservice.GetModel(dr[0].ToString(), dr[1].ToString(), 1);
  1258. if (modeltype == null)
  1259. {//添加设备类型记录
  1260. Model.T_Adjust_DeviceType addmodel = new T_Adjust_DeviceType();
  1261. addmodel.F_DeviceNum = 0;
  1262. addmodel.F_IsDelete = 0;
  1263. addmodel.F_Classification = 1;
  1264. addmodel.F_CreateBy = User.F_UserCode;
  1265. addmodel.F_BigTypeName = dr[0].ToString();
  1266. addmodel.F_TypeName = dr[1].ToString();
  1267. addmodel.F_DicId = dicaddid;
  1268. addmodel.F_CreateTime = DateTime.Now;
  1269. addmodel.F_UpdateTime = DateTime.Now;
  1270. int rees = typeservice.Add(addmodel);
  1271. deviceTypeId = rees;
  1272. }
  1273. var dept = DbHelperSQL.Query(" SELECT * from T_AllDept WHERE F_deptid = " + User.F_DeptId + "").Tables[0];
  1274. // Model.T_Sys_Department dept = new Bll.T_Sys_Department().GetModel(User.F_DeptId);
  1275. model.F_BelongPhone = dr[2].ToString();
  1276. model.F_BelongAddress = dr[3].ToString();
  1277. model.F_BelongDept = User.F_DeptId;
  1278. model.F_BelongDeptName = dept.Rows[0]["keyname"].ToString();
  1279. model.F_DeviceTypeId = modeltype.F_TypeId;
  1280. model.F_AssetCode = dr[4].ToString();
  1281. model.F_DeviceShortCode = dr[5].ToString();
  1282. model.F_Specification = dr[6].ToString();
  1283. model.F_Code = dr[7].ToString();
  1284. model.F_Source = 1;
  1285. model.F_CreateBy = User.F_UserCode;
  1286. model.F_CreateTime = DateTime.Now;
  1287. model.F_UpdateTime = DateTime.Now;
  1288. model.F_DeviceStatus = 1;//待上架
  1289. model.F_Classify = 1;
  1290. model.F_IsDelete = 0;
  1291. deviceservice.Add(model);
  1292. // 设备的数量重新算一遍
  1293. string updatesql = "update T_Adjust_DeviceType set F_DeviceNum = (SELECT Count(1) from T_Adjust_Device WHERE F_IsDelete = 0 and F_DeviceTypeId =" + modeltype.F_TypeId + " ) WHERE F_TypeId =" + modeltype.F_TypeId;
  1294. DbHelperSQL.ExecuteSql(updatesql);
  1295. }
  1296. }
  1297. else
  1298. {
  1299. return Error("第" + headrow + "行,第1,2,5 列信息不能为空,未导入");
  1300. }
  1301. }
  1302. return Success("导入成功!" + returnerror);
  1303. }
  1304. public ActionResult ImportMyDevice()
  1305. {
  1306. HttpPostedFileBase _upfile = Request.Files["upFile"];
  1307. int headrow = 0;
  1308. NPOIHelper npoi = new NPOIHelper();
  1309. DataTable dt = npoi.ExcelToTable(_upfile, headrow);
  1310. try
  1311. {
  1312. bool isValid = true;
  1313. if (dt != null && dt.Columns.Count >= 2)
  1314. {
  1315. string firstColumnName = dt.Columns[8].ColumnName;
  1316. string secondColumnName = dt.Columns[2].ColumnName;
  1317. isValid = (firstColumnName == "描述" && secondColumnName == "科室电话");
  1318. }
  1319. if (isValid == false)
  1320. {
  1321. return Error("导入模版错误");
  1322. }
  1323. }
  1324. catch (Exception)
  1325. {
  1326. return Error("导入模版错误");
  1327. }
  1328. int exportnum = 0;
  1329. string returnerror = "";
  1330. var bll = new Bll.T_Cus_Customer();
  1331. int res = 0;
  1332. // Model.T_Sys_Department dept = new Bll.T_Sys_Department().GetModel(User.F_DeptId);
  1333. var dept = DbHelperSQL.Query(" SELECT * from T_AllDept WHERE F_deptid = " + User.F_DeptId + "").Tables[0];
  1334. foreach (DataRow dr in dt.Rows)
  1335. {
  1336. int dicaddid = 0;
  1337. int deviceTypeId = 0;
  1338. headrow = headrow + 1;
  1339. if (dr[0].ToString() != "" && dr[1].ToString() != "")
  1340. {
  1341. // Model.T_Adjust_DeviceType typemodel = typeservice.GetModel(dr[0].ToString(), dr[1].ToString(),2);
  1342. //根据bigtypename去找dic
  1343. Model.T_Sys_DictionaryValue dic = new Bll.T_Sys_DictionaryValue().GetModel(dr[0].ToString());
  1344. if (dic == null)
  1345. {
  1346. //先添加字典的记录
  1347. dicaddid = new Bll.T_Sys_DictionaryValue().Add(dr[0].ToString());
  1348. }
  1349. Model.T_Adjust_DeviceType modeltype = typeservice.GetModel(dr[0].ToString(), dr[1].ToString(), 1);
  1350. if (modeltype == null)
  1351. {
  1352. continue;
  1353. //添加设备类型记录
  1354. //Model.T_Adjust_DeviceType addmodel = new T_Adjust_DeviceType();
  1355. //addmodel.F_DeviceNum = 0;
  1356. //addmodel.F_IsDelete = 0;
  1357. //addmodel.F_Classification = 2;
  1358. //addmodel.F_CreateBy = User.F_UserCode;
  1359. //addmodel.F_BigTypeName = dr[0].ToString();
  1360. //addmodel.F_TypeName = dr[1].ToString();
  1361. //addmodel.F_DicId = dicaddid;
  1362. //addmodel.F_CreateTime = DateTime.Now;
  1363. //addmodel.F_UpdateTime = DateTime.Now;
  1364. //int rees = typeservice.Add(addmodel);
  1365. //deviceTypeId = rees;
  1366. }
  1367. var existslist = deviceservice.GetModelList(" F_AssetCode ='" + dr[4].ToString() + "' ");
  1368. if (existslist.Count > 0)
  1369. {
  1370. Model.T_Adjust_Device model
  1371. = existslist.First();
  1372. if (model.F_Classify != 2)
  1373. {
  1374. returnerror += "第" + headrow + "行资产编码已存在,属于调配类型";
  1375. continue;
  1376. }
  1377. model.F_DeviceTypeId = deviceTypeId == 0 ? modeltype.F_TypeId : deviceTypeId;
  1378. model.F_BigTypeName = dr[0].ToString();
  1379. model.F_TypeName = dr[1].ToString();
  1380. model.F_DicId = dicaddid == 0 ? dic.F_DictionaryValueId : dicaddid;
  1381. model.F_BelongPhone = dr[2].ToString();
  1382. model.F_BelongAddress = dr[3].ToString();
  1383. model.F_AssetCode = dr[4].ToString();
  1384. model.F_DeviceShortCode = dr[5].ToString();
  1385. model.F_Specification = dr[6].ToString();
  1386. model.F_Code = dr[7].ToString();
  1387. model.F_Desc = dr[8].ToString();
  1388. model.F_UpdateTime = DateTime.Now;
  1389. deviceservice.Update(model);
  1390. }
  1391. else
  1392. {
  1393. Model.T_Adjust_Device model
  1394. = new T_Adjust_Device();
  1395. model.F_BelongDept = User.F_DeptId;
  1396. // model.F_BelongDeptName = dept.F_DeptName;
  1397. model.F_BelongDeptName = dept.Rows[0]["keyname"].ToString();
  1398. model.F_Source = 1;
  1399. model.F_DeviceStatus = 1;//待上架
  1400. model.F_Classify = 2;
  1401. model.F_IsDelete = 0;
  1402. model.F_CreateBy = User.F_UserCode;
  1403. model.F_CreateTime = DateTime.Now;
  1404. model.F_UpdateTime = DateTime.Now;
  1405. model.F_DeviceTypeId = deviceTypeId == 0 ? modeltype.F_TypeId : deviceTypeId;
  1406. model.F_BigTypeName = dr[0].ToString();
  1407. model.F_TypeName = dr[1].ToString();
  1408. model.F_DicId = dicaddid == 0 ? dic.F_DictionaryValueId : dicaddid;
  1409. model.F_BelongPhone = dr[2].ToString();
  1410. model.F_BelongAddress = dr[3].ToString();
  1411. model.F_AssetCode = dr[4].ToString();
  1412. model.F_DeviceShortCode = dr[5].ToString();
  1413. model.F_Specification = dr[6].ToString();
  1414. model.F_Code = dr[7].ToString();
  1415. model.F_Desc = dr[8].ToString();
  1416. int addre = deviceservice.Add(model);
  1417. if (addre > 0)
  1418. {
  1419. exportnum++;
  1420. // 设备的数量重新算一遍
  1421. string updatesql = "update T_Adjust_DeviceType set F_DeviceNum = (SELECT Count(1) from T_Adjust_Device WHERE F_IsDelete = 0 and F_DeviceTypeId =" + model.F_DeviceTypeId + " ) WHERE F_TypeId =" + model.F_DeviceTypeId;
  1422. DbHelperSQL.ExecuteSql(updatesql);
  1423. }
  1424. }
  1425. }
  1426. else
  1427. {
  1428. return Error("第" + headrow + "行,第1,2列信息不能为空,未导入");
  1429. }
  1430. }
  1431. //if (exportnum == 0)
  1432. //{
  1433. // return Success("0条数据被导入");
  1434. //}
  1435. //else {
  1436. return Success("导入成功,导入" + exportnum + "数据");
  1437. //}
  1438. }
  1439. /// <summary>
  1440. /// ordertype 1 是调配 2 是调拨
  1441. /// </summary>
  1442. /// <param name="month">格式是yyyy-mm</param>
  1443. /// <returns></returns>
  1444. [AllowAnonymous]
  1445. public ActionResult GetTotalReport(string month,int ordertype=1)
  1446. {
  1447. if (string.IsNullOrEmpty(month))
  1448. {
  1449. month = DateTime.Now.ToString("yyyy-MM");
  1450. }
  1451. DataTable jiechu = new DataTable();
  1452. DataTable jieru = new DataTable();
  1453. if (ordertype == 1)
  1454. {
  1455. jiechu = DbHelperSQL.Query("SELECT F_BelongDept,F_BelongDeptName,SUM(F_Cost) jiechu FROM [T_Adjust_TpOrder] where F_IsDelete=0 and F_BelongDept is not null and F_OrderStatus=2 and F_ordertype=" + ordertype + " and CONVERT(VARCHAR(7), F_OutTime, 120)='" + month + "' GROUP BY F_BelongDept, F_BelongDeptName").Tables[0];
  1456. jieru = DbHelperSQL.Query("SELECT F_UseDept,F_UseDeptName,SUM(F_Cost) jieru FROM [T_Adjust_TpOrder] where F_IsDelete=0 and F_UseDept is not null and F_OrderStatus=2 and F_ordertype=" + ordertype + " and CONVERT(VARCHAR(7), F_OutTime, 120)='" + month + "' GROUP BY F_UseDept, F_UseDeptName ").Tables[0];
  1457. }
  1458. else {
  1459. jiechu = DbHelperSQL.Query("SELECT F_BelongDept,F_BelongDeptName,SUM(F_total) jiechu FROM [T_Adjust_TpOrder] where F_IsDelete=0 and F_BelongDept is not null and F_OrderStatus=2 and F_ordertype=" + ordertype + " and CONVERT(VARCHAR(7), F_OutTime, 120)='" + month + "' GROUP BY F_BelongDept, F_BelongDeptName").Tables[0];
  1460. jieru = DbHelperSQL.Query("SELECT F_UseDept,F_UseDeptName,SUM(F_total) jieru FROM [T_Adjust_TpOrder] where F_IsDelete=0 and F_UseDept is not null and F_OrderStatus=2 and F_ordertype=" + ordertype + " and CONVERT(VARCHAR(7), F_OutTime, 120)='" + month + "' GROUP BY F_UseDept, F_UseDeptName ").Tables[0];
  1461. }
  1462. var idList = new List<int>(jiechu.AsEnumerable().Select(x => x.Field<int>("F_BelongDept")));
  1463. var idList2 = new List<int>(jieru.AsEnumerable().Select(x => x.Field<int>("F_UseDept")));
  1464. var union = idList.Union(idList2);
  1465. List<ReturnReport> lists = new List<ReturnReport>();
  1466. foreach (int item in union)
  1467. {
  1468. ReturnReport re = new ReturnReport();
  1469. DataRow[] rows = jiechu.Select("F_BelongDept=" + item + " ");
  1470. if (rows != null && rows.Count() > 0)
  1471. {
  1472. re.DeptName = rows[0]["F_BelongDeptName"].ToString();
  1473. // re.jiechu = Convert.ToDecimal(rows[0]["jiechu"].ToString());
  1474. re.jiechu = !string.IsNullOrEmpty(rows[0]["jiechu"].ToString()) ? Convert.ToDecimal(rows[0]["jiechu"].ToString()) : 0;
  1475. }
  1476. re.DeptId = item;
  1477. DataRow[] rows2 = jieru.Select("F_UseDept=" + item + " ");
  1478. if (rows2 != null && rows2.Count() > 0)
  1479. {
  1480. re.DeptName = rows2[0]["F_UseDeptName"].ToString();
  1481. re.jieru = !string.IsNullOrEmpty(rows2[0]["jieru"].ToString())?Convert.ToDecimal(rows2[0]["jieru"].ToString()) :0;
  1482. }
  1483. lists.Add(re);
  1484. }
  1485. var obj = new
  1486. {
  1487. state = "success",
  1488. message = "成功",
  1489. data = lists
  1490. };
  1491. return Content(obj.ToJson());
  1492. }
  1493. public class ReturnReport
  1494. {
  1495. public string DeptName { get; set; }
  1496. public int DeptId { get; set; }
  1497. public decimal jiechu { get; set; }
  1498. public decimal jieru { get; set; }
  1499. }
  1500. /// <summary>
  1501. /// 同步数据,过来是调配类型的
  1502. /// </summary>
  1503. [AllowAnonymous]
  1504. public void tpdata()
  1505. {
  1506. string depsql = "select F_DeptId, keyname from DeptNameName ";
  1507. DataTable deptdt = DbHelperSQL.Query(depsql).Tables[0];
  1508. List<Model.T_Sys_Department> deptlist = deptbll.GetModelList("F_State=1");
  1509. // 转换为字典以加速查找
  1510. var deptDict = deptdt.AsEnumerable()
  1511. .ToDictionary(row => row.Field<int>("F_DeptId"), row => row.Field<string>("keyname"));
  1512. //var departmentDict = deptlist.ToDictionary(d => d.F_DeptName, d => d);
  1513. // 过滤掉重复的部门名称,保留第一个出现的
  1514. var departmentDict = deptlist
  1515. .GroupBy(d => d.F_DeptName)
  1516. .Select(g => g.First())
  1517. .ToDictionary(d => d.F_DeptName, d => d);
  1518. int pageSize = 100; // 每页1000条
  1519. // int currentPage = 0; // 当前页码(从0开始)
  1520. //一次同步1000条,避免卡死
  1521. int rescount = (int)DbHelperSQL.GetSingle("SELECT count(1) FROM T_SyncEquip where tb_bz=1");
  1522. // 预先计算总页数(避免在循环中计算)
  1523. int totalPages = (rescount + pageSize - 1) / pageSize;
  1524. int lastId = 0; // 起始值(根据sb_Id类型调整)
  1525. int processed = 0;
  1526. List<Model.T_Adjust_Device> devicesToAdd = new List<Model.T_Adjust_Device>();
  1527. List<Model.T_Adjust_Device> devicesToUpdate = new List<Model.T_Adjust_Device>();
  1528. Dictionary<int,int> deviceTypeUpdates = new Dictionary<int, int>();
  1529. while (processed < rescount)
  1530. {
  1531. // 使用参数化查询防止注入
  1532. string sql = @"
  1533. SELECT TOP (@take) *
  1534. FROM T_SyncEquip
  1535. WHERE tb_bz=1 and sb_Id > @lastId
  1536. ORDER BY sb_Id";
  1537. SqlParameter[] parameters = {
  1538. new SqlParameter("@take", pageSize),
  1539. new SqlParameter("@lastId", lastId)
  1540. };
  1541. DataTable dt = DbHelperSQL.Query(sql, parameters).Tables[0];
  1542. if (dt.Rows.Count == 0) break;
  1543. // 并行处理当前页数据(行级并行)
  1544. Parallel.For(0, dt.Rows.Count, new ParallelOptions
  1545. {
  1546. MaxDegreeOfParallelism = Environment.ProcessorCount
  1547. }, i =>
  1548. {
  1549. DataRow item = dt.Rows[i];
  1550. int deviceTypeId = 0;
  1551. int dicaddid = 0;
  1552. string bigTypeName = item["category_name"].ToString();//设备类别名称
  1553. string typeName = item["equip_name"].ToString();
  1554. string assetCode = item["equip_code"].ToString();//设备编码唯一
  1555. string specification = item["spec_model"].ToString();//规格型号
  1556. string code = item["precoding"].ToString();//出厂编码/SN
  1557. string mandept_name = item["mandept_name"].ToString();//管理部门
  1558. string usedept_name = item["usedept_name"].ToString();//使用部门
  1559. if (string.IsNullOrEmpty(bigTypeName) && string.IsNullOrEmpty(typeName))
  1560. {
  1561. return;
  1562. //continue;
  1563. // return Error("请选择或者输入设备类型和设备名称");
  1564. }
  1565. if (string.IsNullOrEmpty(assetCode))
  1566. {
  1567. return;
  1568. //continue;
  1569. //return Error("资产编码不能为空");
  1570. }
  1571. Model.T_Adjust_DeviceType typemodel = new T_Adjust_DeviceType();
  1572. //找不到这个设备类型,给它自动添加一个设备类型
  1573. // return Error(" 设备类型不存在");
  1574. //先看字典id
  1575. typemodel = typeservice.GetModel(bigTypeName, typeName, 1);
  1576. Model.T_Sys_DictionaryValue dicmodel = new Bll.T_Sys_DictionaryValue().GetModel(bigTypeName);
  1577. if (dicmodel != null)
  1578. {
  1579. dicaddid = dicmodel.F_DictionaryValueId;
  1580. }
  1581. else
  1582. {
  1583. //// 实时添加新字典值
  1584. //lock (this) // 防止并发添加相同记录
  1585. //{
  1586. // 再次检查,避免并发添加
  1587. // dicmodel = new Bll.T_Sys_DictionaryValue().GetModel(bigTypeName);
  1588. //if (dicmodel == null)
  1589. //{
  1590. dicaddid = new Bll.T_Sys_DictionaryValue().Add(bigTypeName);
  1591. //}
  1592. //else
  1593. //{
  1594. // dicaddid = dicmodel.F_DictionaryValueId;
  1595. //}
  1596. // }
  1597. }
  1598. if (typemodel == null)
  1599. { // 实时添加新设备类型
  1600. //lock (this) // 防止并发添加相同记录
  1601. //{
  1602. // // 再次检查,避免并发添加
  1603. // typemodel = typeservice.GetModel(bigTypeName, typeName, 1);
  1604. // if (typemodel == null)
  1605. // {
  1606. // Model.T_Adjust_DeviceType addtypemodel = new T_Adjust_DeviceType
  1607. // {
  1608. // F_DeviceNum = 0,
  1609. // F_IsDelete = 0,
  1610. // F_Classification = 1,
  1611. // F_CreateBy = "8000",
  1612. // F_BigTypeName = bigTypeName,
  1613. // F_TypeName = typeName,
  1614. // F_DicId = dicaddid,
  1615. // F_CreateTime = DateTime.Now,
  1616. // F_UpdateTime = DateTime.Now
  1617. // };
  1618. // int addres = typeservice.Add(addtypemodel);
  1619. // deviceTypeId = addres;
  1620. // }
  1621. // else
  1622. // {
  1623. // deviceTypeId = typemodel.F_TypeId;
  1624. // }
  1625. //}
  1626. Model.T_Adjust_DeviceType addtypemodel = new T_Adjust_DeviceType
  1627. {
  1628. F_DeviceNum = 0,
  1629. F_IsDelete = 0,
  1630. F_Classification = 1,
  1631. F_CreateBy = "8000",
  1632. F_BigTypeName = bigTypeName,
  1633. F_TypeName = typeName,
  1634. F_DicId = dicaddid,
  1635. F_CreateTime = DateTime.Now,
  1636. F_UpdateTime = DateTime.Now
  1637. };
  1638. int addres = typeservice.Add(addtypemodel);
  1639. deviceTypeId = addres;
  1640. }
  1641. else
  1642. {
  1643. deviceTypeId = typemodel.F_TypeId;
  1644. }
  1645. var existslist = deviceservice.GetModelList(" F_AssetCode ='" + assetCode + "'");
  1646. Model.T_Adjust_Device model = new T_Adjust_Device();
  1647. //检查设备是否已存在(实时查询)
  1648. if (existslist.Count > 0)
  1649. {
  1650. //存在这个资产编码,更新原来的值
  1651. Model.T_Adjust_Device devicemodel = existslist.First();
  1652. if (devicemodel.F_BigTypeName == bigTypeName && devicemodel.F_TypeName == typeName && devicemodel.F_DicId == dicaddid
  1653. && devicemodel.F_Specification == specification && devicemodel.F_Code == code
  1654. )
  1655. {
  1656. return;
  1657. //continue;
  1658. }
  1659. //devicemodel.F_BigTypeName = bigTypeName;
  1660. // devicemodel.F_TypeName = typeName;
  1661. // devicemodel.F_DicId = dicaddid;
  1662. // devicemodel.F_Specification = specification;
  1663. // devicemodel.F_Code = code;
  1664. // devicemodel.F_UpdateTime = DateTime.Now;
  1665. // deviceservice.Update(devicemodel);
  1666. //return;
  1667. devicemodel.F_BigTypeName = bigTypeName;
  1668. devicemodel.F_TypeName = typeName;
  1669. devicemodel.F_DicId = dicaddid;
  1670. devicemodel.F_Specification = specification;
  1671. devicemodel.F_Code = code;
  1672. devicemodel.F_UpdateTime = DateTime.Now;
  1673. lock (devicesToUpdate)
  1674. {
  1675. devicesToUpdate.Add(devicemodel);
  1676. }
  1677. return;
  1678. //continue;
  1679. // return Error("资产编码不能重复!");
  1680. }
  1681. // 10. 设置设备部门和状态信息(使用预加载的部门数据)
  1682. if (departmentDict.TryGetValue(mandept_name, out Model.T_Sys_Department targetDepartment))
  1683. {
  1684. model.F_BelongPhone = targetDepartment.F_OfficeTelephone;
  1685. model.F_BelongAddress = targetDepartment.F_Location;
  1686. model.F_BelongDept = targetDepartment.F_DeptId;
  1687. if (deptDict.TryGetValue(targetDepartment.F_DeptId, out string keyname))
  1688. {
  1689. model.F_BelongDeptName = keyname;
  1690. }
  1691. if (departmentDict.TryGetValue(usedept_name, out Model.T_Sys_Department useDepartment))
  1692. {
  1693. model.F_UseAddress = useDepartment.F_Location;
  1694. model.F_UseDept = useDepartment.F_DeptId;
  1695. if (deptDict.TryGetValue(useDepartment.F_DeptId, out string usekeyname))
  1696. {
  1697. model.F_UseDeptName = usekeyname;
  1698. }
  1699. model.F_UsePhone = useDepartment.F_OfficeTelephone;
  1700. model.F_DeviceStatus = 3; // 使用中
  1701. }
  1702. else
  1703. {
  1704. model.F_DeviceStatus = 1; // 待上架
  1705. }
  1706. }
  1707. else
  1708. {
  1709. model.F_DeviceStatus = 1; // 待上架
  1710. }
  1711. // 11. 设置设备基本信息
  1712. model.F_BigTypeName = bigTypeName;
  1713. model.F_TypeName = typeName;
  1714. model.F_DicId = dicaddid;
  1715. model.F_DeviceTypeId = deviceTypeId;
  1716. model.F_AssetCode = assetCode;
  1717. model.F_Specification = specification;
  1718. model.F_Code = code;
  1719. model.F_Source = 3;
  1720. model.F_CreateBy = "8000";
  1721. model.F_CreateTime = DateTime.Now;
  1722. model.F_UpdateTime = DateTime.Now;
  1723. model.F_Classify = 1;
  1724. model.F_IsDelete = 0;
  1725. // 12. 添加到待处理列表
  1726. lock (devicesToAdd)
  1727. {
  1728. // 关键字段判断:根据业务需求定义重复逻辑
  1729. bool exists = devicesToAdd.Any(x =>x.F_AssetCode == model.F_AssetCode);
  1730. // 仅当不存在重复时添加
  1731. if (!exists)
  1732. {
  1733. devicesToAdd.Add(model);
  1734. }
  1735. }
  1736. // 13. 记录需要更新的设备类型计数
  1737. lock (deviceTypeUpdates)
  1738. {
  1739. if (deviceTypeUpdates.ContainsKey(deviceTypeId))
  1740. {
  1741. deviceTypeUpdates[deviceTypeId]++;
  1742. }
  1743. else
  1744. {
  1745. deviceTypeUpdates[deviceTypeId] = 1;
  1746. }
  1747. }
  1748. // });
  1749. #region
  1750. //有使用科室,并且能查到使用科室id ,就给状态是使用中,不然就待上架
  1751. //Model.T_Sys_Department targetDepartment = deptlist.FirstOrDefault(d => d.F_DeptName == mandept_name);
  1752. // Model.T_Sys_Department useDepartment = deptlist.FirstOrDefault(d => d.F_DeptName == usedept_name);
  1753. // if (targetDepartment != null)
  1754. // {
  1755. // // 找到后的操作
  1756. // model.F_BelongPhone = targetDepartment.F_OfficeTelephone;
  1757. // model.F_BelongAddress = targetDepartment.F_Location;
  1758. // model.F_BelongDept = targetDepartment.F_DeptId;
  1759. // var keyname = deptdt.AsEnumerable()
  1760. // .Where(row => row.Field<int>("F_DeptId") == targetDepartment.F_DeptId)
  1761. // .Select(row => row.Field<string>("keyname"))
  1762. // .FirstOrDefault();
  1763. // if (keyname != null)
  1764. // {
  1765. // model.F_BelongDeptName = keyname;
  1766. // }
  1767. // if (useDepartment != null)
  1768. // {
  1769. // model.F_UseAddress = useDepartment.F_Location;
  1770. // model.F_UseDept = useDepartment.F_DeptId;
  1771. // var usekeyname = deptdt.AsEnumerable()
  1772. // .Where(row => row.Field<int>("F_DeptId") == useDepartment.F_DeptId)
  1773. // .Select(row => row.Field<string>("keyname"))
  1774. // .FirstOrDefault();
  1775. // if (keyname != null)
  1776. // {
  1777. // model.F_UseDeptName = usekeyname;
  1778. // }
  1779. // model.F_UsePhone = useDepartment.F_OfficeTelephone;
  1780. // model.F_DeviceStatus = 3;//使用中
  1781. // }
  1782. // else
  1783. // {
  1784. // model.F_DeviceStatus = 1;//待上架
  1785. // }
  1786. // }
  1787. // else
  1788. // {
  1789. // model.F_DeviceStatus = 1;//待上架
  1790. // }
  1791. // model.F_BigTypeName = bigTypeName;
  1792. // model.F_TypeName = typeName;
  1793. // model.F_DicId = dicaddid;
  1794. // model.F_DeviceTypeId = deviceTypeId;
  1795. // model.F_AssetCode = assetCode;
  1796. // // model.F_DeviceShortCode = deviceShortCode;
  1797. // model.F_Specification = specification;
  1798. // model.F_Code = code;
  1799. // model.F_Source = 3;
  1800. // model.F_CreateBy = "8000";
  1801. // model.F_CreateTime = DateTime.Now;
  1802. // model.F_UpdateTime = DateTime.Now;
  1803. // model.F_DeviceStatus = 1;//待上架
  1804. // model.F_Classify = 1;
  1805. // model.F_IsDelete = 0;
  1806. //// model.F_File = file;
  1807. //try
  1808. //{
  1809. // int res = deviceservice.Add(model);
  1810. // if (res > 0)
  1811. // {
  1812. // //设备类型的数量加1
  1813. // var mm = typeservice.GetModel(deviceTypeId);
  1814. // mm.F_DeviceNum = mm.F_DeviceNum == null ? 1 : mm.F_DeviceNum + 1;
  1815. // mm.F_UpdateTime = DateTime.Now;
  1816. // typeservice.Update(mm);
  1817. // // return Success("添加成功");
  1818. // }
  1819. // else
  1820. // {
  1821. // // return Error("添加失败");
  1822. // }
  1823. //}
  1824. //catch (Exception)
  1825. //{
  1826. //}
  1827. ////}
  1828. // }
  1829. #endregion
  1830. });
  1831. // 14. 批量处理当前页数据
  1832. if (devicesToAdd.Count > 0)
  1833. {
  1834. BatchAddDevices(devicesToAdd);
  1835. DbHelperSQL.ExecuteSql(" insert into T_Log(F_Type,F_Content) values('同步设备,添加','" + devicesToAdd.Count + "')");
  1836. devicesToAdd.Clear();
  1837. }
  1838. if (devicesToUpdate.Count > 0)
  1839. {
  1840. BatchUpdateDevices(devicesToUpdate);
  1841. DbHelperSQL.ExecuteSql(" insert into T_Log(F_Type,F_Content) values('同步设备,修改','" + devicesToUpdate.Count + "')");
  1842. devicesToUpdate.Clear();
  1843. }
  1844. // 15. 更新设备类型计数
  1845. if (deviceTypeUpdates.Count > 0)
  1846. {
  1847. BatchUpdateDeviceTypeCounts(deviceTypeUpdates);
  1848. deviceTypeUpdates.Clear();
  1849. }
  1850. // 更新分页键
  1851. lastId = Convert.ToInt32(dt.Rows[dt.Rows.Count - 1]["sb_Id"]);
  1852. processed += dt.Rows.Count;
  1853. }
  1854. //把所有的tb_bz=1的更新成0
  1855. DbHelperSQL.ExecuteSql("update T_SyncEquip set tb_bz=0 ");
  1856. }
  1857. // 批量添加设备的方法
  1858. private void BatchAddDevices(List<Model.T_Adjust_Device> devices)
  1859. {
  1860. int batchSize = 70; // 根据实际情况调整
  1861. for (int i = 0; i < devices.Count; i += batchSize)
  1862. {
  1863. int count = Math.Min(batchSize, devices.Count - i);
  1864. List<Model.T_Adjust_Device> batch = devices.GetRange(i, count);
  1865. deviceservice.BatchAddDevices(batch);
  1866. }
  1867. }
  1868. // 批量更新设备的方法
  1869. private void BatchUpdateDevices(List<Model.T_Adjust_Device> devices)
  1870. {
  1871. deviceservice.BatchUpdateDevices(devices);
  1872. }
  1873. // 批量更新设备类型计数的方法
  1874. private void BatchUpdateDeviceTypeCounts(Dictionary<int, int> typeCounts)
  1875. {
  1876. deviceservice.BatchUpdateDeviceTypeCounts(typeCounts);
  1877. }
  1878. }
  1879. }