HappinessClerk.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace addons\Happiness\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\user\User;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_store_clerk".
  9. *
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property int $store_id 门店id
  13. * @property int $user_id 会员id
  14. * @property int $goods_id o2o商品id
  15. * @property int $nums 累计核销订单数
  16. * @property float $total_money 累计核销金额
  17. * @property string $remark 备注
  18. * @property int $status 状态[0禁用 1启用 -1删除]
  19. * @property int $created_at 创建时间
  20. * @property int $updated_at 修改时间
  21. */
  22. class HappinessClerk extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_happiness_store_clerk}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'store_id', 'user_id', 'nums', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['total_money'], 'number'],
  39. [['remark'], 'string', 'max' => 255],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'mall_id' => 'Mall ID',
  50. 'store_id' => '门店id',
  51. 'user_id' => '会员id',
  52. 'nums' => '累计核销订单数',
  53. 'total_money' => '累计核销金额',
  54. 'remark' => '备注',
  55. 'status' => '状态[0禁用 1启用 -1删除]',
  56. 'created_at' => '创建时间',
  57. 'updated_at' => '修改时间',
  58. ];
  59. }
  60. public function getUser(){
  61. return $this->hasOne(User::class, ['id' => 'user_id']);
  62. }
  63. public function getStore()
  64. {
  65. return $this->hasOne(HappinessStore::class, ['id' => 'store_id', 'mall_id' => 'mall_id']);
  66. }
  67. /**
  68. * @desc:保存数据
  69. * @Author: hua
  70. * @Date: 2021/10/23
  71. * @Time: 18:07
  72. * @Copyright: copyright (c) 2021 广东七件事集团
  73. * @param array $params
  74. * @return StoreClerk|false|null
  75. */
  76. public static function setData(array $params)
  77. {
  78. try {
  79. if (!empty($params['mall_id']) && !empty($params['user_id'])) {
  80. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  81. if (empty($model)) {
  82. $model = new self();
  83. $model->loadDefaultValues();
  84. }
  85. } else {
  86. $model = new self();
  87. $model->loadDefaultValues();
  88. }
  89. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  90. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  91. return $model;
  92. } catch (\Exception $e) {
  93. self::$static_error = $e->getMessage();
  94. return false;
  95. }
  96. }
  97. public static function updateData(array $params)
  98. {
  99. try {
  100. if (!empty($params['id'])) {
  101. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  102. if (empty($model)) throw new \Exception('服务不存在或已删除');
  103. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  104. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  105. return $model;
  106. }
  107. } catch (\Exception $e) {
  108. self::$static_error = $e->getMessage();
  109. return false;
  110. }
  111. }
  112. /**
  113. * 维护已核销的订单数和已核销的总金额
  114. *
  115. * @param int $id
  116. * @param int $nums
  117. * @param float $totalMoney
  118. *
  119. * @return void
  120. */
  121. public static function incrNumsAndTotalMoney(int $id, int $nums, float $totalMoney)
  122. {
  123. self::updateAllCounters(['nums' => $nums, 'total_money' => $totalMoney], ['id' => $id]);
  124. }
  125. }