WriteOffMember.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace addons\WriteOff\common\models;
  3. use Yii;
  4. use common\models\user\User;
  5. use common\enums\StatusEnum;
  6. /**
  7. * This is the model class for table "{{%addons_write_off_member}}".
  8. *
  9. * @property int $id ID
  10. * @property int $mall_id 商城ID
  11. * @property int $user_id 用户ID
  12. * @property int $num 核销数量
  13. * @property int $total_num 累计核销数量
  14. * @property int $code 核销码
  15. * @property string $code_url 核销二维码
  16. * @property int $status 状态[-1删除; 0禁用; 1正常]
  17. * @property int $created_at 创建时间
  18. * @property int $updated_at 更新时间
  19. */
  20. class WriteOffMember extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_write_off_member}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'user_id', 'num', 'total_num', 'code', 'status', 'created_at', 'updated_at'], 'integer'],
  36. [['code_url'], 'string', 'max' => 255],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => 'Mall ID',
  47. 'user_id' => 'User ID',
  48. 'num' => 'Num',
  49. 'total_num' => 'Total Num',
  50. 'code' => 'Code',
  51. 'code_url' => 'Code Url',
  52. 'status' => 'Status',
  53. 'created_at' => 'Created At',
  54. 'updated_at' => 'Updated At',
  55. ];
  56. }
  57. public function getUser()
  58. {
  59. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id');
  60. }
  61. public static function setData(array $params){
  62. try{
  63. if(!empty($params['user_id'])){
  64. $model = self::findOne(['user_id' => $params['user_id'],'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  65. if(empty($model)) {
  66. $model = new self();
  67. $model->loadDefaultValues();
  68. $model->mall_id = $params['mall_id'];
  69. }
  70. } else {
  71. throw new \Exception('数据不存在或已删除');
  72. }
  73. if(!$model->load($params,'') || !$model->save()){
  74. throw new \Exception($model->getErrorMessage());
  75. }
  76. return $model;
  77. }catch(\Exception $e){
  78. self::setStaticError($e->getMessage());
  79. return false;
  80. }
  81. }
  82. }