Exemple #1
0
        /// <summary>
        ///  订单退款
        /// </summary>
        /// <param name="item"></param>
        /// <param name="oldState"></param>
        /// <param name="BuyMode">默认微信支付</param>
        /// <param name="isPartOut">是否部分退款</param>
        /// <returns></returns>
        public bool EntReFundQueue(EntGoodsOrder item, int oldState, int BuyMode = (int)miniAppBuyMode.微信支付, int?newState = null, bool isPartOut = false)
        {
            //重新加回库存
            if (EntGoodsOrderBLL.SingleModel.updateStock(item, oldState))
            {
                int money = isPartOut ? item.refundFee : item.BuyPrice;//兼容多版本,目前只有专业版订单有部分退款
                item.refundFee = money;
                if (BuyMode == (int)miniAppBuyMode.微信支付)
                {
                    try
                    {
                        item.outOrderDate = DateTime.Now;
                        if (item.BuyPrice == 0)  //金额为0时,回滚库存后,默认退款成功
                        {
                            item.State = (int)MiniAppEntOrderState.退款成功;
                        }
                        else
                        {
                            CityMorders order = _cityMordersBLL.GetModel(item.OrderId);
                            item.State = (int)MiniAppEntOrderState.退款中;
                            if (newState.HasValue)
                            {
                                item.State = newState.Value;
                            }
                            if (order == null)
                            {
                                item.State = (int)MiniAppEntOrderState.退款失败;
                                EntGoodsOrderBLL.SingleModel.Update(item, "State,outOrderDate,Remark,refundFee");
                                return(false);
                            }
                            //微信支付
                            ReFundQueue reModel = new ReFundQueue
                            {
                                minisnsId = -5,
                                money     = item.refundFee,
                                orderid   = order.Id,
                                traid     = order.trade_no,
                                addtime   = DateTime.Now,
                                note      = "小程序行业版退款",
                                retype    = 1
                            };
                            base.Add(reModel);
                        }
                        bool isSuccess = EntGoodsOrderBLL.SingleModel.Update(item, "State,outOrderDate,Remark,refundFee");
                        if (isSuccess)
                        {
                            //发给用户退款通知
                            object orderData = TemplateMsg_Miniapp.EnterpriseGetTemplateMessageData(item, SendTemplateMessageTypeEnum.专业版订单退款通知, "商家操作退款");
                            TemplateMsg_Miniapp.SendTemplateMessage(item.UserId, SendTemplateMessageTypeEnum.专业版订单退款通知, TmpType.小程序专业模板, orderData);
                        }
                    }
                    catch (Exception ex)
                    {
                        log4net.LogHelper.WriteInfo(GetType(), $"{ex.Message} xxxxxxxxxxxxxxxx小程序餐饮退款订单插入队列失败 ID={item.Id}");
                    }
                }
                else
                {
                    XcxAppAccountRelation r = XcxAppAccountRelationBLL.SingleModel.GetModel(item.aId);
                    if (r == null)
                    {
                        return(false);
                    }

                    SaveMoneySetUser saveMoneyUser = SaveMoneySetUserBLL.SingleModel.getModelByUserId(r.AppId, item.UserId);
                    TransactionModel tran          = new TransactionModel();
                    tran.Add(SaveMoneySetUserLogBLL.SingleModel.BuildAddSql(new SaveMoneySetUserLog()
                    {
                        AppId          = r.AppId,
                        UserId         = item.UserId,
                        MoneySetUserId = saveMoneyUser.Id,
                        Type           = 1,
                        BeforeMoney    = saveMoneyUser.AccountMoney,
                        AfterMoney     = saveMoneyUser.AccountMoney + item.refundFee,
                        ChangeMoney    = item.refundFee,
                        ChangeNote     = $"专业版购买商品退款,订单号:{item.OrderNum} ",
                        CreateDate     = DateTime.Now,
                        State          = 1
                    }));

                    item.State = (int)MiniAppEntOrderState.退款成功;
                    if (newState.HasValue)
                    {
                        item.State = newState.Value;
                    }
                    tran.Add($" update SaveMoneySetUser set AccountMoney = AccountMoney + {item.refundFee} where id =  {saveMoneyUser.Id} ; ");
                    tran.Add($" update EntGoodsOrder set State = {item.State },outOrderDate = '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}',Remark = @Remark where Id = {item.Id} and state <> {item.State} ; ",
                             new MySqlParameter[] { new MySqlParameter("@Remark", item.Remark) });//防止重复退款

                    //记录订单储值支付退款日志
                    tran.Add(EntGoodsOrderLogBLL.SingleModel.BuildAddSql(new EntGoodsOrderLog()
                    {
                        GoodsOrderId = item.Id, UserId = item.UserId, LogInfo = $" 储值支付订单退款成功:{item.refundFee * 0.01} 元 ", CreateDate = DateTime.Now
                    }));
                    bool isSuccess = ExecuteTransaction(tran.sqlArray, tran.ParameterArray);

                    if (isSuccess)
                    {
                        //发给用户退款通知
                        object orderData = TemplateMsg_Miniapp.EnterpriseGetTemplateMessageData(item, SendTemplateMessageTypeEnum.专业版订单退款通知, "商家操作退款");
                        TemplateMsg_Miniapp.SendTemplateMessage(item.UserId, SendTemplateMessageTypeEnum.专业版订单退款通知, TmpType.小程序专业模板, orderData);
                    }
                    return(isSuccess);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }