ElectronCouponUsageLog.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace addons\ElectronCoupon\common\models;
  3. use Yii;
  4. class ElectronCouponUsageLog extends \common\models\base\BaseActiveRecord
  5. {
  6. public static function tableName()
  7. {
  8. return '{{%addons_electron_coupon_usage_log}}';
  9. }
  10. public function rules()
  11. {
  12. return [
  13. [['e_coupon_id', 'user_id', 'mall_id', 'user_e_coupon_id'], 'required'],
  14. [['e_coupon_id', 'user_id', 'mall_id', 'user_e_coupon_id', 'clerk_user_id', 'clerk_admin_id', 'proper_user_id', 'receive_time', 'usage_time', 'status', 'created_at', 'updated_at', 'amount'], 'integer'],
  15. [['e_coupon_name'], 'string'],
  16. ];
  17. }
  18. public function attributeLabels()
  19. {
  20. return [
  21. 'id' => 'ID',
  22. 'mall_id' => '商城id',
  23. 'e_coupon_id' => '电子券id',
  24. 'e_coupon_name' => '电子券名称',
  25. 'user_id' => '使用者',
  26. 'clerk_user_id' => '核销员',
  27. 'clerk_admin_id' => '核销员(管理员)',
  28. 'proper_user_id' => '专有者',
  29. 'receive_time' => '领取时间',
  30. 'usage_time' => '使用时间',
  31. 'amount' => '兑换数量',
  32. 'status' => '状态[-1:删除;0:禁用;1启用]',
  33. 'created_at' => '创建时间',
  34. 'updated_at' => '更新时间',
  35. ];
  36. }
  37. /**
  38. * 数据设置
  39. */
  40. public static function setData(array $params)
  41. {
  42. try{
  43. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  44. $mall_id = $params['mall_id'];
  45. if (isset($params['id']) && !empty($params['id'])) {
  46. $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  47. if(empty($model)) throw new Exception('使用记录不存在或已删除');
  48. } else {
  49. $model = new self();
  50. $model->loadDefaultValues();
  51. $model->mall_id = $mall_id;
  52. }
  53. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  54. return $model;
  55. }catch(Exception $e){
  56. self::setStaticError($e->getMessage());
  57. return false;
  58. }
  59. }
  60. }