PushBAL.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using Common.Wechat;
  2. using CoreEntity.DAL;
  3. using CoreEntity.Entity;
  4. using JCSoft.WX.Framework.Api;
  5. using PublicLibrary.Common;
  6. using CoreEntity.TimedTask;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using ZcPeng.weixin.PublicAccount;
  11. using CoreEntity.Push;
  12. using CoreEntity.TimeJob;
  13. using System.Data;
  14. using ZcPeng.PublicLibrary;
  15. using System.Data.SqlClient;
  16. using Common.Model;
  17. namespace CoreEntity.BAL
  18. {
  19. public class PushBAL
  20. {
  21. //public static Dictionary<string,Object> BatchPush(string RecordId, string FeedbackId,
  22. // string PushAccountId, string GoodsId, string GoodsCode
  23. // , string GoodsName, decimal StoreGapNum
  24. // , string GoodsSpec, string Manufacturer
  25. // , string BusinessId, string BusinessCode, string BusinessName
  26. // , string SuppliersId, string BrandName, string BrandId
  27. // , decimal PurchaseTotalNum, Decimal PurPrice
  28. // , string EntId, string Remark
  29. // , string LoginAccountId
  30. // , IApiClient _client)
  31. //{
  32. // List<Object> list = new List<object>(2);
  33. // //Object successRecord1 = 0;
  34. // //Object resultRecord1 = "";
  35. // //添加或修改推送记录
  36. // IDictionary<string, Object> successs1 =
  37. // PushRecordDAL.AddOrUpdatePushFeedBack(
  38. // RecordId
  39. // , FeedbackId,
  40. // Convert.ToInt32(PushAccountId), GoodsId, GoodsCode
  41. // , GoodsName, StoreGapNum
  42. // , GoodsSpec, Manufacturer
  43. // , BusinessId, BusinessCode, BusinessName
  44. // , SuppliersId, BrandName, BrandId
  45. // , EntId, Remark, LoginAccountId, PurPrice, PurchaseTotalNum);
  46. // //successs1.TryGetValue("successRecord", out successRecord1);
  47. // //successs1.TryGetValue("resultRecord", out resultRecord1);
  48. // Dictionary<string,Object> dic = new Dictionary<string, object>(2);
  49. // //dic.Add("successRecord1", successRecord1);
  50. // //dic.Add("resultRecord1", resultRecord1);
  51. // dic.Add("success", successs1);
  52. // return dic;
  53. //}
  54. public class PushInfo
  55. {
  56. private string _goodsName;
  57. private string _goodsNums;
  58. private string _totalAmount;
  59. private string _RecordId;
  60. public string GoodsName { get => _goodsName; set => _goodsName = value; }
  61. public string GoodsNums { get => _goodsNums; set => _goodsNums = value; }
  62. public string TotalAmount { get => _totalAmount; set => _totalAmount = value; }
  63. public string RecordId { get => _RecordId; set => _RecordId = value; }
  64. public static void saveGoodsNames(PushContactWithGoods fb, ref Dictionary<string, PushInfo> groupGoodsNames)
  65. {
  66. string userid = fb.UserId;
  67. if (fb.GetType().ToString() == "CoreEntity.Entity.PushFeedbackExt")
  68. userid = ((PushFeedbackExt)fb).UserId.ToString();
  69. if (groupGoodsNames.ContainsKey(userid))
  70. {
  71. PushInfo pushInfos;
  72. groupGoodsNames.TryGetValue(userid, out pushInfos);
  73. string GoodsName = fb.GoodsName;
  74. if (fb.GetType().ToString() == "CoreEntity.Entity.PushFeedbackExt")
  75. GoodsName = ((PushFeedbackExt)fb).GoodsName.ToString();
  76. decimal PurchaseTotalNum = fb.PurchaseTotalNum;
  77. if (fb.GetType().ToString() == "CoreEntity.Entity.PushFeedbackExt")
  78. PurchaseTotalNum = ((PushFeedbackExt)fb).PurchaseTotalNum;
  79. decimal TotalAmount = fb.PurchaseTotalNum * fb.LastPrice;
  80. if (fb.GetType().ToString() == "CoreEntity.Entity.PushFeedbackExt")
  81. TotalAmount = ((PushFeedbackExt)fb).PurchaseTotalNum * ((PushFeedbackExt)fb).LastPrice;
  82. pushInfos.GoodsName += "," + GoodsName;
  83. pushInfos.GoodsNums += "," + PurchaseTotalNum;
  84. pushInfos.TotalAmount += "," + TotalAmount;
  85. groupGoodsNames.Remove(userid);
  86. groupGoodsNames.Add(userid, pushInfos);
  87. }
  88. else
  89. {
  90. var pushInfo = new PushInfo { GoodsName = "", GoodsNums = "", TotalAmount = "" };
  91. string GoodsName = fb.GoodsName;
  92. if (fb.GetType().ToString() == "CoreEntity.Entity.PushFeedbackExt")
  93. GoodsName = ((PushFeedbackExt)fb).GoodsName.ToString();
  94. decimal PurchaseTotalNum = fb.PurchaseTotalNum;
  95. if (fb.GetType().ToString() == "CoreEntity.Entity.PushFeedbackExt")
  96. PurchaseTotalNum = ((PushFeedbackExt)fb).PurchaseTotalNum;
  97. decimal TotalAmount = fb.PurchaseTotalNum * fb.LastPrice;
  98. if (fb.GetType().ToString() == "CoreEntity.Entity.PushFeedbackExt")
  99. TotalAmount = ((PushFeedbackExt)fb).PurchaseTotalNum * ((PushFeedbackExt)fb).LastPrice;
  100. pushInfo.GoodsName = GoodsName;
  101. pushInfo.GoodsNums = PurchaseTotalNum.ToString();
  102. pushInfo.TotalAmount = (TotalAmount).ToString();
  103. groupGoodsNames.Add(userid, pushInfo);
  104. }
  105. }
  106. }
  107. public static List<Dictionary<string, Object>> pushAll(List<PushContactWithGoods> GetPushContacts, Dictionary<string, PushStorLimit> gapnum,
  108. string LoginAccountId, IApiClient _client)
  109. {
  110. Dictionary<string, List<PushContactWithGoods>> groups = new Dictionary<string, List<PushContactWithGoods>>();
  111. Dictionary<string, PushInfo> groupGoodsNames = new Dictionary<string, PushInfo>();
  112. foreach (PushContactWithGoods fb in GetPushContacts)
  113. {
  114. if (gapnum.ContainsKey(fb.GoodsId))
  115. {
  116. PushStorLimit limit;
  117. gapnum.TryGetValue(fb.GoodsId, out limit);
  118. fb.PurchaseTotalNum = Convert.ToInt32(limit.StoreGapNum);
  119. }
  120. if (groups.ContainsKey(fb.UserId.Trim()+"_"+fb.SaleManId))
  121. {
  122. List<PushContactWithGoods> to;
  123. groups.TryGetValue(fb.UserId.Trim() + "_" + fb.SaleManId, out to);
  124. to.Add(fb);
  125. groups.Remove(fb.UserId.Trim() + "_" + fb.SaleManId);
  126. groups.Add(fb.UserId.Trim() + "_" + fb.SaleManId, to);
  127. }
  128. else
  129. {
  130. List<PushContactWithGoods> to = new List<PushContactWithGoods>();
  131. to.Add(fb);
  132. groups.Add(fb.UserId.Trim() + "_" + fb.SaleManId, to);
  133. }
  134. fb.UserId = fb.UserId.Trim();
  135. PushInfo.saveGoodsNames(fb,ref groupGoodsNames);
  136. }
  137. List<Dictionary<string, Object>> result = new List<Dictionary<string, Object>>();
  138. //添加推送记录
  139. string PushCode = RandHelper.GetTimeRandId().ToString();
  140. var codeids = new Dictionary<string, object>(2);
  141. List<string> Ids = new List<string>();
  142. foreach (string groupkey in groups.Keys)
  143. {
  144. var grouparr = groupkey.Split("_");
  145. string userId = grouparr[0];
  146. string SaleManId = grouparr[1];
  147. var ContactPush = groups[groupkey][0];
  148. Dictionary<string, Object> successs2 =
  149. PushRecordDAL.AddPushRecord(PushCode,
  150. Convert.ToInt32(userId),
  151. ContactPush.ContactId,//contactid 默认关联第一个ContactId 因为可能有多个供应商对应的ContactId
  152. "",
  153. LoginAccountId,
  154. SaleManId
  155. );
  156. result.Add(successs2);
  157. Object successRecord = 0;
  158. successs2.TryGetValue("successRecord", out successRecord);
  159. Object recordId;
  160. successs2.TryGetValue("PushRecordId", out recordId);
  161. string RecordId = ((Guid)recordId).ToString();
  162. Ids.Add(RecordId);
  163. if ((int)successRecord > 0)
  164. {
  165. List<PushContactWithGoods> to;
  166. groups.TryGetValue(groupkey, out to);
  167. PushContactWithGoods fb0 = to.ToArray()[0];
  168. foreach (PushContactWithGoods contact in to)
  169. {
  170. int StoreGapNum = 0;
  171. if (gapnum.ContainsKey(contact.GoodsId))
  172. {
  173. PushStorLimit limit;
  174. gapnum.TryGetValue(contact.GoodsId, out limit);
  175. StoreGapNum = Convert.ToInt32(limit.StoreGapNum);
  176. }
  177. //string PushAccountId = contact.UserId;
  178. string FeedbackId = null;
  179. //var success = PushBAL.BatchPush(RecordId, FeedbackId,
  180. // contact.UserId, contact.GoodsId, contact.GoodsCode
  181. // , contact.GoodsName, StoreGapNum
  182. // , contact.GoodsSpec, contact.Manufacturer
  183. // , contact.BusinessId, contact.BusinessCode, contact.BusinessName
  184. // , contact.SuppliersId, contact.BrandName, contact.BrandId
  185. // , StoreGapNum, contact.LastPrice
  186. //, contact.EntId, ""
  187. //, LoginAccountId
  188. //, _client); IDictionary<string, Object> successs1 =
  189. Dictionary<string, Object>success = (Dictionary<string, Object>)PushRecordDAL.AddOrUpdatePushFeedBack(
  190. PushCode,
  191. RecordId
  192. , FeedbackId,
  193. Convert.ToInt32(userId),
  194. contact.ContactId,
  195. contact.GoodsId, contact.GoodsCode
  196. , contact.GoodsName, StoreGapNum
  197. , contact.GoodsSpec, contact.Manufacturer
  198. , contact.BusinessId, contact.BusinessCode, contact.BusinessName
  199. , contact.SuppliersId.Trim(), contact.BrandName, contact.BrandId
  200. , contact.EntId, contact.Remark, LoginAccountId,
  201. contact.LastPrice,
  202. contact.PurPrice,
  203. StoreGapNum
  204. , contact.SaleManId);
  205. result.Add(success);
  206. //推送供应商联系人状态设置为Y
  207. Dictionary<string, Object> successPush = ContactDAL.UpdatePushContactState(contact.GoodsId,contact.ContactId, contact.SuppliersId);
  208. result.Add(successPush);
  209. }
  210. //推送
  211. PushInfo pushInfos = null;
  212. groupGoodsNames.TryGetValue(userId, out pushInfos);
  213. DateTime EndTime = DateTime.Now.AddHours((double)Config.HoursToCancel);
  214. Message msg = TimeJob.TimeTask.sendSupplyMassMesage(_client,
  215. Convert.ToInt32(userId),
  216. RecordId,
  217. pushInfos.GoodsName,
  218. pushInfos.GoodsNums,
  219. pushInfos.TotalAmount,
  220. EndTime);
  221. Dictionary<string, Object> msgd = new Dictionary<string, object>(1);
  222. msgd.Add("msg", msg);
  223. result.Add(msgd);
  224. }
  225. }
  226. codeids.Add("PushCode", PushCode);
  227. codeids.Add("Ids", Ids);
  228. result.Add(codeids);
  229. return result;
  230. }
  231. public static bool Push(string LoginAccountId, string RoleId,string FilterLimit,string FilterCont,out string resultstr, IApiClient _client=null)
  232. {
  233. string msg;
  234. //获取推送上下限
  235. IList<PushStorLimit> pushLimits = PushRecordDAL.GetPushLimit(FilterLimit);
  236. if (pushLimits.Count == 0)
  237. {
  238. resultstr = "没有选择库存低于下限品种";
  239. return false;
  240. }
  241. string StaffDocId = StaffDocDAL.GetStaffId(LoginAccountId);
  242. //获取待推送账户:上次供货业务员,取最低价格的业务员
  243. string filterstr = FilterCont + " and UserId IS NOT NULL and UserId !='' "
  244. // +" and FocusMicNo IS NOT NULL and FocusMicNo !='' "
  245. + FilterRuleByPur.getRolePermFilter(RoleId, " and SaleManId = '" + StaffDocId + "' ") //只查询当前采购员的联系人
  246. ;
  247. Dictionary<string, PushStorLimit> gapnum;
  248. List<PushContactWithGoods> GetPushContacts = ContactBAL.GetPushContactWithGoods(filterstr, "", pushLimits, out gapnum,out _);
  249. //判断参数是否合法
  250. if (GetPushContacts.Count == 0)
  251. {
  252. resultstr = "没有建立账户,或者业务员没有绑定";
  253. return false;
  254. }
  255. //添加推送记录并推送
  256. var result = PushBAL.pushAll(GetPushContacts, gapnum, LoginAccountId, _client);
  257. var codeids = result[result.Count - 1];
  258. Object Ids;
  259. codeids.TryGetValue("Ids", out Ids);
  260. List<String> ids = (List<String>)Ids;
  261. foreach (String recordId in ids)
  262. {
  263. CancelPushRecordTimedJob.CancelPushRecord(recordId, DateTime.Now, CancelPushRecordTimedJob.HoursToCancel);
  264. }
  265. resultstr = result.ToString();
  266. return true;
  267. }
  268. public static bool PushOrderConfirm(string LoginAccountId, string RoleId, string Filter, out string resultstr, IApiClient _client = null)
  269. {
  270. var sql = "select A.* from PurOrderMT A "+
  271. "left join sup_PushOrderMT B on B.BillNo = A.BillNo " +
  272. "where A.IsEnd = 'N' and A.IsDone = 'N' and A.BillState = 0 and A.source != 'coop.360lj.com' " +
  273. " and B.Id is null ";
  274. string result = "";
  275. DataTable dt = new DataTable();
  276. List<SqlParameter> param = new List<SqlParameter>(0);
  277. DataAccess.GetValues(sql,ref dt, param.ToArray(), out result);
  278. IList<PurOrderMTEx> psls = new List<PurOrderMTEx>();
  279. Dictionary<string, List<PurOrderMTEx>> group = new Dictionary<string, List<PurOrderMTEx>>();
  280. List<DataAccessCommand> list = new List<DataAccessCommand>(5);
  281. if (dt != null && dt.Rows.Count > 0)
  282. {
  283. // 把DataTable转换为IList<UserInfo>
  284. psls = ModelConvertHelper<PurOrderMTEx>.ConvertToModel(dt);
  285. foreach (PurOrderMTEx purOrder in psls)
  286. {
  287. ContactDoc cont = null;
  288. if(!string.IsNullOrWhiteSpace(purOrder.K_ContactId))
  289. cont = ContactDAL.getContactor(purOrder.K_ContactId.Trim(' '));
  290. if(cont!=null && !string.IsNullOrWhiteSpace(cont.UserId) && !string.IsNullOrWhiteSpace(cont.FocusMicNo))
  291. {
  292. var sql1 = "select A.*,B.AccountRealName as Contact,C.GoodsName,C.GoodsSpec,C.GoodsCode,C.Manufacturer" +
  293. ",D.ApprovalNo,E.BusinessName from PurOrderDT A " +
  294. " left join PurOrderMT A1 on A1.BillNo = A.BillNo " +
  295. " left join sup_Account B on B.PurStaffId = A1.SaleManId " +
  296. " left join GoodsDoc C on C.EntId = A.EntId and C.GoodsId = A.GoodsId " +
  297. " left join GoodsAttr D on D.GoodsId = C.GoodsId and D.EntId = C.EntId " +
  298. " left join BusinessDoc E on E.BusinessId = A1.SuppliersId and E.EntId = A1.EntId " +
  299. " where A.BillNo = @BillNo ";
  300. List<SqlParameter> param1 = new List<SqlParameter>(1);
  301. param1.Add(new SqlParameter("BillNo", purOrder.BillNo));
  302. purOrder.Orderdts = DataAccess.GetValues<PurOrderDTEx>(sql1, param1,out var msg);
  303. //添加推送记录
  304. PushOrderDAL.AddPushOrderMT(cont.UserId, purOrder, ref list,out var Id);
  305. //添加推送记录明细
  306. foreach(PurOrderDTEx orderDt in purOrder.Orderdts) {
  307. PushOrderDAL.AddPushOrderDT(cont.UserId, Id, purOrder, orderDt, ref list);
  308. }
  309. //加入推送分组
  310. if (!group.ContainsKey(cont.UserId)) {
  311. List < PurOrderMTEx > orlist = new List<PurOrderMTEx>(3);
  312. orlist.Add(purOrder);
  313. group[cont.UserId] = orlist;
  314. }
  315. else
  316. {
  317. List<PurOrderMTEx> orlist = group[cont.UserId];
  318. orlist.Add(purOrder);
  319. group.Remove(cont.UserId);
  320. group[cont.UserId] = orlist;
  321. }
  322. //foreach (var orderlist in group)
  323. //{
  324. var sendmsg = ConfirmOrderTask.sendSupplyMassMesage(_client, cont.UserId, purOrder.BillNo.ToString(),
  325. purOrder.Orderdts[0].GoodsName+"...", purOrder.Orderdts[0].Num.ToString() + "...",
  326. purOrder.Orderdts[0].TaxAmount.ToString() + "...", new DateTime());
  327. //}
  328. }
  329. }
  330. }
  331. DataAccess.ExecuteBatchCommands(list, out var resultmsg);
  332. resultstr = resultmsg;
  333. return false;
  334. }
  335. public static string PreOPaidTypeId = "PICDM6CL55K";
  336. public static string OPaidTypeId = "PICDM6CGKQC";
  337. public static string PicPreHost = "http://119.130.113.246:8999/";
  338. public static bool PushPaidBackMsg(string LoginAccountId, string RoleId, string Filter, out string resultstr, IApiClient _client = null)
  339. {
  340. var sql = " select A.* from sup_PushPaidBackMsg A "+
  341. " left join sup_PaidBackMsg B on B.ReBillNo = A.ReBillNo " +
  342. " where 1=1 " +
  343. " and B.BillNo is null ";
  344. string result = "";
  345. DataTable dt = new DataTable();
  346. List<SqlParameter> param = new List<SqlParameter>(0);
  347. DataAccess.GetValues(sql, ref dt, param.ToArray(), out result);
  348. IList<sup_PushPaidBackMsg> psls = new List<sup_PushPaidBackMsg>();
  349. Dictionary<string, List<sup_PushPaidBackMsg>> group = new Dictionary<string, List<sup_PushPaidBackMsg>>();
  350. List<DataAccessCommand> list = new List<DataAccessCommand>(5);
  351. if (dt != null && dt.Rows.Count > 0)
  352. {
  353. // 把DataTable转换为IList<UserInfo>
  354. psls = ModelConvertHelper<sup_PushPaidBackMsg>.ConvertToModel(dt);
  355. foreach (sup_PushPaidBackMsg PaidBackMsg in psls)
  356. {
  357. ContactDoc cont = null;
  358. if (!string.IsNullOrWhiteSpace(PaidBackMsg.K_ContactId))
  359. cont = ContactDAL.getContactor(PaidBackMsg.K_ContactId.Trim(' '));
  360. String url = "";
  361. if (cont != null && !string.IsNullOrWhiteSpace(cont.UserId) && !string.IsNullOrWhiteSpace(cont.FocusMicNo))
  362. {
  363. if (PaidBackMsg.PICTYPEID !=null && PaidBackMsg.PICTYPEID.Trim().Equals(PreOPaidTypeId))
  364. {
  365. var sql1 = "select A.*,B.AccountRealName as Contact,C.GoodsName,C.GoodsSpec,C.GoodsCode,C.Manufacturer" +
  366. ",D.ApprovalNo,E.BusinessName from PurOrderDT A " +
  367. " left join PurOrderMT A1 on A1.BillNo = A.BillNo " +
  368. " left join sup_Account B on B.PurStaffId = A1.SaleManId " +
  369. " left join GoodsDoc C on C.EntId = A.EntId and C.GoodsId = A.GoodsId " +
  370. " left join GoodsAttr D on D.GoodsId = C.GoodsId and D.EntId = C.EntId " +
  371. " left join BusinessDoc E on E.BusinessId = A1.SuppliersId and E.EntId = A1.EntId " +
  372. " where A.BillNo = @BillNo ";
  373. List<SqlParameter> param1 = new List<SqlParameter>(1);
  374. param1.Add(new SqlParameter("BillNo", PaidBackMsg.ReBillNo));
  375. PaidBackMsg.Orderdts = DataAccess.GetValues<PurOrderDTEx>(sql1, param1, out var msg);
  376. url = "https://" + Config.Host + "/web/supplymobile/listorder?TypeId=" + PreOPaidTypeId + "&OrderBillNo=" + PaidBackMsg.ReBillNo + "&picUrl=" +
  377. PicPreHost + PaidBackMsg.ATTACHID;
  378. }
  379. else if (PaidBackMsg.PICTYPEID != null && PaidBackMsg.PICTYPEID.Trim().Equals(OPaidTypeId))
  380. {
  381. PaidBackMsg.Orderdts = PushPaidBackMsgDAL.GetPaidBackMsgDT(PaidBackMsg.ReBillNo);
  382. url = "https://" + Config.Host + "/web/supplymobile/listorder?TypeId="+ OPaidTypeId + "&OrderBillNo=" + PaidBackMsg.ReBillNo+"&picUrl="+
  383. PicPreHost + PaidBackMsg.ATTACHID;
  384. }
  385. //添加推送记录
  386. PushOrderDAL.AddPaidBackMsg(cont.UserId, PaidBackMsg, ref list, out var Id);
  387. //加入推送分组
  388. if (!group.ContainsKey(cont.UserId))
  389. {
  390. List<sup_PushPaidBackMsg> orlist = new List<sup_PushPaidBackMsg>(1);
  391. orlist.Add(PaidBackMsg);
  392. group[cont.UserId] = orlist;
  393. }
  394. else
  395. {
  396. List<sup_PushPaidBackMsg> orlist = group[cont.UserId];
  397. orlist.Add(PaidBackMsg);
  398. group.Remove(cont.UserId);
  399. group[cont.UserId] = orlist;
  400. }
  401. //foreach (var orderlist in group)
  402. //{
  403. var sendmsg = PaidBackMsgTask.sendSupplyMassMesage(_client, cont.UserId, PaidBackMsg.ReBillNo.ToString(),
  404. PaidBackMsg.Orderdts[0].GoodsName + "...", PaidBackMsg.Orderdts[0].Num.ToString() + "...",
  405. PaidBackMsg.Orderdts[0].TaxAmount.ToString() + "...",url, new DateTime());
  406. //}
  407. }
  408. }
  409. }
  410. DataAccess.ExecuteBatchCommands(list, out var resultmsg);
  411. resultstr = resultmsg;
  412. return false;
  413. }
  414. }
  415. }