Пример #1
0
 /// <summary>
 /// 作者:Raymond
 /// 日期:2014-5-13
 /// 描述:更新合同明细
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public static bool UpdateContractDetail(ContractDetail model)
 {
     Db.Entry(model).State = EntityState.Modified;
     return Db.SaveChanges() > 0;
 }
Пример #2
0
        /// <summary>
        /// 作者:Raymond
        /// 日期:2014-5-13
        /// 描述:更新合同信息、合同明细、合同级别
        /// -----------------------------------
        /// 更新:Vincen
        /// 时间:2014.06.11
        /// ------------------------------------
        /// 更新:Ark
        /// 时间:2014.08.06
        /// 描述:参数加userId,单个ContractDetail修改为多个
        /// </summary>
        /// <param name="contract"></param>
        /// <param name="userId"></param>
        /// <param name="productIds"></param>
        /// <param name="emeLevels"></param>
        /// <param name="currentLevels"></param>
        /// <param name="contractTypes"></param>
        /// <param name="contractStatuType"></param>
        /// <returns></returns>
        public static bool UpdateContract(Contract contract, int userId, List<int> productIds, List<string> emeLevels, List<int> currentLevels, List<int> contractTypes, int contractStatuType, int loginUserId)
        {
            try
            {
                using (var edb = new EmeEntities())
                {
                    using (var tran = new TransactionScope())
                    {
                        //重新获取对象
                        var contractObj = edb.Contract.FirstOrDefault(p => p.Id == contract.Id);
                        var detailList = edb.ContractDetail.Where(p => p.ContractId == contract.Id).ToList();
                        var detailIds = detailList.Select(d => d.Id).ToList();
                        var contractLevelList = edb.ContractLevel.Where(l => detailIds.Contains(l.ContractDetailId)).ToList();

                        #region 合同状态无效合同相关修改为无效(直接返回)

                        if (contract.Status != ConvertEnum.StatusTypeForActive)
                        {
                            contractObj.Status = contract.Status;
                            contractObj.UpdateBy = contract.UpdateBy;
                            contractObj.UpdateTime = contract.UpdateTime;

                            // 合同明细状态修改为无效
                            foreach (var detail in detailList)
                            {
                                detail.Status = contract.Status;
                                detail.UpdateBy = contract.UpdateBy;
                                detail.UpdateTime = contract.UpdateTime;
                            }
                            // 同时对应的合同级别 ContractLevel 状态修改为无效
                            foreach (var levelItem in contractLevelList)
                            {
                                levelItem.Status = contract.Status;
                                levelItem.UpdateBy = contract.UpdateBy;
                                levelItem.UpdateTime = contract.UpdateTime;
                            }

                            edb.SaveChanges();
                            tran.Complete();
                            return true;  // 直接返回
                        }

                        #endregion
                        #region 1。修改合同

                        if (contractObj != null)
                        {
                            contractObj.CrmContractId = contract.CrmContractId;
                            contractObj.StudentId = contract.StudentId;
                            contractObj.ContractNum = contract.ContractNum;
                            contractObj.Status = contract.Status;
                            contractObj.BeginDate = contract.BeginDate;
                            contractObj.EndDate = contract.EndDate;
                            contractObj.UpdateBy = contract.UpdateBy;
                            contractObj.UpdateTime = contract.UpdateTime;
                            contractObj.Status = contract.Status;

                            edb.Entry(contractObj).State = EntityState.Modified;
                        }

                        #endregion
                        #region 2。合同明细及合同级别状态修改为无效

                        // 合同明细状态修改为无效
                        foreach (var detail in detailList)
                        {
                            detail.Status = ConvertEnum.StatusTypeForInactive;
                            detail.UpdateBy = contract.UpdateBy;
                            detail.UpdateTime = contract.UpdateTime;
                        }
                        // 同时对应的合同级别 ContractLevel 状态修改为无效
                        foreach (var levelItem in contractLevelList)
                        {
                            levelItem.IsCurrent = false;
                            levelItem.Status = ConvertEnum.StatusTypeForInactive;
                            levelItem.UpdateBy = contract.UpdateBy;
                            levelItem.UpdateTime = contract.UpdateTime;
                        }

                        edb.SaveChanges();

                        #endregion
                        #region 3。同步Crm合同明细及合同级别

                        int contractType;
                        ContractDetail contractDetail = null;
                        int productId;
                        //是否vip
                        var isVip = false;
                        int[] levels;
                        var userBranch = UserBLL.GetUserBranch(userId, true);

                        for (int i = 0; i < productIds.Count; i++)
                        {
                            // 主修课时总数
                            int majorTotal = 0;
                            // 选修课时总数
                            int electiveTotal = 0;
                            productId = productIds[i];
                            contractType = contractTypes[i];
                            contractDetail = detailList.FirstOrDefault(d => d.ProductId == productId);
                            levels = Array.ConvertAll<string, int>(emeLevels[i].Trim().Trim(',').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries),
                                delegate(string s) { return int.Parse(s); });

                            // 此合同明细(产品)存在修改,不存在新增
                            if (null != contractDetail)
                            {
                                var product = edb.Product.FirstOrDefault(p => p.Id == productId);

                                // 获取是否Vip学员,通过产品表Product的IsVip
                                if (null != product)
                                    isVip = product.IsVip;

                                contractDetail.IsVip = isVip;
                                contractDetail.ContractTypeId = contractType;
                                contractDetail.BeginDate = contract.BeginDate;
                                contractDetail.EndDate = contract.EndDate;
                                contractDetail.ContractStatusType = contractStatuType;
                                contractDetail.Status = contract.Status;

                                #region 合同级别

                                foreach (var level in levels)
                                {
                                    // 是否当前级别
                                    var curlevel = (level == currentLevels[i]);
                                    // 合同类型级别(定义主修、选修总课时)
                                    var ctpl = GetContractTypeProductLevel(userBranch.BranchId, contractType, level); // 此处 userBranch.Id 改为 userBranch.BranchId
                                    var repairTotal = ctpl.Repair;
                                    var absenceTotal = ctpl.Absence;
                                    majorTotal += ctpl.MajorTotal;
                                    electiveTotal += ctpl.ElectiveTotal;
                                    var contractLevel = contractLevelList.FirstOrDefault(l => l.ProductLevelId == level);

                                    if (null != contractLevel)
                                    {
                                        contractLevel.IsCurrent = curlevel;
                                        contractLevel.MajorTotal = ctpl.MajorTotal;
                                        contractLevel.ElectiveTotal = ctpl.ElectiveTotal;
                                        contractLevel.RepairTotal = repairTotal;
                                        contractLevel.AbsenceTotal = absenceTotal;
                                        contractLevel.Status = contract.Status;
                                    }
                                    else
                                    {
                                        contractLevel = new ContractLevel()
                                        {
                                            ContractDetailId = contractDetail.Id,
                                            ProductLevelId = level,
                                            IsCurrent = curlevel,
                                            RepairTotal = repairTotal,
                                            AbsenceTotal = absenceTotal,
                                            MajorTotal = ctpl.MajorTotal,           // 主修总课时
                                            ElectiveTotal = ctpl.ElectiveTotal,     // 选修总课时
                                            Status = contract.Status,
                                            CreateBy = contract.CreateBy,
                                            CreateTime = contract.CreateTime,
                                        };
                                        edb.Entry(contractLevel).State = EntityState.Added;
                                    }
                                }

                                #endregion

                                // 修改主选修课时总数
                                contractDetail.MajorTotal = majorTotal;
                                contractDetail.ElectiveTotal = electiveTotal;
                                edb.SaveChanges();
                            }
                            else
                            {
                                // 新增
                                contractDetail = new ContractDetail()
                                {
                                    ContractId = contract.Id,
                                    ContractTypeId = contractType,
                                    ProductId = productId,
                                    MajorTotal = 0,        // 添加时默认0,添加级别后修改
                                    ElectiveTotal = 0,
                                    IsVip = isVip,
                                    BeginDate = contract.BeginDate,
                                    EndDate = contract.EndDate,
                                    IsLimitBook = false,
                                    AllowUnBookTimes = 0,
                                    ContractStatusType = contractStatuType,
                                    Status = contract.Status,
                                    CreateBy = contract.CreateBy,
                                    CreateTime = contract.CreateTime
                                };
                                edb.Entry(contractDetail).State = EntityState.Added;
                                edb.SaveChanges();

                                #region 合同级别

                                foreach (var level in levels)
                                {
                                    // 合同类型级别(定义主修、选修总课时)
                                    var ctpl = GetContractTypeProductLevel(userBranch.BranchId, contractType, level); // 此处 userBranch.Id 改为 userBranch.BranchId
                                    var repairTotal = ctpl.Repair;
                                    var absenceTotal = ctpl.Absence;
                                    majorTotal += ctpl.MajorTotal;
                                    electiveTotal += ctpl.ElectiveTotal;
                                    // 是否当前级别
                                    var curlevel = (level == currentLevels[i]);

                                    var contractLevel = new ContractLevel()
                                    {
                                        ContractDetailId = contractDetail.Id,
                                        ProductLevelId = level,
                                        IsCurrent = curlevel,
                                        RepairTotal = repairTotal,
                                        AbsenceTotal = absenceTotal,
                                        MajorTotal = ctpl.MajorTotal,           // 主修总课时
                                        ElectiveTotal = ctpl.ElectiveTotal,     // 选修总课时
                                        Status = contract.Status,
                                        CreateBy = contract.CreateBy,
                                        CreateTime = contract.CreateTime,
                                    };
                                    edb.Entry(contractLevel).State = EntityState.Added;
                                }

                                // 修改合同明细主先修课时总数
                                contractDetail.MajorTotal = majorTotal;
                                contractDetail.ElectiveTotal = electiveTotal;

                                #endregion
                            }
                        }

                        edb.SaveChanges();
                        tran.Complete();
                        return true;

                        #endregion
                    }
                }
            }
            catch (Exception e)
            {
                // 异常日志消息队列
                Common.MSMQ.QueueManager.AddExceptionLog(new ExceptionLogs()
                {
                    ExceptionType = CommonHelper.To<int>(ExceptionType.Function),
                    Message = string.Format("StudentBLL-UpdateContract:{0};{1};{2}", e.Message, e.InnerException.Message, e.HelpLink),
                    IsTreat = false,
                    CreateBy = loginUserId,
                    CreateTime = DateTime.Now
                });

                return false;
            }
        }
Пример #3
0
        /// <summary>
        /// 作者:Raymond
        /// 日期:2014-5-13
        /// 描述:更新合同信息、合同明细、合同级别
        /// -----------------------------------
        /// 更新:Vincen
        /// 时间:2014.06.11
        /// ------------------------------------
        /// 更新:Ark
        /// 时间:2014.08.06
        /// 描述:参数加userId,单个ContractDetail修改为多个
        /// </summary>
        /// <param name="contract"></param>
        /// <param name="userId"></param>
        /// <param name="productIds"></param>
        /// <param name="emeLevels"></param>
        /// <param name="currentLevels"></param>
        /// <param name="contractTypes"></param>
        /// <param name="contractStatuType"></param>
        /// <returns></returns>
        public static bool UpdateContract(Contract contract, int userId, List<int> productIds, List<string> emeLevels, List<string> currentLevels, List<int> contractTypes, int contractStatuType, int loginUserId)
        {
            try
            {
                using (var edb = new EmeEntities())
                {
                    using (var tran = new TransactionScope())
                    {
                        //获取对象
                        var contractObj = edb.Contract.FirstOrDefault(p => p.Id == contract.Id);

                        if (null == contractObj)
                            return false;

                        var detailList = edb.ContractDetail.Where(p => p.ContractId == contract.Id).ToList();
                        var detailIds = detailList.Select(d => d.Id).ToList();
                        var contractLevelList = edb.ContractLevel.Where(l => detailIds.Contains(l.ContractDetailId)).ToList();

                        #region 合同状态无效合同相关修改为无效(直接返回)

                        if (contract.Status != ConvertEnum.StatusTypeForActive)
                        {
                            contractObj.Status = contract.Status;
                            contractObj.UpdateBy = contract.UpdateBy;
                            contractObj.UpdateTime = contract.UpdateTime;

                            // 合同明细状态修改为无效
                            foreach (var detail in detailList)
                            {
                                detail.Status = contract.Status;
                                detail.UpdateBy = contract.UpdateBy;
                                detail.UpdateTime = contract.UpdateTime;
                            }
                            // 同时对应的合同级别 ContractLevel 状态修改为无效
                            foreach (var levelItem in contractLevelList)
                            {
                                levelItem.Status = contract.Status;
                                levelItem.UpdateBy = contract.UpdateBy;
                                levelItem.UpdateTime = contract.UpdateTime;
                            }

                            edb.SaveChanges();
                            tran.Complete();
                            return true;  // 直接返回
                        }

                        #endregion
                        #region 1。修改合同

                        if (contractObj != null)
                        {
                            contractObj.CrmContractId = contract.CrmContractId;
                            contractObj.StudentId = contract.StudentId;
                            contractObj.ContractNum = contract.ContractNum;
                            contractObj.BeginDate = contract.BeginDate;
                            contractObj.EndDate = contract.EndDate;
                            contractObj.UpdateBy = contract.UpdateBy;
                            contractObj.UpdateTime = contract.UpdateTime;
                            contractObj.Status = contract.Status;

                            edb.Entry(contractObj).State = EntityState.Modified;
                        }

                        #endregion
                        #region 2。合同明细及合同级别状态修改为无效

                        // 合同明细状态修改为无效
                        foreach (var detail in detailList)
                        {
                            detail.Status = ConvertEnum.StatusTypeForInactive;
                            detail.UpdateBy = contract.UpdateBy;
                            detail.UpdateTime = contract.UpdateTime;
                        }
                        // 同时对应的合同级别 ContractLevel 状态修改为无效
                        foreach (var levelItem in contractLevelList)
                        {
                            levelItem.Status = ConvertEnum.StatusTypeForInactive;
                            levelItem.UpdateBy = contract.UpdateBy;
                            levelItem.UpdateTime = contract.UpdateTime;
                        }

                        // 学员别的合同
                        var otherContractList = edb.Contract.Where(c => c.StudentId == contractObj.StudentId && c.Id != contract.Id);
                        var otherContractIds = otherContractList.Select(c => c.Id);
                        // 学员别的冻结、执行的合同详情
                        var otherDetailList = edb.ContractDetail.Where(d => otherContractIds.Contains(d.ContractId) &&
                            (d.ContractStatusType == ConvertEnum.ContractStatusTypeForExcute || d.ContractStatusType == ConvertEnum.ContractStatusTypeForFreeze));
                        // 学员别的冻结、执行的合同Id
                        var tempContractIds = otherDetailList.Select(d => d.ContractId).Distinct();
                        otherContractList = otherContractList.Where(c => tempContractIds.Contains(c.Id));

                        foreach (var otherContract in otherContractList)
                        {
                            otherContract.BeginDate = contract.BeginDate;
                            otherContract.EndDate = contract.EndDate;
                            edb.Entry(otherContract).State = EntityState.Modified;
                        }
                        foreach (var otherDetail in otherDetailList)
                        {
                            otherDetail.BeginDate = contract.BeginDate;
                            otherDetail.EndDate = contract.EndDate;
                            edb.Entry(otherDetail).State = EntityState.Modified;
                        }

                        edb.SaveChanges();

                        #endregion
                        #region 3。同步Crm合同明细及合同级别

                        int contractType;
                        ContractDetail contractDetail = null;
                        int productId;
                        //是否vip
                        var isVip = false;
                        int[] levels;
                        var userBranch = UserBLL.GetUserBranch(userId, true);

                        for (int i = 0; i < productIds.Count; i++)
                        {
                            // 主修课时总数
                            int majorTotal = 0;
                            // 选修课时总数
                            int electiveTotal = 0;
                            productId = productIds[i];
                            contractType = contractTypes[i];
                            contractDetail = detailList.FirstOrDefault(d => d.ProductId == productId);
                            levels = Array.ConvertAll<string, int>(emeLevels[i].Trim().Trim(',').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries),
                                delegate(string s) { return int.Parse(s); });
                            List<int> curlastIndexs = new List<int>();
                            int[] productCurLevels = Array.ConvertAll<string, int>(currentLevels[i].Trim().Trim(',').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries),
                                delegate(string s) { return int.Parse(s); });

                            foreach (var level in productCurLevels)
                            {
                                curlastIndexs.Add(levels.ToList().LastIndexOf(level));
                            }

                            // 取当前级别在级别中的最后索引
                            //int lastIndex = levels.ToList().LastIndexOf(currentLevels[i]);
                            // 临时索引
                            int tempIndex = 0;

                            // 此合同明细(产品)存在修改,不存在新增
                            if (null != contractDetail)
                            {
                                var product = edb.Product.FirstOrDefault(p => p.Id == productId);

                                // 获取是否Vip学员,通过产品表Product的IsVip
                                if (null != product)
                                    isVip = product.IsVip;

                                contractDetail.IsVip = isVip;
                                contractDetail.ContractTypeId = contractType;
                                contractDetail.BeginDate = contract.BeginDate;
                                contractDetail.EndDate = contract.EndDate;
                                contractDetail.ContractStatusType = contractStatuType;
                                contractDetail.Status = contract.Status;

                                #region 合同级别

                                foreach (var level in levels)
                                {
                                    // 是否当前级别 (两索引相等时设为当前级别)
                                    //一个当前级别时采用 索引,取最后一个为当前级别
                                    // var curlevel = (tempIndex == lastIndex); //(level == currentLevels[i]);
                                    //多个当前级别时,每个都设置为当前级别
                                    // if (currentLevels.Count > 1)
                                    //      curlevel = currentLevels.Contains(level);
                                    var curlevel = curlastIndexs.Contains(tempIndex);

                                    // 合同类型级别(定义主修、选修总课时)
                                    var ctpl = edb.ContractTypeProductLevel.FirstOrDefault(p => p.BranchId == userBranch.BranchId && p.ContractTypeId == contractType && p.ProductLevelId == level && p.Status == ConvertEnum.StatusTypeForActive);
                                    if (ctpl == null)
                                    {
                                        ctpl = edb.ContractTypeProductLevel.FirstOrDefault(p => p.ContractTypeId == contractType && p.ProductLevelId == level && p.Status == ConvertEnum.StatusTypeForActive && p.IsSystem == true);
                                    }

                                    var repairTotal = ctpl.Repair;
                                    var absenceTotal = ctpl.Absence;
                                    majorTotal += ctpl.MajorTotal;
                                    electiveTotal += ctpl.ElectiveTotal;
                                    var contractLevel = contractLevelList.FirstOrDefault(l => l.ContractDetailId == contractDetail.Id && l.ProductLevelId == level);

                                    if (null != contractLevel)
                                    {
                                        // 找到级别,缓存移除此级别(为了处理级别转换,两相级别相同)
                                        contractLevelList.Remove(contractLevel);
                                        contractLevel.IsCurrent = curlevel;

                                        //2015.05.25 beta修改,针对特殊修改操作
                                        var remark = "手动修改";
                                        if (contractLevel.Remark != remark)
                                        {
                                            contractLevel.MajorTotal = ctpl.MajorTotal;
                                            contractLevel.ElectiveTotal = ctpl.ElectiveTotal;
                                        }
                                        contractLevel.RepairTotal = repairTotal;
                                        contractLevel.AbsenceTotal = absenceTotal;
                                        contractLevel.Status = contract.Status;
                                    }
                                    else
                                    {
                                        contractLevel = new ContractLevel()
                                        {
                                            ContractDetailId = contractDetail.Id,
                                            ProductLevelId = level,
                                            IsCurrent = curlevel,
                                            RepairTotal = repairTotal,
                                            AbsenceTotal = absenceTotal,
                                            MajorTotal = ctpl.MajorTotal,           // 主修总课时
                                            ElectiveTotal = ctpl.ElectiveTotal,     // 选修总课时
                                            Status = contract.Status,
                                            CreateBy = contract.CreateBy,
                                            CreateTime = contract.CreateTime,
                                        };
                                        edb.Entry(contractLevel).State = EntityState.Added;
                                    }

                                    tempIndex++;
                                }

                                #endregion

                                // 修改主选修课时总数
                                contractDetail.MajorTotal = majorTotal;
                                contractDetail.ElectiveTotal = electiveTotal;
                                edb.SaveChanges();
                            }
                            else
                            {
                                // 新增
                                contractDetail = new ContractDetail()
                                {
                                    ContractId = contract.Id,
                                    ContractTypeId = contractType,
                                    ProductId = productId,
                                    MajorTotal = 0,        // 添加时默认0,添加级别后修改
                                    ElectiveTotal = 0,
                                    IsVip = isVip,
                                    BeginDate = contract.BeginDate,
                                    EndDate = contract.EndDate,
                                    IsLimitBook = false,
                                    AllowUnBookTimes = 0,
                                    ContractStatusType = contractStatuType,
                                    Status = contract.Status,
                                    CreateBy = contract.CreateBy,
                                    CreateTime = contract.CreateTime
                                };
                                edb.Entry(contractDetail).State = EntityState.Added;
                                edb.SaveChanges();

                                #region 合同级别

                                foreach (var level in levels)
                                {
                                    // 合同类型级别(定义主修、选修总课时)
                                    var ctpl = edb.ContractTypeProductLevel.FirstOrDefault(p => p.BranchId == userBranch.BranchId && p.ContractTypeId == contractType && p.ProductLevelId == level && p.Status == ConvertEnum.StatusTypeForActive);
                                    if (ctpl == null)
                                    {
                                        ctpl = edb.ContractTypeProductLevel.FirstOrDefault(p => p.ContractTypeId == contractType && p.ProductLevelId == level && p.Status == ConvertEnum.StatusTypeForActive && p.IsSystem == true);
                                    }
                                    //var ctpl = GetContractTypeProductLevel(userBranch.BranchId, contractType, level); // 此处 userBranch.Id 改为 userBranch.BranchId
                                    var repairTotal = ctpl.Repair;
                                    var absenceTotal = ctpl.Absence;
                                    majorTotal += ctpl.MajorTotal;
                                    electiveTotal += ctpl.ElectiveTotal;
                                    // 是否当前级别 (两索引相等时设为当前级别)
                                    // var curlevel = (tempIndex == lastIndex); //(level == currentLevels[i]);
                                    var curlevel = curlastIndexs.Contains(tempIndex);
                                    var contractLevel = new ContractLevel()
                                    {
                                        ContractDetailId = contractDetail.Id,
                                        ProductLevelId = level,
                                        IsCurrent = curlevel,
                                        RepairTotal = repairTotal,
                                        AbsenceTotal = absenceTotal,
                                        MajorTotal = ctpl.MajorTotal,           // 主修总课时
                                        ElectiveTotal = ctpl.ElectiveTotal,     // 选修总课时
                                        Status = contract.Status,
                                        CreateBy = contract.CreateBy,
                                        CreateTime = contract.CreateTime,
                                    };
                                    edb.Entry(contractLevel).State = EntityState.Added;
                                    tempIndex++;
                                }

                                // 修改合同明细主先修课时总数
                                contractDetail.MajorTotal = majorTotal;
                                contractDetail.ElectiveTotal = electiveTotal;

                                #endregion
                            }
                        }

                        edb.SaveChanges();
                        tran.Complete();
                        return true;

                        #endregion
                    }
                }
            }
            catch (Exception e)
            {
                // 异常日志消息队列
                Common.MSMQ.QueueManager.AddExceptionLog(new ExceptionLogs()
                {
                    ExceptionType = CommonHelper.To<int>(ExceptionType.Function),
                    Message = string.Format("StudentBLL-UpdateContract:{0};{1};{2}", e.Message, e.InnerException.Message, e.HelpLink),
                    IsTreat = false,
                    CreateBy = loginUserId,
                    CreateTime = DateTime.Now
                });

                return false;
            }
        }
Пример #4
0
        /// <summary>
        /// 作者:Raymond
        /// 日期:2014-5-14
        /// 描述:新增合同、合同明细、合同级别
        /// ------------------------------------
        /// 更新:Vincen
        /// 时间:2014.06.11
        /// ------------------------------------
        /// 更新:Ark
        /// 时间:2014.08.06
        /// 描述:参数加userId,单个ContractDetail修改为多个,获取IsVip通过产品表
        /// </summary>
        /// <param name="contract"></param>
        /// <param name="userId"></param>
        /// <param name="productIds"></param>
        /// <param name="emeLevels"></param>
        /// <param name="currentLevels"></param>
        /// <param name="contractTypes"></param>
        /// <param name="contractStatuType"></param>
        /// <returns></returns>
        public static bool CreateContract(Contract contract, int userId, List<int> productIds, List<string> emeLevels, List<int> currentLevels, List<int> contractTypes, int contractStatuType)
        {
            try
            {
                using (var edb = new EmeEntities())
                {
                    using (var tran = new TransactionScope())
                    {
                        #region 新合同及合同明细、合同级别处理

                        // Step1.新增主合同
                        edb.Contract.Add(contract); // 重新定义Contract并赋值 改为 直接添加
                        edb.SaveChanges();

                        // Step2.合同明细对象
                        int contractType;
                        bool isVip = false;
                        ContractDetail detail = null;
                        UserBranch userBranch = UserBLL.GetUserBranch(userId, true);
                        int[] levels;

                        for (int i = 0; i < productIds.Count; i++)
                        {
                            levels = Array.ConvertAll<string, int>(emeLevels[i].Trim().Trim(',').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries),
                                delegate(string s) { return int.Parse(s); });
                            contractType = contractTypes[i];

                            var productId = productIds[i];
                            var product = edb.Product.FirstOrDefault(p => p.Id == productId);

                            // 获取是否Vip学员,通过产品表Product的IsVip
                            if (null != product)
                                isVip = product.IsVip;
                            // 添加合同详情
                            detail = new ContractDetail()
                            {
                                ContractId = contract.Id,
                                ContractTypeId = contractType,
                                ProductId = productId,
                                MajorTotal = 0,        // 添加时默认0,添加级别后修改
                                ElectiveTotal = 0,
                                IsVip = isVip,
                                BeginDate = contract.BeginDate,
                                EndDate = contract.EndDate,
                                IsLimitBook = false,
                                AllowUnBookTimes = 0,
                                ContractStatusType = contractStatuType,
                                Status = contract.Status,
                                CreateBy = contract.CreateBy,
                                CreateTime = contract.CreateTime
                            };
                            edb.Entry(detail).State = EntityState.Added;
                            edb.SaveChanges();

                            // Step3.合同级别信息
                            // 获取此合同类型的主修课时总数,选修课时总数
                            // 主修课时总数
                            int majorTotal = 0;
                            // 选修课时总数
                            int electiveTotal = 0;

                            foreach (var level in levels)
                            {
                                // 合同类型级别(定义主修、选修总课时)
                                var ctpl = GetContractTypeProductLevel(userBranch.BranchId, contractType, level); // 此处 userBranch.Id 改为 userBranch.BranchId
                                var repairTotal = ctpl.Repair;
                                var absenceTotal = ctpl.Absence;
                                majorTotal += ctpl.MajorTotal;
                                electiveTotal += ctpl.ElectiveTotal;
                                // 是否当前级别
                                var curlevel = (level == currentLevels[i]);

                                var contractLevel = new ContractLevel()
                                {
                                    ContractDetailId = detail.Id,
                                    ProductLevelId = level,
                                    IsCurrent = curlevel,
                                    RepairTotal = repairTotal,
                                    AbsenceTotal = absenceTotal,
                                    MajorTotal = ctpl.MajorTotal,           // 主修总课时
                                    ElectiveTotal = ctpl.ElectiveTotal,     // 选修总课时
                                    Status = contract.Status,
                                    CreateBy = contract.CreateBy,
                                    CreateTime = contract.CreateTime,
                                };
                                edb.Entry(contractLevel).State = EntityState.Added;
                            }

                            // 修改合同明细主先修课时总数
                            detail.MajorTotal = majorTotal;
                            detail.ElectiveTotal = electiveTotal;

                            edb.SaveChanges();
                        }

                        #endregion

                        #region 合并老合同级别

                        ////判断是否有相同产品的有效级别,如果有就合并
                        //foreach (var oldL in oldLevelList)
                        //{
                        //    //合同级别状态设为无效
                        //    oldL.Status = ConvertEnum.StatusTypeForInactive;
                        //    var newL = new ContractLevel()
                        //    {
                        //        Status = ConvertEnum.StatusTypeForActive,
                        //        ContractDetailId = contractDetailId,
                        //        ProductLevelId = oldL.ProductLevelId,
                        //        IsCurrent = oldL.IsCurrent,
                        //        Online = oldL.Online,
                        //        Offline = oldL.Offline,
                        //        Total = oldL.Total,
                        //        Repair = oldL.Repair,
                        //        Absence = oldL.Absence,
                        //        StartStudyTime = oldL.StartStudyTime,
                        //        EndStudyTime = oldL.EndStudyTime,
                        //        CreateBy = oldL.CreateBy,
                        //        CreateTime = contract.CreateTime,
                        //    };
                        //    //新记录的状态为可用
                        //    edb.Entry(oldL).State = EntityState.Modified;
                        //    edb.Entry(newL).State = EntityState.Added;
                        //    edb.SaveChanges();
                        //}

                        #endregion

                        tran.Complete();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                // 异常日志消息队列
                QueueManager.AddExceptionLog(new ExceptionLogs()
                {
                    ExceptionType = CommonHelper.To<int>(ExceptionType.Function),
                    Message = string.Format("StudentBLL-CreateContract:{0};{1};{2}", e.Message, e.InnerException.Message, e.HelpLink),
                    IsTreat = false,
                    CreateBy = contract.CreateBy,
                    CreateTime = DateTime.Now
                });

                return false;
            }
        }
Пример #5
0
        /// <summary>
        /// 作者:Raymond
        /// 日期:2014-5-14
        /// 描述:新增合同、合同明细、合同级别
        /// ------------------------------------
        /// 更新:Vincen
        /// 时间:2014.06.11
        /// ------------------------------------
        /// 更新:Ark
        /// 时间:2014.08.06
        /// 描述:参数加userId,单个ContractDetail修改为多个,获取IsVip通过产品表
        /// </summary>
        /// <param name="contract"></param>
        /// <param name="userId"></param>
        /// <param name="productIds"></param>
        /// <param name="emeLevels"></param>
        /// <param name="currentLevels"></param>
        /// <param name="contractTypes"></param>
        /// <param name="contractStatuType"></param>
        /// <param name="updatingBookRecordIds">需要更新的bookRecordId</param>
        /// <returns></returns>
        public static bool CreateContract(Contract contract, int userId, List<int> productIds, List<string> emeLevels, List<string> currentLevels,
            List<int> contractTypes, int contractStatuType, int[] updatingBookRecordIds, int? oldcontractNum)
        {
            using (var edb = new EmeEntities())
            {
                using (var tran = new TransactionScope())
                {
                    try
                    {
                        #region 新合同及合同明细、合同级别处理

                        // Step1.新增主合同
                        edb.Contract.Add(contract); // 重新定义Contract并赋值 改为 直接添加
                        edb.SaveChanges();

                        // 学员别的合同
                        var otherContractList = edb.Contract.Where(c => c.StudentId == contract.StudentId && c.Id != contract.Id);
                        var otherContractIds = otherContractList.Select(c => c.Id);
                        // 学员别的冻结、执行的合同详情
                        var otherDetailList = edb.ContractDetail.Where(d => otherContractIds.Contains(d.ContractId) &&
                            (d.ContractStatusType == ConvertEnum.ContractStatusTypeForExcute || d.ContractStatusType == ConvertEnum.ContractStatusTypeForFreeze));
                        // 学员别的冻结、执行的合同Id
                        var tempContractIds = otherDetailList.Select(d => d.ContractId).Distinct();
                        otherContractList = otherContractList.Where(c => tempContractIds.Contains(c.Id));

                        foreach (var otherContract in otherContractList)
                        {
                            otherContract.BeginDate = contract.BeginDate;
                            otherContract.EndDate = contract.EndDate;
                            edb.Entry(otherContract).State = EntityState.Modified;
                        }
                        foreach (var otherDetail in otherDetailList)
                        {
                            otherDetail.BeginDate = contract.BeginDate;
                            otherDetail.EndDate = contract.EndDate;
                            edb.Entry(otherDetail).State = EntityState.Modified;
                        }

                        edb.SaveChanges();

                        // Step2.合同明细对象
                        int contractType;
                        bool isVip = false;
                        ContractDetail detail = null;
                        UserBranch userBranch = UserBLL.GetUserBranch(userId, true);
                        int[] levels;

                        for (int i = 0; i < productIds.Count; i++)
                        {
                            levels = Array.ConvertAll<string, int>(emeLevels[i].Trim().Trim(',').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries),
                                delegate(string s) { return int.Parse(s); });
                            contractType = contractTypes[i];

                            var productId = productIds[i];
                            var product = edb.Product.FirstOrDefault(p => p.Id == productId);

                            List<int> curlastIndexs = new List<int>();
                            int[] productCurLevels = Array.ConvertAll<string, int>(currentLevels[i].Trim().Trim(',').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries),
                                delegate(string s) { return int.Parse(s); });

                            foreach (var level in productCurLevels)
                            {
                                curlastIndexs.Add(levels.ToList().LastIndexOf(level));
                            }

                            // 获取是否Vip学员,通过产品表Product的IsVip
                            if (null != product)
                                isVip = product.IsVip;
                            // 添加合同详情
                            detail = new ContractDetail()
                            {
                                ContractId = contract.Id,
                                ContractTypeId = contractType,
                                ProductId = productId,
                                MajorTotal = 0,        // 添加时默认0,添加级别后修改
                                ElectiveTotal = 0,
                                IsVip = isVip,
                                BeginDate = contract.BeginDate,
                                EndDate = contract.EndDate,
                                IsLimitBook = false,
                                AllowUnBookTimes = 0,
                                ContractStatusType = contractStatuType,
                                Status = contract.Status,
                                CreateBy = contract.CreateBy,
                                CreateTime = contract.CreateTime
                            };
                            edb.Entry(detail).State = EntityState.Added;
                            edb.SaveChanges();

                            // Step3.合同级别信息
                            // 获取此合同类型的主修课时总数,选修课时总数
                            // 主修课时总数
                            int majorTotal = 0;
                            // 选修课时总数
                            int electiveTotal = 0;
                            // 取当前级别在级别中的最后索引
                            // int lastIndex = levels.ToList().LastIndexOf(currentLevels[i]);
                            // 临时索引
                            int tempIndex = 0;

                            var contractLevelListTemp = new List<ContractLevel>(levels.Length);
                            foreach (var level in levels)
                            {
                                // 合同类型级别(定义主修、选修总课时)
                                var ctpl = edb.ContractTypeProductLevel.FirstOrDefault(p => p.BranchId == userBranch.BranchId && p.ContractTypeId == contractType && p.ProductLevelId == level && p.Status == ConvertEnum.StatusTypeForActive);
                                if (ctpl == null)
                                {
                                    ctpl = edb.ContractTypeProductLevel.FirstOrDefault(p => p.ContractTypeId == contractType && p.ProductLevelId == level && p.Status == ConvertEnum.StatusTypeForActive && p.IsSystem == true);
                                }

                                var repairTotal = ctpl.Repair;
                                var absenceTotal = ctpl.Absence;
                                majorTotal += ctpl.MajorTotal;
                                electiveTotal += ctpl.ElectiveTotal;
                                // 是否当前级别 (两索引相等时设为当前级别)
                                //var curlevel = (tempIndex == lastIndex); //(level == currentLevels[i]);
                                var curlevel = curlastIndexs.Contains(tempIndex);
                                var contractLevel = new ContractLevel()
                                {
                                    ContractDetailId = detail.Id,
                                    ProductLevelId = level,
                                    IsCurrent = curlevel,
                                    RepairTotal = repairTotal,
                                    AbsenceTotal = absenceTotal,
                                    MajorTotal = ctpl.MajorTotal,           // 主修总课时
                                    ElectiveTotal = ctpl.ElectiveTotal,     // 选修总课时
                                    Status = contract.Status,
                                    CreateBy = contract.CreateBy,
                                    CreateTime = contract.CreateTime,
                                };
                                edb.Entry(contractLevel).State = EntityState.Added;
                                tempIndex++;
                                contractLevelListTemp.Add(contractLevel);
                            }

                            // 修改合同明细主先修课时总数
                            detail.MajorTotal = majorTotal;
                            detail.ElectiveTotal = electiveTotal;

                            edb.SaveChanges();

                            #region 将学员旧的bookRecord绑定为当前合同-特殊业务逻辑。
                            //3.0升级4.0时,存在部分学员的原CrmContracId为null的数据,当从Crm同步合同时,会认为是新合同,从而将原有学习记录冲掉的情况
                            //此情况只存在于3.0升级4.0时导入的学员数据,待该部分所有数据都正常后可以删除 by ian fang 2015-01-12
                            var originBookRecords = (from br in edb.BookRecord
                                                     join cl in edb.ContractLevel on br.ContractLevelId equals cl.Id
                                                     where updatingBookRecordIds.Contains(br.Id)
                                                     select new { BookRecord = br, ContractLevel = cl }).ToList();

                            #region 短期转常规,将短期班的上课课时绑定到常规合同级别下面
                            if (oldcontractNum.HasValue)
                            {
                                originBookRecords = (from br in edb.BookRecord
                                                     join cl in edb.ContractLevel on br.ContractLevelId equals cl.Id
                                                     join cd in edb.ContractDetail on cl.ContractDetailId equals cd.Id
                                                     join c in edb.Contract on cd.ContractId equals c.Id
                                                     where c.CrmContractId == oldcontractNum
                                                     select new { BookRecord = br, ContractLevel = cl }).ToList();
                            }
                            #endregion

                            foreach (var contractLevel in contractLevelListTemp)
                            {
                                if (updatingBookRecordIds != null)
                                {
                                    var originBookRecordsTemp = originBookRecords.Where(obr => obr.ContractLevel.ProductLevelId == contractLevel.ProductLevelId).ToList();
                                    bool levelUpdateFlag = false;
                                    foreach (var bookRecord in originBookRecordsTemp)
                                    {
                                        if (!levelUpdateFlag)
                                        {
                                            //复制原ContractLevel中的数据
                                            contractLevel.MajorOffline = bookRecord.ContractLevel.MajorOffline;
                                            contractLevel.MajorOnline = bookRecord.ContractLevel.MajorOnline;
                                            contractLevel.ElectiveOnline = bookRecord.ContractLevel.ElectiveOnline;
                                            contractLevel.ElectiveOffline = bookRecord.ContractLevel.ElectiveOffline;
                                            contractLevel.Repair = bookRecord.ContractLevel.Repair;
                                            contractLevel.Absence = bookRecord.ContractLevel.Absence;

                                            edb.Entry(contractLevel).State = EntityState.Modified;
                                            levelUpdateFlag = true;
                                        }

                                        //将原bookRecord中的ContractlevelId更新为新建的
                                        bookRecord.BookRecord.ContractLevelId = contractLevel.Id;
                                        edb.Entry(bookRecord.BookRecord).State = EntityState.Modified;
                                    }
                                }
                            }
                            edb.SaveChanges();

                            #endregion
                        }

                        #endregion

                        tran.Complete();
                        return true;
                    }
                    catch (Exception e)
                    {
                        // 异常日志消息队列
                        QueueManager.AddExceptionLog(new ExceptionLogs()
                        {
                            ExceptionType = CommonHelper.To<int>(ExceptionType.Function),
                            Message = string.Format("StudentBLL-CreateContract:{0};{1};{2}", e.Message, e.InnerException.Message, e.HelpLink),
                            IsTreat = false,
                            CreateBy = contract.CreateBy,
                            CreateTime = DateTime.Now
                        });
                        return false;
                    }
                }
            }
        }