BossAchievementMonth.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace addons\BossAchievement\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\User;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%addons_boss_achievement_month}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $user_id 用户ID
  13. * @property float|null $achievement 当月总业绩,单位:元
  14. * @property float $bonus 可得分红,单位:元
  15. * @property float $settlement_money 结算金额,单位:元
  16. * @property int $settlement_num 结算人数
  17. * @property float $standard_money 考核要求,单位:元
  18. * @property int $month 月份,如:202207
  19. * @property int $is_settlement 是否已结算[0=否,1=是]
  20. * @property int $status 状态[-1:删除;0:禁用;1启用]
  21. * @property int $created_at
  22. * @property int $updated_at
  23. */
  24. class BossAchievementMonth extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_boss_achievement_month}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id', 'user_id', 'settlement_num', 'month', 'is_settlement', 'status', 'created_at', 'updated_at'], 'integer'],
  40. [['achievement', 'bonus', 'settlement_money','standard_money'], 'number'],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => '商城ID',
  51. 'user_id' => '用户ID',
  52. 'achievement' => '当月总业绩,单位:元',
  53. 'bonus' => '可得分红,单位:元',
  54. 'settlement_money' => '结算金额,单位:元',
  55. 'settlement_num' => '结算人数',
  56. 'standard_money' => '考核要求,单位:元',
  57. 'month' => '月份,如:202207',
  58. 'is_settlement' => '是否已结算[0=否,1=是]',
  59. 'status' => '状态[-1:删除;0:禁用;1启用]',
  60. 'created_at' => 'Created At',
  61. 'updated_at' => 'Updated At',
  62. ];
  63. }
  64. /**
  65. * 关联用户信息
  66. * @Author: lun
  67. * @DateTime: 2022/7/10 0010 17:46
  68. * @Copyright: copyright (c) 2021 广东七件事集团
  69. * @return \yii\db\ActiveQuery
  70. */
  71. public function getUser()
  72. {
  73. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,mobile,avatar_url');
  74. }
  75. /**
  76. * 保存数据
  77. * @Author: lun
  78. * @DateTime: 2022/7/10 0010 16:24
  79. * @Copyright: copyright (c) 2021 广东七件事集团
  80. * @param $params
  81. * @return bool
  82. */
  83. public static function setData($params)
  84. {
  85. try {
  86. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'month' => $params['month'], 'status' => StatusEnum::ENABLED]);
  87. if (empty($model)) {
  88. $model = new self();
  89. $model->mall_id = $params['mall_id'];
  90. $model->user_id = $params['user_id'];
  91. $model->month = $params['month'];
  92. $model->loadDefaultValues();
  93. }
  94. if (!empty($params['goods_price'])) $model->achievement += $params['goods_price'];
  95. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  96. return true;
  97. } catch (\Exception $e) {
  98. self::setStaticError($e->getMessage());
  99. return false;
  100. }
  101. }
  102. /**
  103. * 批量插入数据
  104. * @Author: lun
  105. * @DateTime: 2022/7/10 0010 16:28
  106. * @Copyright: copyright (c) 2021 广东七件事集团
  107. * @param $parent_ids
  108. * @param $params
  109. */
  110. public static function batchSetData($parent_ids, $params)
  111. {
  112. try {
  113. foreach ($parent_ids as $user_id) {
  114. $params['user_id'] = $user_id;
  115. $res = self::setData($params);
  116. if ($res === false) throw new Exception(self::getStaticError());
  117. }
  118. return true;
  119. } catch (\Exception $e) {
  120. self::setStaticError($e->getMessage());
  121. return false;
  122. }
  123. }
  124. }