GiftController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. class GiftController extends Controller
  6. {
  7. //
  8. public function list(Request $request) {
  9. $objUser = unserialize($request->get("account"));
  10. //
  11. $objGift = \App\Models\Gift::with(['attire'])->OrderBy("sort")->get()->toArray();
  12. $return = [];
  13. foreach($objGift as $v) {
  14. // 按照自然月计算
  15. if($v['type'] == 1) {
  16. $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id)
  17. ->where("gift_id", $v['id'])
  18. ->where("created_at",">=", date("Y-m-01 00:00:00", strtotime("first day of last month")))
  19. ->sum('num');
  20. } else {
  21. $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id)->where("gift_id", $v['id'])->sum('num');
  22. }
  23. //
  24. $stillCnt = $v['limit'] - $cnt;
  25. if($stillCnt <= 0) {
  26. $stillCnt = 0;
  27. }
  28. //
  29. if($v['type'] == 1) {
  30. $tmp = [
  31. 'id' => $v['id'],
  32. 'name' => '礼劵',
  33. 'img' => \Storage::disk('cosv5')->url($v['img']),
  34. 'cost' => $v['cost_pasters'],
  35. 'info' => $v['info'],
  36. 'limit' => $stillCnt,
  37. ];
  38. } elseif($v['type'] == 2) {
  39. $tmp = [
  40. 'id' => $v['id'],
  41. 'name' => $v['attire']['name'],
  42. 'img' => \Storage::disk('cosv5')->url($v['attire']['img_1']),
  43. 'cost' => $v['cost_pasters'],
  44. 'info' => $v['info'],
  45. 'limit' => $stillCnt,
  46. ];
  47. }
  48. $return[] = $tmp;
  49. }
  50. return [
  51. 'errno'=> 10000,
  52. "errmsg" => 'ok',
  53. 'data' => $return,
  54. ];
  55. }
  56. // 兑换接口
  57. public function exchange(Request $request) {
  58. $objUser = unserialize($request->get("account"));
  59. //
  60. $giftId = $request->input("id", 0);
  61. $num = $request->input("num", 0);
  62. if(!$giftId || !$num) {
  63. return [
  64. 'errno'=> 10001,
  65. "errmsg" => '参数错误',
  66. ];
  67. }
  68. $objGift = \App\Models\Gift::where("id", $giftId)->with(['attire'])->first();
  69. //
  70. if(!$objGift) {
  71. return [
  72. 'errno'=> 10002,
  73. "errmsg" => '参数错误',
  74. ];
  75. }
  76. if($objGift->limit < $num) {
  77. return [
  78. 'errno'=> 10005,
  79. "errmsg" => '参数错误',
  80. ];
  81. }
  82. //
  83. if($objGift->type == 1) {
  84. $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id)
  85. ->where("gift_id", $giftId)
  86. ->where("created_at",">=", date("Y-m-01 00:00:00", strtotime("first day of last month")))
  87. ->count();
  88. } else {
  89. $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id)->where("gift_id", $giftId)->count();
  90. }
  91. // $cnt = \App\Models\GiftRecord::where("account_id", $objUser->id)->where("gift_id", $giftId)->count();
  92. $stillCnt = $objGift->limit - $cnt;
  93. if($stillCnt <= 0) {
  94. $stillCnt = 0;
  95. }
  96. if($num > $stillCnt) {
  97. return [
  98. 'errno'=> 10003,
  99. "errmsg" => '超出剩余限购次数('.$stillCnt.')',
  100. ];
  101. }
  102. if($objUser->paster < $objGift->cost_pasters*$num) {
  103. return [
  104. 'errno'=> 10004,
  105. "errmsg" => '剩余贴纸数('.$objUser->paster.')不足,无法兑换',
  106. ];
  107. }
  108. // 如果试衣间已经有了,不能进行兑换
  109. if($objGift->type == 2) {
  110. $tmp = \App\Models\FittingRoom::where("account_id", $objUser->id)->where("attire_id", $objGift->attire_id)->first();
  111. if($tmp) {
  112. return [
  113. 'errno'=> 10006,
  114. "errmsg" => '已经拥有这个装扮,兑换失败',
  115. ];
  116. }
  117. }
  118. // 使用事务进行处理
  119. DB::transaction(function () use($objUser, $objGift, $num) {
  120. $costPaster = $objGift->cost_pasters*$num;
  121. //
  122. DB::table('accounts')
  123. ->where('id', $objUser->id)
  124. ->decrement('paster', $costPaster);
  125. //
  126. if($objGift->type == 1) {
  127. $singleCoupon = 1;
  128. DB::table('accounts')
  129. ->where('id', $objUser->id)
  130. ->increment('coupon', $singleCoupon*$num);
  131. } elseif($objGift->type == 2) {
  132. $fittimgRooms = [
  133. "account_id" => $objUser->id,
  134. "attire_id" => $objGift->attire_id,
  135. 'created_at' =>date("Y-m-d H:i:s"),
  136. 'updated_at' =>date("Y-m-d H:i:s"),
  137. ];
  138. \App\Models\FittingRoom::insertOrIgnore($fittimgRooms);
  139. }
  140. // 礼品记录表
  141. $objGiftRecord = new \App\Models\GiftRecord();
  142. $objGiftRecord->account_id = $objUser->id;
  143. $objGiftRecord->gift_id = $objGift->id;
  144. $objGiftRecord->num = $num;
  145. $objGiftRecord->cost_paster = $costPaster;
  146. $objGiftRecord->save();
  147. // 贴纸明细表
  148. $objPasterRecord = new \App\Models\PasterRecord();
  149. $objPasterRecord->account_id = $objUser->id;
  150. $objPasterRecord->add_cnt = -1*$costPaster;
  151. $objPasterRecord->type = 2;
  152. $objPasterRecord->gift_record_id = $objGiftRecord->id;
  153. $objPasterRecord->day = date("Y-m-d");
  154. $objPasterRecord->save();
  155. // 礼劵明细记录
  156. if($objGift->type==1) {
  157. $objCouponRecord = new \App\Models\CouponRecord();
  158. $objCouponRecord->account_id = $objUser->id;
  159. $objCouponRecord->add_cnt = $singleCoupon*$num;
  160. $objCouponRecord->type = 4;
  161. $objCouponRecord->gift_record_id = $objGiftRecord->id;
  162. $objCouponRecord->day = date("Y-m-d");
  163. $objCouponRecord->save();
  164. }
  165. });
  166. return [
  167. 'errno'=> 10000,
  168. "errmsg" => 'ok',
  169. ];
  170. }
  171. }