TeamGradesPool.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\models;
  3. use yii\behaviors\TimestampBehavior;
  4. use yii\helpers\ArrayHelper;
  5. /**
  6. * This is the model class for table "{{%team_grades_pool}}".
  7. *
  8. * @property integer $id
  9. * @property integer $user_id
  10. * @property integer $team_grades_id
  11. * @property integer $store_id
  12. * @property integer $start_time
  13. * @property integer $end_time
  14. * @property integer $is_send
  15. * @property integer $send_time
  16. * @property integer $created_at
  17. * @property integer $updated_at
  18. * @property integer $bonus_type
  19. */
  20. class TeamGradesPool extends \yii\db\ActiveRecord
  21. {
  22. const SEND_STATUS_NO = 0;
  23. const SEND_STATUS_YES = 1;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%team_grades_pool}}';
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['id', 'store_id', 'start_time', 'end_time', 'is_send', 'send_time', 'created_at', 'updated_at', 'user_id','team_grades_id', 'bonus_type'], 'integer'],
  38. ];
  39. }
  40. public function behaviors()
  41. {
  42. return [
  43. [
  44. 'class' => TimestampBehavior::class
  45. ]
  46. ];
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => '',
  55. 'store_id' => '商城id',
  56. 'user_id' => '用户id',
  57. 'team_grades_id' => '团队分红人员id',
  58. 'start_time' => '开始时间',
  59. 'end_time' => '结束时间',
  60. 'is_send' => '是否发放',
  61. 'send_time' => '发放时间',
  62. 'created_at' => '',
  63. 'updated_at' => '',
  64. 'bonus_type' => '奖励类型:0=业绩阶梯模式;1=级差模式;',
  65. ];
  66. }
  67. /**
  68. * 还需要获取团队层级 应该是通过设置的层级获取层级内用户是否是团队分红人员 如果是要逐级增加业绩分红
  69. * 判断时间段是否在未发放的奖金池 如果存在就获取当前未发放的奖金池 如果不存在就创建新的并返回
  70. */
  71. public static function getPool($store_id, $user_id, $bonus_type = 0) {
  72. $time = time();
  73. $query = self::find()->alias('p')->where(['p.store_id' => $store_id, 'p.bonus_type' => $bonus_type])
  74. ->leftJoin(['pd' => TeamGradesPoolDetail::tableName()], 'p.id = pd.pool_id');
  75. if($bonus_type == 0) {
  76. $query->andWhere(['OR', ['pd.user_id' => $user_id], ['p.user_id' => $user_id]]);
  77. } elseif($bonus_type == 1) {//如果是级差模式,当用户不一致时单独创建池子
  78. $query->andWhere(['p.user_id' => $user_id]);
  79. }
  80. $pool = $query
  81. ->andWhere(['<=', 'p.start_time', $time])
  82. ->andWhere(['>=', 'p.end_time', $time])
  83. ->andWhere(['p.is_send' => 0])->asArray()->one();
  84. if (!$pool) {
  85. $end_time = 0;
  86. // 获取时间段配置
  87. $team_grades_setting = Option::get('team_grades_setting', $store_id, 'team_grades_setting')['value'];
  88. if (!empty($team_grades_setting)) {
  89. $team_grades_setting = json_decode($team_grades_setting, true);
  90. if (!empty($team_grades_setting)) {
  91. if (intval($team_grades_setting['team_cycle']) > 0) {
  92. // 按天
  93. if (intval($team_grades_setting['team_cycle_type']) === TeamGrades::TEAM_CYCLE_TYPE_DAY) {
  94. $end_time = strtotime('+' . $team_grades_setting['team_cycle'] . ' Day');
  95. }
  96. // 按周
  97. if (intval($team_grades_setting['team_cycle_type']) === TeamGrades::TEAM_CYCLE_TYPE_WEEK) {
  98. $end_time = strtotime('+' . $team_grades_setting['team_cycle'] . ' Week');
  99. }
  100. // 按月
  101. if (intval($team_grades_setting['team_cycle_type']) === TeamGrades::TEAM_CYCLE_TYPE_MONTH) {
  102. $end_time = strtotime('+' . $team_grades_setting['team_cycle'] . ' Month');
  103. }
  104. }
  105. }
  106. }
  107. if ($end_time > 0 && $end_time > $time) {
  108. $pool = new self();
  109. $pool->store_id = $store_id;
  110. $pool->start_time = $time;
  111. $pool->end_time = $end_time;
  112. $pool->user_id = $user_id;
  113. $pool->is_send = self::SEND_STATUS_NO;
  114. $pool->bonus_type = $bonus_type;
  115. if ($pool->save()) {
  116. return ArrayHelper::toArray($pool);
  117. }
  118. }
  119. }
  120. return $pool;
  121. }
  122. }