| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- namespace addons\Purchase\common\models;
- use addons\Purchase\common\enums\PurchaseEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use common\models\goods\GoodsMarketing;
- use common\models\user\UserWallet;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_purchase_goods}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $goods_id 商品ID
- * @property string $exchange_price 兑换价说明
- * @property int $exchange_num 已兑换数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class PurchaseGoods extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_purchase_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'exchange_num', 'status', 'created_at', 'updated_at'], 'integer'],
- [['exchange_price'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'goods_id' => '商品ID',
- 'exchange_price' => '兑换价说明',
- 'exchange_num' => '已兑换数量',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * @notes:管理主商城商品
- * @return \yii\db\ActiveQuery
- * @author: lun
- * @Time: 2022/10/22 10:31
- */
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name,cover_pic,price,is_on_sale,status');
- }
- /**
- * @notes:关联规格
- * @return \yii\db\ActiveQuery
- * @author: lun
- * @Time: 2022/10/22 11:05
- */
- public function getAttr()
- {
- return $this->hasMany(PurchaseGoodsAttr::class, ['goods_id' => 'goods_id'])->where(['status' => StatusEnum::ENABLED]);
- }
- /**
- * @notes:关联分类
- * @return \yii\db\ActiveQuery
- * @author: lun
- * @Time: 2022/10/22 13:52
- */
- public function getCats()
- {
- return $this->hasMany(PurchaseCate::class, ['id' => 'cate_id'])
- ->via('purchaseGoodsCateRelate');
- }
- /**
- * @notes:关联分类
- * @return \yii\db\ActiveQuery
- * @author: lun
- * @Time: 2022/10/22 16:25
- */
- public function getPurchaseGoodsCateRelate()
- {
- return $this->hasMany(PurchaseGoodsCateRelate::class, ['goods_id' => 'goods_id']);
- }
- /**
- * @notes:数据保存
- * @param $mall_id
- * @param $params
- * @return bool
- * @author: lun
- * @Time: 2022/10/22 16:00
- */
- public static function setData($mall_id, $params)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- // 查询是否已存在该商品
- $goods = self::find()->where(['mall_id' => $mall_id, 'goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED])->asArray()->one();
- if (!empty($params['id'])) {
- $model = self::findOne(['mall_id' => $mall_id, 'id' => $params['id']]);
- if (empty($model)) throw new \Exception('查无此商品');
- if ($model->id != $goods['id'] && $goods['goods_id'] == $model->goods_id) throw new \Exception('已存在该商品,请另选商品');
- } else {
- $model = new self();
- $model->mall_id = $mall_id;
- $model->loadDefaultValues();
- }
- $model->goods_id = $params['goods_id'];
- $params['goods_id'] != $model->goods_id && $model->exchange_num = 0;
- // 获取商品兑换价
- $exchange = $params['attr'][0] ?? [];
- $model->exchange_price = '最低抵扣:'.($exchange['min_limit'] === 'false' ? $exchange['min_score'] : '无限制').',最高抵扣:'.($exchange['max_limit'] === 'false' ? $exchange['max_score'] : '无限制');
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // 保存商品分类
- if (!empty($params['cats'])) {
- $res = PurchaseGoodsCateRelate::setData($model->goods_id, $params['cats']);
- if ($res === false) throw new \Exception(PurchaseGoodsCateRelate::getStaticError());
- }
- // 保存商品规格
- if (!empty($params['attr'])) {
- $res = PurchaseGoodsAttr::setData($params['attr']);
- if ($res === false) throw new \Exception(PurchaseGoodsAttr::getStaticError());
- }
- // 保存商品无法编辑设置
- $installData = [
- 'mall_id' => $mall_id,
- 'goods_id' => $model->goods_id,
- 'marketing_id' => $model->id,
- 'status' => StatusEnum::ENABLED,
- 'marketing_type' => PurchaseEnum::ADDONS_NAME
- ];
- $res = GoodsMarketing::setData($installData);
- if ($res === false) throw new \Exception(GoodsMarketing::getStaticError());
- // 商品无法搜索设置
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes:商品删除
- * @param $mall_id
- * @param $id
- * @return bool
- * @author: lun
- * @Time: 2022/10/22 16:58
- */
- public static function del($mall_id, $id)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $model = self::findOne(['mall_id' => $mall_id, 'id' => $id, 'status' => StatusEnum::ENABLED]);
- if (empty($model)) throw new \Exception('该商品已被删除');
- // 删除商品
- $model->status = StatusEnum::DELETE;
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // 删除分类
- PurchaseGoodsCateRelate::deleteAll(['goods_id' => $model->goods_id]);
- // 删除规格
- PurchaseGoodsAttr::updateAll(['status' => StatusEnum::DELETE, 'updated_at' => time()], ['goods_id' => $model->goods_id]);
- // 删除商品无法编辑
- $installData = [
- 'mall_id' => $mall_id,
- 'goods_id' => $model->goods_id,
- 'marketing_id' => $model->id,
- 'status' => StatusEnum::DELETE,
- 'marketing_type' => PurchaseEnum::ADDONS_NAME
- ];
- $res = GoodsMarketing::setData($installData);
- if ($res === false) throw new \Exception(GoodsMarketing::getStaticError());
- // 删除商品无法搜索
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes:获取兑换价格
- * @param $goods
- * @param $score_config
- * @param $num
- * @param $user_id
- * @return array
- * @author: lun
- * @Time: 2022/10/25 18:36
- */
- public static function getExchangePrice($goods, $score_config, $num = 1, $user_id = 0)
- {
- $data = ['status' => 1, 'deduct_score' => 0, 'deduct_money' => 0, 'score_name' => $score_config['score_name']];// 初始化
- // 获取属性设置
- $attr = PurchaseGoodsAttr::find()->where(['goods_id' => $goods['goods_id'], 'goods_attr_id' => $goods['goods_attr_id'], 'status' => StatusEnum::ENABLED])->asArray()->one();
- if (empty($attr)) throw new \Exception('该商品的积分抵扣设置不存在');
- $data['goods_id'] = $attr['goods_id'];
- $deduct_ratio = $score_config['score_deduct_ratio'];
- $min_limit = $attr['min_limit'];
- $min_score = $attr['min_score'];
- $max_limit = $attr['max_limit'];
- $max_score = $attr['max_score'];
- // 最低抵扣不做限制,则最低零点抵扣
- $low_score = empty($min_limit) ? $min_score * $num : 0;// 最低积分
- // 最高抵扣不做限制,则最高抵扣为商品原价
- if ($max_limit == 0) {
- $high_price = ceil($max_score / $deduct_ratio) * $num;// 最高价
- if ($high_price >= $goods['price'] * $num) {// 设置的最高价若大于等级商品售价则显示商品售价
- $high_score = ceil($goods['price'] * $deduct_ratio * $num);// 最高积分
- } else {
- $high_score = $max_score * $num;// 最高积分
- }
- } else {
- $high_score = ceil($goods['price'] * $deduct_ratio * $num);// 最高积分
- }
- // 获取用户的现有积分
- if ($score_config['score'] < $low_score) {// 不够积分情况
- $data['deduct_score'] = $low_score;// 支付的积分
- $data['deduct_money'] = ceil($low_score / $deduct_ratio);// 支付的金额
- $min_limit == 0 && $data['status'] = 0;// 设置了最低积分,若不够积分则不能下单
- } elseif ($score_config['score'] >= $low_score && $score_config['score'] < $high_score) {// 积分足够情况
- $data['deduct_score'] = $score_config['score'];// 支付的积分
- $data['deduct_money'] = ceil($score_config['score'] / $deduct_ratio);// 支付的金额
- } else {// 积分足够并且超出最大设置
- $data['deduct_score'] = $high_score;// 支付的积分
- $data['deduct_money'] = ceil($high_score / $deduct_ratio);// 支付的金额
- }
- return $data;
- }
- /**
- * @notes:批量添加商品
- * @param $mall_id
- * @param $params
- * @return bool
- * @author: lun
- * @Time: 2022/11/15 14:45
- */
- public static function batchSetData($mall_id, $params)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $attr_setting = $params['attr'];
- $score_deduct_ratio = Yii::$app->services->setting->mall->get($mall_id, 'score.score_deduct_ratio');
- foreach ($params['goods'] as $item) {
- // 查询是否已存在该商品
- $check = self::find()->where(['mall_id' => $mall_id, 'goods_id' => $item['id'], 'status' => StatusEnum::ENABLED])->asArray()->one();
- if (!empty($check)) throw new \Exception('商品ID=' . $item['id'] . '的商品已存在,请勿重复添加');
- // 获取该商品的所有规格
- $where = [];
- $where[] = ['=', 'goods_id', $item['id']];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $attrs = GoodsAttr::getGoodsAttrs($where, [], true, ['id as goods_attr_id', 'goods_id', 'name', 'price','pic_url']);
- if (empty($attrs)) throw new \Exception('商品ID=' . $item['id'] . '的商品商品规格不存在,请检查主商城商品信息');
- $attr_setting['min_limit'] === 'false' && $min_score = ceil($attr_setting['min_score'] * $score_deduct_ratio * $attrs[0]['price']) / 100;
- $attr_setting['max_limit'] === 'false' && $max_score = ceil($attr_setting['max_score'] * $score_deduct_ratio * $attrs[0]['price']) / 100;
- // 新增数据
- $model = new self();
- $model->mall_id = $mall_id;
- $model->goods_id = $item['id'];
- $model->exchange_price = '最低抵扣:'.($attr_setting['min_limit'] === 'true' ? '无限制' : $min_score).',最高抵扣:'.($attr_setting['max_limit'] === 'true' ? '无限制' : $max_score);
- $model->loadDefaultValues();
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // 保存商品分类
- if (!empty($params['cats'])) {
- $res = PurchaseGoodsCateRelate::setData($model->goods_id, $params['cats'], 'batch');
- if ($res === false) throw new \Exception(PurchaseGoodsCateRelate::getStaticError());
- }
- // 保存商品规格
- $res = PurchaseGoodsAttr::percentSetData($attrs, $attr_setting, $score_deduct_ratio);
- if ($res === false) throw new \Exception(PurchaseGoodsAttr::getStaticError());
- // 保存商品无法编辑设置
- $installData = [
- 'mall_id' => $mall_id,
- 'goods_id' => $model->goods_id,
- 'marketing_id' => $model->id,
- 'status' => StatusEnum::ENABLED,
- 'marketing_type' => PurchaseEnum::ADDONS_NAME
- ];
- $res = GoodsMarketing::setData($installData);
- if ($res === false) throw new \Exception(GoodsMarketing::getStaticError());
- }
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|