StoreGoodsReserveSku.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use Yii;
  6. use yii\helpers\ArrayHelper;
  7. /**
  8. * This is the model class for table "qimall_addons_store_goods_reserve_sku".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城id
  12. * @property int $store_id 商家id
  13. * @property string $name 规格名称
  14. * @property string $sign_id 规格ID标识
  15. * @property string $pic_url 规格图片
  16. * @property int $goods_id 商品id
  17. * @property float $price 售价
  18. * @property float $original_price 原价(划线价仅展示)
  19. * @property float $cost_price 成本价
  20. * @property int $reserve_date_type 可预约时间类型(1范围 2自选日期)
  21. * @property string|null $reserve_week 可预约周
  22. * @property string|null $reserve_date 可预约日期
  23. * @property int $reserve_time_range_type 预约模式(1按天预约 2按时间段预约)
  24. * @property int $time_slot_type 时间段划分方式(1自定义划分 2自动划分)
  25. * @property string|null $reserve_time_slot 时间段划分
  26. * @property int $reserve_price_type 价格设置方式(0统一售价 1按日期时间段设置)
  27. * @property string $reserve_time_slot_price 时间段价格设置
  28. * @property int $reserve_max_buy 每天最大预约数量
  29. * @property int $created_at 创建时间
  30. * @property int $updated_at 修改时间
  31. * @property int $service_min 服务分钟
  32. * @property string $unit 单位
  33. */
  34. class StoreGoodsReserveSku extends \common\models\base\BaseActiveRecord
  35. {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return 'qimall_addons_store_goods_reserve_sku';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['mall_id', 'store_id', 'goods_id', 'reserve_date_type', 'reserve_time_range_type', 'time_slot_type', 'reserve_price_type', 'reserve_max_buy', 'created_at', 'updated_at', 'service_min'], 'integer'],
  50. [['price', 'original_price', 'cost_price'], 'number'],
  51. [['reserve_date', 'reserve_time_slot', 'reserve_time_slot_price'], 'string'],
  52. [['name'], 'string', 'max' => 500],
  53. [['sign_id', 'pic_url'], 'string', 'max' => 255],
  54. [['reserve_week'], 'string', 'max' => 32],
  55. [['unit'], 'string', 'max' => 50],
  56. ];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'mall_id' => 'Mall ID',
  66. 'store_id' => 'Store ID',
  67. 'name' => 'Name',
  68. 'sign_id' => 'Sign ID',
  69. 'pic_url' => 'Pic Url',
  70. 'goods_id' => 'Goods ID',
  71. 'price' => 'Price',
  72. 'original_price' => 'Original Price',
  73. 'cost_price' => 'Cost Price',
  74. 'reserve_date_type' => 'Reserve Date Type',
  75. 'reserve_week' => 'Reserve Week',
  76. 'reserve_date' => 'Reserve Date',
  77. 'reserve_time_range_type' => 'Reserve Time Range Type',
  78. 'time_slot_type' => 'Time Slot Type',
  79. 'reserve_time_slot' => 'Reserve Time Slot',
  80. 'reserve_price_type' => 'Reserve Price Type',
  81. 'reserve_time_slot_price' => 'Reserve Time Slot Price',
  82. 'reserve_max_buy' => 'Reserve Max Buy',
  83. 'created_at' => 'Created At',
  84. 'updated_at' => 'Updated At',
  85. 'service_min' => 'Service Min',
  86. 'unit' => 'Unit',
  87. ];
  88. }
  89. public function getReserve()
  90. {
  91. return $this->hasOne(StoreGoodsReserve::class, ['goods_id' => 'goods_id']);
  92. }
  93. public function getGoods()
  94. {
  95. return $this->hasOne(StoreGoods::class, ['id' => 'goods_id']);
  96. }
  97. public function getTimes()
  98. {
  99. return $this->hasMany(StoreGoodsReserveTime::class, ['reserve_sku_id' => 'id']);
  100. }
  101. // 无规格sku存储
  102. public static function setSku($reserveGoods)
  103. {
  104. try {
  105. // 清除SKU
  106. // self::deleteAll(['goods_id' => $reserveGoods['goods_id']]);
  107. $model = self::findOne(['goods_id' => $reserveGoods['goods_id']]);
  108. if (empty($model)) {
  109. $model = new self();
  110. $model->loadDefaultValues();
  111. $model->goods_id = $reserveGoods['goods_id'];
  112. }
  113. $trans = Yii::$app->db->beginTransaction();
  114. if (!$model->load($reserveGoods, '') || !$model->save()) {
  115. throw new Exception($model->getErrorMessage());
  116. }
  117. // 写入预约时间
  118. $reserveGoodsSku = ArrayHelper::toArray($model);
  119. $res = StoreGoodsReserveTime::setSkuTime($reserveGoodsSku);
  120. if ($res === false) throw new Exception('处理预约商品预约时间异常,msg:' . StoreGoodsReserveTime::getStaticError());
  121. $trans->commit();
  122. return true;
  123. } catch (Exception $e) {
  124. if (isset($trans)) $trans->rollback();
  125. self::$static_error = $e->getMessage();
  126. return false;
  127. }
  128. }
  129. /**
  130. * @param array $goods
  131. * @param array $skus
  132. * @param StoreGoodsReserve $reserve
  133. * @return bool
  134. */
  135. public static function setSkus($goods, $skus, $reserve)
  136. {
  137. try {
  138. $attrGroups = json_decode($goods['attr_groups'], true);
  139. if (!empty($skus)) {
  140. $sign_ids = [];
  141. foreach ($skus as $sku) {
  142. $sku_ids = array_column($sku['attr_list'], 'attr_id');
  143. asort($sku_ids);
  144. $sku['pic_url'] = $skuPicList[$sku_ids[0]] ?? '';
  145. $sign_ids[] = implode(':', $sku_ids);
  146. }
  147. self::deleteAll([
  148. 'and',
  149. ['goods_id' => $goods['id']],
  150. ['not in', 'sign_id', $sign_ids]
  151. ]);
  152. $total_stock = 0 ;
  153. $skuPicList = array_column($attrGroups[0]['attr_list'], 'pic_url', 'attr_id');
  154. foreach ($skus as $sku) {
  155. //print_r($sku);
  156. $sku['mall_id'] = $goods['mall_id'];
  157. $sku['store_id'] = $goods['store_id'];
  158. $sku['goods_id'] = $goods['id'];
  159. $sku['price'] = $sku['price'] ?? $goods['price'];
  160. $sku['original_price'] = $sku['original_price'] ?? $goods['original_price'];
  161. $sku['cost_price'] = $goods['cost_price'];
  162. $sku['service_min'] = !empty($sku['service_min']) ? $sku['service_min'] : $reserve->service_min;
  163. $sku['unit'] = !empty($sku['unit']) ? $sku['unit'] : $goods['unit'];
  164. $sku_ids = array_column($sku['attr_list'], 'attr_id');
  165. asort($sku_ids);
  166. $sku['pic_url'] = $skuPicList[$sku_ids[0]] ?? '';
  167. $sku['sign_id'] = implode(':', $sku_ids);
  168. is_array($sku['reserve_week']) && $sku['reserve_week'] = implode(',',$sku['reserve_week']);
  169. is_array($sku['reserve_date']) && $sku['reserve_date'] = json_encode($sku['reserve_date'],JSON_UNESCAPED_SLASHES);
  170. is_array($sku['reserve_time_slot']) && $sku['reserve_time_slot'] = json_encode($sku['reserve_time_slot'],JSON_UNESCAPED_SLASHES);
  171. is_array($sku['reserve_time_slot_price']) && $sku['reserve_time_slot_price'] = json_encode($sku['reserve_time_slot_price'],JSON_UNESCAPED_SLASHES);
  172. $sku['reserve_max_buy'] = $sku['reserve_max_buy'] > 0 ? $sku['reserve_max_buy'] : 0;
  173. $total_stock = $total_stock + $sku['reserve_max_buy'];
  174. $sku_group_name_list = array_column($sku['attr_list'], 'attr_group_name');
  175. $sku_name_list = array_column($sku['attr_list'], 'attr_name');
  176. $sku_name_arr = [];
  177. foreach ($sku_group_name_list as $index => $sku_group_name) {
  178. $sku_name_arr[] = $sku_group_name . ':' . $sku_name_list[$index];
  179. }
  180. $sku['name'] = implode(',', $sku_name_arr);
  181. if (!empty($sku['sign_id'])) {
  182. $model = self::findOne(['sign_id' => $sku['sign_id'], 'goods_id' => $goods['id']]);
  183. if (empty($model)) {
  184. $model = new self();
  185. $model->loadDefaultValues();
  186. $model->goods_id = $goods['id'];
  187. }
  188. } else {
  189. $model = new self();
  190. $model->loadDefaultValues();
  191. $model->goods_id = $goods['id'];
  192. }
  193. if (!$model->load($sku, '') || !$model->save()) {
  194. throw new Exception($model->getErrorMessage());
  195. }
  196. // 写入预约时间
  197. $reserveGoodsSku = ArrayHelper::toArray($model);
  198. $res = StoreGoodsReserveTime::setSkuTime($reserveGoodsSku);
  199. if ($res === false) throw new Exception('处理预约商品预约时间异常,msg:' . StoreGoodsReserveTime::getStaticError());
  200. }
  201. //修改商品总库存
  202. if($total_stock > 0){
  203. $goods = StoreGoods::findOne($goods['id']);
  204. $goods->stock = $goods->stock + $total_stock;
  205. if (!$goods->save()) throw new Exception($goods->getErrorMessage());
  206. }
  207. }
  208. return true;
  209. } catch (Exception $e) {
  210. if (isset($trans)) $trans->rollback();
  211. self::$static_error = $e->getMessage();
  212. return false;
  213. }
  214. }
  215. }