ElectronCouponApprovalIncomeLog.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace addons\ElectronCoupon\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use common\models\user\User;
  6. /**
  7. * This is the model class for table "{{%addons_electron_coupon_approval_income_log}}".
  8. *
  9. * @property int $id 自增id
  10. * @property int $e_coupon_code_id 核销码id
  11. * @property int $user_e_coupon_id 会员电子券id
  12. * @property int $e_coupon_id 电子券id
  13. * @property int $created_at
  14. * @property int $updated_at
  15. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int $user_id 使用会员user_id
  17. * @property string $e_coupon_name 电子券名称
  18. * @property int $amount 数量
  19. * @property int|null $mall_id 商城id
  20. * @property int $price_type 收益类型1积分2佣金
  21. * @property float $commission 积分或佣金金额
  22. * @property int $clerk_user_id 核销员user_id
  23. */
  24. class ElectronCouponApprovalIncomeLog extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_electron_coupon_approval_income_log}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['e_coupon_code_id', 'user_e_coupon_id', 'e_coupon_id', 'user_id', 'e_coupon_name', 'clerk_user_id'], 'required'],
  40. [['e_coupon_code_id', 'user_e_coupon_id', 'e_coupon_id', 'created_at', 'updated_at', 'status', 'user_id', 'amount', 'mall_id', 'price_type', 'clerk_user_id', 'clerk_admin_id'], 'integer'],
  41. [['commission'], 'number'],
  42. [['e_coupon_name'], 'string', 'max' => 255],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => '自增id',
  52. 'e_coupon_code_id' => '核销码id',
  53. 'user_e_coupon_id' => '会员电子券id',
  54. 'e_coupon_id' => '电子券id',
  55. 'created_at' => 'Created At',
  56. 'updated_at' => 'Updated At',
  57. 'status' => '状态[-1:删除;0:禁用;1启用]',
  58. 'user_id' => '使用会员user_id',
  59. 'e_coupon_name' => '电子券名称',
  60. 'amount' => '数量',
  61. 'mall_id' => '商城id',
  62. 'price_type' => '收益类型1积分2佣金',
  63. 'commission' => '积分或佣金金额',
  64. 'clerk_user_id' => '核销员user_id',
  65. 'clerk_admin_id' => '核销员admin_id(后台核销)',
  66. ];
  67. }
  68. public function getUser()
  69. {
  70. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,username');
  71. }
  72. /**
  73. * 数据设置
  74. */
  75. public static function setData(array $params)
  76. {
  77. try{
  78. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  79. $mall_id = $params['mall_id'];
  80. if (isset($params['id']) && !empty($params['id'])) {
  81. $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  82. if(empty($model)) throw new Exception('收益记录不存在或已删除');
  83. } else {
  84. $model = new self();
  85. $model->loadDefaultValues();
  86. $model->mall_id = $mall_id;
  87. }
  88. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  89. return $model;
  90. }catch(Exception $e){
  91. self::setStaticError($e->getMessage());
  92. return false;
  93. }
  94. }
  95. }