StoreGoodsReserveTimeSpec.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace addons\Store\common\models;
  3. use Exception;
  4. use Yii;
  5. /**
  6. * This is the model class for table "qimall_addons_store_goods_reserve_time_spec".
  7. *
  8. * @property int $id
  9. * @property string|null $spec_time spec标题
  10. * @property string|null $start 开始时间点
  11. * @property string|null $end 结束时间点
  12. * @property int $reserve_time_id 商城id
  13. * @property int|null $reserve_sku_id 预约商品SKU
  14. * @property int $checked 是否选中
  15. * @property int $is_next 结束时间[0当日 1次日]
  16. * @property int $stock 库存
  17. * @property float $price 售价
  18. * @property float $original_price 原价(划线价仅展示)
  19. * @property float $cost_price 成本价
  20. * @property int $reserve_max_buy 每天最大预约数量
  21. * @property int $updated_at 修改时间
  22. * @property int $created_at 创建时间
  23. */
  24. class StoreGoodsReserveTimeSpec extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return 'qimall_addons_store_goods_reserve_time_spec';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['reserve_time_id', 'reserve_sku_id', 'checked', 'is_next', 'stock', 'reserve_max_buy', 'updated_at', 'created_at'], 'integer'],
  40. [['price', 'original_price', 'cost_price'], 'number'],
  41. [['spec_time'], 'string', 'max' => 16],
  42. [['start', 'end'], 'string', 'max' => 32],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'ID',
  52. 'spec_time' => 'Spec Time',
  53. 'start' => 'Start',
  54. 'end' => 'End',
  55. 'reserve_time_id' => 'Reserve Time ID',
  56. 'reserve_sku_id' => 'Reserve Sku ID',
  57. 'checked' => 'Checked',
  58. 'is_next' => 'Is Next',
  59. 'stock' => 'Stock',
  60. 'price' => 'Price',
  61. 'original_price' => 'Original Price',
  62. 'cost_price' => 'Cost Price',
  63. 'reserve_max_buy' => 'Reserve Max Buy',
  64. 'updated_at' => 'Updated At',
  65. 'created_at' => 'Created At',
  66. ];
  67. }
  68. // 设置sku的可预约时间
  69. public static function setTimeSpec($reserveTime, $timeSpecs,&$totalStock)
  70. {
  71. try {
  72. array_map(function ($val) use (&$reserveTime,&$totalStock) {
  73. $model = new self();
  74. $model->loadDefaultValues();
  75. $attributes = [
  76. 'spec_time' => $val['spec_time'],
  77. 'start' => $val['start'],
  78. 'end' => $val['end'],
  79. 'reserve_time_id' => $reserveTime['id'],
  80. 'price' => $val['price'] ?? 0,
  81. 'original_price' => $val['original_price'] ?? 0,
  82. 'cost_price' => $val['cost_price'] ?? 0,
  83. 'reserve_max_buy' => $val['reserve_max_buy'] ?? 0,
  84. 'reserve_sku_id' => $reserveTime['reserve_sku_id'] ?? 0,
  85. 'stock' => $val['reserve_max_buy'] ?? 0,
  86. 'checked' => $val['checked'],
  87. 'is_next' => $val['is_next'],
  88. ];
  89. $totalStock += $val['reserve_max_buy'];
  90. if (!$model->load($attributes, '') || !$model->save()) {
  91. throw new Exception($model->getErrorMessage());
  92. }
  93. }, $timeSpecs);
  94. } catch (Exception $e) {
  95. if (isset($trans)) $trans->rollback();
  96. self::$static_error = $e->getMessage();
  97. return false;
  98. }
  99. }
  100. }