Certificate.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace addons\Certificate\common\models;
  3. use Yii;
  4. use yii\db\Exception;
  5. /**
  6. * This is the model class for table "{{%addons_certificate}}".
  7. *
  8. * @property int $id
  9. * @property int|null $mall_id 商城ID
  10. * @property string|null $name 证书名称
  11. * @property int|null $download_num 下载次数
  12. * @property string|null $content 证书设置 json形式保存
  13. * @property string|null $bg 证书背景
  14. * @property string|null $status 状态(-1是已删除,0是禁用,1是正常)
  15. * @property int|null $created_at 创建时间
  16. * @property int|null $updated_at 更新时间
  17. */
  18. class Certificate extends \common\models\base\BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_certificate}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'status', 'download_num', 'created_at', 'updated_at'], 'integer'],
  34. [['content', 'member_level'], 'string'],
  35. [['name', 'bg'], 'string', 'max' => 255],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => '商城ID',
  46. 'name' => '证书名称',
  47. 'download_num' => '下载次数',
  48. 'content' => '证书设置 json形式保存',
  49. 'member_level' => '允许查看及保存的会员等级',
  50. 'bg' => '证书背景',
  51. 'status' => '状态(-1是已删除,0是禁用,1是正常)',
  52. 'created_at' => '创建时间',
  53. 'updated_at' => '更新时间',
  54. ];
  55. }
  56. /**
  57. * 保存数据
  58. * @Author: ltm
  59. * @DateTime: 2022/5/8 0022 17:13
  60. * @Copyright: copyright (c) 2021 广东七件事集团
  61. * @param array $params
  62. * @return string
  63. */
  64. public static function setData(array $params){
  65. try{
  66. if (isset($params['member_level']) && is_array($params['member_level'])) {
  67. $params['member_level'] = array_filter($params['member_level'], function ($item) {
  68. return trim($item) !== '';
  69. });
  70. $params['member_level'] = implode(',', $params['member_level']);
  71. }
  72. if (!empty($params['id'])) {
  73. $model = self::findOne(['id'=>$params['id'],'mall_id'=>$params['mall_id']]);
  74. if (empty($model)) throw new Exception('记录不存在或已删除');
  75. }else{
  76. $model = new self();
  77. $model->loadDefaultValues();
  78. }
  79. $model->mall_id = $params['mall_id'];
  80. if(!$model->load($params,'') || !$model->save()){
  81. throw new Exception($model->getErrorMessage());
  82. }
  83. return $model;
  84. }catch(\Exception $e){
  85. self::$static_error = $e->getMessage();
  86. return false;
  87. }
  88. }
  89. }