CloudStockPushDelayLog.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace addons\CloudStock\common\models;
  3. use addons\CloudStock\common\logic\deduction\DeductionJob;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_cloud_stock_push_delay_log".
  9. *
  10. * @property int $id 自增id
  11. * @property int $mall_id 商城id
  12. * @property int $user_id 用户id
  13. * @property int $agent_user_id 用户id
  14. * @property int $stock_log_id stock_log_id
  15. * @property int $goods_id goods_id
  16. * @property int $order_id order_id
  17. * @property int $not_yet_num 商品id
  18. * @property string $order_type 订单类型
  19. * @property string|null $time_limit_fill_user_ids
  20. * @property int $status 状态[-1:删除;0:禁用;1启用]
  21. * @property int $created_at
  22. * @property int $updated_at
  23. */
  24. class CloudStockPushDelayLog extends BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_cloud_stock_push_delay_log}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id', 'user_id', 'agent_user_id', 'stock_log_id', 'goods_id', 'order_id', 'not_yet_num', 'status', 'created_at', 'updated_at'], 'integer'],
  40. [['order_type'], 'required'],
  41. [['time_limit_fill_user_ids'], 'string'],
  42. [['order_type'], 'string', 'max' => 255],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => '自增id',
  52. 'mall_id' => '商城id',
  53. 'user_id' => '用户id',
  54. 'agent_user_id' => '用户id',
  55. 'stock_log_id' => 'stock_log_id',
  56. 'goods_id' => '商品id',
  57. 'order_id' => 'order_id',
  58. 'not_yet_num' => '商品id',
  59. 'order_type' => '订单类型',
  60. 'time_limit_fill_user_ids' => 'Time Limit Fill User Ids',
  61. 'status' => '状态[-1:删除;0:禁用;1启用]',
  62. 'created_at' => 'Created At',
  63. 'updated_at' => 'Updated At',
  64. ];
  65. }
  66. public static function setData($data)
  67. {
  68. try {
  69. $model = self::findOne(['mall_id' => $data['mall_id'], 'status' => StatusEnum::ENABLED, 'agent_user_id' => $data['agent_user_id'], 'stock_log_id' => $data['stock_log_id']]);
  70. if (empty($model)) {
  71. $model = new self();
  72. $model->loadDefaultValues();
  73. $model->mall_id = $data['mall_id'];
  74. }
  75. $model->loadDefaultValues();
  76. if (!$model->load($data, '')) throw new \Exception($model->getErrorMessage());
  77. if(empty($data['time_limit_fill_user_ids'])) $data['time_limit_fill_user_ids'] = [];
  78. $model->time_limit_fill_user_ids = json_encode($data['time_limit_fill_user_ids']);
  79. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  80. return $model;
  81. } catch (\Exception $e) {
  82. var_dump( $e->getMessage());
  83. self::$static_error = $e->getMessage();
  84. return false;
  85. }
  86. }
  87. public function updateDelayLog($mall_id,$goods_id,$agent_user_id,$num){
  88. $push_delay_logs = CloudStockPushDelayLog::lists([
  89. 'where'=>[
  90. ['mall_id'=>$mall_id],
  91. ['goods_id'=>$goods_id],
  92. ['agent_user_id'=>$agent_user_id],
  93. ['status' => StatusEnum::ENABLED],
  94. ],
  95. 'order'=>'id asc'
  96. ],false);
  97. if(!empty($push_delay_logs)){
  98. $temp_num = $num;
  99. foreach ($push_delay_logs as $item){
  100. $not_yet_num = $item['not_yet_num'];
  101. if($temp_num<$item['not_yet_num']){
  102. $not_yet_num = $temp_num;
  103. $temp_num = 0;
  104. }else{
  105. $temp_num-= $not_yet_num;
  106. }
  107. if($not_yet_num>0){
  108. $data = [
  109. 'user_id' => $item['user_id'],
  110. 'agent_user_id' => $item['agent_user_id'],
  111. 'stock_log_id' => $item['stock_log_id'],
  112. 'goods_id' => $item['goods_id'],
  113. 'order_id' => $item['order_id'],
  114. 'not_yet_num' => $not_yet_num,
  115. 'order_type' => $item['order_type'],
  116. 'mall_id' => $item['mall_id'],
  117. 'time_limit_fill_user_ids' =>json_decode($item['time_limit_fill_user_ids'],true),
  118. 'is_lock'=>false,
  119. ];
  120. (new DeductionJob())->handleSync($data);
  121. // $item->not_yet_num-=$not_yet_num;
  122. // if($item->not_yet_num<=0){
  123. // $item->not_yet_num = 0;
  124. // $item->status = -1;
  125. // }
  126. // $item->save();
  127. }
  128. }
  129. }
  130. }
  131. }