| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- use yii\helpers\ArrayHelper;
- /**
- * This is the model class for table "{{%team_grades_pool}}".
- *
- * @property integer $id
- * @property integer $user_id
- * @property integer $team_grades_id
- * @property integer $store_id
- * @property integer $start_time
- * @property integer $end_time
- * @property integer $is_send
- * @property integer $send_time
- * @property integer $created_at
- * @property integer $updated_at
- * @property integer $bonus_type
- */
- class TeamGradesPool extends \yii\db\ActiveRecord
- {
- const SEND_STATUS_NO = 0;
- const SEND_STATUS_YES = 1;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%team_grades_pool}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'store_id', 'start_time', 'end_time', 'is_send', 'send_time', 'created_at', 'updated_at', 'user_id','team_grades_id', 'bonus_type'], 'integer'],
- ];
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => '',
- 'store_id' => '商城id',
- 'user_id' => '用户id',
- 'team_grades_id' => '团队分红人员id',
- 'start_time' => '开始时间',
- 'end_time' => '结束时间',
- 'is_send' => '是否发放',
- 'send_time' => '发放时间',
- 'created_at' => '',
- 'updated_at' => '',
- 'bonus_type' => '奖励类型:0=业绩阶梯模式;1=级差模式;',
- ];
- }
- /**
- * 还需要获取团队层级 应该是通过设置的层级获取层级内用户是否是团队分红人员 如果是要逐级增加业绩分红
- * 判断时间段是否在未发放的奖金池 如果存在就获取当前未发放的奖金池 如果不存在就创建新的并返回
- */
- public static function getPool($store_id, $user_id, $bonus_type = 0) {
- $time = time();
- $query = self::find()->alias('p')->where(['p.store_id' => $store_id, 'p.bonus_type' => $bonus_type])
- ->leftJoin(['pd' => TeamGradesPoolDetail::tableName()], 'p.id = pd.pool_id');
- if($bonus_type == 0) {
- $query->andWhere(['OR', ['pd.user_id' => $user_id], ['p.user_id' => $user_id]]);
- } elseif($bonus_type == 1) {//如果是级差模式,当用户不一致时单独创建池子
- $query->andWhere(['p.user_id' => $user_id]);
- }
- $pool = $query
- ->andWhere(['<=', 'p.start_time', $time])
- ->andWhere(['>=', 'p.end_time', $time])
- ->andWhere(['p.is_send' => 0])->asArray()->one();
- if (!$pool) {
- $end_time = 0;
- // 获取时间段配置
- $team_grades_setting = Option::get('team_grades_setting', $store_id, 'team_grades_setting')['value'];
- if (!empty($team_grades_setting)) {
- $team_grades_setting = json_decode($team_grades_setting, true);
- if (!empty($team_grades_setting)) {
- if (intval($team_grades_setting['team_cycle']) > 0) {
- // 按天
- if (intval($team_grades_setting['team_cycle_type']) === TeamGrades::TEAM_CYCLE_TYPE_DAY) {
- $end_time = strtotime('+' . $team_grades_setting['team_cycle'] . ' Day');
- }
- // 按周
- if (intval($team_grades_setting['team_cycle_type']) === TeamGrades::TEAM_CYCLE_TYPE_WEEK) {
- $end_time = strtotime('+' . $team_grades_setting['team_cycle'] . ' Week');
- }
- // 按月
- if (intval($team_grades_setting['team_cycle_type']) === TeamGrades::TEAM_CYCLE_TYPE_MONTH) {
- $end_time = strtotime('+' . $team_grades_setting['team_cycle'] . ' Month');
- }
- }
- }
- }
- if ($end_time > 0 && $end_time > $time) {
- $pool = new self();
- $pool->store_id = $store_id;
- $pool->start_time = $time;
- $pool->end_time = $end_time;
- $pool->user_id = $user_id;
- $pool->is_send = self::SEND_STATUS_NO;
- $pool->bonus_type = $bonus_type;
- if ($pool->save()) {
- return ArrayHelper::toArray($pool);
- }
- }
- }
- return $pool;
- }
- }
|