CloudStockGoodsStockLog.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. namespace addons\CloudStock\common\models;
  3. use addons\CloudStock\common\events\order\CloudStockChangeEvent;
  4. use addons\CloudStock\common\events\order\CloudStockChangeSyncEvent;
  5. use addons\CloudStock\common\logic\order\RestrictPurchases;
  6. use common\enums\StatusEnum;
  7. use addons\CloudStock\common\enums\CloudStockEnum;
  8. use common\models\base\BaseActiveRecord;
  9. use common\models\goods\Goods;
  10. use common\models\user\User;
  11. use common\models\user\UserPartitionRelationship;
  12. use Yii;
  13. /**
  14. * This is the model class for table "{{%addons_cloud_stock_goods_stock_log".
  15. *
  16. * @property int $id 自增id
  17. * @property int $mall_id 商城id
  18. * @property int $user_id 用户id
  19. * @property int $goods_id 商品id
  20. * @property int $changes_dir 库存变动方向;1增加;2减少
  21. * @property int $order_id 相关的订单id
  22. * @property int $order_type order类型,1:会员购买订单,2:补货订单;
  23. * @property int $changes_type 库存变动类型;100向上级补货;101上级给自己配货;102退货退库存;103系统添加;104平台补货赠送;105购买礼包赠送;200会员买货;201给下级配货;202下级向自己补货;203自提货到线下;204系统扣减;
  24. * @property int $num 数量
  25. * @property int $status 状态[-1:删除;0:禁用;1启用]
  26. * @property int $created_at
  27. * @property int $updated_at
  28. */
  29. class CloudStockGoodsStockLog extends BaseActiveRecord
  30. {
  31. private static $stock_good = null;
  32. private static $good = null;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function tableName()
  37. {
  38. return '{{%addons_cloud_stock_goods_stock_log}}';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['mall_id', 'user_id','goods_id', 'changes_dir', 'order_id', 'order_type', 'changes_type', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
  47. [['changes_dir', 'changes_type', 'num'], 'required'],
  48. ];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => '自增id',
  57. 'mall_id' => '商城id',
  58. 'user_id' => '用户id',
  59. 'goods_id' => '商品id',
  60. 'changes_dir' => '库存变动方向;1增加;2减少',
  61. 'order_id' => '相关的订单id',
  62. 'order_type' => 'order类型,1:会员购买订单,2:补货订单;',
  63. 'changes_type' => '库存变动类型;100向上级补货;101上级给自己配货;102退货退库存;103系统添加;104平台补货赠送;105购买礼包赠送;200会员买货;201给下级配货;202下级向自己补货;203自提货到线下;204系统扣减;',
  64. 'num' => '数量',
  65. 'status' => '状态[-1:删除;0:禁用;1启用]',
  66. 'created_at' => 'Created At',
  67. 'updated_at' => 'Updated At',
  68. ];
  69. }
  70. public function getStockDeductionDetail()
  71. {
  72. return $this->hasOne(CloudStockDeductionDetail::class, ['stock_log_id' => 'id'])->select(['*']);
  73. }
  74. public function getStockIncreaseDetail()
  75. {
  76. return $this->hasOne(CloudStockDeductionDetail::class, ['stock_log_id' => 'id'])->select(['*']);
  77. }
  78. public function getUser()
  79. {
  80. return $this->hasOne(User::class, ['id' => 'user_id'])->select(['*']);
  81. }
  82. public function getGoods()
  83. {
  84. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
  85. }
  86. public static function setData(int $mall_id, array $data)
  87. {
  88. if (!empty($data['id'])) {
  89. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  90. } else {
  91. $model = new self();
  92. $model->loadDefaultValues();
  93. $model->mall_id = $mall_id;
  94. }
  95. foreach ($data as $key => $val) {
  96. $model->$key = $val;
  97. }
  98. // dd($model);
  99. if (!$model->save()) {
  100. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  101. return false;
  102. }
  103. return $model;
  104. }
  105. /**
  106. * @name:
  107. * @msg: 库存处理
  108. * @param {int} $mall_id
  109. * @param {array} $data number:商品变动数量;goods_id:商品id,user_id:需要处理的会员id;target_user_id:目标会员id
  110. * @param {int} $changes_type 库存变动类型,具体参考 CloudStockEnum::getStockChangeTypeMap()
  111. * @return bool
  112. */
  113. public static function stockHandle(int $mall_id, array $data, int $changes_type): bool
  114. {
  115. $user_id = $data['user_id'];
  116. if (empty($data['target_user_id'])) {
  117. $target_user_id = 0;
  118. $is_adjoint = 0;
  119. } else {
  120. $target_user_id = $data['target_user_id'];
  121. $is_adjoint = 1;
  122. }
  123. try {
  124. RestrictPurchases::check($mall_id,$data['target_user_id'],[$data['goods_id']],$data['number'],'fill',2);
  125. }catch (\Exception $e){
  126. self::$static_error = $e->getMessage();
  127. return false;
  128. }
  129. $transaction = \Yii::$app->db->beginTransaction();
  130. try {
  131. $rules = [
  132. [['goods_id', 'number', 'user_id'], 'required'],
  133. [['goods_id', 'number', 'user_id', 'target_user_id', 'order_id', 'order_type'], 'integer'],
  134. [['order_id', 'order_type'], 'required', 'when' => function($model) use($changes_type) {
  135. if (
  136. ($changes_type >= CloudStockEnum::STOCK_CHANGE_TYPE100 &&
  137. $changes_type <= CloudStockEnum::STOCK_CHANGE_TYPE105 &&
  138. in_array($changes_type, [CloudStockEnum::STOCK_CHANGE_TYPE100, CloudStockEnum::STOCK_CHANGE_TYPE102])) && // 增/减库存分界线,上分下减
  139. $changes_type >= CloudStockEnum::STOCK_CHANGE_TYPE200 &&
  140. $changes_type <= CloudStockEnum::STOCK_CHANGE_TYPE204
  141. ) {
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. }],
  147. ];
  148. $res = \Yii::$app->services->validate->validate($data, $rules);
  149. if ($res === false) throw new \Exception(\Yii::$app->services->validate->getErrorMessage());
  150. if ($changes_type >= CloudStockEnum::STOCK_CHANGE_TYPE100 && $changes_type <= CloudStockEnum::STOCK_CHANGE_TYPE105) {
  151. // 加库存
  152. $changes_dir = CloudStockEnum::STOCK_CHANGE_ADD;
  153. } else if ($changes_type >= CloudStockEnum::STOCK_CHANGE_TYPE200 && $changes_type <= CloudStockEnum::STOCK_CHANGE_TYPE204) {
  154. // 减库存
  155. $changes_dir = CloudStockEnum::STOCK_CHANGE_SUB;
  156. } else {
  157. throw new \Exception('不支持的库存变动类型');
  158. }
  159. $data['target_user_id'] = $target_user_id;
  160. // 检查会员两方是否符合条件
  161. self::checkAgenUser($mall_id, $data, $is_adjoint);
  162. // 检查商品
  163. self::checkAgentGood($mall_id, $data,$changes_dir);
  164. // dd($changes_dir, $changes_type);
  165. $adjoint_change_type = self::adjointChangeType($changes_type);
  166. if (0 != $adjoint_change_type) {
  167. // 共轭变动明细需要记录
  168. // 处理共轭变动会员的库存并记录log
  169. if (empty($data['target_user_id'])) throw new \Exception('目标会员id不能为空');
  170. $adjoint_change_dir = self::adjointChangeDir($changes_dir);
  171. $data['user_id'] = $data['target_user_id'];
  172. $configs = \Yii::$app->services->setting->addons->get($mall_id, CloudStockEnum::ADDONS_NAME, 'basic');
  173. $is_can_pick_free = $configs['is_can_pick_free'] ?? 0;
  174. // if (1 == $is_can_pick_free) {
  175. //允许自由配货,下级没有的商品也可以直接配货
  176. //检查商品
  177. self::checkAgentGood($mall_id, $data,$adjoint_change_dir);
  178. // }
  179. // 处理当前会员库存增减并记录log
  180. $adjoint_change_dir_id = self::agentUserSaveLog($mall_id, $data, $adjoint_change_dir, $adjoint_change_type);
  181. }
  182. // 处理当前会员库存增减并记录log
  183. $data['user_id'] = $user_id;
  184. $changes_dir_id = self::agentUserSaveLog($mall_id, $data, $changes_dir, $changes_type);
  185. CloudStockAgent::fixStock($mall_id,$data['user_id']);//扣减库存
  186. CloudStockAgent::fixStock($mall_id,$data['target_user_id']);//增加库存
  187. $transaction->commit();
  188. if (isset($adjoint_change_dir_id) && isset($adjoint_change_dir)) {
  189. //触发库存变动事件
  190. $event = new CloudStockChangeSyncEvent($mall_id);
  191. $event_data = [
  192. 'id' => $adjoint_change_dir_id,
  193. 'type' => $adjoint_change_dir,
  194. 'mall_id' => $mall_id,
  195. ];
  196. \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners);
  197. }
  198. if (isset($changes_dir_id)) {
  199. //触发库存变动事件
  200. $event = new CloudStockChangeSyncEvent($mall_id);
  201. $event_data = [
  202. 'id' => $changes_dir_id,
  203. 'type' => $changes_dir,
  204. 'mall_id' => $mall_id,
  205. ];
  206. \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners);
  207. }
  208. return true;
  209. } catch (\Exception $e) {
  210. \Yii::error($e->getMessage() . ', file: ' . $e->getFile() . ', line: ' . $e->getLine());\Yii::getLogger()->flush(true);
  211. self::$static_error = $e->getMessage();
  212. $transaction->rollBack();
  213. return false;
  214. }
  215. }
  216. /**
  217. * @name:
  218. * @msg: 库存处理
  219. * @param {int} $mall_id
  220. * @param {array} $data number:商品变动数量;goods_id:商品id,user_id:需要处理的会员id;target_user_id:目标会员id
  221. * @param {int} $changes_type 库存变动类型,具体参考 CloudStockEnum::getStockChangeTypeMap()
  222. * @return bool
  223. */
  224. public static function stockHandleV2(int $mall_id, array $data, int $changes_type): bool
  225. {
  226. $user_id = $data['user_id'];
  227. if (empty($data['target_user_id'])) {
  228. $target_user_id = 0;
  229. $is_adjoint = 0;
  230. } else {
  231. $target_user_id = $data['target_user_id'];
  232. $is_adjoint = 1;
  233. }
  234. $transaction = \Yii::$app->db->beginTransaction();
  235. try {
  236. $rules = [
  237. [['user_id'], 'required'],
  238. [['user_id', 'target_user_id', 'order_id', 'order_type'], 'integer'],
  239. [['order_info'], 'safe'],
  240. [['order_id', 'order_type'], 'required', 'when' => function($model) use($changes_type) {
  241. if (
  242. ($changes_type >= CloudStockEnum::STOCK_CHANGE_TYPE100 &&
  243. $changes_type <= CloudStockEnum::STOCK_CHANGE_TYPE105 &&
  244. in_array($changes_type, [CloudStockEnum::STOCK_CHANGE_TYPE100, CloudStockEnum::STOCK_CHANGE_TYPE102])) && // 增/减库存分界线,上分下减
  245. $changes_type >= CloudStockEnum::STOCK_CHANGE_TYPE200 &&
  246. $changes_type <= CloudStockEnum::STOCK_CHANGE_TYPE204
  247. ) {
  248. return true;
  249. } else {
  250. return false;
  251. }
  252. }],
  253. ];
  254. $res = \Yii::$app->services->validate->validate($data, $rules);
  255. if ($res === false) throw new \Exception(\Yii::$app->services->validate->getErrorMessage());
  256. if ($changes_type >= CloudStockEnum::STOCK_CHANGE_TYPE100 && $changes_type <= CloudStockEnum::STOCK_CHANGE_TYPE105) {
  257. // 加库存
  258. $changes_dir = CloudStockEnum::STOCK_CHANGE_ADD;
  259. } else if ($changes_type >= CloudStockEnum::STOCK_CHANGE_TYPE200 && $changes_type <= CloudStockEnum::STOCK_CHANGE_TYPE204) {
  260. // 减库存
  261. $changes_dir = CloudStockEnum::STOCK_CHANGE_SUB;
  262. } else {
  263. throw new \Exception('不支持的库存变动类型');
  264. }
  265. $data['target_user_id'] = $target_user_id;
  266. // 检查会员两方是否符合条件
  267. self::checkAgenUser($mall_id, $data, $is_adjoint);
  268. $adjoint_change_type = self::adjointChangeType($changes_type);
  269. foreach ($data['order_info'] as $orderItem) {
  270. $data['goods_id'] = $orderItem['goods_id'];
  271. $data['number'] = $orderItem['num'];
  272. // 检查商品
  273. self::checkAgentGood($mall_id, $data, $changes_dir);
  274. // dd($changes_dir, $changes_type);
  275. if (0 != $adjoint_change_type) {
  276. // 共轭变动明细需要记录
  277. // 处理共轭变动会员的库存并记录log
  278. if (empty($data['target_user_id'])) throw new \Exception('目标会员id不能为空');
  279. $adjoint_change_dir = self::adjointChangeDir($changes_dir);
  280. $data['user_id'] = $data['target_user_id'];
  281. $configs = \Yii::$app->services->setting->addons->get($mall_id, CloudStockEnum::ADDONS_NAME, 'basic');
  282. $is_can_pick_free = $configs['is_can_pick_free'] ?? 0;
  283. // if (1 == $is_can_pick_free) {
  284. //允许自由配货,下级没有的商品也可以直接配货
  285. //检查商品
  286. self::checkAgentGood($mall_id, $data,$adjoint_change_dir);
  287. // }
  288. // 处理当前会员库存增减并记录log
  289. $adjoint_change_dir_id = self::agentUserSaveLog($mall_id, $data, $adjoint_change_dir, $adjoint_change_type);
  290. }
  291. // 处理当前会员库存增减并记录log
  292. $data['user_id'] = $user_id;
  293. $changes_dir_id = self::agentUserSaveLog($mall_id, $data, $changes_dir, $changes_type);
  294. }
  295. CloudStockAgent::fixStock($mall_id,$data['user_id']);//扣减库存
  296. CloudStockAgent::fixStock($mall_id,$data['target_user_id']);//增加库存
  297. $transaction->commit();
  298. if (isset($adjoint_change_dir_id) && isset($adjoint_change_dir)) {
  299. //触发库存变动事件
  300. $event = new CloudStockChangeSyncEvent($mall_id);
  301. $event_data = [
  302. 'id' => $adjoint_change_dir_id,
  303. 'type' => $adjoint_change_dir,
  304. 'mall_id' => $mall_id,
  305. ];
  306. \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners);
  307. }
  308. if (isset($changes_dir_id)) {
  309. //触发库存变动事件
  310. $event = new CloudStockChangeSyncEvent($mall_id);
  311. $event_data = [
  312. 'id' => $changes_dir_id,
  313. 'type' => $changes_dir,
  314. 'mall_id' => $mall_id,
  315. ];
  316. \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners);
  317. }
  318. return true;
  319. } catch (\Exception $e) {
  320. \Yii::error($e->getMessage() . ', file: ' . $e->getFile() . ', line: ' . $e->getLine());\Yii::getLogger()->flush(true);
  321. self::$static_error = $e->getMessage();
  322. $transaction->rollBack();
  323. return false;
  324. }
  325. }
  326. /**
  327. * @name:
  328. * @msg: 检查会员两方是否符合条件
  329. * @param {int} $mall_id
  330. * @param {array} $data user_id target_user_id
  331. * @return {*}
  332. */
  333. private static function checkAgenUser(int $mall_id, array $data, int $is_adjoint = 1)
  334. {
  335. $agent = CloudStockAgent::findOne(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'status' => StatusEnum::ENABLED]);
  336. if (empty($agent)) {
  337. throw new \Exception('当前会员不是代理商');
  338. }
  339. if (!empty($is_adjoint)) {
  340. $parent_ids = UserPartitionRelationship::getParentIdArr($data['target_user_id']);
  341. if (!in_array($data['user_id'], $parent_ids)) {
  342. throw new \Exception('对方不是你的下级会员,不能配货');
  343. }
  344. $sub_agent = CloudStockAgent::findOne(['mall_id' => $mall_id, 'user_id' => $data['target_user_id'], 'status' => StatusEnum::ENABLED]);
  345. if (empty($sub_agent)) {
  346. throw new \Exception('对方不是代理商');
  347. }
  348. }
  349. return true;
  350. }
  351. /**
  352. * @name:
  353. * @msg: 检查商品状态
  354. * @param {int} $mall_id
  355. * @param {array} $data goods_id:商品id,user_id:当前会员id
  356. * @return {*}
  357. */
  358. private static function checkAgentGood(int $mall_id, array $data, $changes_dir)
  359. {
  360. $good = Goods::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['goods_id'], 'is_on_sale' => 1, 'is_attr' => 0]);
  361. if (empty($good)) throw new \Exception('商品数据异常');
  362. $stock_good = CloudStockAgentGoods::findOne(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'goods_id' => $data['goods_id'], 'status' => StatusEnum::ENABLED]);
  363. if (empty($stock_good)) {
  364. // throw new \Exception('未找到代理商品,请先补货');
  365. $stock_good = new CloudStockAgentGoods();
  366. $stock_good->loadDefaultValues();
  367. $stock_good->mall_id = $mall_id;
  368. $stock_good->goods_id = $data['goods_id'];
  369. $stock_good->user_id = $data['user_id'];
  370. $stock_good->num = 0;
  371. $stock_good->save();
  372. }
  373. if ($changes_dir == CloudStockEnum::STOCK_CHANGE_SUB) {
  374. if ($data['number'] > $stock_good['num']) {
  375. throw new \Exception('商品库存不足');
  376. }
  377. }
  378. self::$good = $good;
  379. self::$stock_good = $stock_good;
  380. return true;
  381. }
  382. /**
  383. * @name:
  384. * @msg: 获取共轭改变类型。例如:当前会员的改变类型是给下级配货(201),则其共轭改变类型就是 上级给自己配货(101)
  385. * @param {int} $change_type
  386. * @return {*}
  387. */
  388. private static function adjointChangeType(int $changes_type = null)
  389. {
  390. $adjoint_change_type_map = [
  391. CloudStockEnum::STOCK_CHANGE_TYPE100 => CloudStockEnum::STOCK_CHANGE_TYPE202,
  392. CloudStockEnum::STOCK_CHANGE_TYPE101 => CloudStockEnum::STOCK_CHANGE_TYPE201,
  393. CloudStockEnum::STOCK_CHANGE_TYPE102 => 0,
  394. CloudStockEnum::STOCK_CHANGE_TYPE103 => 0,
  395. CloudStockEnum::STOCK_CHANGE_TYPE104 => 0,
  396. CloudStockEnum::STOCK_CHANGE_TYPE105 => 0,
  397. CloudStockEnum::STOCK_CHANGE_TYPE200 => 0,
  398. CloudStockEnum::STOCK_CHANGE_TYPE201 => CloudStockEnum::STOCK_CHANGE_TYPE101,
  399. CloudStockEnum::STOCK_CHANGE_TYPE202 => CloudStockEnum::STOCK_CHANGE_TYPE100,
  400. CloudStockEnum::STOCK_CHANGE_TYPE203 => 0,
  401. CloudStockEnum::STOCK_CHANGE_TYPE204 => 0,
  402. ];
  403. if (!empty($changes_type)) $adjoint_change_type_map = $adjoint_change_type_map[$changes_type];
  404. return $adjoint_change_type_map;
  405. }
  406. /**
  407. * @name:
  408. * @msg: 获取共轭变动方向
  409. * @param {int} $changes_dir 当前变动方向
  410. * @return mixed
  411. */
  412. private static function adjointChangeDir(int $changes_dir = null)
  413. {
  414. $adjoint_change_dir_map = [
  415. CloudStockEnum::STOCK_CHANGE_ADD => CloudStockEnum::STOCK_CHANGE_SUB,
  416. CloudStockEnum::STOCK_CHANGE_SUB => CloudStockEnum::STOCK_CHANGE_ADD,
  417. ];
  418. if (!empty($changes_dir)) $adjoint_change_dir_map = $adjoint_change_dir_map[$changes_dir];
  419. return $adjoint_change_dir_map;
  420. }
  421. /**
  422. * @name:
  423. * @msg: 代理商库存处理并记录明细
  424. * @param {int} $mall_id
  425. * @param {array} $data goods_id:商品id,user_id:需要处理的会员id;order_id:订单id,非必需;order_type:订单类型,非必需;number:商品变动数量;
  426. * @param {int} $changes_dir
  427. * @param {int} $changes_type
  428. * @return {*}
  429. */
  430. private static function agentUserSaveLog(int $mall_id, array $data, int $changes_dir, int $changes_type)
  431. {
  432. // 当前会员库存明细记录 --- start ----
  433. $params_sub = [
  434. 'goods_id' => $data['goods_id'],
  435. 'changes_dir' => $changes_dir,
  436. 'user_id' => $data['user_id'],
  437. 'order_id' => $data['order_id'] ?? 0,
  438. 'order_type' => $data['order_type'] ?? 0,
  439. 'changes_type' => $changes_type,
  440. 'num' => $data['number'],
  441. 'status' => StatusEnum::ENABLED,
  442. ];
  443. $res_save_log = self::setData($mall_id, $params_sub);
  444. if (false === $res_save_log) {
  445. throw new \Exception(self::$static_error);
  446. }
  447. // 当前会员减库存明细记录 --- end ----
  448. // 当前会员库存增减明细表数据记录 --- start ---
  449. // unset($stock_good);
  450. $stock_good = CloudStockAgentGoods::findOne(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'goods_id' => $data['goods_id'], 'status' => StatusEnum::ENABLED]);
  451. if (empty($stock_good)) {
  452. $msg = '代理商商品未找到';
  453. if ($data['user_id'] == $data['target_user_id']) {
  454. $msg = '需要下级代理商进货过该商品之后才可以配货';
  455. }
  456. throw new \Exception($msg);
  457. }
  458. // $msg = __METHOD__ . ' ======= mall_id => ' . $mall_id . ', data => ' . var_export($data, true) . ', changes_dir => ' . $changes_dir . ', changes_type => ' . $changes_type . ', stock_good->num => ' . $stock_good->num . PHP_EOL;
  459. // \Yii::error($msg);\Yii::getLogger()->flush(true);
  460. if (CloudStockEnum::STOCK_CHANGE_SUB == $changes_dir) {
  461. // 减库存
  462. $good_change_number = max($stock_good->num - $data['number'], 0);
  463. $deduction_params = [
  464. 'user_id' => $data['user_id'],
  465. 'num' => $data['number'],
  466. 'mall_id'=>$mall_id,
  467. 'goods_id' => $data['goods_id'],
  468. 'order_id' => $data['order_id'] ?? 0,
  469. 'order_type' => $data['order_type'] ?? 0,
  470. 'changes_type' => $changes_type,
  471. 'not_yet_num' => $good_change_number,
  472. 'stock_log_id' => $res_save_log->id,
  473. 'status' => StatusEnum::ENABLED,
  474. ];
  475. $res_deduct = CloudStockDeductionDetail::setData($deduction_params);
  476. if (false === $res_deduct) {
  477. throw new \Exception(self::$static_error);
  478. }
  479. $id = $res_deduct->id;
  480. } else {
  481. // 加库存
  482. $good_change_number = $stock_good->num + $data['number'];
  483. $add_params = [
  484. 'user_id' => $data['user_id'],
  485. 'num' => $data['number'],
  486. 'mall_id'=>$mall_id,
  487. 'goods_id' => $data['goods_id'],
  488. 'order_id' => $data['order_id'] ?? 0,
  489. 'order_type' => $data['order_type'] ?? 0,
  490. 'changes_type' => $changes_type,
  491. 'stock_log_id' => $res_save_log->id,
  492. 'status' => StatusEnum::ENABLED,
  493. ];
  494. $res_add = CloudStockIncreaseDetail::setData($add_params);
  495. if (false === $res_add) {
  496. throw new \Exception(self::$static_error);
  497. }
  498. $id = $res_add->id;
  499. }
  500. // 当前会员库存增减明细表数据记录 --- end ---
  501. // 当前会员代理商品记录表相应商品数量变动更新 ---- start -----
  502. if (empty($stock_good)) {
  503. $update_stock_good_params = [
  504. 'user_id' => $data['user_id'],
  505. 'goods_id' => $data['goods_id'],
  506. 'num' => $data['number'],
  507. ];
  508. } else {
  509. $update_stock_good_params = [
  510. 'id' => $stock_good->id,
  511. 'num' => $good_change_number,
  512. ];
  513. }
  514. $res_agenuser_update = CloudStockAgentGoods::setData($mall_id, $update_stock_good_params);
  515. if (false === $res_agenuser_update) {
  516. throw new \Exception(self::$static_error);
  517. }
  518. // 当前会员代理商品记录表相应商品数量变动更新 ---- end -----
  519. unset($res_save_log);
  520. return $id;
  521. }
  522. }