/// <summary> /// 更新订单状态为已提货或卖 /// </summary> /// <param name="agentId"></param> /// <param name="order"></param> /// <param name="dataMember"></param> /// <param name="operationEntity"></param> /// <returns></returns> public static bool UpdateOrder(string agentId, OrderEntity order, List<UpdateDataMember> dataMember, OrderOperationEntity operationEntity) { List<DeliverEntity> deliverList = new List<DeliverEntity>();//需要更新的交割单列表 List<DeliverRecordEntity> deliverRecordList = new List<DeliverRecordEntity>(); List<UpdateDataMember> updateList = new List<UpdateDataMember>();//需要更新的订单信息 List<UpdateDataMember> userList = new List<UpdateDataMember>();//需要更新用户数量 OrderEntity entity = LoadByOrderCode(order.OrderCode);//获取订单信息 StockEntity stockEntity = LoadByAccount(entity.UserId);//获取订单所属用户信息 Direction d = entity.OrderType == (int)OrderType.提货单 ? Direction.提货单 : Direction.卖单; #region 合法性验证 //获取用户交割单各物品总重量 Dictionary<GoodsType, GoodsEntity> goodsDic = GetGoodsDic(entity.Account, d); //判断用户交割单种类是否小于0 或者小于提交的种类数量 if (goodsDic.Count <= 0 || goodsDic.Count < dataMember.Count) return false; //判断用户物品数量是否与交割物品数量相等 2013/4/6 if (!CheckTotal(order, stockEntity)) return false; //循环判断提交重量是否大于用户交割单总重量 foreach (var m in dataMember) { foreach (var g in goodsDic.Values) { if (m.field.ToLower() == g.Symbol.ToString().ToLower() && Convert.ToDecimal(m.value) > g.Total) { return false; } else { if (m.field.ToLower() == "au") { entity.Au = Convert.ToDecimal(m.value); updateList.Add(new UpdateDataMember { field = "Au", value = m.value }); updateList.Add(new UpdateDataMember { field = "AuP", value = stockEntity.AuPrice }); if (Convert.ToDecimal(m.value) > stockEntity.AuAmount) { updateList.Add(new UpdateDataMember { field = "AuQuantity", value = stockEntity.AuAmount }); } else { updateList.Add(new UpdateDataMember { field = "AuQuantity", value = m.value }); } break; } if (m.field.ToLower() == "ag") { entity.Ag = Convert.ToDecimal(m.value); updateList.Add(new UpdateDataMember { field = "Ag", value = m.value }); updateList.Add(new UpdateDataMember { field = "AgP", value = stockEntity.AgPrice }); if (Convert.ToDecimal(m.value) > stockEntity.AuAmount) { updateList.Add(new UpdateDataMember { field = "AgQuantity", value = stockEntity.AgAmount }); } else { updateList.Add(new UpdateDataMember { field = "AgQuantity", value = m.value }); } break; } if (m.field.ToLower() == "pt") { entity.Pt = Convert.ToDecimal(m.value); updateList.Add(new UpdateDataMember { field = "Pt", value = m.value }); updateList.Add(new UpdateDataMember { field = "PtP", value = stockEntity.PtPrice }); if (Convert.ToDecimal(m.value) > stockEntity.AuAmount) { updateList.Add(new UpdateDataMember { field = "PtQuantity", value = stockEntity.PtAmount }); } else { updateList.Add(new UpdateDataMember { field = "PtQuantity", value = m.value }); } break; } if (m.field.ToLower() == "pd") { entity.Pd = Convert.ToDecimal(m.value); updateList.Add(new UpdateDataMember { field = "Pd", value = m.value }); updateList.Add(new UpdateDataMember { field = "PdP", value = stockEntity.AuPrice }); if (Convert.ToDecimal(m.value) > stockEntity.AuAmount) { updateList.Add(new UpdateDataMember { field = "PdQuantity", value = stockEntity.AuAmount }); } else { updateList.Add(new UpdateDataMember { field = "PdQuantity", value = m.value }); } break; } } } } #endregion //获取交割单信息 List<DeliverEntity> list = GetListByAccount(entity.Account, d, 1); if (list == null || list.Count <= 0) return false; #region 计算交割单信息 Dictionary<GoodsType, decimal> dic = new Dictionary<GoodsType, decimal>(); dic.Add(GoodsType.Au, entity.Au); dic.Add(GoodsType.Ag, entity.Ag); dic.Add(GoodsType.Pd, entity.Pd); dic.Add(GoodsType.Pt, entity.Pt); foreach (var key in dic.Keys) { var tmp = list.Where(m => m.Goods.Equals(key)).ToList(); var values = dic[key]; if (values <= 0) continue; foreach (var mm in tmp) { DeliverRecordEntity recordEntity = new DeliverRecordEntity { DeliverId = mm.DeliverId, DeliverNo = mm.DeliverNo, Goods = mm.Goods, Direction = mm.Direction, LockPrice = mm.LockPrice, OrderType = order.OrderType, OrderId = entity.OrderId, OrderNo = entity.OrderNo }; if (values > mm.AvailableTotal) { recordEntity.UseTotal = mm.AvailableTotal; values -= mm.AvailableTotal; mm.AvailableTotal = 0; mm.State = 0; deliverList.Add(mm); deliverRecordList.Add(recordEntity); } else { mm.State = values.Equals(mm.AvailableTotal) ? 0 : 1; recordEntity.UseTotal = values; mm.AvailableTotal -= values; deliverList.Add(mm); deliverRecordList.Add(recordEntity); break; } } } #endregion #region 部分实体赋值 AgentDeliverEntity agentDeliverEntity = new AgentDeliverEntity { AgentInfoId = agentId, OrderId = entity.OrderId, FromTo = 1, Direction = d, Ag = entity.Ag, Au = entity.Au, Pd = entity.Pd, Pt = entity.Pt }; #endregion updateList.Add(new UpdateDataMember { field = "State", value = "2" }); updateList.Add(new UpdateDataMember { field = "Version", value = (entity.Version + 1).ToString() }); updateList.Add(new UpdateDataMember { field = "EndDate", value = DateTime.Now.ToString() }); UserLogEntity userLogEntity = new UserLogEntity(); userLogEntity.Account = agentId; userLogEntity.DESC = string.Format(@"金商{5},用户{0}提货:Ag:{1},Au:{2},Pt:{3},Pd:{4};", entity.Account, entity.Au, entity.Ag, entity.Pt, entity.Pd, entity.AgentName); userLogEntity.UserType = (int)LogType.用户库存调整减少; operationEntity.OrderId = entity.OrderId; operationEntity.OrderNo = entity.OrderNo; YicelTransaction tran = new YicelTransaction(); try { tran.BeginTransaction(); UpdateColumns(entity.OrderId, updateList, tran); CreateOrderOperation(operationEntity, tran);//创建订单操作记录 deliverList.ForEach(m => UpdateDeliver(m, entity, operationEntity.OperationId, tran));//更新交割单信息 deliverRecordList.ForEach(m => CreateDeliverRecord(m, tran));//创建订单对应交割单信息 UpdateUserTotal(entity, stockEntity, tran);//更新用户库存数量 CreateAgent(agentDeliverEntity, tran);//创建金商库存 CreateLog(userLogEntity, tran); tran.Commit(); return true; } catch { tran.Rollback(); return false; } }
/// <summary> /// 金商库存 /// </summary> /// <param name="entity"></param> /// <param name="tran"></param> /// <returns></returns> public static bool CreateAgent(AgentDeliverEntity entity, YicelTransaction tran) { entity.AgentDeliverId = Guid.NewGuid().ToString("n"); entity.CreateDate = DateTime.Now.ToString(); entity.State = 1; entity.AvailableAg = entity.Ag; entity.AvailableAu = entity.Au; entity.AvailablePd = entity.Pd; entity.AvailablePt = entity.Pt; UpdateAgentTotal(entity.AgentInfoId, entity.Direction, GoodsType.Au, entity.Au, tran); UpdateAgentTotal(entity.AgentInfoId, entity.Direction, GoodsType.Ag, entity.Ag, tran); UpdateAgentTotal(entity.AgentInfoId, entity.Direction, GoodsType.Pt, entity.Pt, tran); UpdateAgentTotal(entity.AgentInfoId, entity.Direction, GoodsType.Pd, entity.Pd, tran); CreateAgentDeliver(entity, tran); return true; }
/// <summary> /// 金商交割单 /// </summary> /// <param name="entity"></param> /// <param name="tran"></param> /// <returns></returns> public static int CreateAgentDeliver(AgentDeliverEntity entity, YicelTransaction tran) { SqlParameter[] parms = new SqlParameter[] { new SqlParameter("@AgentDeliverId", entity.AgentDeliverId), new SqlParameter("@AgentInfoId", entity.AgentInfoId), new SqlParameter("@FromTo", entity.FromTo), new SqlParameter("@OrderId", entity.OrderId), new SqlParameter("@Direction", (int)entity.Direction), new SqlParameter("@Au", entity.Au), new SqlParameter("@Ag", entity.Ag), new SqlParameter("@Pt", entity.Pt), new SqlParameter("@Pd", entity.Pd), new SqlParameter("@AvailableAu", entity.AvailableAu), new SqlParameter("@AvailableAg", entity.AvailableAg), new SqlParameter("@AvailablePt", entity.AvailablePt), new SqlParameter("@AvailablePd", entity.AvailablePd), new SqlParameter("@CreateDate", entity.CreateDate), new SqlParameter("@State", entity.State) }; StringBuilder strSql = new StringBuilder(); strSql.AppendFormat("insert into AgentDeliver_BZJ({0})", Fields.Agent_FIELD_LIST); strSql.AppendFormat(" values ({0})", "@" + Fields.Agent_FIELD_LIST.Replace(",", ",@")); object obj = DbHelper.ExecuteNonQuery(strSql.ToString(), parms, tran.Transaction); return obj == null || obj == DBNull.Value ? 0 : Convert.ToInt32(obj); }