<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%CALC_BONUS_BS_DETAIL}}".
 */
class CalcBonusBsDetail extends \common\components\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%CALC_BONUS_BS_DETAIL}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['USER_ID', 'PERIOD_NUM', 'CALC_MONTH', 'CALCULATED_AT', 'CREATED_AT'], 'required'],
            [['AMOUNT', 'ORI_BONUS', 'MANAGE_TAX'], 'number'],
            [['LAST_STATUS', 'PERIOD_NUM', 'CALC_MONTH', 'CALCULATED_AT', 'CREATED_AT', 'LAST_FROM_STATUS'], 'integer'],
            [['ID', 'USER_ID', 'LAST_DEC_LV', 'LAST_EMP_LV', 'FROM_USER_ID',  'LAST_FROM_DEC_LV', 'LAST_FROM_EMP_LV'], 'string', 'max' => 32],
            [['LOGS'], 'string', 'max' => 2000],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'INTRODUCER_ID' => '推荐人的ID',
            'LAST_DEC_LV' => 'LAST_DEC_LV',
            'LAST_EMP_LV' => 'LAST_EMP_LV',
            'LAST_STATUS' => '结算时状态',
            'LEVEL_ID' => '当期蓝星会员最新聘级.聘级ID',
            'ORI_BONUS' => '原奖金 包含管理费的奖金',
            'MANAGE_TAX' => '管理费, 原奖金-管理费=实际总收入',
            'AMOUNT' => '期实际总收入,发给用户的实际金额,BONUS=BONUS10+……+BONUS90',
            'PRODUCT_POINT' => '兑换积分,如果本人有小组业绩（GPV)，同时没有任何奖金，则把这个业绩*12%换成积分',
            'PERIOD_NUM' => '结算期数',
            'CALC_YEAR' => '结算年',
            'CALC_MONTH' => '结算月',
            'P_CALC_MONTH' => '结算年月日',
            'CREATED_AT' => '创建时间',
            'LAYER' => 'LAYER',
            'PV' => 'PV',
            'GPV10' => 'GPV10',
            'GPV' => 'GPV',
            'GPV_4_CALC' => 'GPV_4_CALC',
            'USER_TYPE10' => 'USER_TYPE10',
            'USER_TYPE' => 'USER_TYPE',
            'BONUS10' => 'BONUS10',
            'BONUS20' => 'BONUS20',
            'BONUS30' => 'BONUS30',
            'BONUS40' => 'BONUS40',
            'BONUS50' => 'BONUS50',
            'BONUS60' => 'BONUS60',
            'BONUS70' => 'BONUS70',
            'BONUS80' => 'BONUS80',
            'BONUS90' => 'BONUS90',
            'BONUS100' => 'BONUS100',
            'BONUS110' => 'BONUS110',
            'ORI_BONUS_ABBR'=>'蓝星业绩原奖金',
            'MANAGE_TAX_ABBR'=>'蓝星业绩管理费',
            'AMOUNT_ABBR'=>'蓝星业绩实发',
            'ORI_BONUS_MNT'=>'蓝星管理原奖金',
            'MANAGE_TAX_MNT'=>'蓝星管理奖管理费',
            'AMOUNT_MNT'=>'蓝星管理奖实发'
        ];
    }

    // 判断用户是否是无聘级,并且oribonus大于0
    public static function isCommonShare($userId, $periodNum, $levelId) {
        $isCommon = CalcBonusBsDetail::find()
        ->where(
            'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM  AND LEVEL_ID=:LEVEL_ID  AND ORI_BONUS>0',
            [':USER_ID'=>$userId,':PERIOD_NUM'=>$periodNum,':LEVEL_ID'=>$levelId]
        )->asArray()
        ->one();
        if (!empty($isCommon)) {
            return true;
        } else {
            return false;
        }
    }
}
