CloudStockDispenseOrder.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace addons\CloudStock\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\goods\Goods;
  6. use common\models\user\User;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_addons_cloud_stock_dispense_order".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城id
  13. * @property int $type 1上级配货;2:平台充值
  14. * @property int $user_id 代理商用户id
  15. * @property float $agent_price 代理拿货价
  16. * @property int $child_id 配货下级用户id
  17. * @property float $child_price 配货下级的拿货价
  18. * @property float $money 配货金额
  19. * @property string $order_no 补货订单编号
  20. * @property int $goods_id 商品id
  21. * @property int $num 补货商品数量
  22. * @property int $status 状态[-1:删除;0:禁用;1启用]
  23. * @property int $created_at
  24. * @property int $updated_at
  25. */
  26. class CloudStockDispenseOrder extends BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_cloud_stock_dispense_order}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id', 'user_id', 'child_id', 'order_no', 'goods_id'], 'required'],
  42. [['mall_id', 'type', 'user_id', 'child_id', 'goods_id', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
  43. [['agent_price', 'child_price', 'money'], 'number'],
  44. [['order_no'], 'string', 'max' => 255],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => 'Mall ID',
  55. 'type' => 'Type',
  56. 'user_id' => 'User ID',
  57. 'agent_price' => 'Agent Price',
  58. 'child_id' => 'Child ID',
  59. 'child_price' => 'Child Price',
  60. 'money' => 'Money',
  61. 'order_no' => 'Order No',
  62. 'goods_id' => 'Goods ID',
  63. 'num' => 'Num',
  64. 'status' => 'Status',
  65. 'created_at' => 'Created At',
  66. 'updated_at' => 'Updated At',
  67. ];
  68. }
  69. public function getUser()
  70. {
  71. return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'nickname', 'avatar_url', 'mobile']);
  72. }
  73. public function getChild()
  74. {
  75. return $this->hasOne(User::class, ['id' => 'child_id'])->select(['id', 'nickname', 'avatar_url', 'mobile']);
  76. }
  77. public function getGoods()
  78. {
  79. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
  80. }
  81. public static function addData($params)
  82. {
  83. $cloud_stock_goods = CloudStockGoods::findOne(['goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED]);
  84. if (empty($cloud_stock_goods)) {
  85. throw new \Exception('商品不存在');
  86. }
  87. $agent_price = json_decode($cloud_stock_goods['agent_price'], true);
  88. $agent_price_list = array_column($agent_price, null, 'level');
  89. $child_cloud_stock_agent = CloudStockAgent::findOne(['user_id' => $params['target_user_id'], 'status' => StatusEnum::ENABLED]);
  90. if (empty($child_cloud_stock_agent)) {
  91. throw new \Exception('代理商不存在');
  92. }
  93. $agent_price = 0;
  94. if (!empty($params['user_id'])) {
  95. $cloud_stock_agent = CloudStockAgent::findOne(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
  96. $agent_price = $agent_price_list[$cloud_stock_agent['level']]['price'] ?? 0;
  97. }
  98. $child_price = $agent_price_list[$child_cloud_stock_agent['level']]['price'] ?? 0;
  99. $add = [
  100. 'mall_id' => $params['mall_id'],
  101. 'user_id' => $params['user_id'],
  102. 'child_id' => $params['target_user_id'],
  103. 'agent_price' => $agent_price,
  104. 'child_price' => $child_price,
  105. 'money' => $child_price * $params['number'],
  106. 'order_no' => self::getOrderNo($params['mall_id']),
  107. 'goods_id' => $params['goods_id'],
  108. 'num' => $params['number'],
  109. 'type' => $params['type'],
  110. 'status' => StatusEnum::ENABLED,
  111. ];
  112. return self::setData($params['mall_id'], $add);
  113. }
  114. public static function setData(int $mall_id, array $data)
  115. {
  116. if (!empty($data['id'])) {
  117. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  118. if (empty($model)) {
  119. self::$static_error = '数据异常';
  120. return false;
  121. }
  122. } else {
  123. $model = new self();
  124. $model->loadDefaultValues();
  125. $model->mall_id = $mall_id;
  126. }
  127. foreach ($data as $key => $val) {
  128. $model->$key = $val;
  129. }
  130. if (!$model->save()) {
  131. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  132. return false;
  133. }
  134. return $model;
  135. }
  136. public static function getOrderNo($mall_id)
  137. {
  138. $date = date('Ymd');
  139. $start = strtotime($date . ' 00:00:00');
  140. $end = strtotime($date . ' 23:59:59');
  141. $res = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->andWhere(['between', 'created_at', $start, $end])->orderBy('id desc')->one();
  142. if ($res) {
  143. $num = $res['order_no'];
  144. $num = substr($num, 8);
  145. $num = intval($num) + 1;
  146. $num = str_pad($num, 5, '0', STR_PAD_LEFT);
  147. return $date . $num;
  148. } else {
  149. return $date . '00001';
  150. }
  151. }
  152. }