public dm_user_relationEntity GetEntityByUserID(int?id)
 {
     try
     {
         string cacheKey = "UserRelation_" + id;
         dm_user_relationEntity dm_User_RelationEntity = redisCache.Read <dm_user_relationEntity>(cacheKey, 7L);
         if (dm_User_RelationEntity.IsEmpty())
         {
             dm_User_RelationEntity = BaseRepository("dm_data").FindEntity((dm_user_relationEntity t) => t.user_id == id);
             if (!dm_User_RelationEntity.IsEmpty())
             {
                 redisCache.Write(cacheKey, dm_User_RelationEntity, DateTime.Now.AddSeconds(30), 7L);
             }
         }
         return(dm_User_RelationEntity);
     }
     catch (Exception ex)
     {
         if (ex is ExceptionEx)
         {
             throw;
         }
         throw ExceptionEx.ThrowServiceException(ex);
     }
 }
예제 #2
0
        public decimal GetSort(dm_user_relationEntity dm_User_RelationEntity, decimal totalCommission)
        {
            int days = Time.DiffDays(DateTime.Parse("2020-01-01 00:00:00"), DateTime.Now);

            if (dm_User_RelationEntity == null)
            {
                return(Math.Round(days * 0.4M + totalCommission * 0.4M, 2));
            }
            else
            {
                return(Math.Round(days * 0.4M + totalCommission * 0.4M + dm_User_RelationEntity.taskcount * 0.2M - dm_User_RelationEntity.taskreportcount, 2));
            }
        }
예제 #3
0
 public void SaveEntity(int?keyValue, dm_user_relationEntity entity)
 {
     try
     {
         dM_UserRelationService.SaveEntity(keyValue, entity);
     }
     catch (Exception ex)
     {
         if (ex is ExceptionEx)
         {
             throw;
         }
         throw ExceptionEx.ThrowBusinessException(ex);
     }
 }
예제 #4
0
 /// <summary>
 /// 通过邀请码获取实体信息
 /// </summary>
 /// <param name="InviteCode"></param>
 /// <returns></returns>
 public dm_userEntity GetEntityByInviteCode(string InviteCode, ref dm_user_relationEntity dm_User_RelationEntity)
 {
     try
     {
         return(dM_UserService.GetEntityByInviteCode(InviteCode, ref dm_User_RelationEntity));
     }
     catch (Exception ex)
     {
         if (ex is ExceptionEx)
         {
             throw;
         }
         throw ExceptionEx.ThrowBusinessException(ex);
     }
 }
 public void SaveEntity(int?keyValue, dm_user_relationEntity entity)
 {
     try
     {
         if (keyValue > 0)
         {
             entity.Modify(keyValue);
             BaseRepository("dm_data").Update(entity);
         }
         else
         {
             entity.Create();
             BaseRepository("dm_data").Insert(entity);
         }
     }
     catch (Exception ex)
     {
         if (ex is ExceptionEx)
         {
             throw;
         }
         throw ExceptionEx.ThrowServiceException(ex);
     }
 }
예제 #6
0
        public void ReceiveAwards(int user_id, int?task_id)
        {
            IRepository db = null;

            try
            {
                #region 获取任务记录
                dm_task_person_settingEntity dm_Task_Person_SettingEntity = GetEntity(task_id);
                #endregion
                if (dm_Task_Person_SettingEntity.s_type == 1)
                {
                    dm_UserService.SignIn(user_id);
                }
                else
                {
                    dm_userEntity dm_UserEntity = dm_UserService.GetEntityByCache(user_id);

                    if (dm_Task_Person_SettingEntity.s_type == 4)
                    {
                        dm_user_relationEntity dm_User_RelationEntity = dm_UserRelationService.GetEntityByUserID(user_id);
                        if (dm_User_RelationEntity.ordercount < dm_Task_Person_SettingEntity.needcount)
                        {
                            throw new Exception("请完成购物任务后再来领取!");
                        }
                    }
                    else if (dm_Task_Person_SettingEntity.s_type == 2)
                    {
                        if (dm_UserEntity.mychildcount < dm_Task_Person_SettingEntity.needcount)
                        {
                            throw new Exception("请完成邀请任务后再来领取!");
                        }
                    }

                    dm_task_person_recordEntity dm_Task_Person_RecordEntity = dm_Task_Person_RecordService.GetMyPersonRecord(user_id, task_id);
                    if (dm_Task_Person_RecordEntity.IsEmpty())
                    {
                        #region 增加任务领取记录
                        dm_Task_Person_RecordEntity            = new dm_task_person_recordEntity();
                        dm_Task_Person_RecordEntity.user_id    = user_id;
                        dm_Task_Person_RecordEntity.task_id    = task_id;
                        dm_Task_Person_RecordEntity.createtime = DateTime.Now;
                        dm_Task_Person_RecordEntity.status     = 1;
                        #endregion


                        db = BaseRepository("dm_data").BeginTrans();
                        //积分任务
                        if (dm_Task_Person_SettingEntity.rewardtype == 0)
                        {
                            #region 增加积分变更明细
                            int stepvalue = int.Parse(dm_Task_Person_SettingEntity.rewardcount.ToString());//积分无小数
                            dm_intergraldetailEntity dm_IntergraldetailEntity = new dm_intergraldetailEntity();
                            dm_IntergraldetailEntity.type         = 4;
                            dm_IntergraldetailEntity.profitLoss   = 1;
                            dm_IntergraldetailEntity.user_id      = user_id;
                            dm_IntergraldetailEntity.createtime   = DateTime.Now;
                            dm_IntergraldetailEntity.currentvalue = dm_UserEntity.integral + stepvalue;
                            dm_IntergraldetailEntity.stepvalue    = stepvalue;
                            dm_IntergraldetailEntity.title        = dm_Task_Person_SettingEntity.title;
                            dm_IntergraldetailEntity.remark       = dm_Task_Person_SettingEntity.remark;
                            dm_IntergraldetailEntity.Create();

                            dm_UserEntity.integral = dm_IntergraldetailEntity.currentvalue;
                            #endregion
                            db.Insert(dm_UserEntity);
                        }
                        else
                        { //余额任务
                            #region 增加余额变更明细
                            dm_accountdetailEntity dm_AccountdetailEntity = new dm_accountdetailEntity();
                            dm_AccountdetailEntity.user_id      = user_id;
                            dm_AccountdetailEntity.type         = 10;
                            dm_AccountdetailEntity.profitLoss   = CommonHelper.GetProfitLoss(10);
                            dm_AccountdetailEntity.createtime   = DateTime.Now;
                            dm_AccountdetailEntity.currentvalue = dm_UserEntity.accountprice + dm_Task_Person_SettingEntity.rewardcount;
                            dm_AccountdetailEntity.stepvalue    = dm_Task_Person_SettingEntity.rewardcount;
                            dm_AccountdetailEntity.title        = dm_Task_Person_SettingEntity.title;
                            dm_AccountdetailEntity.remark       = dm_Task_Person_SettingEntity.remark;
                            dm_AccountdetailEntity.Create();
                            #endregion

                            dm_UserEntity.accountprice = dm_AccountdetailEntity.currentvalue;
                            db.Insert(dm_AccountdetailEntity);
                        }

                        dm_UserEntity.Modify(user_id);
                        db.Update(dm_UserEntity);
                        db.Insert(dm_Task_Person_RecordEntity);

                        db.Commit();
                    }
                    else
                    {
                        throw new Exception("该任务已领取!");
                    }
                }
            }
            catch (Exception ex)
            {
                if (db != null)
                {
                    db.Rollback();
                }

                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
예제 #7
0
        public IEnumerable <dm_task_person_settingEntity> GetPersonProcess(int user_id, string appid)
        {
            try
            {
                IEnumerable <dm_task_person_settingEntity> person_SettingEntities = GetListByCache(appid, 0);
                foreach (var item in person_SettingEntities)
                {
                    //每日签到任务
                    if (item.s_type == 1)
                    {
                        //获取今日是否签到
                        dm_intergraldetailEntity dm_IntergraldetailEntity = dm_IntergralDetailService.GetLastSignData(user_id);
                        if (dm_IntergraldetailEntity.IsEmpty())
                        {
                            item.finishcount  = 0;
                            item.finishstatus = 0;
                            item.btntext      = "签到";
                        }
                        else
                        {
                            item.finishcount  = dm_IntergraldetailEntity.createtime.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd") ? 1 : 0;
                            item.finishstatus = item.finishcount == 1 ? 2 : 0;
                            item.btntext      = item.finishstatus == 0 ? "签到" : "已完成";
                        }
                    }
                    else if (item.s_type == 2)
                    {//邀请粉丝任务
                        dm_userEntity dm_UserEntity = dm_UserService.GetEntityByCache(user_id);
                        item.finishcount  = (int)dm_UserEntity.mychildcount;
                        item.finishstatus = GetFinishStatus(item.finishcount, item.needcount, user_id, item.id);
                        item.remark      += string.Format("({0}/{1})", item.finishcount, item.needcount);

                        item.btntext = getBtnText(item.finishstatus, item.s_type);
                    }
                    else if (item.s_type == 4)
                    {//购物任务
                        dm_user_relationEntity dm_User_RelationEntity = dm_UserRelationService.GetEntityByUserID(user_id);
                        item.finishcount  = dm_User_RelationEntity.ordercount;
                        item.finishstatus = GetFinishStatus(item.finishcount, item.needcount, user_id, item.id);

                        item.remark += string.Format("({0}/{1})", item.finishcount, item.needcount);

                        item.btntext = getBtnText(item.finishstatus, item.s_type);
                    }
                    else if (item.s_type == 6)
                    {
                        item.finishstatus = GetFinishStatus(item.finishcount, item.needcount, user_id, item.id);
                        item.finishcount  = 0;//浏览任务此字段无意义

                        item.btntext = item.finishstatus == 2 ? "已完成" : "去浏览";
                    }
                }
                return(person_SettingEntities);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
예제 #8
0
        /// <summary>
        /// 发布任务
        /// </summary>
        /// <param name="entity"></param>
        public void ReleaseTask(dm_taskEntity entity)
        {
            IRepository db = null;

            try
            {
                if (entity.singlecommission <= 0)
                {
                    throw new Exception("任务佣金不能小于0!");
                }
                if (entity.needcount <= 0)
                {
                    throw new Exception("任务参与数不能小于0!");
                }
                dm_userEntity dm_UserEntity = dm_UserService.GetEntity(entity.user_id);

                entity.totalcommission = entity.singlecommission * entity.needcount;//需要从用户账户扣除的金额
                if (dm_UserEntity.accountprice < entity.totalcommission)
                {
                    throw new Exception("账户余额不足,请充值后重新发布!");
                }

                dm_user_relationEntity dm_User_RelationEntity = dm_UserRelationService.GetEntityByUserID(entity.user_id);
                if (dm_User_RelationEntity.IsEmpty())
                {
                    throw new Exception("用户数据异常!");
                }

                dm_basesettingEntity dm_BaseSettingEntity = dm_BaseSettingService.GetEntityByCache(entity.appid);
                entity.seniorcommission = Math.Round((entity.singlecommission * dm_BaseSettingEntity.task_do_senior) / 100, 2);  //高级代理佣金
                entity.juniorcommission = Math.Round((entity.singlecommission * dm_BaseSettingEntity.task_do_junior) / 100, 2);  //初级代理佣金
                entity.servicefee       = Math.Round((entity.singlecommission * dm_BaseSettingEntity.task_servicefee) / 100, 2); //任务服务费(奖励在服务费中分发)
                entity.task_no          = DateTime.Now.ToString("yyyyMMddHHmmssfff") + entity.user_id.ToString().PadLeft(6, '0');
                entity.sort             = GetSort(dm_User_RelationEntity, entity.totalcommission);

                if (dm_BaseSettingEntity.taskchecked == 1)
                {
                    entity.task_status = -2;
                }
                else
                {
                    entity.task_status = 0;
                }

                entity.Create();
                dm_UserEntity.accountprice -= entity.totalcommission;
                dm_UserEntity.Modify(dm_UserEntity.id);
                dm_User_RelationEntity.taskcount += 1;
                dm_User_RelationEntity.Modify(dm_User_RelationEntity.id);


                dm_accountdetailEntity dm_AccountdetailEntity = new dm_accountdetailEntity
                {
                    createtime   = DateTime.Now,
                    remark       = "发布任务扣除" + entity.task_no,
                    stepvalue    = entity.totalcommission,
                    currentvalue = dm_UserEntity.accountprice,
                    title        = "发布任务",
                    type         = 12,
                    profitLoss   = CommonHelper.GetProfitLoss(12),
                    user_id      = entity.user_id
                };

                db = BaseRepository("dm_data").BeginTrans();
                db.Insert(entity);
                db.Update(dm_UserEntity);
                db.Insert(dm_AccountdetailEntity);
                db.Update(dm_User_RelationEntity);
                db.Commit();
            }
            catch (Exception ex)
            {
                if (db != null)
                {
                    db.Rollback();
                }

                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
예제 #9
0
        /// <summary>
        /// 支付回调中开通代理(并完成返佣)
        /// </summary>
        /// <param name="dm_Alipay_RecordEntity"></param>
        public void OpenAgent(dm_alipay_recordEntity dm_Alipay_RecordEntity)
        {
            IRepository db = null;

            try
            {
                /*
                 * 根据外部交易单号获取订单记录
                 * 更改用户状态并完成返利
                 * 1、一级代理返利(代理等级为初级、高级、合伙人才可得到2021-01-06)
                 * 2、二级代理返利(代理等级为初级、高级、合伙人才可得到2021-01-06)
                 * 3、三级代理返利(代理等级为初级、高级、合伙人才可得到2021-01-06)
                 * 3、一级合伙人(不校验代理等级,可能为后台开通)
                 * 4、二级合伙人(不校验代理等级,可能为后台开通)
                 */
                dm_alipay_recordEntity dm_Alipay_RecordEntity_old = this.BaseRepository("dm_data").FindEntity <dm_alipay_recordEntity>(t => t.out_trade_no == dm_Alipay_RecordEntity.out_trade_no);

                ///如果老的记录是已支付状态则不需要再执行修改和返利
                if (!dm_Alipay_RecordEntity_old.alipay_status.IsEmpty() && dm_Alipay_RecordEntity_old.alipay_status.ToUpper() == "TRADE_SUCCESS")
                {
                    return;
                }

                if (dm_Alipay_RecordEntity_old.templateid == 99)//余额充值
                {
                    dm_userEntity dm_UserEntity = dM_UserService.GetEntity(dm_Alipay_RecordEntity_old.user_id);
                    if (dm_UserEntity.IsEmpty())
                    {
                        throw new Exception("用户信息异常!");
                    }
                    ///修改账户余额
                    dm_UserEntity.accountprice += dm_Alipay_RecordEntity_old.total_amount;//充值成功后的账户余额
                    dm_UserEntity.Modify(dm_UserEntity.id);

                    dm_accountdetailEntity dm_AccountdetailEntity = GeneralAccountDetail(dm_Alipay_RecordEntity_old.user_id, 21, "余额充值", "账户充值", (decimal)dm_Alipay_RecordEntity_old.total_amount, dm_UserEntity.accountprice);

                    dm_Alipay_RecordEntity.Modify(dm_Alipay_RecordEntity_old.id);

                    db = BaseRepository("dm_data").BeginTrans();
                    db.Update(dm_UserEntity);
                    db.Insert(dm_AccountdetailEntity);
                    db.Update(dm_Alipay_RecordEntity);//优化支付成功未更改支付状态  造成金额多次修改
                    db.Commit();

                    CacheHelper.UpdateUserInfo(dm_UserEntity);
                }
                else
                {//代理开通
                    dm_AccountdetailEntities.Clear();
                    calculateComissionEntities.Clear();

                    decimal one_agent_commission = 0, two_agent_commission = 0, three_agent_commission = 0, one_partners_commission = 0, two_partners_commission = 0;

                    dm_userEntity currentUser = null, one_User = null, two_User = null, three_User = null, one_partners = null, two_partners = null;

                    string one_tip = "", two_tip = "", three_tip = "", one_parners_tip = "", two_parners_tip = "";

                    if (dm_Alipay_RecordEntity_old.IsEmpty())
                    {
                        throw new Exception("根据外部交易单号未能查询到支付记录,当前传入外部交易单号" + dm_Alipay_RecordEntity.out_trade_no);
                    }
                    dm_Alipay_RecordEntity.Modify(dm_Alipay_RecordEntity_old.id);//更改交易信息


                    #region 获取上下级关系
                    IEnumerable <dm_user_relationEntity> userRelationList = dM_UserRelationService.GetParentRelation(dm_Alipay_RecordEntity_old.user_id);
                    #endregion

                    #region 获取上下级关系的用户信息
                    IEnumerable <dm_userEntity> userList = dM_UserService.GetParentUser(dm_Alipay_RecordEntity_old.user_id);
                    #endregion

                    #region 更改当前用户等级
                    currentUser = userList.Where(t => t.id == dm_Alipay_RecordEntity_old.user_id).FirstOrDefault();
                    if (currentUser.IsEmpty())
                    {
                        throw new Exception("用户信息异常!");
                    }
                    //初级代理
                    if (dm_Alipay_RecordEntity_old.templateid == 1)
                    {
                        currentUser.userlevel = 1;
                    }
                    //高级代理
                    else if (dm_Alipay_RecordEntity_old.templateid == 2)
                    {
                        currentUser.userlevel = 2;
                    }
                    //升级代理
                    else if (dm_Alipay_RecordEntity_old.templateid == 3)
                    {
                        currentUser.userlevel = 2;
                    }
                    calculateComissionEntities.Add(currentUser);
                    #endregion

                    #region 增加开通代理消息记录
                    dm_messagerecordEntity dm_MessagerecordEntity = new dm_messagerecordEntity();
                    dm_MessagerecordEntity.user_id        = dm_Alipay_RecordEntity_old.user_id;
                    dm_MessagerecordEntity.messagetitle   = "开通代理";
                    dm_MessagerecordEntity.messagecontent = "代理开通成功,发展下级可享受永久提成!";
                    dm_MessagerecordEntity.messagetype    = 1;
                    dm_MessagerecordEntity.createtime     = DateTime.Now;
                    dm_MessagerecordEntity.isread         = 0;
                    #endregion

                    dm_basesettingEntity dm_BasesettingEntity = dM_BaseSettingService.GetEntityByCache(currentUser.appid);

                    dm_basesetting_tipEntity dm_Basesetting_TipEntity = dm_Basesetting_TipService.GetEntityByAppID(currentUser.appid);

                    #region 更改一级账户余额及明细

                    /*2021-03-06
                     * 1、业务逻辑调整  等级分佣时如果有普通用户将不再断层,仍然会执行三级分佣
                     * 2、增加普通用户返佣比例设置,增加普通用户返佣比例说明*/
                    dm_user_relationEntity dm_User_RelationEntity_one = userRelationList.Where(t => t.user_id == dm_Alipay_RecordEntity_old.user_id).FirstOrDefault();
                    if (!dm_User_RelationEntity_one.IsEmpty())
                    {
                        one_User = userList.Where(t => t.id == dm_User_RelationEntity_one.parent_id).FirstOrDefault();
                        if (!one_User.IsEmpty())
                        {
                            if (one_User.userlevel == 0 || one_User.userlevel == 1 || one_User.userlevel == 2 || one_User.partnersstatus == 2)
                            {//高级代理或合伙人才能享受代理提成  2021-01-03 业务逻辑调整 上级只要是付费用户都可以返佣金
                                #region 开通会员一级返佣计算
                                if (one_User.userlevel != 0)
                                {//非普通用户
                                    one_agent_commission = ConvertComission(dm_BasesettingEntity.openagent_one * dm_Alipay_RecordEntity_old.total_amount);
                                }
                                else
                                {
                                    one_agent_commission = ConvertComission(dm_BasesettingEntity.opengent_general_member * dm_Alipay_RecordEntity_old.total_amount);
                                }

                                if (one_agent_commission > 0)
                                {
                                    string general_member_one_tip = "";
                                    if (one_User.userlevel == 0)
                                    {
                                        decimal real_opengent_general_member_one = ConvertComission(dm_BasesettingEntity.openagent_one * dm_Alipay_RecordEntity_old.total_amount);
                                        if (real_opengent_general_member_one > 0)
                                        {
                                            general_member_one_tip = dm_Basesetting_TipEntity.opengent_general_tip.Replace("[升级预估佣金]", real_opengent_general_member_one.ToString());
                                        }
                                    }

                                    one_tip  = GetText(dm_Basesetting_TipEntity, dm_Alipay_RecordEntity_old.templateid, 1);
                                    one_User = CalculateComission(one_User.id, one_agent_commission, one_User.accountprice);
                                    dm_AccountdetailEntities.Add(GeneralAccountDetail(one_User.id, 6, one_tip, "您的" + one_tip + "《" + currentUser.nickname + "》开通代理成功,奖励已到账,继续努力哟!" + general_member_one_tip, one_agent_commission, one_User.accountprice));
                                }
                                #endregion

                                #region 更改二级账户余额及明细
                                dm_user_relationEntity dm_User_RelationEntity_two = userRelationList.Where(t => t.user_id == one_User.id).FirstOrDefault();
                                if (!dm_User_RelationEntity_two.IsEmpty())
                                {
                                    two_User = userList.Where(t => t.id == dm_User_RelationEntity_two.parent_id).FirstOrDefault();
                                    if (!two_User.IsEmpty())
                                    {
                                        if (two_User.userlevel == 0 || two_User.userlevel == 1 || two_User.userlevel == 2 || two_User.partnersstatus == 2)
                                        {
                                            #region 开通会员二级返佣比例
                                            if (two_User.userlevel != 0)
                                            {
                                                two_agent_commission = ConvertComission(dm_BasesettingEntity.openagent_two * dm_Alipay_RecordEntity_old.total_amount);
                                            }
                                            else
                                            {
                                                two_agent_commission = ConvertComission(dm_BasesettingEntity.opengent_general_member * dm_Alipay_RecordEntity_old.total_amount);
                                            }
                                            if (two_agent_commission > 0)
                                            {
                                                string general_member_tow_tip = "";
                                                if (two_User.userlevel == 0)
                                                {
                                                    decimal real_opengent_general_member_two = ConvertComission(dm_BasesettingEntity.openagent_two * dm_Alipay_RecordEntity_old.total_amount);
                                                    if (real_opengent_general_member_two > 0)
                                                    {
                                                        general_member_tow_tip = dm_Basesetting_TipEntity.opengent_general_tip.Replace("[升级预估佣金]", real_opengent_general_member_two.ToString());
                                                    }
                                                }
                                                two_tip  = GetText(dm_Basesetting_TipEntity, dm_Alipay_RecordEntity_old.templateid, 2);
                                                two_User = CalculateComission(two_User.id, two_agent_commission, two_User.accountprice);
                                                dm_AccountdetailEntities.Add(GeneralAccountDetail(two_User.id, 7, two_tip, "您的" + two_tip + "《" + currentUser.nickname + "》开通代理成功,奖励已到账,继续努力哟!" + general_member_tow_tip, two_agent_commission, two_User.accountprice));
                                            }
                                            #endregion

                                            #region 更改三级账户余额及明细
                                            dm_user_relationEntity dm_User_RelationEntity_three = userRelationList.Where(t => t.user_id == two_User.id).FirstOrDefault();
                                            if (!dm_User_RelationEntity_three.IsEmpty())
                                            {
                                                three_User = userList.Where(t => t.id == dm_User_RelationEntity_three.parent_id).FirstOrDefault();
                                                if (!three_User.IsEmpty())
                                                {
                                                    if (three_User.userlevel == 0 || three_User.userlevel == 1 || three_User.userlevel == 2 || three_User.partnersstatus == 2)
                                                    {
                                                        #region 开通会员三级返佣比例
                                                        if (three_User.userlevel != 0)
                                                        {
                                                            three_agent_commission = ConvertComission(dm_BasesettingEntity.openagent_three * dm_Alipay_RecordEntity_old.total_amount);
                                                        }
                                                        else
                                                        {
                                                            three_agent_commission = ConvertComission(dm_BasesettingEntity.opengent_general_member * dm_Alipay_RecordEntity_old.total_amount);
                                                        }

                                                        if (three_agent_commission > 0)
                                                        {
                                                            string general_member_three_tip = "";
                                                            if (three_User.userlevel == 0)
                                                            {
                                                                decimal real_opengent_general_member_three = ConvertComission(dm_BasesettingEntity.openagent_three * dm_Alipay_RecordEntity_old.total_amount);
                                                                if (real_opengent_general_member_three > 0)
                                                                {
                                                                    general_member_three_tip = dm_Basesetting_TipEntity.opengent_general_tip.Replace("[升级预估佣金]", real_opengent_general_member_three.ToString());
                                                                }
                                                            }

                                                            three_tip  = GetText(dm_Basesetting_TipEntity, dm_Alipay_RecordEntity_old.templateid, 3);
                                                            three_User = CalculateComission(three_User.id, three_agent_commission, three_User.accountprice);
                                                            dm_AccountdetailEntities.Add(GeneralAccountDetail(three_User.id, 22, three_tip, "您的" + three_tip + "《" + currentUser.nickname + "》开通代理成功,奖励已到账,继续努力哟!" + general_member_three_tip, three_agent_commission, three_User.accountprice));
                                                        }
                                                        #endregion
                                                    }
                                                }
                                            }
                                            #endregion
                                        }
                                    }
                                }
                                #endregion
                            }
                        }
                    }

                    #endregion

                    #region 获取当前用户所属合伙人(一级合伙人)
                    one_partners = dM_UserService.GetUserByPartnersID(dm_User_RelationEntity_one.partners_id);
                    if (!one_partners.IsEmpty())
                    {
                        one_partners_commission = ConvertComission(dm_BasesettingEntity.openagent_one_partners * dm_Alipay_RecordEntity_old.total_amount);
                        if (one_partners_commission > 0)
                        {
                            one_parners_tip = GetText(dm_Basesetting_TipEntity, dm_Alipay_RecordEntity_old.templateid, 4);
                            one_partners    = CalculateComission(one_partners.id, one_partners_commission, one_partners.accountprice);
                            dm_AccountdetailEntities.Add(GeneralAccountDetail(one_partners.id, 8, one_parners_tip, "您的" + one_parners_tip + "《" + currentUser.nickname + "》开通代理成功,奖励已到账,继续努力哟!", one_partners_commission, one_partners.accountprice));
                        }

                        #region 二级合伙人
                        dm_user_relationEntity dm_User_RelationEntity_one_partners = dM_UserRelationService.GetEntityByUserID(one_partners.id);
                        two_partners = dM_UserService.GetEntityByCache(dm_User_RelationEntity_one_partners.parent_id);
                        if (!two_partners.IsEmpty())
                        {
                            if (two_partners.partnersstatus == 2)
                            {//二级用户为合伙人时才进行返利
                                two_partners_commission = ConvertComission(dm_BasesettingEntity.openagent_two_partners * dm_Alipay_RecordEntity_old.total_amount);
                                if (two_partners_commission > 0)
                                {
                                    two_parners_tip = GetText(dm_Basesetting_TipEntity, dm_Alipay_RecordEntity_old.templateid, 5);
                                    two_partners    = CalculateComission(two_partners.id, two_partners_commission, two_partners.accountprice);
                                    dm_AccountdetailEntities.Add(GeneralAccountDetail(two_partners.id, 9, two_parners_tip, "您的" + two_parners_tip + "《" + currentUser.nickname + "》开通代理成功,奖励已到账,继续努力哟!", two_partners_commission, two_partners.accountprice));
                                }
                            }
                        }
                        #endregion
                    }
                    #endregion


                    if (calculateComissionEntities.Count > 0)
                    {
                        //必须加上这个变量,用于清除当前返利账户的余额
                        foreach (var item in calculateComissionEntities)
                        {
                            item.Modify(item.id);
                        }
                        db = BaseRepository("dm_data").BeginTrans();
                        db.Update(dm_Alipay_RecordEntity);     //修改原有的支付宝支付记录
                        db.Insert(dm_MessagerecordEntity);     //增加消息记录
                        db.Insert(dm_AccountdetailEntities);   //增加账户余额明细
                        db.Update(calculateComissionEntities); //批量修改用户信息
                        db.Commit();

                        CacheHelper.UpdateUserInfo(currentUser);
                    }
                }
            }
            catch (Exception ex)
            {
                if (db != null)
                {
                    db.Rollback();
                }
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
 public dm_userEntity Register(dm_userEntity dm_UserEntity, string VerifiCode, string appid)
 {
     lock (lockObject)
     {
         IRepository db        = BaseRepository("dm_data").BeginTrans();
         int         parent_id = 0;
         int?        id        = 0;
         try
         {
             parent_id = DecodeInviteCode(VerifiCode);
             if (parent_id <= 0)
             {
                 throw new Exception("邀请码错误!");
             }
             dm_userEntity parent_user_entity = GetEntity(parent_id.ToInt());
             if (parent_user_entity == null)
             {
                 throw new Exception("您的邀请人用户异常!");
             }
             dm_user_relationEntity dm_Parent_User_RelationEntity = dm_UserRelationService.GetEntityByUserID(parent_id);
             dm_userEntity          dm_UserEntity_exist           = GetEntityByPhone(dm_UserEntity.phone, appid);
             if (dm_UserEntity_exist != null)
             {
                 throw new Exception("该手机号已注册!");
             }
             dm_basesettingEntity dm_BasesettingEntity = dm_BaseSettingService.GetEntityByCache(appid);
             dm_UserEntity.pwd   = Md5Helper.Encrypt(dm_UserEntity.pwd, 16);
             dm_UserEntity.token = Guid.NewGuid().ToString();
             BaseRepository("dm_data").Insert(dm_UserEntity);
             dm_userEntity updateEntity = new dm_userEntity();
             id = (updateEntity.id = BaseRepository("dm_data").FindObject("SELECT LAST_INSERT_ID();").ToInt());
             updateEntity.invitecode = EncodeInviteCode(updateEntity.id);
             updateEntity.integral   = dm_BasesettingEntity.new_people;
             updateEntity.Create();
             db.Update(updateEntity);
             db.Insert(new dm_intergraldetailEntity
             {
                 createtime   = DateTime.Now,
                 currentvalue = updateEntity.integral,
                 title        = "新用户注册奖励",
                 stepvalue    = dm_BasesettingEntity.new_people,
                 type         = 1,
                 user_id      = id
             });
             parent_user_entity.integral += dm_BasesettingEntity.new_people_parent;
             db.Update(parent_user_entity);
             db.Insert(new dm_intergraldetailEntity
             {
                 createtime   = DateTime.Now,
                 currentvalue = parent_user_entity.integral,
                 title        = "邀请好友奖励",
                 stepvalue    = dm_BasesettingEntity.new_people_parent,
                 type         = 3,
                 user_id      = parent_id
             });
             dm_user_relationEntity dm_User_RelationEntity = new dm_user_relationEntity
             {
                 user_id     = id,
                 parent_id   = parent_id,
                 partners_id = dm_Parent_User_RelationEntity.partners_id,
                 createtime  = DateTime.Now
             };
             db.Insert(dm_User_RelationEntity);
             db.Commit();
             return(GetEntity(id.ToInt()));
         }
         catch (Exception ex)
         {
             db.Rollback();
             Log      log = LogFactory.GetLogger("workflowapi");
             string[] obj = new string[6]
             {
                 "上下级绑定失败,当前用户",
                 null,
                 null,
                 null,
                 null,
                 null
             };
             int?num2 = id;
             obj[1] = num2.ToString();
             obj[2] = ",上级用户";
             num2   = parent_id;
             obj[3] = num2.ToString();
             obj[4] = ex.Message;
             obj[5] = ex.StackTrace;
             log.Error(string.Concat(obj));
             if (ex is ExceptionEx)
             {
                 throw;
             }
             throw ExceptionEx.ThrowServiceException(ex);
         }
     }
 }
예제 #11
0
        public dm_task_reviceEntity AuditTask(int revice_id)
        {
            lock (_object)
            {
                IRepository db = null;
                try
                {
                    /*
                     * 任务审核(任务审核成功)
                     * 1、更改任务接受表的状态
                     * 2、更改任务主表的完成数量
                     * 3、向任务接受人发送消息记录
                     * 4、执行返佣
                     */

                    decimal do_task_commission = 0, one_agent_commission = 0, two_agent_commission = 0, one_partners_commission = 0, two_partners_commission = 0;

                    dm_userEntity currentUser = null, one_User = null, two_User = null, one_partners = null, two_partners = null;
                    string        currentNickName = "";

                    dm_task_reviceEntity dm_Task_ReviceEntity = GetEntity(revice_id);
                    if (dm_Task_ReviceEntity.status == 4)
                    {
                        throw new Exception("该任务已取消,审核失败!");
                    }
                    if (dm_Task_ReviceEntity.status == 1)
                    {
                        throw new Exception("该任务未提交资料,审核失败!");
                    }
                    if (dm_Task_ReviceEntity.status == 3)
                    {
                        throw new Exception("该任务已审核通过,请勿重复审核!");
                    }
                    dm_Task_ReviceEntity.status     = 3;
                    dm_Task_ReviceEntity.check_time = DateTime.Now;

                    #region 修改任务主表数据
                    dm_taskEntity dm_TaskEntity = dm_TaskService.GetEntity(dm_Task_ReviceEntity.task_id);
                    if (dm_TaskEntity.task_status == 2)
                    {
                        throw new Exception("该任务已取消,审核失败!");
                    }
                    dm_TaskEntity.finishcount += 1;
                    if (dm_TaskEntity.finishcount >= dm_TaskEntity.needcount)
                    {
                        dm_TaskEntity.task_status = 1;
                    }
                    dm_TaskEntity.Modify(dm_TaskEntity.id);
                    #endregion

                    if (dm_TaskEntity.isactivity == 0)
                    {
                        #region 任务审核消息
                        dm_messagerecordEntity dm_MessagerecordEntity = new dm_messagerecordEntity
                        {
                            isread         = 0,
                            user_id        = dm_Task_ReviceEntity.user_id,
                            messagetitle   = "任务审核通过",
                            messagecontent = "您接受的任务编号:" + dm_TaskEntity.task_no + "已审核通过,请及时查看!",
                            messagetype    = 3,
                            createtime     = DateTime.Now
                        };
                        #endregion

                        #region 执行返佣
                        /*做任务人根据等级返佣金*/
                        /*上级从服务费中计算佣金*/
                        //获取上下级关系
                        IEnumerable <dm_user_relationEntity> userRelationList = dM_UserRelationService.GetParentRelation(dm_Task_ReviceEntity.user_id);

                        ///获取上下级关系的用户信息
                        IEnumerable <dm_userEntity> userList = dM_UserService.GetParentUser(dm_Task_ReviceEntity.user_id);

                        #region 做任务人返佣
                        currentUser     = userList.Where(t => t.id == dm_Task_ReviceEntity.user_id).FirstOrDefault();
                        currentNickName = currentUser.nickname;//记录下接单人的昵称  防止丢失
                        if (currentUser.IsEmpty())
                        {
                            throw new Exception("用户信息异常!");
                        }
                        if (currentUser.userlevel == 0)
                        {
                            throw new Exception("用户等级异常,返佣失败!");
                        }
                        else if (currentUser.userlevel == 1)
                        {
                            do_task_commission = dm_TaskEntity.juniorcommission;
                        }
                        else if (currentUser.userlevel == 2)
                        {
                            do_task_commission = dm_TaskEntity.seniorcommission;
                        }
                        else
                        {
                            throw new Exception("用户无等级,返佣失败!");
                        }

                        dm_basesettingEntity dm_BasesettingEntity = dM_BaseSettingService.GetEntityByCache(dm_TaskEntity.appid);

                        dm_basesetting_tipEntity dm_Basesetting_TipEntity = dm_Basesetting_TipService.GetEntityByAppID(dm_TaskEntity.appid);

                        if (do_task_commission > 0)
                        {
                            dm_Task_ReviceEntity.comissionamount = do_task_commission;
                            currentUser = CalculateComission(currentUser.id, do_task_commission, currentUser.accountprice);
                            dm_AccountdetailEntities.Add(GeneralAccountDetail(currentUser.id, 14, dm_Basesetting_TipEntity.task_do_tip, "您接受的任务编号" + dm_TaskEntity.task_no + "已返佣到账,请及时查收!", do_task_commission, currentUser.accountprice));
                        }
                        #endregion
                        dm_Task_ReviceEntity.Modify(dm_Task_ReviceEntity.id);//由于需要修改任务所得佣金  故修改放到此处

                        #region 更改一级账户余额及明细
                        dm_user_relationEntity dm_User_RelationEntity_one = userRelationList.Where(t => t.user_id == dm_Task_ReviceEntity.user_id).FirstOrDefault();
                        if (!dm_User_RelationEntity_one.IsEmpty())
                        {
                            one_User = userList.Where(t => t.id == dm_User_RelationEntity_one.parent_id).FirstOrDefault();
                            if (!one_User.IsEmpty())
                            {
                                if (one_User.userlevel == 1 || one_User.userlevel == 2 || one_User.partnersstatus == 2)
                                {//做任务人为代理身份才会返佣
                                    one_agent_commission = ConvertComission(dm_BasesettingEntity.task_one * dm_TaskEntity.servicefee);
                                    if (one_agent_commission > 0)
                                    {
                                        one_User = CalculateComission(one_User.id, one_agent_commission, one_User.accountprice);
                                        dm_AccountdetailEntities.Add(GeneralAccountDetail(one_User.id, 15, dm_Basesetting_TipEntity.task_one_tip, "您的" + dm_Basesetting_TipEntity.task_one_tip + "《" + currentNickName + "》任务已完成,奖励已发放到您的账户,继续努力哟!", one_agent_commission, one_User.accountprice));
                                    }

                                    #region 更改二级账户余额及明细
                                    dm_user_relationEntity dm_User_RelationEntity_two = userRelationList.Where(t => t.user_id == one_User.id).FirstOrDefault();
                                    if (!dm_User_RelationEntity_two.IsEmpty())
                                    {
                                        two_User = userList.Where(t => t.id == dm_User_RelationEntity_two.parent_id).FirstOrDefault();
                                        if (!two_User.IsEmpty())
                                        {
                                            if (two_User.userlevel == 1 || two_User.userlevel == 2 || two_User.partnersstatus == 2)
                                            {
                                                two_agent_commission = ConvertComission(dm_BasesettingEntity.task_two * dm_TaskEntity.servicefee);
                                                if (two_agent_commission > 0)
                                                {
                                                    two_User = CalculateComission(two_User.id, two_agent_commission, two_User.accountprice);
                                                    dm_AccountdetailEntities.Add(GeneralAccountDetail(two_User.id, 16, dm_Basesetting_TipEntity.task_two_tip, "您的" + dm_Basesetting_TipEntity.task_two_tip + "《" + currentNickName + "》任务已完成,奖励已发放到您的账户,继续努力哟!", two_agent_commission, two_User.accountprice));
                                                }
                                            }
                                        }
                                    }
                                    #endregion
                                }
                            }
                        }
                        #endregion

                        #region 获取当前用户所属合伙人(一级合伙人)
                        one_partners = dM_UserService.GetUserByPartnersID(dm_User_RelationEntity_one.partners_id);
                        if (!one_partners.IsEmpty())
                        {
                            one_partners_commission = ConvertComission(dm_BasesettingEntity.task_one_partners * dm_TaskEntity.servicefee);
                            if (one_partners_commission > 0)
                            {
                                one_partners = CalculateComission(one_partners.id, one_partners_commission, one_partners.accountprice);
                                dm_AccountdetailEntities.Add(GeneralAccountDetail(one_partners.id, 17, dm_Basesetting_TipEntity.task_parners_one_tip, "您的" + dm_Basesetting_TipEntity.task_parners_one_tip + "《" + currentNickName + "》任务已完成,奖励已发放到您的账户,继续努力哟!", one_partners_commission, one_partners.accountprice));
                            }

                            #region 二级合伙人
                            dm_user_relationEntity dm_User_RelationEntity_one_partners = dM_UserRelationService.GetEntityByUserID(one_partners.id);
                            two_partners = dM_UserService.GetEntityByCache(dm_User_RelationEntity_one_partners.parent_id);
                            //two_partners = dM_UserService.GetUserByPartnersID(dm_User_RelationEntity_one_partners.partners_id);
                            if (!two_partners.IsEmpty())
                            {
                                if (two_partners.partnersstatus == 2)
                                {//二级用户为合伙人时才进行返利
                                    two_partners_commission = ConvertComission(dm_BasesettingEntity.task_two_partners * dm_TaskEntity.servicefee);
                                    if (two_partners_commission > 0)
                                    {
                                        two_partners = CalculateComission(two_partners.id, two_partners_commission, two_partners.accountprice);
                                        dm_AccountdetailEntities.Add(GeneralAccountDetail(two_partners.id, 18, dm_Basesetting_TipEntity.task_parners_two_tip, "您的" + dm_Basesetting_TipEntity.task_parners_two_tip + "《" + currentNickName + "》任务已完成,奖励已发放到您的账户,继续努力哟!", two_partners_commission, two_partners.accountprice));
                                    }
                                }
                            }
                            #endregion
                        }
                        #endregion
                        #endregion

                        if (calculateComissionEntities.Count > 0)
                        {
                            //必须加上这个变量,用于清除当前返利账户的余额
                            foreach (var item in calculateComissionEntities)
                            {
                                item.Modify(item.id);
                            }
                            db = BaseRepository("dm_data").BeginTrans();
                            db.Update(dm_Task_ReviceEntity);       //更改接单表信息
                            db.Update(dm_TaskEntity);              //更改任务主表信息
                            db.Insert(dm_MessagerecordEntity);     //增加任务审核消息(发给接受任务的人)
                            db.Insert(dm_MessagerecordEntity);     //增加消息记录
                            db.Insert(dm_AccountdetailEntities);   //增加账户余额明细
                            db.Update(calculateComissionEntities); //批量修改用户信息
                            db.Commit();
                        }
                    }
                    else
                    {
                        currentUser = dM_UserService.GetEntity(dm_Task_ReviceEntity.user_id);
                        if (currentUser.IsEmpty())
                        {
                            throw new Exception("用户信息异常!");
                        }

                        dm_activity_manageEntity dm_Activity_ManageEntity = new dm_activity_manageService().GetActivityInfo();
                        if (dm_Activity_ManageEntity.IsEmpty())
                        {
                            throw new Exception(CommonConfig.NoActivityTip);
                        }

                        dm_activity_recordEntity dm_Activity_RecordEntity = new dm_activity_recordService().GetEntityByUserID((int)dm_Task_ReviceEntity.user_id, dm_Activity_ManageEntity.f_id);
                        if (dm_Activity_RecordEntity.IsEmpty())
                        {
                            throw new Exception("未找到对应的加入活动任务信息!");
                        }

                        if (dm_Activity_ManageEntity.ActivityType == 0)
                        {
                            #region 初始用红包类型的任务
                            #region 活动账户增加金额
                            currentUser.activityprice += dm_TaskEntity.singlecommission;
                            #endregion

                            /*判断当前所有任务是否都已完成*/
                            List <dm_task_reviceEntity> dm_Task_ReviceEntities = this.BaseRepository("dm_data").FindList <dm_task_reviceEntity>(t => t.user_id == dm_Task_ReviceEntity.user_id && t.status != 3 && t.status != 4 && t.activitycode == dm_Activity_ManageEntity.f_id).ToList();
                            if (dm_Task_ReviceEntities.Count() <= 0 || (dm_Task_ReviceEntities.Count() == 1 && dm_Task_ReviceEntities[0].id == dm_Task_ReviceEntity.id))
                            {//活动任务已全部完成
                                currentUser.accountprice += currentUser.activityprice;
                                dm_AccountdetailEntities.Add(new dm_accountdetailEntity
                                {
                                    createtime   = DateTime.Now,
                                    remark       = "活动奖励" + dm_Activity_ManageEntity.ActivityCode,
                                    stepvalue    = currentUser.activityprice,
                                    currentvalue = currentUser.accountprice,
                                    title        = "活动奖励",
                                    type         = 24,
                                    user_id      = dm_Task_ReviceEntity.user_id,
                                    profitLoss   = 1
                                });

                                #region  级用户返利
                                dm_user_relationEntity parnetUserRelation = dM_UserRelationService.GetEntityByUserID(dm_Task_ReviceEntity.user_id);
                                if (!parnetUserRelation.IsEmpty())
                                {
                                    one_User = dM_UserService.GetEntity(parnetUserRelation.parent_id);
                                    if (!one_User.IsEmpty())
                                    {
                                        decimal next_user_activity_commission = Math.Round((decimal)currentUser.activityprice * 0.2M, 2);
                                        one_User.accountprice += next_user_activity_commission;

                                        dm_AccountdetailEntities.Add(new dm_accountdetailEntity
                                        {
                                            createtime   = DateTime.Now,
                                            remark       = "活动奖励" + dm_Activity_ManageEntity.ActivityCode,
                                            stepvalue    = next_user_activity_commission,
                                            currentvalue = one_User.accountprice,
                                            title        = "下级完成活动任务奖励",
                                            type         = 25,
                                            user_id      = one_User.id,
                                            profitLoss   = 1
                                        });
                                    }
                                }
                                #endregion

                                dm_Activity_RecordEntity.finishtime = DateTime.Now;//设置活动任务完成时间
                            }


                            db = BaseRepository("dm_data").BeginTrans();
                            db.Update(currentUser);
                            db.Update(dm_TaskEntity);
                            db.Update(dm_Task_ReviceEntity);
                            if (dm_AccountdetailEntities.Count > 0)
                            {
                                db.Insert(dm_AccountdetailEntities);
                            }
                            if (!one_User.IsEmpty())
                            {
                                db.Update(one_User);
                            }
                            db.Update(dm_Activity_RecordEntity);
                            db.Commit();
                            #endregion
                        }
                        else
                        {
                            List <dm_task_reviceEntity> dm_Task_ReviceEntities = this.BaseRepository("dm_data").FindList <dm_task_reviceEntity>(t => t.user_id == dm_Task_ReviceEntity.user_id && t.status != 3 && t.status != 4 && t.activitycode == dm_Activity_ManageEntity.f_id).ToList();

                            db = BaseRepository("dm_data").BeginTrans();
                            db.Update(dm_TaskEntity);
                            db.Update(dm_Task_ReviceEntity);

                            if (dm_Task_ReviceEntities.Count() <= 0 || (dm_Task_ReviceEntities.Count() == 1 && dm_Task_ReviceEntities[0].id == dm_Task_ReviceEntity.id))
                            {//活动任务已全部完成
                                decimal?activityprice = dm_Activity_ManageEntity.RewardPrice;
                                currentUser.accountprice += activityprice;
                                //currentUser.activityprice += activityprice;

                                dm_AccountdetailEntities.Add(new dm_accountdetailEntity
                                {
                                    createtime   = DateTime.Now,
                                    remark       = "活动奖励" + dm_Activity_ManageEntity.ActivityCode,
                                    stepvalue    = activityprice,
                                    currentvalue = currentUser.accountprice,
                                    title        = "活动奖励",
                                    type         = 24,
                                    user_id      = dm_Task_ReviceEntity.user_id,
                                    profitLoss   = 1
                                });

                                dm_Activity_RecordEntity.finishtime = DateTime.Now;//设置活动任务完成时间

                                db.Update(currentUser);
                                if (dm_AccountdetailEntities.Count > 0)
                                {
                                    db.Insert(dm_AccountdetailEntities);
                                }
                                db.Update(dm_Activity_RecordEntity);
                            }
                            db.Commit();
                        }
                    }

                    return(dm_Task_ReviceEntity);
                }
                catch (Exception ex)
                {
                    if (db != null)
                    {
                        db.Rollback();
                    }
                    if (ex is ExceptionEx)
                    {
                        throw;
                    }
                    else
                    {
                        throw ExceptionEx.ThrowServiceException(ex);
                    }
                }
            }
        }
        /// <summary>
        /// 更改会员上级并重置两个会员的统计数据
        /// </summary>
        /// <param name="UserID">需要设置上级的会员ID</param>
        /// <param name="ParentID">需要设置到的会员ID</param>
        public void UpdateUserParent(int UserID, int ParentID)
        {
            try
            {
                dm_user_relationEntity dm_User_RelationEntity = GetEntityByUserID(UserID);


                if (dm_User_RelationEntity.IsEmpty())
                {
                    throw new Exception("当前会员信息异常!");
                }
                int old_ParentUserID = dm_User_RelationEntity.parent_id;

                dm_user_relationEntity dm_User_ParentRelationEntity = GetEntityByUserID(UserID);
                if (dm_User_ParentRelationEntity.IsEmpty())
                {
                    throw new Exception("上级会员信息异常!");
                }

                if (dm_User_RelationEntity.parent_id == ParentID)
                {
                    throw new Exception("当前用户已属于该邀请人,无需重复设置!");
                }

                if (dm_User_ParentRelationEntity.parent_id == UserID)
                {
                    throw new Exception("用户关系无法双向绑定!");
                }

                if (dm_User_RelationEntity.partners_id != dm_User_ParentRelationEntity.partners_id)
                {
                    throw new Exception("当前设置关系不在同一团队,无法操作!");
                }

                dm_userEntity parentUserEntity = this.BaseRepository("dm_data").FindEntity <dm_userEntity>(ParentID);

                dm_User_RelationEntity.parent_id       = ParentID;
                dm_User_RelationEntity.parent_nickname = parentUserEntity.nickname;
                dm_User_RelationEntity.Modify(dm_User_RelationEntity.id);
                this.BaseRepository("dm_data").Update(dm_User_RelationEntity);

                #region 重置统计信息
                string excuteStatistic = "";
                if (!parentUserEntity.IsEmpty())
                {
                    excuteStatistic += string.Format("CALL ResetStatistic({0},{1},{2});", ParentID, parentUserEntity.partnersstatus, parentUserEntity.partners ?? 0);
                }


                dm_userEntity oldParentUserEntity = this.BaseRepository("dm_data").FindEntity <dm_userEntity>(old_ParentUserID);
                if (!oldParentUserEntity.IsEmpty())
                {
                    excuteStatistic += string.Format("CALL ResetStatistic({0},{1},{2});", old_ParentUserID, oldParentUserEntity.partnersstatus, oldParentUserEntity.partners ?? 0);
                }

                if (!excuteStatistic.IsEmpty())
                {
                    this.BaseRepository("dm_data").ExecuteBySql(excuteStatistic);
                }
                #endregion
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                throw ExceptionEx.ThrowServiceException(ex);
            }
        }
예제 #13
0
        /// <summary>
        /// 支付回调中开通代理(并完成返佣)
        /// </summary>
        /// <param name="dm_Alipay_RecordEntity"></param>
        public void OpenAgent(dm_alipay_recordEntity dm_Alipay_RecordEntity)
        {
            IRepository db = null;

            try
            {
                dm_AccountdetailEntities.Clear();
                calculateComissionEntities.Clear();

                /*
                 * 根据外部交易单号获取订单记录
                 * 更改用户状态并完成返利
                 * 1、一级代理返利(代理等级为高级才可得到)
                 * 2、二级代理返利(代理等级为高级才可得到)
                 * 3、一级合伙人(不校验代理等级,可能为后台开通)
                 * 4、二级合伙人(不校验代理等级,可能为后台开通)
                 */
                dm_alipay_recordEntity dm_Alipay_RecordEntity_old = this.BaseRepository("dm_data").FindEntity <dm_alipay_recordEntity>(t => t.out_trade_no == dm_Alipay_RecordEntity.out_trade_no);

                decimal one_agent_commission = 0, two_agent_commission = 0, one_partners_commission = 0, two_partners_commission = 0;

                dm_userEntity currentUser = null, one_User = null, two_User = null, one_partners = null, two_partners = null;

                ///如果老的记录是已支付状态则不需要再执行修改和返利
                if (dm_Alipay_RecordEntity_old.alipay_status.ToUpper() == "TRADE_SUCCESS")
                {
                    return;
                }

                if (dm_Alipay_RecordEntity_old.IsEmpty())
                {
                    throw new Exception("根据外部交易单号未能查询到支付记录,当前传入外部交易单号" + dm_Alipay_RecordEntity.out_trade_no);
                }
                dm_Alipay_RecordEntity.Modify(dm_Alipay_RecordEntity_old.id);//更改交易信息


                #region 获取上下级关系
                IEnumerable <dm_user_relationEntity> userRelationList = dM_UserRelationService.GetParentRelation(dm_Alipay_RecordEntity_old.user_id);
                #endregion

                #region 获取上下级关系的用户信息
                IEnumerable <dm_userEntity> userList = dM_UserService.GetParentUser(dm_Alipay_RecordEntity_old.user_id);
                #endregion

                #region 更改当前用户等级
                currentUser = userList.Where(t => t.id == dm_Alipay_RecordEntity_old.user_id).FirstOrDefault();
                if (currentUser.IsEmpty())
                {
                    throw new Exception("用户信息异常!");
                }
                //初级代理
                if (dm_Alipay_RecordEntity_old.templateid == 1)
                {
                    currentUser.userlevel = 1;
                }
                //高级代理
                else if (dm_Alipay_RecordEntity_old.templateid == 2)
                {
                    currentUser.userlevel = 2;
                }
                //升级代理
                else if (dm_Alipay_RecordEntity.templateid == 3)
                {
                    currentUser.userlevel = 2;
                }
                currentUser.Modify(currentUser.id);//必须加上这句,不然不会清除缓存
                calculateComissionEntities.Add(currentUser);
                #endregion

                #region 增加开通代理消息记录
                dm_messagerecordEntity dm_MessagerecordEntity = new dm_messagerecordEntity();
                dm_MessagerecordEntity.user_id        = dm_Alipay_RecordEntity_old.user_id;
                dm_MessagerecordEntity.messagetitle   = "开通代理";
                dm_MessagerecordEntity.messagecontent = "代理开通成功,发展下级可享受永久提成!";
                dm_MessagerecordEntity.messagetype    = 1;
                dm_MessagerecordEntity.createtime     = DateTime.Now;
                dm_MessagerecordEntity.isread         = 0;
                #endregion

                dm_basesettingEntity dm_BasesettingEntity = dM_BaseSettingService.GetEntityByCache(currentUser.appid);

                #region 更改一级账户余额及明细
                dm_user_relationEntity dm_User_RelationEntity_one = userRelationList.Where(t => t.user_id == dm_Alipay_RecordEntity_old.user_id).FirstOrDefault();
                one_User = userList.Where(t => t.id == dm_User_RelationEntity_one.parent_id).FirstOrDefault();
                if (!one_User.IsEmpty())
                {
                    if (one_User.userlevel == 2)
                    {//高级代理才能享受代理提成
                        one_agent_commission = ConvertComission(dm_BasesettingEntity.openagent_one * dm_Alipay_RecordEntity_old.total_amount);
                        if (one_agent_commission > 0)
                        {
                            one_User = CalculateComission(one_User.id, one_agent_commission, one_User.accountprice);
                            dm_AccountdetailEntities.Add(GeneralAccountDetail(one_User.id, 6, "下级开通代理", "您的下级《" + currentUser.nickname + "》开通代理成功,奖励已到账,继续努力哟!", one_agent_commission, one_User.accountprice));
                        }

                        #region 更改二级账户余额及明细
                        dm_user_relationEntity dm_User_RelationEntity_two = userRelationList.Where(t => t.user_id == one_User.id).FirstOrDefault();
                        two_User = userList.Where(t => t.id == dm_User_RelationEntity_two.parent_id).FirstOrDefault();
                        if (!two_User.IsEmpty())
                        {
                            two_agent_commission = ConvertComission(dm_BasesettingEntity.openagent_two * dm_Alipay_RecordEntity_old.total_amount);
                            if (two_agent_commission > 0)
                            {
                                two_User = CalculateComission(two_User.id, two_agent_commission, two_User.accountprice);
                                dm_AccountdetailEntities.Add(GeneralAccountDetail(two_User.id, 7, "二级开通代理", "您的二级《" + currentUser.nickname + "》开通代理成功,奖励已到账,继续努力哟!", two_agent_commission, two_User.accountprice));
                            }
                        }
                        #endregion
                    }
                }
                #endregion

                #region 获取当前用户所属合伙人(一级合伙人)
                one_partners = dM_UserService.GetUserByPartnersID(dm_User_RelationEntity_one.partners_id);
                if (!one_partners.IsEmpty())
                {
                    one_partners_commission = ConvertComission(dm_BasesettingEntity.openagent_one_partners * dm_Alipay_RecordEntity_old.total_amount);
                    if (one_partners_commission > 0)
                    {
                        one_partners = CalculateComission(one_partners.id, one_partners_commission, one_partners.accountprice);
                        dm_AccountdetailEntities.Add(GeneralAccountDetail(one_partners.id, 8, "团队成员开通代理", "您的团队成员《" + currentUser.nickname + "》开通代理成功,奖励已到账,继续努力哟!", one_partners_commission, one_partners.accountprice));
                    }

                    #region 二级合伙人
                    dm_user_relationEntity dm_User_RelationEntity_one_partners = dM_UserRelationService.GetEntityByUserID(one_partners.id);
                    two_partners = dM_UserService.GetEntityByCache(dm_User_RelationEntity_one_partners.parent_id);
                    if (!two_partners.IsEmpty())
                    {
                        two_partners_commission = ConvertComission(dm_BasesettingEntity.openagent_two_partners * dm_Alipay_RecordEntity_old.total_amount);
                        if (two_partners_commission > 0)
                        {
                            two_partners = CalculateComission(two_partners.id, two_partners_commission, two_partners.accountprice);
                            dm_AccountdetailEntities.Add(GeneralAccountDetail(two_partners.id, 9, "下级团队成员开通代理", "您的下级团队成员《" + currentUser.nickname + "》开通代理成功,奖励已到账,继续努力哟!", two_partners_commission, two_partners.accountprice));
                        }
                    }
                    #endregion
                }
                #endregion


                if (calculateComissionEntities.Count > 0)
                {
                    //必须加上这个变量,用于清除当前返利账户的余额
                    foreach (var item in calculateComissionEntities)
                    {
                        item.Modify(item.id);
                    }
                    db = BaseRepository("dm_data").BeginTrans();
                    db.Update(dm_Alipay_RecordEntity);     //修改原有的支付宝支付记录
                    db.Insert(dm_MessagerecordEntity);     //增加消息记录
                    db.Insert(dm_AccountdetailEntities);   //增加账户余额明细
                    db.Update(calculateComissionEntities); //批量修改用户信息
                    db.Commit();
                }
            }
            catch (Exception ex)
            {
                if (db != null)
                {
                    db.Rollback();
                }
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }