| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace addons\Store\common\models;
- use common\enums\StatusEnum;
- use Exception;
- use Yii;
- use yii\helpers\ArrayHelper;
- /**
- * This is the model class for table "qimall_addons_store_goods_reserve_sku".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $store_id 商家id
- * @property string $name 规格名称
- * @property string $sign_id 规格ID标识
- * @property string $pic_url 规格图片
- * @property int $goods_id 商品id
- * @property float $price 售价
- * @property float $original_price 原价(划线价仅展示)
- * @property float $cost_price 成本价
- * @property int $reserve_date_type 可预约时间类型(1范围 2自选日期)
- * @property string|null $reserve_week 可预约周
- * @property string|null $reserve_date 可预约日期
- * @property int $reserve_time_range_type 预约模式(1按天预约 2按时间段预约)
- * @property int $time_slot_type 时间段划分方式(1自定义划分 2自动划分)
- * @property string|null $reserve_time_slot 时间段划分
- * @property int $reserve_price_type 价格设置方式(0统一售价 1按日期时间段设置)
- * @property string $reserve_time_slot_price 时间段价格设置
- * @property int $reserve_max_buy 每天最大预约数量
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $service_min 服务分钟
- * @property string $unit 单位
- */
- class StoreGoodsReserveSku extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_store_goods_reserve_sku';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['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'],
- [['price', 'original_price', 'cost_price'], 'number'],
- [['reserve_date', 'reserve_time_slot', 'reserve_time_slot_price'], 'string'],
- [['name'], 'string', 'max' => 500],
- [['sign_id', 'pic_url'], 'string', 'max' => 255],
- [['reserve_week'], 'string', 'max' => 32],
- [['unit'], 'string', 'max' => 50],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- 'name' => 'Name',
- 'sign_id' => 'Sign ID',
- 'pic_url' => 'Pic Url',
- 'goods_id' => 'Goods ID',
- 'price' => 'Price',
- 'original_price' => 'Original Price',
- 'cost_price' => 'Cost Price',
- 'reserve_date_type' => 'Reserve Date Type',
- 'reserve_week' => 'Reserve Week',
- 'reserve_date' => 'Reserve Date',
- 'reserve_time_range_type' => 'Reserve Time Range Type',
- 'time_slot_type' => 'Time Slot Type',
- 'reserve_time_slot' => 'Reserve Time Slot',
- 'reserve_price_type' => 'Reserve Price Type',
- 'reserve_time_slot_price' => 'Reserve Time Slot Price',
- 'reserve_max_buy' => 'Reserve Max Buy',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'service_min' => 'Service Min',
- 'unit' => 'Unit',
- ];
- }
- public function getReserve()
- {
- return $this->hasOne(StoreGoodsReserve::class, ['goods_id' => 'goods_id']);
- }
- public function getGoods()
- {
- return $this->hasOne(StoreGoods::class, ['id' => 'goods_id']);
- }
- public function getTimes()
- {
- return $this->hasMany(StoreGoodsReserveTime::class, ['reserve_sku_id' => 'id']);
- }
- // 无规格sku存储
- public static function setSku($reserveGoods)
- {
- try {
- // 清除SKU
- // self::deleteAll(['goods_id' => $reserveGoods['goods_id']]);
- $model = self::findOne(['goods_id' => $reserveGoods['goods_id']]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->goods_id = $reserveGoods['goods_id'];
- }
- $trans = Yii::$app->db->beginTransaction();
- if (!$model->load($reserveGoods, '') || !$model->save()) {
- throw new Exception($model->getErrorMessage());
- }
- // 写入预约时间
- $reserveGoodsSku = ArrayHelper::toArray($model);
- $res = StoreGoodsReserveTime::setSkuTime($reserveGoodsSku);
- if ($res === false) throw new Exception('处理预约商品预约时间异常,msg:' . StoreGoodsReserveTime::getStaticError());
- $trans->commit();
- return true;
- } catch (Exception $e) {
- if (isset($trans)) $trans->rollback();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @param array $goods
- * @param array $skus
- * @param StoreGoodsReserve $reserve
- * @return bool
- */
- public static function setSkus($goods, $skus, $reserve)
- {
- try {
- $attrGroups = json_decode($goods['attr_groups'], true);
- if (!empty($skus)) {
- $sign_ids = [];
- foreach ($skus as $sku) {
- $sku_ids = array_column($sku['attr_list'], 'attr_id');
- asort($sku_ids);
- $sku['pic_url'] = $skuPicList[$sku_ids[0]] ?? '';
- $sign_ids[] = implode(':', $sku_ids);
- }
- self::deleteAll([
- 'and',
- ['goods_id' => $goods['id']],
- ['not in', 'sign_id', $sign_ids]
- ]);
- $total_stock = 0 ;
- $skuPicList = array_column($attrGroups[0]['attr_list'], 'pic_url', 'attr_id');
- foreach ($skus as $sku) {
- //print_r($sku);
- $sku['mall_id'] = $goods['mall_id'];
- $sku['store_id'] = $goods['store_id'];
- $sku['goods_id'] = $goods['id'];
- $sku['price'] = $sku['price'] ?? $goods['price'];
- $sku['original_price'] = $sku['original_price'] ?? $goods['original_price'];
- $sku['cost_price'] = $goods['cost_price'];
- $sku['service_min'] = !empty($sku['service_min']) ? $sku['service_min'] : $reserve->service_min;
- $sku['unit'] = !empty($sku['unit']) ? $sku['unit'] : $goods['unit'];
- $sku_ids = array_column($sku['attr_list'], 'attr_id');
- asort($sku_ids);
- $sku['pic_url'] = $skuPicList[$sku_ids[0]] ?? '';
- $sku['sign_id'] = implode(':', $sku_ids);
- is_array($sku['reserve_week']) && $sku['reserve_week'] = implode(',',$sku['reserve_week']);
- is_array($sku['reserve_date']) && $sku['reserve_date'] = json_encode($sku['reserve_date'],JSON_UNESCAPED_SLASHES);
- is_array($sku['reserve_time_slot']) && $sku['reserve_time_slot'] = json_encode($sku['reserve_time_slot'],JSON_UNESCAPED_SLASHES);
- is_array($sku['reserve_time_slot_price']) && $sku['reserve_time_slot_price'] = json_encode($sku['reserve_time_slot_price'],JSON_UNESCAPED_SLASHES);
- $sku['reserve_max_buy'] = $sku['reserve_max_buy'] > 0 ? $sku['reserve_max_buy'] : 0;
- $total_stock = $total_stock + $sku['reserve_max_buy'];
- $sku_group_name_list = array_column($sku['attr_list'], 'attr_group_name');
- $sku_name_list = array_column($sku['attr_list'], 'attr_name');
- $sku_name_arr = [];
- foreach ($sku_group_name_list as $index => $sku_group_name) {
- $sku_name_arr[] = $sku_group_name . ':' . $sku_name_list[$index];
- }
- $sku['name'] = implode(',', $sku_name_arr);
- if (!empty($sku['sign_id'])) {
- $model = self::findOne(['sign_id' => $sku['sign_id'], 'goods_id' => $goods['id']]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->goods_id = $goods['id'];
- }
- } else {
- $model = new self();
- $model->loadDefaultValues();
- $model->goods_id = $goods['id'];
- }
- if (!$model->load($sku, '') || !$model->save()) {
- throw new Exception($model->getErrorMessage());
- }
- // 写入预约时间
- $reserveGoodsSku = ArrayHelper::toArray($model);
- $res = StoreGoodsReserveTime::setSkuTime($reserveGoodsSku);
- if ($res === false) throw new Exception('处理预约商品预约时间异常,msg:' . StoreGoodsReserveTime::getStaticError());
- }
- //修改商品总库存
- if($total_stock > 0){
- $goods = StoreGoods::findOne($goods['id']);
- $goods->stock = $goods->stock + $total_stock;
- if (!$goods->save()) throw new Exception($goods->getErrorMessage());
- }
- }
- return true;
- } catch (Exception $e) {
- if (isset($trans)) $trans->rollback();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|