| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace addons\ElectronCoupon\common\models;
- use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
- use common\enums\StatusEnum;
- use common\models\base\User;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_electron_coupon_goods}}".
- *
- * @property int $id 主键id
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- * @property int $goods_id 商品id
- * @property string|null $e_coupon_id 赠送的电子券ID;
- * @property int $is_open 是否开启
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $mall_id 商城id
- * @property int|null $num 发行数量
- */
- class ElectronCouponGoods extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_electron_coupon_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['created_at', 'updated_at', 'goods_id', 'is_open', 'status', 'mall_id', 'num','e_coupon_id', 'send_type'], 'integer'],
- [['goods_id', 'mall_id'], 'required'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键id',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'goods_id' => '商品id',
- 'e_coupon_id' => '赠送的电子券ID;',
- 'is_open' => '是否开启',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'mall_id' => '商城id',
- 'num' => '发行数量',
- 'send_type' => '赠送类型',
- ];
- }
- /**
- * 保存数据
- * @Author: ltm
- * @DateTime: 2022/6/27 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return string
- */
- public static function setGoodsData($mall_id,$goods_id,$params){
- try{
- $model = self::findOne(['goods_id'=>$goods_id,'e_coupon_id'=> $params['e_coupon_id'],'mall_id'=>$mall_id]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- $model->mall_id = $mall_id;
- $model->goods_id = $goods_id;
- //$model->cate_id = $params['cate_id'];
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- return $model;
- }catch(Exception $e){
- //var_dump($e->getMessage());
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 设置商品无效
- * @Author: ltm
- * @DateTime: 2022/6/27 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return string
- */
- public static function setCanceGoods($params){
- $model = self::updateAll(['status' => StatusEnum::DISABLED],['mall_id'=>$params['mall_id'],'goods_id'=>$params['goods_id']]);
- return $model;
- }
- /**
- * 关联用户信息
- * @Author:ltm
- * @DateTime: 2022/6/28 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,username,mobile,avatar_url');
- }
- /**
- * 关联活动信息
- * @Author:ltm
- * @DateTime: 2022/6/28 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getCoupon()
- {
- return $this->hasOne(ElectronCoupon::class, ['id' => 'e_coupon_id']);
- }
- /**
- * 购买商品赠送电子券
- * @param $mall_id
- * @param $user_id
- * @param $order_detail
- */
- public static function sendUserCoupon($mall_id, $user_id, $order_detail)
- {
- foreach ($order_detail as $k => $v){
- $coupon_goods = self::find()
- ->where(['goods_id' => $v['goods_id']])
- ->andWhere(['mall_id' => $mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['is_open' => ElectronCouponEnums::OPEN_GOODS])
- ->asArray()
- ->all();
- if (!$coupon_goods) {
- continue;
- }
- foreach ($coupon_goods as $item) {
- $receive_data = [
- 'e_coupon_id' => $item['e_coupon_id'],
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'get_type' => ElectronCouponEnums::COUPON_GET_TYPE_GOODS,
- 'goods_id' => $v['goods_id'],
- 'pieces_amount' => (int) ($v['num'] * $item['num'])
- ];
- ElectronCouponUserCoupon::userReceiveCoupon($receive_data);
- }
- }
- }
- /**
- * 通过订单详情赠送电子券
- *
- * @param integer $mall_id
- * @param integer $user_id
- * @param array $detail
- * @return void
- */
- public static function sendUserCouponByDetail(int $mall_id, int $user_id, array $detail)
- {
- $coupon_goods = self::find()
- ->where(['goods_id' => $detail['goods_id']])
- ->andWhere(['mall_id' => $mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['is_open' => ElectronCouponEnums::OPEN_GOODS])
- ->asArray()
- ->all();
- if (empty($coupon_goods)) return;
-
- foreach ($coupon_goods as $item) {
- $receive_data = [
- 'e_coupon_id' => $item['e_coupon_id'],
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'get_type' => ElectronCouponEnums::COUPON_GET_TYPE_GOODS,
- 'goods_id' => $detail['goods_id'],
- 'pieces_amount' => (int) ($detail['num'] * $item['num'])
- ];
- ElectronCouponUserCoupon::userReceiveCoupon($receive_data);
- }
- }
- /**
- * 获取商品绑定电子券赠送类型
- *
- * @param integer $mall_id
- * @param integer $goods_id
- * @return string|int|null|false the value of the first column in the first row of the query result.
- */
- public static function getGoodsSendType(int $mall_id, int $goods_id)
- {
- return static::find()
- ->where([
- 'is_open' => ElectronCouponEnums::OPEN_GOODS,
- 'goods_id' => $goods_id,
- 'mall_id' => $mall_id,
- 'status' => StatusEnum::ENABLED
- ])
- ->select(['send_type'])
- ->scalar();
- }
- }
|