CardSecret.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace addons\CardSecret\common\models;
  3. use addons\CardSecret\common\models\Cate;
  4. use common\enums\AddonsEnum;
  5. use common\enums\StatusEnum;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%addons_CardSecret}}".
  10. *
  11. * @property int $id
  12. * @property int|null $mall_id 商城ID
  13. * @property string|null $name 卡号名称
  14. * @property string|null $password 卡号密码
  15. * @property int|null $use_status 使用状态 (1.未领取,2.已领取,3.已失效)
  16. * @property int|null $status 状态(-1是已删除,0是禁用,1是正常)
  17. * @property int|null $created_at 创建时间
  18. * @property int|null $updated_at 更新时间
  19. * @property int|null $cate_id 分类ID
  20. */
  21. class CardSecret extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_card_secret}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'use_status', 'status', 'created_at', 'updated_at', 'cate_id'], 'integer'],
  37. [['name', 'password'], 'string', 'max' => 255],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'name' => '卡号名称',
  49. 'password' => '卡号密码',
  50. 'use_status' => '使用状态 (1.未领取,2.待领取,3已领取,4.已失效)',
  51. 'status' => '状态(-1是已删除,0是禁用,1是正常)',
  52. 'created_at' => '创建时间',
  53. 'updated_at' => '更新时间',
  54. 'cate_id' => '分类ID',
  55. ];
  56. }
  57. /**
  58. * 保存数据
  59. * @Author: ltm
  60. * @DateTime: 2022/5/6 0022 17:13
  61. * @Copyright: copyright (c) 2021 广东七件事集团
  62. * @param array $params
  63. * @return string
  64. */
  65. public static function setData(array $params){
  66. try{
  67. $is_add = 0;
  68. $is_detele = 0;
  69. $find_model = self::findOne(['password'=>$params['password'],'mall_id'=>$params['mall_id'],'status'=> StatusEnum::ENABLED]);
  70. if (!empty($params['id'])) {
  71. $model = self::findOne(['id'=>$params['id'],'mall_id'=>$params['mall_id']]);
  72. if($find_model && $model->password != $params['password']) throw new Exception('卡密已经存在,请重新输入卡密');
  73. if($params['status'] == StatusEnum::DELETE){
  74. $is_detele = 1;
  75. }
  76. if (empty($model)) throw new Exception('记录不存在或已删除');
  77. }else{
  78. $model = new self();
  79. $model->loadDefaultValues();
  80. $is_add = 1;
  81. if($find_model) throw new Exception('卡密已经存在,请重新输入卡密');
  82. }
  83. $model->mall_id = $params['mall_id'];
  84. if(!$model->load($params,'') || !$model->save()){
  85. throw new Exception($model->getErrorMessage());
  86. }
  87. if($is_detele){
  88. $cate_model = Cate::findOne(['id'=>$model['cate_id'],'mall_id'=>$params['mall_id']]);
  89. $cate_model-> no_use_num = $cate_model-> no_use_num - 1;
  90. $cate_model->save();
  91. }
  92. if($is_add){
  93. $cate_model = Cate::findOne(['id'=>$params['cate_id'],'mall_id'=>$params['mall_id']]);
  94. $cate_model-> no_use_num = $cate_model-> no_use_num + 1;
  95. $cate_model->save();
  96. }
  97. return $model;
  98. }catch(\Exception $e){
  99. self::$static_error = $e->getMessage();
  100. return false;
  101. }
  102. }
  103. }