123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using MySystem.SpModels;
- using Library;
- using LitJson;
- using System.Threading;
- namespace MySystem
- {
- public class SycnSpMerchantService
- {
- public readonly static SycnSpMerchantService Instance = new SycnSpMerchantService();
- private SycnSpMerchantService()
- { }
- public void Start()
- {
- Thread th = new Thread(StartDo);
- th.IsBackground = true;
- th.Start();
- }
- public void StartDo()
- {
- while (true)
- {
- try
- {
- WebCMSEntities spdb = new WebCMSEntities();
- PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
- DateTime start = DateTime.Now.AddDays(-5);
- var Mers = spdb.Merchants.Where(m => m.CreateTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
- foreach (var Mer in Mers)
- {
- var tran = db.Database.BeginTransaction();
- try
- {
- PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo) ?? new PxcModels.MachineForMerNo();
- PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId) ?? new PxcModels.PosMachinesTwo();
- if (pos.BindingState == 1)
- {
- PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users();
- int TopUserId = 0;
- if (!string.IsNullOrEmpty(user.ParentNav))
- {
- TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
- }
- int BrandId = int.Parse(Mer.ProductType);
- if (BrandId == 1 && Mer.MerNo.StartsWith("M900"))
- {
- BrandId = 3;
- }
- if (BrandId == 4 && Mer.Field1 == "200")
- {
- BrandId = 5;
- }
- PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mer.MerNo);
- if (add == null)
- {
- add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
- {
- CreateDate = Mer.CreateTime,
- KqMerNo = Mer.MerNo,
- MerchantNo = Mer.MerNo,
- }).Entity;
- db.SaveChanges();
- }
- add.UpdateDate = Mer.UpdateTime;
- add.TopUserId = TopUserId;
- add.BrandId = BrandId;
- add.SnStoreId = pos.StoreId;
- add.SnType = pos.PosSnType;
- add.UserId = pos.UserId;
- add.MgrName = Mer.AgentName;
- add.MerStatus = 1;
- add.KqSnNo = Mer.SnNo;
- add.MerIdcardNo = Mer.MerIdcardNo;
- add.MerRealName = Mer.MerRealName;
- add.MerchantMobile = Mer.MerMobile;
- add.MerchantName = Mer.MerName;
- pos.BindMerchantId = add.Id;
- Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
- if (edit != null)
- {
- edit.Status = 2;
- spdb.SaveChanges();
- }
- tran.Commit();
- }
- }
- catch (Exception ex)
- {
- tran.Rollback();
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
- }
- tran.Dispose();
- }
- spdb.SaveChanges();
- spdb.Dispose();
- db.SaveChanges();
- db.Dispose();
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
- }
- Thread.Sleep(1000);
- }
- }
- }
- }
|