GoodsReserveSku.php 10 KB

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