StoreClerk.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace addons\Store\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 StoreClerk extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_store_clerk}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'store_id', 'user_id', 'goods_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. 'goods_id' => 'o2o商品id',
  53. 'nums' => '累计核销订单数',
  54. 'total_money' => '累计核销金额',
  55. 'remark' => '备注',
  56. 'status' => '状态[0禁用 1启用 -1删除]',
  57. 'created_at' => '创建时间',
  58. 'updated_at' => '修改时间',
  59. ];
  60. }
  61. public function getUser(){
  62. return $this->hasOne(User::class, ['id' => 'user_id']);
  63. }
  64. /**
  65. * @desc:保存数据
  66. * @Author: hua
  67. * @Date: 2021/10/23
  68. * @Time: 18:07
  69. * @Copyright: copyright (c) 2021 广东七件事集团
  70. * @param array $params
  71. * @return StoreClerk|false|null
  72. */
  73. public static function setData(array $params)
  74. {
  75. try {
  76. if (!empty($params['mall_id']) && !empty($params['store_id']) && !empty($params['user_id'])) {
  77. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'], 'user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  78. if (empty($model)) {
  79. $model = new self();
  80. $model->loadDefaultValues();
  81. }
  82. } else {
  83. $model = new self();
  84. $model->loadDefaultValues();
  85. }
  86. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  87. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  88. return $model;
  89. } catch (\Exception $e) {
  90. self::$static_error = $e->getMessage();
  91. return false;
  92. }
  93. }
  94. public static function updateData(array $params)
  95. {
  96. try {
  97. if (!empty($params['id'])) {
  98. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  99. if (empty($model)) throw new \Exception('服务不存在或已删除');
  100. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  101. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  102. return $model;
  103. }
  104. } catch (\Exception $e) {
  105. self::$static_error = $e->getMessage();
  106. return false;
  107. }
  108. }
  109. /**
  110. * 维护已核销的订单数和已核销的总金额
  111. *
  112. * @param int $id
  113. * @param int $nums
  114. * @param float $totalMoney
  115. *
  116. * @return void
  117. */
  118. public static function incrNumsAndTotalMoney(int $id, int $nums, float $totalMoney)
  119. {
  120. self::updateAllCounters(['nums' => $nums, 'total_money' => $totalMoney], ['id' => $id]);
  121. }
  122. }