ImportOrderJob.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Jobs\Dwbs;
  3. use App\Jobs\Job;
  4. use App\Repositories\Enums\Dwbs\UserJifenTypeEnum;
  5. use App\Repositories\Enums\ModelStatusEnum;
  6. use App\Repositories\Models\Base\Setting;
  7. use App\Repositories\Models\Base\User;
  8. use App\Repositories\Models\Dwbs\Good;
  9. use App\Repositories\Models\Dwbs\Order;
  10. use App\Repositories\Models\Dwbs\OrderGood;
  11. use App\Repositories\Models\Dwbs\UserJifen;
  12. use App\Repositories\Models\Dwbs\UserXuefen;
  13. use Carbon\Carbon;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\DB;
  16. class ImportOrderJob extends Job
  17. {
  18. protected $data;
  19. public function __construct($data)
  20. {
  21. $this->data = $data;
  22. }
  23. public function handle()
  24. {
  25. /*
  26. * data示例
  27. * {"Type":1,"RetailID":134,"Code":"2024102947561000002","Domain":"http://hnsystemse.njjinhao.top/","data":[{"ProductGroupID":9,"ProductGroupSKUID":9,"ProductName":"大卫博士罐装内裤(青春版)","ProductCode":"青春版","SKUCode":"青春版","AttributesVales":[],"Qty":2}]}
  28. * */
  29. $data = $this->data;
  30. $arr = json_decode($data, true);
  31. if (!isset($arr['AuthorizationCode'])) {
  32. self::error('no AuthorizationCode', $arr);
  33. return;
  34. }
  35. $authCode = $arr['AuthorizationCode'];//用户code,唯一标识
  36. if (!isset($arr['Code'])) {
  37. self::error('no Code', $arr);
  38. return;
  39. }
  40. $orderNo = $arr['Code'];//零售单号
  41. if (!isset($arr['data'])) {
  42. self::error('no data', $arr);
  43. return;
  44. }
  45. $orderGoods = $arr['data'];
  46. //todo:检测是否重复进入
  47. $cache_key = "job:importOrder:{$orderNo}";
  48. if (Cache::get($cache_key, 0)) {
  49. return;
  50. }
  51. $user = User::query()->where('code', $authCode)->first();
  52. if (!$user) {
  53. self::error('no User', $arr);
  54. return;
  55. }
  56. $day = date('Y-m-d');
  57. $orderTime = Carbon::now()->toDateTimeString();
  58. //测试
  59. if (isset($arr['OrderTime'])) {
  60. $day = Carbon::parse($arr['OrderTime'])->format('Y-m-d');
  61. $orderTime = $arr['OrderTime'];
  62. }
  63. $orderData = [
  64. 'user_id' => $user['id'],
  65. 'order_no' => $orderNo,
  66. 'order_time' => $orderTime,
  67. 'day' => $day,
  68. 'xuefen' => 0,
  69. 'jifen' => 0,
  70. 'status' => ModelStatusEnum::OK
  71. ];
  72. DB::beginTransaction();
  73. try {
  74. $orderModel = Order::query()->create($orderData);
  75. //获取学分兑换积分的比率
  76. $xuefenToJifen = Setting::byCodeGetSetting('h5_base_xuefen_to_jifen');
  77. //学分和积分目前是按照1:1
  78. $xuefenAll = 0;
  79. $jifenAll = 0;
  80. foreach ($orderGoods as $good) {
  81. //todo:应该不全吧
  82. $goodsName = $good['ProductCode'];//商品名字
  83. $goodsNum = $good['Qty'];//数量
  84. $goodsInfo = Good::query()->where('name', $goodsName)->first();//商品信息
  85. if (!$goodsInfo) {
  86. self::error("商品找不到[{$goodsName}]", $arr);
  87. continue;
  88. }
  89. $goodsInfo->nums += $goodsNum;
  90. $goodsInfo->save();
  91. $integral = $goodsInfo['xuefen'];//此商品的学分
  92. $xuefen = $integral * $goodsNum;
  93. $xuefenAll += $xuefen;//应获得学分
  94. $jifen = $xuefen * $xuefenToJifen;
  95. $jifenAll += $jifen;
  96. //订单详情插入
  97. OrderGood::query()->create([
  98. 'order_id' => $orderModel['id'],
  99. 'good_id' => $goodsInfo['id'],
  100. 'user_id' => $user['id'],
  101. 'order_time' => $orderTime,
  102. 'nums' => $goodsNum,
  103. 'xuefen' => $xuefen,
  104. 'jifen' => $jifen,
  105. 'status' => ModelStatusEnum::OK,
  106. ]);
  107. }
  108. $orderModel->xuefen = $xuefenAll;//此单应获得学分
  109. $orderModel->jifen = $jifenAll;//积分
  110. $orderModel->save();
  111. $user->xuefen += $xuefenAll;
  112. $user->jifen += $jifenAll;
  113. $user->last_update_time = $orderTime;
  114. $user->save();
  115. //学分记录
  116. $this->xuefen($user['id'], $orderModel['id'], $user['xuefen'], $xuefenAll, $day);
  117. //积分记录
  118. $this->jifen($user['id'], $orderModel['id'], $user['jifen'], $jifenAll, $day);
  119. User::refreshUserTag($user->id);
  120. //todo:缺少用户消息通知
  121. DB::commit();
  122. Cache::put($cache_key, true, Carbon::now()->addHour());
  123. } catch (\Exception $exception) {
  124. DB::rollBack();
  125. self::error('异常错误', $arr);
  126. log_record('订单接收', $exception);
  127. }
  128. }
  129. /**
  130. * 积分记录
  131. * @param $userId
  132. * @param $orderId
  133. * @param $integral
  134. * @param $integralAll
  135. * @param $day
  136. * @return void
  137. */
  138. protected function jifen($userId, $orderId, $integral, $integralAll, $day)
  139. {
  140. $Bonus = new UserJifen();
  141. $Bonus->user_id = $userId;
  142. $Bonus->source_id = $orderId;
  143. $Bonus->source_type = Order::class;
  144. $Bonus->type = UserJifenTypeEnum::xuefenduihuan;
  145. $Bonus->day = $day;
  146. $Bonus->user_jifen = $integral;
  147. $Bonus->jifen = $integralAll;
  148. $Bonus->save();
  149. }
  150. /**
  151. * 学分记录
  152. * @param $userId
  153. * @param $orderId
  154. * @param $integral
  155. * @param $integralAll
  156. * @param $day
  157. * @return void
  158. */
  159. protected function xuefen($userId, $orderId, $integral, $integralAll, $day)
  160. {
  161. $integralM = new UserXuefen();
  162. $integralM->user_id = $userId;
  163. $integralM->source_id = $orderId;
  164. $integralM->source_type = Order::class;
  165. $integralM->day = $day;
  166. $integralM->user_xuefen = $integral;
  167. $integralM->xuefen = $integralAll;
  168. $integralM->save();
  169. }
  170. /**
  171. * 异常错误
  172. * @param $position
  173. * @param $data
  174. * @return void
  175. */
  176. protected static function error($position, $data)
  177. {
  178. //todo:建议插入数据库
  179. log_record($position, $data);
  180. }
  181. }