123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\Jobs\Dwbs;
- use App\Jobs\Job;
- use App\Repositories\Enums\Dwbs\UserJifenTypeEnum;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Base\Setting;
- use App\Repositories\Models\Base\User;
- use App\Repositories\Models\Dwbs\Good;
- use App\Repositories\Models\Dwbs\Order;
- use App\Repositories\Models\Dwbs\OrderGood;
- use App\Repositories\Models\Dwbs\UserJifen;
- use App\Repositories\Models\Dwbs\UserXuefen;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- class ImportOrderJob extends Job
- {
- protected $data;
- public function __construct($data)
- {
- $this->data = $data;
- }
- public function handle()
- {
- /*
- * data示例
- * {"Type":1,"RetailID":134,"Code":"2024102947561000002","Domain":"http://hnsystemse.njjinhao.top/","data":[{"ProductGroupID":9,"ProductGroupSKUID":9,"ProductName":"大卫博士罐装内裤(青春版)","ProductCode":"青春版","SKUCode":"青春版","AttributesVales":[],"Qty":2}]}
- * */
- $data = $this->data;
- $arr = json_decode($data, true);
- if (!isset($arr['AuthorizationCode'])) {
- self::error('no AuthorizationCode', $arr);
- return;
- }
- $authCode = $arr['AuthorizationCode'];//用户code,唯一标识
- if (!isset($arr['Code'])) {
- self::error('no Code', $arr);
- return;
- }
- $orderNo = $arr['Code'];//零售单号
- if (!isset($arr['data'])) {
- self::error('no data', $arr);
- return;
- }
- $orderGoods = $arr['data'];
- //todo:检测是否重复进入
- $cache_key = "job:importOrder:{$orderNo}";
- if (Cache::get($cache_key, 0)) {
- return;
- }
- $user = User::query()->where('code', $authCode)->first();
- if (!$user) {
- self::error('no User', $arr);
- return;
- }
- $day = date('Y-m-d');
- $orderTime = Carbon::now()->toDateTimeString();
- //测试
- if (isset($arr['OrderTime'])) {
- $day = Carbon::parse($arr['OrderTime'])->format('Y-m-d');
- $orderTime = $arr['OrderTime'];
- }
- $orderData = [
- 'user_id' => $user['id'],
- 'order_no' => $orderNo,
- 'order_time' => $orderTime,
- 'day' => $day,
- 'xuefen' => 0,
- 'jifen' => 0,
- 'status' => ModelStatusEnum::OK
- ];
- DB::beginTransaction();
- try {
- $orderModel = Order::query()->create($orderData);
- //获取学分兑换积分的比率
- $xuefenToJifen = Setting::byCodeGetSetting('h5_base_xuefen_to_jifen');
- //学分和积分目前是按照1:1
- $xuefenAll = 0;
- $jifenAll = 0;
- foreach ($orderGoods as $good) {
- //todo:应该不全吧
- $goodsName = $good['ProductCode'];//商品名字
- $goodsNum = $good['Qty'];//数量
- $goodsInfo = Good::query()->where('name', $goodsName)->first();//商品信息
- if (!$goodsInfo) {
- self::error("商品找不到[{$goodsName}]", $arr);
- continue;
- }
- $goodsInfo->nums += $goodsNum;
- $goodsInfo->save();
- $integral = $goodsInfo['xuefen'];//此商品的学分
- $xuefen = $integral * $goodsNum;
- $xuefenAll += $xuefen;//应获得学分
- $jifen = $xuefen * $xuefenToJifen;
- $jifenAll += $jifen;
- //订单详情插入
- OrderGood::query()->create([
- 'order_id' => $orderModel['id'],
- 'good_id' => $goodsInfo['id'],
- 'user_id' => $user['id'],
- 'order_time' => $orderTime,
- 'nums' => $goodsNum,
- 'xuefen' => $xuefen,
- 'jifen' => $jifen,
- 'status' => ModelStatusEnum::OK,
- ]);
- }
- $orderModel->xuefen = $xuefenAll;//此单应获得学分
- $orderModel->jifen = $jifenAll;//积分
- $orderModel->save();
- $user->xuefen += $xuefenAll;
- $user->jifen += $jifenAll;
- $user->last_update_time = $orderTime;
- $user->save();
- //学分记录
- $this->xuefen($user['id'], $orderModel['id'], $user['xuefen'], $xuefenAll, $day);
- //积分记录
- $this->jifen($user['id'], $orderModel['id'], $user['jifen'], $jifenAll, $day);
- User::refreshUserTag($user->id);
- //todo:缺少用户消息通知
- DB::commit();
- Cache::put($cache_key, true, Carbon::now()->addHour());
- } catch (\Exception $exception) {
- DB::rollBack();
- self::error('异常错误', $arr);
- log_record('订单接收', $exception);
- }
- }
- /**
- * 积分记录
- * @param $userId
- * @param $orderId
- * @param $integral
- * @param $integralAll
- * @param $day
- * @return void
- */
- protected function jifen($userId, $orderId, $integral, $integralAll, $day)
- {
- $Bonus = new UserJifen();
- $Bonus->user_id = $userId;
- $Bonus->source_id = $orderId;
- $Bonus->source_type = Order::class;
- $Bonus->type = UserJifenTypeEnum::xuefenduihuan;
- $Bonus->day = $day;
- $Bonus->user_jifen = $integral;
- $Bonus->jifen = $integralAll;
- $Bonus->save();
- }
- /**
- * 学分记录
- * @param $userId
- * @param $orderId
- * @param $integral
- * @param $integralAll
- * @param $day
- * @return void
- */
- protected function xuefen($userId, $orderId, $integral, $integralAll, $day)
- {
- $integralM = new UserXuefen();
- $integralM->user_id = $userId;
- $integralM->source_id = $orderId;
- $integralM->source_type = Order::class;
- $integralM->day = $day;
- $integralM->user_xuefen = $integral;
- $integralM->xuefen = $integralAll;
- $integralM->save();
- }
- /**
- * 异常错误
- * @param $position
- * @param $data
- * @return void
- */
- protected static function error($position, $data)
- {
- //todo:建议插入数据库
- log_record($position, $data);
- }
- }
|