WriteOffMemberCode.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace addons\WriteOff\common\models;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. /**
  6. * This is the model class for table "{{%addons_write_off_member_code}}".
  7. *
  8. * @property int $id ID
  9. * @property int $mall_id 商城ID
  10. * @property int $user_id 用户ID
  11. * @property int $code 核销码
  12. * @property int $is_used 是否已核销[0否; 1是]
  13. * @property int $status 状态[-1删除; 0禁用; 1正常]
  14. * @property int $created_at 创建时间
  15. * @property int $updated_at 更新时间
  16. */
  17. class WriteOffMemberCode extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%addons_write_off_member_code}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id', 'user_id', 'code', 'is_used', 'status', 'created_at', 'updated_at'], 'integer'],
  33. ];
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function attributeLabels()
  39. {
  40. return [
  41. 'id' => 'ID',
  42. 'mall_id' => 'Mall ID',
  43. 'user_id' => 'User ID',
  44. 'code' => 'Code',
  45. 'is_used' => 'Is Used',
  46. 'status' => 'Status',
  47. 'created_at' => 'Created At',
  48. 'updated_at' => 'Updated At',
  49. ];
  50. }
  51. /**
  52. * 保存数据
  53. * @Author: lun
  54. * @DateTime: 2021/10/20 0020 18:01
  55. * @Copyright: copyright (c) 2021 广东七件事集团
  56. * @param array $params
  57. * @return AddonsBossLevel|bool
  58. */
  59. public static function setData(array $params){
  60. $trans = Yii::$app->db->beginTransaction();
  61. try{
  62. if(!empty($params['id'])){
  63. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  64. if(empty($model)) throw new \Exception('数据不存在或已删除');
  65. }else{
  66. $model = new self();
  67. $model->loadDefaultValues();
  68. $model->mall_id = $params['mall_id'];
  69. }
  70. if(!$model->load($params,'') || !$model->save()){
  71. throw new \Exception($model->getErrorMessage());
  72. }
  73. $trans->commit();
  74. return $model;
  75. }catch(\Exception $e){
  76. $trans->rollBack();
  77. self::$static_error = $e->getMessage();
  78. return false;
  79. }
  80. }
  81. }