DepartmentController.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. using System;
  2. using System.Linq;
  3. using System.Web.Mvc;
  4. using Central.Control.Domain;
  5. using Ant.Service.Common.Enums;
  6. using Ant.Service.Common;
  7. using System.Collections.Generic;
  8. using MES.Production.Service.IService;
  9. using System.Collections;
  10. using System.Globalization;
  11. using ChangFa.Machinery.WebPage.Controllers;
  12. using System.Web;
  13. using System.Xml;
  14. using System.Text;
  15. using System.IO;
  16. using System.Net;
  17. using System.Drawing;
  18. using Ant.Service.Utility;
  19. using System.Text.RegularExpressions;
  20. using Newtonsoft.Json;
  21. using MES.Production.Entity;
  22. using Ant.ORM;
  23. using Ant.Data;
  24. using MES.Production.Entity.Entity;
  25. using JsonHelper = Ant.Service.Common.JsonHelper;
  26. using JsonConverter = Ant.Service.Common.JsonConverter;
  27. namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
  28. {
  29. /// <summary>
  30. /// 部门管理控制器
  31. /// add 作者: 季健国 QQ:181589805 by 2016-05-25
  32. /// </summary>
  33. public class DepartmentController : BaseController
  34. {
  35. #region 声明容器
  36. /// <summary>
  37. /// 部门
  38. /// </summary>
  39. IDepartmentManage DepartmentManage { get; set; }
  40. /// <summary>
  41. /// 用户部门
  42. /// </summary>
  43. IUserDepartmentManage UserDepartmentManage { get; set; }
  44. /// <summary>
  45. /// 岗位人员
  46. /// </summary>
  47. IPostUserManage PostUserManage { get; set; }
  48. /// <summary>
  49. /// 岗位部门
  50. /// </summary>
  51. IPostDepartmentManage PostDepartmentManage { get; set; }
  52. #endregion
  53. /// <summary>
  54. /// 加载导航页
  55. /// </summary>
  56. /// <returns></returns>
  57. [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "View")]
  58. public ActionResult Home()
  59. {
  60. return View();
  61. }
  62. /// <summary>
  63. /// 加载主页
  64. /// </summary>
  65. [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "View")]
  66. public ActionResult Index()
  67. {
  68. try
  69. {
  70. #region 处理查询参数
  71. string dptId = Request.QueryString["dptid"];
  72. #endregion
  73. #region 加载列表
  74. //加载部门信息
  75. var result = this.DepartmentManage.Get(p => p.ID == dptId) ?? new SYS_DISTRIBUTORS();
  76. if (!string.IsNullOrEmpty(dptId))
  77. {
  78. //加载岗位
  79. var postlist = this.PostDepartmentManage.GetPostIDByDptId(dptId);
  80. ViewData["postInfo"] = postlist;
  81. //加载部门岗位关系ID
  82. var postIdStr = postlist.Aggregate(string.Empty, (current, t) => current + t.ID + ",").TrimEnd(',');
  83. //读取相关用户,构造用户,增加岗位ID列
  84. var userList = this.PostUserManage.LoadAll(p => postIdStr.Contains(p.SYS_POST_DEPARTMENT.FK_POST_ID) && p.SYS_POST_DEPARTMENT.FK_DEPARTMENT_ID == dptId).Select(c => new
  85. {
  86. POSTID = c.SYS_POST_DEPARTMENT.FK_POST_ID,
  87. c.SYS_USER.NAME
  88. }).GroupBy(c => c).Select(d => new { POSTID = d.Key.POSTID, NAME = d.Key.NAME }).ToList();
  89. ViewData["userInfo"] = JsonConverter.JsonClass(userList);
  90. }
  91. #endregion
  92. return View(result);
  93. }
  94. catch (Exception e)
  95. {
  96. WriteLog(enumOperator.Select, "经销商管理加载主页:", e);
  97. throw e.InnerException;
  98. }
  99. }
  100. /// <summary>
  101. /// 加载详情页
  102. /// </summary>
  103. [HttpGet]
  104. [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "Detail")]
  105. public ActionResult Detail(string id)
  106. {
  107. try
  108. {
  109. string filePathName = string.Empty;
  110. DistributorUser distributorUser = new DistributorUser();
  111. SYS_DISTRIBUTORS entity = new SYS_DISTRIBUTORS();
  112. int uid = 0;
  113. if (CurrentUser.USERTYPE == 2)
  114. {
  115. uid = CurrentUser.Id;
  116. }
  117. ViewBag.moduleparent = this.DepartmentManage.GetDepartmentByDetailNew(uid);
  118. //添加子部门
  119. string parentId = Request.QueryString["parentId"];
  120. if (!string.IsNullOrEmpty(parentId))
  121. {
  122. //entity.PARENTID = parentId;
  123. distributorUser.distributorInfo.PARENTID = parentId;
  124. return View(distributorUser);
  125. }
  126. if (!string.IsNullOrEmpty(id))
  127. {
  128. entity = this.DepartmentManage.Get(p => p.ID == id);
  129. distributorUser.distributorInfo = entity;
  130. using (AntORM orm = new AntORM())
  131. {
  132. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  133. RequestModel res = new RequestModel
  134. {
  135. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
  136. };
  137. var reslut = orm.Queryable<EntYW_UserDistributor>().Where(p => p.distributorId == id).ToList();
  138. if (reslut.IsSuccess)
  139. {
  140. string strlist = "";
  141. foreach (EntYW_UserDistributor mod in reslut.ResultModel)
  142. {
  143. int userid = mod.userId.ToInt32();
  144. var userreslut = orm.Queryable<En_SYS_USER>().Where(p => p.ID == userid).FirstOrDefault(res);
  145. if (userreslut.IsSuccess)
  146. {
  147. En_SYS_USER usermod = userreslut.ResultModel;
  148. strlist += usermod.ID + "|" + usermod.NAME;
  149. }
  150. }
  151. ViewData["userid"] = strlist;
  152. }
  153. var usereslut = orm.Queryable<En_SYS_USER>().Where(p => p.DPTID == entity.ID).FirstOrDefault(res);
  154. if (usereslut.IsSuccess)
  155. {
  156. distributorUser.userInfo = usereslut.ResultModel;
  157. }
  158. }
  159. }
  160. else
  161. {
  162. distributorUser.distributorInfo = new SYS_DISTRIBUTORS();
  163. distributorUser.distributorInfo.PARENTID = "32f7a4bd-84de-4587-be29-734d65ad6f70";
  164. distributorUser.userInfo = new En_SYS_USER() { ISCANLOGIN = false };
  165. }
  166. return View(distributorUser);
  167. }
  168. catch (Exception e)
  169. {
  170. WriteLog(enumOperator.Select, "经销商管理加载详情页:", e);
  171. throw e.InnerException;
  172. }
  173. }
  174. /// <summary>
  175. /// 保存部门
  176. /// </summary>
  177. [ValidateInput(false)]
  178. [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "Add,Edit")]
  179. public ActionResult Save(DistributorUser entity)
  180. {
  181. bool isEdit = false; bool bz = true;
  182. var fileid = Request.Form["hidfileid"];
  183. var fileurl = Request.Form["hidfilename"];
  184. var paretid = Request.Form["PARENTID"];
  185. int userid = 0;
  186. var citystr = Request.Form["hkprov"] + "," + Request.Form["hkcity"] + "," + Request.Form["hkcountry"];
  187. var json = new JsonHelper() { Msg = "保存成功", Status = "n" };
  188. try
  189. {
  190. var _entity = new SYS_DISTRIBUTORS(); var _userenty = new En_SYS_USER();
  191. if (entity.distributorInfo != null)
  192. {
  193. if (!string.IsNullOrEmpty(entity.distributorInfo.ID))
  194. {
  195. EntYW_UserDistributor yW_User = new EntYW_UserDistributor();
  196. using (AntORM orm = new AntORM())
  197. {
  198. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  199. RequestModel res = new RequestModel
  200. {
  201. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
  202. };
  203. var disreult = orm.DeleteByLambda<EntYW_UserDistributor>(p => p.distributorId == entity.distributorInfo.ID, res);
  204. foreach (string str in Request.Form["hidsendUserid"].Split(','))
  205. {
  206. if (!str.IsEmpty())
  207. {
  208. EntYW_UserDistributor entmod = new EntYW_UserDistributor();
  209. entmod.userId = str.ToInt32();
  210. entmod.distributorId = entity.distributorInfo.ID;
  211. entmod.ID = Guid.NewGuid();
  212. var reslutuser = orm.Save(entmod, res);
  213. }
  214. }
  215. }
  216. #region 修改
  217. _entity = this.DepartmentManage.Get(p => p.ID == entity.distributorInfo.ID);
  218. entity.distributorInfo.CREATEDATE = _entity.CREATEDATE;
  219. entity.distributorInfo.CREATEPERID = _entity.CREATEPERID;
  220. entity.distributorInfo.PARENTID = paretid;
  221. entity.distributorInfo.UPDATEDATE = DateTime.Now;
  222. entity.distributorInfo.UPDATEUSER = this.CurrentUser.Name;
  223. entity.distributorInfo.Address = Request.Form["distributorInfo.Address"];
  224. entity.distributorInfo.FenceRadius = Request.Form["example_length"].ToInt32();
  225. entity.distributorInfo.LICENSEURL = fileurl;
  226. entity.distributorInfo.LICENSEVALUE = fileid;
  227. entity.distributorInfo.ProvinceCity = citystr;
  228. if (entity.distributorInfo.PARENTID != _entity.PARENTID)
  229. {
  230. entity.distributorInfo.CODE = this.DepartmentManage.CreateCode(entity.distributorInfo.PARENTID);
  231. }
  232. else
  233. {
  234. entity.distributorInfo.CODE = _entity.CODE;
  235. }
  236. //获取父级记录
  237. if (string.IsNullOrEmpty(paretid))
  238. {
  239. //业务等级
  240. entity.distributorInfo.BUSINESSLEVEL = 1;
  241. entity.distributorInfo.PARENTCODE = null;
  242. }
  243. else
  244. {
  245. var parententity = this.DepartmentManage.Get(p => p.ID == _entity.PARENTID);
  246. entity.distributorInfo.BUSINESSLEVEL = parententity.BUSINESSLEVEL + 1;
  247. entity.distributorInfo.PARENTCODE = parententity.CODE;
  248. entity.distributorInfo.ParentName = parententity.NAME;
  249. }
  250. #endregion
  251. _entity = entity.distributorInfo;
  252. _userenty = entity.userInfo;
  253. _userenty.ACCOUNT = _userenty.PHONE;
  254. _userenty.USERTYPE = 2;
  255. isEdit = true;
  256. using (AntORM orm = new AntORM())
  257. {
  258. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  259. RequestModel res = new RequestModel
  260. {
  261. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
  262. };
  263. var resuser = orm.UpdateById(_userenty, res);
  264. }
  265. }
  266. else
  267. {
  268. #region 添加
  269. _entity = entity.distributorInfo;
  270. _entity.ID = Guid.NewGuid().ToString();
  271. _entity.CREATEDATE = DateTime.Now;
  272. _entity.CREATEPERID = this.CurrentUser.Name;
  273. _entity.PARENTID = paretid;
  274. _entity.UPDATEDATE = DateTime.Now;
  275. _entity.UPDATEUSER = this.CurrentUser.Name;
  276. _entity.Address = entity.distributorInfo.Address;
  277. _entity.FenceRadius = Request.Form["example_length"].ToInt32();
  278. _entity.LICENSEURL = fileurl;
  279. _entity.LICENSEVALUE = fileid;
  280. _entity.ProvinceCity = citystr;
  281. _entity.Province = Request.Form["hkprov"].ToInt32();
  282. _entity.City = Request.Form["hkcity"].ToInt32();
  283. _entity.Area = Request.Form["hkcountry"].ToInt32();
  284. _userenty = entity.userInfo;
  285. _userenty.DPTID = _entity.ID;
  286. var pwd = PasswordUtils.MD5_Encrypt("111111", 32);
  287. _userenty.USERTYPE = 2;
  288. _userenty.ACCOUNT = _userenty.PHONE;
  289. _userenty.PASSWORD = pwd;
  290. _userenty.CREATEDATE = DateTime.Now;
  291. _userenty.CREATEPER = this.CurrentUser.Name;
  292. _userenty.UPDATEDATE = DateTime.Now;
  293. _userenty.UPDATEUSER = this.CurrentUser.Name;
  294. using (AntORM orm = new AntORM())
  295. {
  296. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  297. RequestModel res = new RequestModel
  298. {
  299. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
  300. };
  301. var resuser = orm.SaveHaveNull(_userenty, res);
  302. if (resuser.IsSuccess)
  303. {
  304. userid = resuser.RecordNum;
  305. _userenty.ID = userid;
  306. }
  307. }
  308. //根据上级部门的ID确定当前部门的CODE
  309. _entity.CODE = this.DepartmentManage.CreateCode(_entity.PARENTID);
  310. //获取父级记录
  311. if (string.IsNullOrEmpty(_entity.PARENTID))
  312. {
  313. //业务等级
  314. entity.distributorInfo.BUSINESSLEVEL = 1;
  315. entity.distributorInfo.PARENTCODE = null;
  316. }
  317. else
  318. {
  319. var parententity = this.DepartmentManage.Get(p => p.ID == _entity.PARENTID);
  320. entity.distributorInfo.BUSINESSLEVEL = parententity.BUSINESSLEVEL + 1;
  321. entity.distributorInfo.PARENTCODE = parententity.CODE;
  322. entity.distributorInfo.ParentName = parententity.NAME;
  323. }
  324. #endregion
  325. EntYW_UserDistributor yW_User = new EntYW_UserDistributor();
  326. using (AntORM orm = new AntORM())
  327. {
  328. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  329. RequestModel res = new RequestModel
  330. {
  331. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
  332. };
  333. foreach (string str in Request.Form["hidsendUserid"].Split(','))
  334. {
  335. if (!str.IsEmpty())
  336. {
  337. int uid = str.ToInt32();
  338. var num = orm.Queryable<EntYW_UserDistributor>().Where(p => p.distributorId == _entity.ID && p.userId == uid).Count();
  339. if (num > 0)
  340. {
  341. bz = false;
  342. }
  343. }
  344. }
  345. if (bz)
  346. {
  347. foreach (string str in Request.Form["hidsendUserid"].Split(','))
  348. {
  349. EntYW_UserDistributor entmod = new EntYW_UserDistributor();
  350. entmod.userId = str.ToInt32();
  351. entmod.distributorId = _entity.ID;
  352. entmod.ID = Guid.NewGuid();
  353. var reslutuser = orm.Save(entmod, res);
  354. }
  355. }
  356. }
  357. EntSYS_USER_ROLE roleMod = new EntSYS_USER_ROLE();
  358. using (AntORM orm = new AntORM())
  359. {
  360. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  361. RequestModel res = new RequestModel
  362. {
  363. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
  364. };
  365. roleMod.FK_ROLEID = 3;
  366. roleMod.FK_USERID = _userenty.ID.ToInt32();
  367. orm.Save(roleMod, res);
  368. }
  369. }
  370. //先取消电子围栏
  371. var addressmod = AddressToGPS(entity.distributorInfo.Address);
  372. if (addressmod.Geocodes.Count > 0)
  373. {
  374. foreach (Geocode mod in addressmod.Geocodes)
  375. {
  376. string lnglatstr = mod.Location;
  377. _entity.Theodolite = lnglatstr;
  378. }
  379. }
  380. #region 老版创建电子围栏
  381. //var addressmod = AddressToGPS(entity.distributorInfo.Address);
  382. //if (addressmod.Geocodes.Count > 0)
  383. //{
  384. // foreach (Geocode mod in addressmod.Geocodes)
  385. // {
  386. // string lnglatstr = mod.Location;
  387. // _entity.Theodolite = lnglatstr;
  388. // if (lnglatstr.IndexOf(',') >= 0)
  389. // {
  390. // string lng = lnglatstr.Split(',')[0];
  391. // string lat = lnglatstr.Split(',')[1];
  392. // if (entity.distributorInfo.FenceID.IsEmpty())
  393. // {
  394. // var reslutFences = ToolGPSamap.CreaterGeofence(lng.ToDou(), lat.ToDou(), entity.distributorInfo.FenceRadius.ToDou(), entity.distributorInfo.NAME);
  395. // if (!reslutFences.data.gid.IsEmpty())
  396. // {
  397. // entity.distributorInfo.FenceID = reslutFences.data.gid;
  398. // _userenty = entity.userInfo;
  399. // _userenty.DPTID = _entity.ID;
  400. // var pwd = PasswordUtils.MD5_Encrypt("111111", 32);
  401. // _userenty.USERTYPE = 2;
  402. // _userenty.PASSWORD = pwd;
  403. // _userenty.CREATEDATE = DateTime.Now;
  404. // _userenty.CREATEPER = this.CurrentUser.Name;
  405. // _userenty.UPDATEDATE = DateTime.Now;
  406. // _userenty.UPDATEUSER = this.CurrentUser.Name;
  407. // using (AntORM orm = new AntORM())
  408. // {
  409. // orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  410. // RequestModel res = new RequestModel
  411. // {
  412. // newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
  413. // };
  414. // var resuser = orm.Save(_userenty, res);
  415. // }
  416. // }
  417. // else
  418. // {
  419. // json.Status = "n";
  420. // json.Msg = reslutFences.data.message;
  421. // return Json(json);
  422. // }
  423. // }
  424. // else
  425. // {
  426. // RequestQueryFence request = new RequestQueryFence();
  427. // request.gid = entity.distributorInfo.FenceID;
  428. // var FenceReslut = ToolGPSamap.QueryGeoFence(request);
  429. // var list = FenceReslut.Data.RsList;
  430. // foreach (RsList mm in list)
  431. // {
  432. // var fencereslut = ToolGPSamap.UpdateGeofence(lng.ToDou(), lat.ToDou(), entity.distributorInfo.FenceRadius.ToDou(), entity.distributorInfo.NAME, mm.Gid);
  433. // }
  434. // }
  435. // }
  436. // }
  437. //}
  438. //else
  439. //{
  440. // string lnglatstr = _entity.Theodolite;
  441. // if (lnglatstr.IndexOf(',') >= 0)
  442. // {
  443. // string lng = lnglatstr.Split(',')[0];
  444. // string lat = lnglatstr.Split(',')[1];
  445. // if (entity.distributorInfo.FenceID.IsEmpty())
  446. // {
  447. // var reslutFences = ToolGPSamap.CreaterGeofence(lng.ToDou(), lat.ToDou(), entity.distributorInfo.FenceRadius.ToDou(), entity.distributorInfo.NAME);
  448. // if (!reslutFences.data.gid.IsEmpty())
  449. // {
  450. // entity.distributorInfo.FenceID = reslutFences.data.gid;
  451. // _userenty = entity.userInfo;
  452. // _userenty.DPTID = _entity.ID;
  453. // var pwd = PasswordUtils.MD5_Encrypt("111111", 32);
  454. // _userenty.USERTYPE = 2;
  455. // _userenty.PASSWORD = pwd;
  456. // _userenty.CREATEDATE = DateTime.Now;
  457. // _userenty.CREATEPER = this.CurrentUser.Name;
  458. // _userenty.UPDATEDATE = DateTime.Now;
  459. // _userenty.UPDATEUSER = this.CurrentUser.Name;
  460. // using (AntORM orm = new AntORM())
  461. // {
  462. // orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  463. // RequestModel res = new RequestModel
  464. // {
  465. // newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
  466. // };
  467. // var resuser = orm.Save(_userenty, res);
  468. // }
  469. // }
  470. // }
  471. // else
  472. // {
  473. // RequestQueryFence request = new RequestQueryFence();
  474. // request.gid = entity.distributorInfo.FenceID;
  475. // var FenceReslut = ToolGPSamap.QueryGeoFence(request);
  476. // var list = FenceReslut.Data.RsList;
  477. // foreach (RsList mm in list)
  478. // {
  479. // var fencereslut = ToolGPSamap.UpdateGeofence(lng.ToDou(), lat.ToDou(), entity.distributorInfo.FenceRadius.ToDou(), entity.distributorInfo.NAME, mm.Gid);
  480. // }
  481. // }
  482. // }
  483. //}
  484. #endregion
  485. //判断同一个部门下,是否重名
  486. var predicate = PredicateBuilder.True<SYS_DISTRIBUTORS>();
  487. predicate = predicate.And(p => p.PARENTID == _entity.PARENTID);
  488. predicate = predicate.And(p => p.NAME == _entity.NAME);
  489. predicate = predicate.And(p => p.ID != _entity.ID);
  490. if (!this.DepartmentManage.IsExist(predicate))
  491. {
  492. if (this.DepartmentManage.SaveOrUpdate(_entity, isEdit))
  493. {
  494. json.Status = "y";
  495. try
  496. {
  497. //string imei = "356802032734930";
  498. string imei = "333333333333333";
  499. string discode = Ant.SuperSocket.Common.DataHelper.StringToHex(_entity.CODE);
  500. var str = Ant.SuperSocket.Common.DataHelper.sendCarData(Convert.ToInt64(imei), "9922" + discode);
  501. var body = Ant.SuperSocket.Common.DataHelper.HexToByte(str);
  502. SocketClient client = new SocketClient();
  503. string ip = System.Web.Configuration.WebConfigurationManager.AppSettings["ip"].ToString();
  504. // string ip = "";
  505. int port = System.Web.Configuration.WebConfigurationManager.AppSettings["port"].ToInt32();
  506. client.Connect(ip, port);
  507. var flag = client.SendData(body);
  508. client.Disconnect();
  509. }
  510. catch (Exception ex) { }
  511. }
  512. else
  513. {
  514. json.Msg = "保存失败";
  515. }
  516. }
  517. else
  518. {
  519. json.Msg = "经销商" + entity.distributorInfo.NAME + "已存在,不能重复添加";
  520. }
  521. }
  522. else
  523. {
  524. json.Msg = "未找到需要保存的经销商信息";
  525. }
  526. if (isEdit)
  527. {
  528. WriteLog(enumOperator.Edit, "修改经销商信息,结果:" + json.Msg, enumLog4net.INFO);
  529. }
  530. else
  531. {
  532. WriteLog(enumOperator.Add, "添加经销商信息,结果:" + json.Msg, enumLog4net.INFO);
  533. }
  534. }
  535. catch (Exception e)
  536. {
  537. json.Msg = "保存经销商信息发生内部错误!";
  538. WriteLog(enumOperator.None, "保存经销商信息:", e);
  539. }
  540. return Json(json);
  541. }
  542. /// <summary>
  543. /// 将地址转GPS
  544. /// </summary>
  545. /// <param name="address"></param>
  546. /// <returns></returns>
  547. private AddressGPS AddressToGPS(string address)
  548. {
  549. WebClient server = new WebClient();
  550. string url = String.Format("http://restapi.amap.com/v3/geocode/geo?key={0}&address={1}&city={2}", "fa36ec6323075f37020083ceffe8eb24", address, "");
  551. var replyBuffer = server.DownloadData(url);
  552. var reply = System.Text.Encoding.UTF8.GetString(replyBuffer);
  553. var mod = JsonConvert.DeserializeObject<AddressGPS>(reply);
  554. return mod;
  555. }
  556. /// <summary>
  557. /// 删除部门
  558. /// </summary>
  559. [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "Remove")]
  560. public ActionResult Delete(string idList)
  561. {
  562. JsonHelper json = new JsonHelper() { Msg = "删除经销商成功", ReUrl = "/Department/Index", Status = "n" };
  563. try
  564. {
  565. if (!string.IsNullOrEmpty(idList))
  566. {
  567. idList = idList.TrimEnd(',');
  568. //判断是否有下属部门
  569. if (!this.DepartmentManage.DepartmentIsExists(idList))
  570. {
  571. //判断是否有用户归属该部门
  572. if (!this.UserDepartmentManage.IsExist(p => idList.Contains(p.DEPARTMENT_ID)))
  573. {
  574. //判断该部门是否有岗位
  575. if (!this.PostDepartmentManage.IsExist(p => idList.Contains(p.FK_DEPARTMENT_ID)))
  576. {
  577. var idList1 = idList.Split(',').ToList();
  578. this.DepartmentManage.Delete(p => idList.Contains(p.ID));
  579. json.Status = "y";
  580. }
  581. else
  582. {
  583. json.Msg = "该经销商有岗位信息不能删除,请先取消岗位分配";
  584. }
  585. }
  586. else
  587. {
  588. json.Msg = "该经销商存在用户不能删除";
  589. }
  590. }
  591. else
  592. {
  593. json.Msg = "该经销商有下属部门不能删除";
  594. }
  595. }
  596. else
  597. {
  598. json.Msg = "未找到要删除的部门记录";
  599. }
  600. WriteLog(enumOperator.Remove, "删除部门:" + json.Msg, enumLog4net.WARN);
  601. }
  602. catch (Exception e)
  603. {
  604. json.Msg = "删除经销商发生内部错误!";
  605. WriteLog(enumOperator.Remove, "删除部门:", e);
  606. }
  607. return Json(json);
  608. }
  609. #region 帮助方法及其他控制器调用
  610. /// <summary>
  611. /// 获取部门导航树
  612. /// </summary>
  613. public string GetTree()
  614. {
  615. string type = Request.Form["type"];
  616. JsonHelper json = new JsonHelper() { Msg = "Success", Status = "y" };
  617. try
  618. {
  619. var query = this.DepartmentManage.LoadAll(null).OrderBy(p => p.SHOWORDER).ToList();
  620. var result = query.Select(m => new
  621. {
  622. id = m.ID,
  623. pId = m.PARENTID,
  624. name = m.NAME,
  625. open = true,
  626. target = "DeployBase",
  627. url = GetType(type) + "?dptid=" + m.ID.ToString(CultureInfo.InvariantCulture)
  628. }).ToList();
  629. json.Data = result;
  630. }
  631. catch (Exception e)
  632. {
  633. json.Status = "Error";
  634. json.Msg = "获取经销商导航树发生内部错误!";
  635. WriteLog(enumOperator.Select, "经销商管理,获取经销商导航树:", e);
  636. }
  637. return JsonConverter.Serialize(json, true);
  638. }
  639. private string GetType(string type)
  640. {
  641. if (type == "0")
  642. {
  643. return "/Sys/User/Index";
  644. }
  645. else
  646. {
  647. return "/Sys/Department/Index";
  648. }
  649. }
  650. /// <summary>
  651. /// 选择部门视图
  652. /// </summary>
  653. [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "Select")]
  654. public ActionResult GetSelectDpt()
  655. {
  656. return View();
  657. }
  658. /// <summary>
  659. /// 获取部门导航树,带复选框
  660. /// </summary>
  661. public ActionResult GetSelectTree()
  662. {
  663. string olddptlist = Request.Form["dptlist"];
  664. JsonHelper json = new JsonHelper() { Msg = "Success", Status = "y" };
  665. try
  666. {
  667. //获取部门列表
  668. var dptlist = this.DepartmentManage.LoadAll(null).OrderBy(p => p.SHOWORDER).ToList();
  669. if (dptlist != null && dptlist.Count > 0)
  670. {
  671. var result = new List<object>();
  672. result.AddRange(dptlist.Select(p => new
  673. {
  674. id = p.ID,
  675. name = p.NAME,
  676. pId = p.PARENTID,
  677. open = true,
  678. checkeds = olddptlist.Split(',').Any(c => c.Equals(p.ID))
  679. }));
  680. json.Data = JsonConverter.Serialize(result).Replace("checkeds", "checked");
  681. }
  682. }
  683. catch (Exception e)
  684. {
  685. json.Status = "n";
  686. json.Msg = "获取经销商导航树发生内部错误!";
  687. WriteLog(enumOperator.Select, "获取经销商导航树(带复选框):", e);
  688. }
  689. return Json(json);
  690. }
  691. #endregion
  692. }
  693. }