using Library;
using MySystem.PxcModels;
using System;
using System.Linq;

namespace MySystem
{
    public class TeamOfferApplyHelper
    {
        public readonly static TeamOfferApplyHelper Instance = new TeamOfferApplyHelper();
        private TeamOfferApplyHelper()
        { }

        public string Start()
        {
            bool op = true;
            WebCMSEntities db = new WebCMSEntities();
            string result = "";
            int total = 0;
            while (op)
            {
                TeamOfferApply PopData = new TeamOfferApply();
                try
                {
                    //获取apserver待同步的数据，执行入库
                    PopData = RedisDbconn.Instance.RPop<TeamOfferApply>("Pop:TeamOfferApply");
                    if (PopData != null)
                    {
                        TeamOfferApply checkExist = db.TeamOfferApply.FirstOrDefault(m => m.Id == PopData.Id);
                        if (checkExist != null)
                        {
                            checkExist = PopData;
                        }
                        else
                        {
                            db.TeamOfferApply.Add(PopData);
                        }
                        if (total >= 20)
                        {
                            total = 0;
                            db.SaveChanges();
                        }
                        if (string.IsNullOrEmpty(result)) result = "success";
                    }
                    else
                    {
                        db.SaveChanges();
                        op = false;
                    }
                }
                catch (Exception ex)
                {
                    ErrorMsg msg = new ErrorMsg();
                    msg.Obj = PopData;
                    msg.Time = DateTime.Now;
                    msg.ErrorContent = ex.ToString();
                    Utils.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "Pop:TeamOfferApply:Error");
                    result = "有异常，请查看Pop:TeamOfferApply:Error队列";
                }
            }
            db.Dispose();
            return result;
        }
    }
}