示例#1
0
        public bool SaveLog(List <CrmData> crmdata, CrmApiData modelConfig)
        {
            TransactionModel             tran = new TransactionModel();
            AgentDistributionRelationBLL agentDistributionRelationBLL = new AgentDistributionRelationBLL();

            if (modelConfig == null)
            {
                return(false);
            }

            if (crmdata == null || crmdata.Count <= 0)
            {
                UpdateCrmApiData(modelConfig);
                return(false);
            }

            string         phones      = "'" + string.Join("','", crmdata.Select(s => s.phone).Distinct()) + "'";
            List <Account> accountList = AccountBLL.SingleModel.GetListByPhones(phones);

            if (accountList == null || accountList.Count <= 0)
            {
                UpdateCrmApiData(modelConfig);
                return(false);
            }

            string accountids = "'" + string.Join("','", accountList.Select(s => s.Id).Distinct()) + "'";
            List <AgentDistributionRelation> agentdistributionlist = agentDistributionRelationBLL.GetListByAgent(accountids);

            if (agentdistributionlist == null || agentdistributionlist.Count <= 0)
            {
                UpdateCrmApiData(modelConfig);
                return(false);
            }

            foreach (CrmData item in crmdata)
            {
                Account accountitem = accountList.FirstOrDefault(f => f.ConsigneePhone == item.phone);
                if (accountitem == null)
                {
                    continue;
                }
                AgentDistributionRelation adbitem = agentdistributionlist.FirstOrDefault(f => f.UserAccountId == accountitem.Id.ToString());
                if (adbitem == null)
                {
                    continue;
                }

                AgentFollowLog followlog = new AgentFollowLog();
                followlog.AddTime    = item.AddTime;
                followlog.UpdateTime = item.AddTime;
                followlog.State      = 1;
                followlog.Type       = 1;
                followlog.SouceFrom  = 1;
                followlog.AgentDistributionRelatioinId = adbitem.Id;
                followlog.Desc = "业务员:" + item.name + ",跟进状态:" + item.statecontent + ",备注:" + item.content;
                tran.Add($"insert into AgentFollowLog(agentdistributionrelatioinid,`desc`,state,addtime,updatetime,type,writer,soucefrom) values ({followlog.AgentDistributionRelatioinId},'{followlog.Desc}',{followlog.State},'{followlog.AddTime.ToString("yyyy-MM-dd HH:mm:ss")}','{followlog.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss")}',{followlog.Type},'',{followlog.SouceFrom})");
            }

            if (tran.sqlArray.Length > 0)
            {
                bool isok = base.ExecuteTransaction(tran.sqlArray);
                if (isok)
                {
                    UpdateCrmApiData(modelConfig);
                }
                return(isok);
            }
            //else
            //{
            //    if (DateTime.Now.CompareTo(DateTime.Parse(modelConfig.CurrentTime).AddDays(1)) > 0)
            //    {
            //        string columns = "pageindex,TotalPageSize,CurrentTime";
            //        modelConfig.PageIndex = 1;
            //        modelConfig.TotalPageSize = 0;
            //        modelConfig.CurrentTime = DateTime.Parse(modelConfig.CurrentTime).AddDays(1).ToShortDateString();

            //        base.Update(modelConfig, columns);
            //    }
            //}

            return(false);
        }
        public List <AgentDistributionRelation> GetAgentDistributionRelationList(string starttime, string endtime, string loginid, int agentqrcodeid, int agentid, int pageIndex, int pageSize, ref int count, bool reflesh = false)
        {
            string accountids = "";

            if (!string.IsNullOrEmpty(loginid))
            {
                Account accountlist = AccountBLL.SingleModel.GetModelByLoginid(loginid);
                if (accountlist == null)
                {
                    return(new List <AgentDistributionRelation>());
                }
                accountids = "'" + accountlist.Id + "'";
                reflesh    = true;
            }
            if (!string.IsNullOrEmpty(starttime) || !string.IsNullOrEmpty(endtime))
            {
                reflesh = true;
            }

            RedisModel <AgentDistributionRelation> model = new RedisModel <AgentDistributionRelation>();

            model = RedisUtil.Get <RedisModel <AgentDistributionRelation> >(string.Format(_redis_AgentDistributionRelationKey, agentid, agentqrcodeid, pageSize, pageIndex));
            int dataversion = RedisUtil.GetVersion(string.Format(_redis_AgentDistributionRelationVersion, agentid));

            if (reflesh || model == null || model.DataList == null || model.DataList.Count <= 0 || model.DataVersion != dataversion)
            {
                model = new RedisModel <AgentDistributionRelation>();

                List <AgentDistributionRelation> list = new List <AgentDistributionRelation>();

                string sqlcount = $" select Count(*) from AgentDistributionRelation ad left join agentinfo a on ad.agentid = a.id ";
                string sql      = $" select ad.*,a.useraccountid,a.LastDeposit,a.name agentname from AgentDistributionRelation ad left join agentinfo a on ad.agentid = a.id ";
                string sqlwhere = $" where ad.state>=0 and ad.ParentAgentId = {agentid} and qrcodeid={agentqrcodeid}";
                if (accountids.Length > 0)
                {
                    sqlwhere += $" and useraccountid in ({accountids}) ";
                }
                if (!string.IsNullOrEmpty(starttime))
                {
                    sqlwhere += $" and ad.addtime>='{starttime}' ";
                }
                if (!string.IsNullOrEmpty(endtime))
                {
                    sqlwhere += $" and ad.addtime<='{endtime} 23:59' ";
                }

                string sqllimit = $" ORDER BY ad.addtime desc LIMIT {(pageIndex - 1) * pageSize},{pageSize} ";

                using (MySqlDataReader dr = SqlMySql.ExecuteDataReaderMaster(connName, CommandType.Text, sql + sqlwhere + sqllimit, null))
                {
                    while (dr.Read())
                    {
                        AgentDistributionRelation amodel = base.GetModel(dr);
                        amodel.UserAccountId = dr["useraccountid"].ToString();
                        amodel.UserName      = dr["agentname"].ToString();
                        if (dr["LastDeposit"] != DBNull.Value)
                        {
                            amodel.LastDeposit = Convert.ToInt32(dr["LastDeposit"]);
                        }
                        AgentFollowLog agentlogmodel = AgentFollowLogBLL.SingleModel.GetLastModel(amodel.Id);
                        if (agentlogmodel != null)
                        {
                            amodel.Desc = agentlogmodel.Desc;
                        }
                        list.Add(amodel);
                    }
                }

                if (list == null || list.Count <= 0)
                {
                    return(new List <AgentDistributionRelation>());
                }

                //查找来源
                string qrcodeid = string.Join(",", list.Select(s => s.QrCodeId).Distinct());
                List <AgentCustomerRelation> customerRelationlist = AgentCustomerRelationBLL.SingleModel.GetListByQrCodeId(qrcodeid);
                string cruserids = "'" + string.Join("','", customerRelationlist.Select(s => s.useraccountid)) + "'";
                string userids   = "'" + string.Join("','", list.Select(s => s.UserAccountId)) + "'";
                if (cruserids.Length > 0)
                {
                    userids = userids + "," + cruserids;
                }
                List <Account> accountlist = AccountBLL.SingleModel.GetListByAccountids(userids);
                foreach (AgentDistributionRelation item in list)
                {
                    Account aitem = accountlist?.Where(w => w.Id.ToString() == item.UserAccountId).FirstOrDefault();
                    item.LoginId = aitem?.LoginId;
                    item.Phone   = aitem?.ConsigneePhone;
                    AgentCustomerRelation crmodel = customerRelationlist?.Where(w => w.QrcodeId == item.QrCodeId).FirstOrDefault();
                    Account critem = accountlist?.Where(w => w.Id.ToString() == crmodel?.useraccountid).FirstOrDefault();
                    item.SourceFrom = crmodel?.username + "/" + (string.IsNullOrEmpty(critem?.ConsigneePhone) ? "未绑定手机号" : critem?.ConsigneePhone);
                }

                count             = base.GetCountBySql(sqlcount + sqlwhere);
                model.DataList    = list;
                model.DataVersion = dataversion;
                model.Count       = count;
                if (!reflesh)
                {
                    RedisUtil.Set <RedisModel <AgentDistributionRelation> >(string.Format(_redis_AgentDistributionRelationKey, agentid, agentqrcodeid, pageSize, pageIndex), model);
                }
            }
            else
            {
                count = model.Count;
            }

            return(model.DataList);
        }