public int PointExchangeCoupon(CustPointExChangeCouponModel model)
{
var guid = Guid.NewGuid().ToString();
const string sqlISent =
@"insert into coupon_sent(innerid, cardid, custid, isreceive, createdtime, receivetime, sourceid) values (uuid(), @cardid, @custid, 1, @createdtime, @receivetime, @sourceid);";
const string sqlIRecord =
@"insert into point_record (innerid, custid, `type`, sourceid, `point`, remark, validtime, createdtime) values (@innerid, @custid, 2, 100, @point, @remark, null, @createdtime);";
const string sqlIExChange =
@"insert into point_exchange (innerid, custid, recordid, `point`, `code`, createdtime) values (uuid(), @custid, @recordid, @point, @code, @createdtime);";
const string sqlICode =
@"insert into coupon_code (innerid, cardid, `code`, custid, gettime, sourceid, qrcode,vstart,vend) values (uuid(), @cardid, @code, @custid, @gettime, @sourceid, @qrcode,@vstart,@vend);";
const string sqlUCoupon = "update coupon_card set count=count-1 where innerid=@cardid;";
const string sqlUPoint = "update cust_total_info set currpoint=currpoint-@point,currpouponnum=currpouponnum+1 where custid=@custid;";
using (var conn = Helper.GetConnection())
{
var tran = conn.BeginTransaction();
try
{
//插入领取通知
conn.Execute(sqlISent, new
{
cardid = model.Cardid,
custid = model.Custid,
createdtime = model.Createdtime,
receivetime = model.Createdtime,
sourceid = model.Sourceid
}, tran);
//插入积分变更记录
conn.Execute(sqlIRecord, new
{
innerid = guid,
custid = model.Custid,
point = model.Point,
createdtime = model.Createdtime,
remark = model.Remark
}, tran);
//插入兑换礼券记录
conn.Execute(sqlIExChange, new
{
custid = model.Custid,
recordid = guid,
point = model.Point,
code = model.Code,
createdtime = model.Createdtime
}, tran);
//插入礼券code
conn.Execute(sqlICode, new
{
cardid = model.Cardid,
custid = model.Custid,
code = model.Code,
gettime = model.Createdtime,
sourceid = model.Sourceid,
qrcode = model.QrCode,
vstart = model.Vstart?.ToShortDateString(),
vend = model.Vend?.ToString("yyyy-MM-dd 23:23:59")
}, tran);
//更新卡券库存
conn.Execute(sqlUCoupon, new { cardid = model.Cardid }, tran);
//更新会员的积分
conn.Execute(sqlUPoint, new { custid = model.Custid, point = model.Point }, tran);
tran.Commit();
return 1;
}
catch (Exception ex)
{
LoggerFactories.CreateLogger().Write("购买礼券异常:", TraceEventType.Information, ex);
tran.Rollback();
return 0;
}
}
}