<?php

namespace app\common\model\score;

use think\Model;


class Team extends Model
{

    

    

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

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

    // 追加属性
    protected $append = [
        "score_employee_ids"
    ];


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



    public function getScoreEmployeeIdsAttr($value, $data)
    {
        $value = $value ?: ($data['id'] ?? '');
        if(!$value) return "";
        $score_employee_ids  = Employee::where( "team_id", $value)->column( "id");
        return implode(",", $score_employee_ids);
    }







    /** 查询积分统计，按时间查询
     * @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_team_id',$id)->where('status','=',1)->where(...$where)->sum('score');

        return $pay_fee;
    }




}
