<?php

namespace app\common\model\score;

use think\Model;


class Employee extends Model
{

    

    

    // 表名
    protected $name = 'score_employee';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['entry' => __('Status entry'), 'exit' => __('Status exit')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }


    public function getImageAttr($value, $data)
    {
        if (!empty($value)) return cdnurl($value, true);
    }




    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function team()
    {
        return $this->belongsTo('Team', 'team_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    /** 查询积分统计，按时间查询
     * @param $coach_id
     * @param $start_time
     * @param $end_time
     */
    public static function getPriceByCoachAndTime($id,$start_time=0,$end_time=0){
        $where = [[]];
        if($start_time){
            $where = ['createtime', 'between time', [$start_time, $end_time]];
        }
        //统计已核销未退款金额
        $pay_fee = Log::where('score_employee_id',$id)->where('status','=',1)->where(...$where)->sum('score');

        return $pay_fee;
    }

}
