| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace addons\CardSecret\common\models;
- use addons\CardSecret\common\models\Cate;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_CardSecret}}".
- *
- * @property int $id
- * @property int|null $mall_id 商城ID
- * @property string|null $name 卡号名称
- * @property string|null $password 卡号密码
- * @property int|null $use_status 使用状态 (1.未领取,2.已领取,3.已失效)
- * @property int|null $status 状态(-1是已删除,0是禁用,1是正常)
- * @property int|null $created_at 创建时间
- * @property int|null $updated_at 更新时间
- * @property int|null $cate_id 分类ID
- */
- class CardSecret extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_card_secret}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'use_status', 'status', 'created_at', 'updated_at', 'cate_id'], 'integer'],
- [['name', 'password'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'name' => '卡号名称',
- 'password' => '卡号密码',
- 'use_status' => '使用状态 (1.未领取,2.待领取,3已领取,4.已失效)',
- 'status' => '状态(-1是已删除,0是禁用,1是正常)',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'cate_id' => '分类ID',
- ];
- }
- /**
- * 保存数据
- * @Author: ltm
- * @DateTime: 2022/5/6 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return string
- */
- public static function setData(array $params){
- try{
- $is_add = 0;
- $is_detele = 0;
- $find_model = self::findOne(['password'=>$params['password'],'mall_id'=>$params['mall_id'],'status'=> StatusEnum::ENABLED]);
- if (!empty($params['id'])) {
- $model = self::findOne(['id'=>$params['id'],'mall_id'=>$params['mall_id']]);
- if($find_model && $model->password != $params['password']) throw new Exception('卡密已经存在,请重新输入卡密');
- if($params['status'] == StatusEnum::DELETE){
- $is_detele = 1;
- }
- if (empty($model)) throw new Exception('记录不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $is_add = 1;
- if($find_model) throw new Exception('卡密已经存在,请重新输入卡密');
- }
- $model->mall_id = $params['mall_id'];
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- if($is_detele){
- $cate_model = Cate::findOne(['id'=>$model['cate_id'],'mall_id'=>$params['mall_id']]);
- $cate_model-> no_use_num = $cate_model-> no_use_num - 1;
- $cate_model->save();
- }
- if($is_add){
- $cate_model = Cate::findOne(['id'=>$params['cate_id'],'mall_id'=>$params['mall_id']]);
- $cate_model-> no_use_num = $cate_model-> no_use_num + 1;
- $cate_model->save();
- }
- return $model;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|