遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

一个典型的记录同步任务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EC.Model;

namespace EC_DataSync
{
    class ECSyncProducePlan
    {
        public void Do()
        {
            var logId = 0L;
            try
            {
                #region

                WriteInfo("启动数据同步," + DateTime.Now);

                using (var ctx = DBCtx.GetCtx())
                {



                    WriteInfo("加载同步信息");
                    var it = ctx.EC_PlanSyncLog.Where(ent => ent.Status == "完成").OrderByDescending(ent => ent.RecId).Take(1).FirstOrDefault();
                    if (it == null) throw new Exception("未发现有效的前导记录!");

                    var maxPlsh = it.Plsh;
                    #region 写同步日志
                    var syncLog = new EC_PlanSyncLog();
                    syncLog.BTime = DateTime.Now;
                    syncLog.Status = "执行中...";
                    syncLog.AddTime = DateTime.Now;
                    syncLog.Plsh = maxPlsh;
                    ctx.EC_PlanSyncLog.AddObject(syncLog);
                    ctx.SaveChanges();
                    logId = syncLog.RecId;
                    #endregion

                    WriteInfo(string.Format("检索Plsh>{0}的记录",it.Plsh));
                    var list = DownloadList(it);
                    WriteInfo("发现" + list.Count + "条记录");
                   
                    if (list.Count > 0)
                    {
                        maxPlsh= list.Max(ent => ent.Plsh);
                    }
                    int saveCount = 0;
                    WriteInfo("保存数据...");
                    foreach (var item in list)
                    {
                        if (ctx.EC_ProducePlan.Any(ent => ent.Plsh == item.Plsh)) continue;
                        var tInfo = new EC_ProducePlan();
                        AssignPlan(tInfo, item);
                        ctx.EC_ProducePlan.AddObject(tInfo);
                        saveCount++;
                        if ((saveCount % 100) == 0) ctx.SaveChanges();
                    }
                    ctx.SaveChanges();
                    WriteInfo("更新同步日志...");

                    syncLog.Status = "完成";
                    syncLog.ETime = DateTime.Now;
                    syncLog.Plsh = maxPlsh.Trim();
                    ctx.SaveChanges();


                }
                WriteInfo("完成," + DateTime.Now);
                #endregion
            }
            catch (Exception ex)
            {

                WriteInfo("错误:" + ex.Message);
                WriteErr(ex, logId);
 
            }
        }

        private void AssignPlan(EC_ProducePlan plan, p_productplan item)
        {
            plan.PlanID = item.PlanID;
            plan.P8code = item.P8code;
            plan.Pname = item.Pname;
            plan.PlanType = item.PlanType;

            plan.Ppname = item.Ppname;
            plan.Psetting = item.Psetting;
            plan.batno = item.batno;
            plan.P_yw = item.P_yw;
            plan.Poutput = item.Poutput;
            plan.Pdate = item.Pdate;

            //设置默认列

            plan.Pweek1 = item.Pweek1;
            plan.Pplanner = item.Pplanner;
            plan.Pmanger = item.Pmanger;
            plan.Pcheckreust = item.Pcheckreust;
            plan.Pcheckdate = item.Pcheckdate;
            plan.Ptype = item.Ptype;
            plan.Pday1 = item.Pday1;
            plan.Plsh = item.Plsh;
        }

        private List<p_productplan> DownloadList(EC_PlanSyncLog it)
        {
            using (var ctx = DBCtx.GetMFGCtx())
            {
                var list = ctx.p_productplan.Where(ent => string.Compare(ent.Plsh, it.Plsh, true) > 0).OrderBy(ent => ent.Plsh).Take(200).ToList();
                return list;
            }
        }

        private void WriteErr(Exception ex, long logId)
        {
            try
            {
                using (var ctx = DBCtx.GetCtx())
                {
                    var it = ctx.EC_PlanSyncLog.FirstOrDefault(ent => ent.RecId == logId);
                    if (it == null) return;
                    it.Status = "完成_错误";
                    it.Msg = ex.Message;
                    ctx.SaveChanges();

                }
            }
            catch (Exception)
            {

            }
        }


        private void WriteInfo(string msg)
        {
            Console.WriteLine(msg);
        }
    }
}
View Code

 

posted on 2018-06-12 07:33  遗忘海岸  阅读(284)  评论(0编辑  收藏  举报