get("account")); // $objGift = \App\Models\Gift::with(['attire'])->OrderBy("sort")->get()->toArray(); $return = []; foreach($objGift as $v) { // 按照自然月计算 if($v['type'] == 1) { $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id) ->where("gift_id", $v['id']) ->where("created_at",">=", date("Y-m-01 00:00:00", strtotime("first day of last month"))) ->sum('num'); } else { $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id)->where("gift_id", $v['id'])->sum('num'); } // $stillCnt = $v['limit'] - $cnt; if($stillCnt <= 0) { $stillCnt = 0; } // if($v['type'] == 1) { $tmp = [ 'id' => $v['id'], 'name' => '礼劵', 'img' => \Storage::disk('cosv5')->url($v['img']), 'cost' => $v['cost_pasters'], 'info' => $v['info'], 'limit' => $stillCnt, ]; } elseif($v['type'] == 2) { $tmp = [ 'id' => $v['id'], 'name' => $v['attire']['name'], 'img' => \Storage::disk('cosv5')->url($v['attire']['img_1']), 'cost' => $v['cost_pasters'], 'info' => $v['info'], 'limit' => $stillCnt, ]; } $return[] = $tmp; } return [ 'errno'=> 10000, "errmsg" => 'ok', 'data' => $return, ]; } // 兑换接口 public function exchange(Request $request) { $objUser = unserialize($request->get("account")); // $giftId = $request->input("id", 0); $num = $request->input("num", 0); if(!$giftId || !$num) { return [ 'errno'=> 10001, "errmsg" => '参数错误', ]; } $objGift = \App\Models\Gift::where("id", $giftId)->with(['attire'])->first(); // if(!$objGift) { return [ 'errno'=> 10002, "errmsg" => '参数错误', ]; } if($objGift->limit < $num) { return [ 'errno'=> 10005, "errmsg" => '参数错误', ]; } // if($objGift->type == 1) { $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id) ->where("gift_id", $giftId) ->where("created_at",">=", date("Y-m-01 00:00:00", strtotime("first day of last month"))) ->count(); } else { $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id)->where("gift_id", $giftId)->count(); } // $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id)->where("gift_id", $giftId)->count(); $stillCnt = $objGift->limit - $cnt; if($stillCnt <= 0) { $stillCnt = 0; } if($num > $stillCnt) { return [ 'errno'=> 10003, "errmsg" => '超出剩余限购次数('.$stillCnt.')', ]; } if($objUser->paster < $objGift->cost_pasters*$num) { return [ 'errno'=> 10004, "errmsg" => '剩余贴纸数('.$objUser->paster.')不足,无法兑换', ]; } // 如果试衣间已经有了,不能进行兑换 if($objGift->type == 2) { $tmp = \App\Models\FittingRoom::where("account_id", $objUser->id)->where("attire_id", $objGift->attire_id)->first(); if($tmp) { return [ 'errno'=> 10006, "errmsg" => '已经拥有这个装扮,兑换失败', ]; } } // 使用事务进行处理 DB::transaction(function () use($objUser, $objGift, $num) { $costPaster = $objGift->cost_pasters*$num; // DB::table('accounts') ->where('id', $objUser->id) ->decrement('paster', $costPaster); // if($objGift->type == 1) { $singleCoupon = 1; DB::table('accounts') ->where('id', $objUser->id) ->increment('coupon', $singleCoupon*$num); } elseif($objGift->type == 2) { $fittimgRooms = [ "account_id" => $objUser->id, "attire_id" => $objGift->attire_id, 'created_at' =>date("Y-m-d H:i:s"), 'updated_at' =>date("Y-m-d H:i:s"), ]; \App\Models\FittingRoom::insertOrIgnore($fittimgRooms); } // 礼品记录表 $objGiftRecord = new \App\Models\GiftRecord(); $objGiftRecord->account_id = $objUser->id; $objGiftRecord->gift_id = $objGift->id; $objGiftRecord->num = $num; $objGiftRecord->cost_paster = $costPaster; $objGiftRecord->save(); // 贴纸明细表 $objPasterRecord = new \App\Models\PasterRecord(); $objPasterRecord->account_id = $objUser->id; $objPasterRecord->add_cnt = -1*$costPaster; $objPasterRecord->type = 2; $objPasterRecord->gift_record_id = $objGiftRecord->id; $objPasterRecord->day = date("Y-m-d"); $objPasterRecord->save(); // 礼劵明细记录 if($objGift->type==1) { $objCouponRecord = new \App\Models\CouponRecord(); $objCouponRecord->account_id = $objUser->id; $objCouponRecord->add_cnt = $singleCoupon*$num; $objCouponRecord->type = 4; $objCouponRecord->gift_record_id = $objGiftRecord->id; $objCouponRecord->day = date("Y-m-d"); $objCouponRecord->save(); } }); return [ 'errno'=> 10000, "errmsg" => 'ok', ]; } }