| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace common\models\goods;
- use common\enums\StatusEnum;
- use common\helpers\ArrayHelper;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * 预约商品SKU模型
- * This is the model class for table "{{%goods_reserve_sku}}"
- * @property int int $id
- * @property int $mall_id 商城id
- * @property int $mch_id 商家id
- * @property string $name 规格名称
- * @property string $pic_url 规格图片
- * @property string $sign_id 规格ID标识
- * @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 $reserve_week 可预约周(逗号分隔)
- * @property string $reserve_date 可预约日期
- * @property int $reserve_time_range_type 预约模式(1按天预约 2按时间段预约)
- * @property int $time_slot_type 时间段划分方式(1自定义划分 2自动划分)
- * @property string $reserve_time_slot 时间段划分
- * @property int $reserve_price_type 价格设置方式(0统一售价 1按日期时间段设置)
- * @property int $reserve_time_slot_price 时间段价格设置
- * @property int $reserve_max_buy 每天最大预约数量
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $service_min 修改时间
- */
- class GoodsReserveSku extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%goods_reserve_sku}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'mch_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'],
- [['name','sign_id','pic_url','reserve_date', 'reserve_time_slot', 'reserve_time_slot_price', 'reserve_week', 'unit'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'mch_id' => '商家id',
- 'name' => '规格名称',
- 'sign_id' => '规格ID标识',
- 'pic_url' => '规格图片',
- 'goods_id' => '商品id',
- 'price' => '售价',
- 'original_price' => '原价(划线价仅展示)',
- 'cost_price' => '成本价',
- 'reserve_date_type' => '可预约时间类型(1范围 2自选日期)',
- 'reserve_week' => '可预约周',
- 'reserve_date' => '可预约日期',
- 'reserve_time_range_type' => '预约模式(1按天预约 2按时间段预约)',
- 'time_slot_type' => '时间段划分方式(1自定义划分 2自动划分)',
- 'reserve_time_slot' => '时间段划分',
- 'reserve_price_type' => '价格设置方式(0统一售价 1按日期时间段设置)',
- 'reserve_time_slot_price' => '时间段价格设置',
- 'reserve_max_buy' => '每天最大预约数量',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- public function getReserve()
- {
- return $this->hasOne(GoodsReserve::class, ['goods_id' => 'goods_id']);
- }
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id']);
- }
- public function getTimes()
- {
- return $this->hasMany(GoodsReserveTime::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 = GoodsReserveTime::setSkuTime($reserveGoodsSku);
- if ($res === false) throw new Exception('处理预约商品预约时间异常,msg:' . GoodsReserveTime::getStaticError());
- if (!empty($reserveGoods['reserve_max_buy'])) { // 如果有最大预约数量
- $goodsModel = Goods::findOne($reserveGoods['goods_id']);
- $goodsModel->stock = $reserveGoods['reserve_max_buy'];
- if (!$goodsModel->save()) {
- throw new Exception('更新商品库存异常');
- }
- }
- $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 GoodsReserve $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['mch_id'] = $goods['mch_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'];
- if (is_array($sku['unit'])) {
- \Yii::error("预约服务单位错误: " . Json::encode(['goods' => $goods, 'skus' => $skus]));
- throw new Exception("单位错误");
- }
- $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 = GoodsReserveTime::setSkuTime($reserveGoodsSku);
- if ($res === false) throw new Exception('处理预约商品预约时间异常,msg:' . GoodsReserveTime::getStaticError());
- }
- //修改商品总库存
- if($total_stock > 0){
- $goods = Goods::findOne($goods['id']);
- $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;
- }
- }
- }
|