Пример #1
0
    private MFReturnValue <bool> Reduce(int roleId, int roleBackpackId, int goodsType, int goodsId, int goodsServerId, int count, GoodsOutType outType)
    {
        MFReturnValue <bool> retValue = new MFReturnValue <bool>();

        //1.查询物品的售价 物品的已有数量
        int  sellPrice = 0;     //售价
        int  maxAmount = 0;     //叠加数量
        int  hasCount  = 0;     //已有数量
        bool isCanSell = false; //是否可以销售

        StringBuilder sbrSql    = new StringBuilder();
        StringBuilder sbrSqlLog = new StringBuilder();

        #region 查询 售价 叠加数量 已有数量
        switch ((GoodsType)goodsType)
        {
        case GoodsType.Equip:
            EquipEntity equipEntity = EquipDBModel.Instance.Get(goodsId);
            if (equipEntity != null)
            {
                sellPrice = equipEntity.SellMoney;
                maxAmount = 1;
            }

            sbrSql.AppendFormat("[Status]=1 and [EquipId]={0}", goodsId);
            if (goodsServerId > 0)
            {
                sbrSql.AppendFormat(" and [Id]={0}", goodsServerId);
            }

            //已有数量
            hasCount = Role_EquipDBModel.Instance.GetCount(sbrSql.ToString());

            if (hasCount >= count)
            {
                isCanSell = true;
            }
            break;

        case GoodsType.Item:
            ItemEntity itemEntity = ItemDBModel.Instance.Get(goodsId);
            if (itemEntity != null)
            {
                sellPrice = itemEntity.SellMoney;
                maxAmount = itemEntity.maxAmount;
            }

            sbrSql.AppendFormat("[Status]=1 and [ItemId]={0}", goodsId);
            if (goodsServerId > 0 && maxAmount == 1)
            {
                sbrSql.AppendFormat(" and [Id]={0}", goodsServerId);
            }

            hasCount = Role_ItemDBModel.Instance.GetCount(sbrSql.ToString());

            if (hasCount >= count)
            {
                isCanSell = true;
            }

            break;

        case GoodsType.Material:
            MaterialEntity materialEntity = MaterialDBModel.Instance.Get(goodsId);
            if (materialEntity != null)
            {
                sellPrice = materialEntity.SellMoney;
                maxAmount = materialEntity.maxAmount;
            }

            sbrSql.AppendFormat("[Status]=1 and [MaterialId]={0}", goodsId);
            if (goodsServerId > 0 && maxAmount == 1)
            {
                sbrSql.AppendFormat(" and [Id]={0}", goodsServerId);
            }

            hasCount = Role_MaterialDBModel.Instance.GetCount(sbrSql.ToString());

            if (hasCount >= count)
            {
                isCanSell = true;
            }

            break;
        }
        #endregion

        //2.核对玩家拥有的物品数量是否满足出售数量
        if (!isCanSell)
        {
            retValue.HasError   = true;
            retValue.ReturnCode = -1; //数量不足
            return(retValue);
        }

        Log_GoodsInEntity  logInEntity;
        Log_GoodsOutEntity logOutEntity;
        int currGoodsServerId = 0;

        int currSellCount = count;

        //3.从对应的物品表中移除
        #region 从对应的物品表中移除
        while (currSellCount > 0)
        {
            sbrSql.Clear();
            sbrSqlLog.Clear();

            //从对应的物品表中删除
            switch ((GoodsType)goodsType)
            {
            case GoodsType.Equip:
                sbrSql.AppendFormat("[Status]=1 and [EquipId]={0}", goodsId);
                if (goodsServerId > 0)
                {
                    sbrSql.AppendFormat(" and [Id]={0}", goodsServerId);
                }
                Role_EquipEntity equipEntity = Role_EquipDBModel.Instance.GetEntity(sbrSql.ToString());
                Role_EquipDBModel.Instance.Delete(equipEntity.Id);     //删除物品
                currGoodsServerId = equipEntity.Id.Value;
                break;

            case GoodsType.Item:
                sbrSql.AppendFormat("[Status]=1 and [ItemId]={0}", goodsId);
                if (goodsServerId > 0 && maxAmount == 1)
                {
                    sbrSql.AppendFormat(" and [Id]={0}", goodsServerId);
                }
                Role_ItemEntity itemEntity = Role_ItemDBModel.Instance.GetEntity(sbrSql.ToString());
                Role_ItemDBModel.Instance.Delete(itemEntity.Id);     //删除物品
                currGoodsServerId = itemEntity.Id.Value;
                break;

            case GoodsType.Material:
                sbrSql.AppendFormat("[Status]=1 and [MaterialId]={0}", goodsId);
                if (goodsServerId > 0 && maxAmount == 1)
                {
                    sbrSql.AppendFormat(" and [Id]={0}", goodsServerId);
                }
                Role_MaterialEntity materialEntity = Role_MaterialDBModel.Instance.GetEntity(sbrSql.ToString());
                Role_MaterialDBModel.Instance.Delete(materialEntity.Id);     //删除物品
                currGoodsServerId = materialEntity.Id.Value;
                break;
            }

            sbrSqlLog.AppendFormat("[Status]=1 and [RoleId]={0} and [GoodsType]={1} and [GoodsId]={2} and [GoodsServerId]={3}",
                                   roleId, goodsType, goodsId, currGoodsServerId);

            //添加出库日志
            logInEntity = Log_GoodsInDBModel.Instance.GetEntity(sbrSqlLog.ToString()); //获取对应的入库日志

            logOutEntity               = new Log_GoodsOutEntity();
            logOutEntity.Status        = Mmcoy.Framework.AbstractBase.EnumEntityStatus.Released;
            logOutEntity.RoleId        = roleId;
            logOutEntity.Type          = (byte)outType;
            logOutEntity.GoodsType     = (byte)goodsType;
            logOutEntity.GoodsId       = goodsId;
            logOutEntity.GoodsServerId = currGoodsServerId;
            logOutEntity.LogGoodsInId  = logInEntity.Id.Value;
            logOutEntity.CreateTime    = DateTime.Now;
            logOutEntity.UpdateTime    = DateTime.Now;

            Log_GoodsOutDBModel.Instance.Create(logOutEntity);

            currSellCount--;
        }
        #endregion

        //4.修改或者删除对应的背包项
        currSellCount = count;

        //背包项改变列表
        List <Role_BackpackItemChangeEntity> lst = new List <Role_BackpackItemChangeEntity>();

        //roleBackpackId 更新背包项的时候,优先从这个背包项更新(修改或者删除)
        while (currSellCount > 0)
        {
            //roleBackpackId 第一次减的时候,这个背包项是肯定存在的

            //出售50个 叠加是20 当前背包项13个

            //1.先从传递过来的背包项中删除物品数量
            Role_BackpackEntity backpackEntity = null;
            if (roleBackpackId > 1)
            {
                //获取传递过来的背包项
                backpackEntity = this.GetEntity(roleBackpackId);
                roleBackpackId = 0;
            }
            else
            {
                //获取其他背包项
                backpackEntity = this.GetEntity(string.Format("[Status]=1 and [RoleId]={0} and [GoodsType]={1} and [GoodsId]={2}",
                                                              roleId, goodsType, goodsId));
            }

            if (currSellCount >= backpackEntity.GoodsOverlayCount)
            {
                //如果出售的数量 大于等于当前的背包项数量 则把这个背包项删除
                currSellCount -= backpackEntity.GoodsOverlayCount;

                //删除背包项目
                this.Delete(backpackEntity.Id);

                //添加到背包项改变列表
                lst.Add(new Role_BackpackItemChangeEntity()
                {
                    BackpackId    = backpackEntity.Id.Value,
                    Type          = BackpackItemChangeType.Delete,
                    GoodsType     = (GoodsType)goodsType,
                    GoodsId       = goodsId,
                    GoodsServerId = backpackEntity.GoodsServerId,
                    GoodsCount    = 0
                });
            }
            else
            {
                //如果出售的数量小于 当前背包项的数量 则从当前背包项中 减去出售的数量
                backpackEntity.GoodsOverlayCount -= currSellCount;
                this.Update(backpackEntity);

                //更新背包项目
                currSellCount = 0;

                //添加到背包项改变列表
                lst.Add(new Role_BackpackItemChangeEntity()
                {
                    BackpackId    = backpackEntity.Id.Value,
                    Type          = BackpackItemChangeType.Update,
                    GoodsType     = (GoodsType)goodsType,
                    GoodsId       = goodsId,
                    GoodsServerId = backpackEntity.GoodsServerId,
                    GoodsCount    = backpackEntity.GoodsOverlayCount
                });
            }
        }

        retValue.SetOutputValue("BackpackItemChange", lst);
        retValue.SetOutputValue("TotalSellPrice", sellPrice * count); //总售价 = 单价 * 数量

        return(retValue);
    }
Пример #2
0
 /// <summary>
 /// 修改
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public MFReturnValue <object> Update(Log_GoodsOutEntity entity)
 {
     return(this.DBModel.Update(entity));
 }