Attendance.php 3.99 KB
<?php

namespace addons\qingdong\model;

use think\Model;
use traits\model\SoftDelete;
use think\Db;

/**
 *考勤管理
 */
class Attendance Extends Model {
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'qingdong_attendance';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [
        'files',
    ];

    /**
     * 
     * 每天执行一次,获取一星期是否是节假日再判断是否可以打卡签到
     * 查询今天和后面七天的数据
     */
    public function resetWeekdidi()
    {
        $tarr = [];
        for($i=0;$i<7;$i++){
            $tarr[] = date('Y-m-d', strtotime("+$i days"));
        }

//        halt("https://apis.tianapi.com/jiejiari/index?key=YOUR_API_KEY&date=".implode(',',$tarr)."&type=0");
           
//        $ret = \fast\Http::get("https://apis.tianapi.com/jiejiari/index?key=YOUR_API_KEY&date=".implode(',',$tarr)."&type=0");//date('Y-m-d H:i:s', strtotime('+7 days'));
		$curl = curl_init();
        $data = [
            "date"=>$tarr,
            "type"=>0,
        ];
        $jsonData = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); // 生成 JSON 字符串
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://apis.tianapi.com/jiejiari/index?key=YOUR_API_KEY',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_POSTFIELDS =>$jsonData,
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ),
        ));
        $ret = curl_exec($curl);
        curl_close($curl);
		
        $data = json_decode($ret,true);//weekday
//     halt($data);
        if(isset($data['code']) && $data['code']==200){
            $list = $data['result']['list'];
            $week = [
               'weekday','monday','tuesday','wednesday','thursday','friday','saturday'
            ];
            $updata = [];
            // halt($list);
            $updateobj = [];
            foreach($list as $item){
                $updata[$week[$item['weekday']]] = !$item['isnotwork'];
                $updateobj[] = [ 
                    'name'=>$week[$item['weekday']],
                    'value'=>!$item['isnotwork'],
                    'date'=>$item['date']
                ];
            }
            //可添加特殊上班日期
            $lists = Db::name('qingdong_attendance_time')->whereNull('deletetime')->select();//->update($updata);
            foreach ($lists as $ii){
                $yarr = explode(',',$ii['more']);
                $up = [];
                foreach($updateobj as &$iv){
                    $up[$iv['name']] = $iv['value'];
                    if(in_array($iv['date'],$yarr)){
                        $up[$iv['name']] = !$iv['value'];
                    }
                    Db::name('qingdong_attendance_time')->where('id',$ii['id'])->setField($iv['name'],$up[$iv['name']]);
                }
                
            }
        }
        //weekday 星期数字 0-6 0表示星期天
        
    }
    //创建人
    public function createStaff() {
        return $this->hasOne(Staff::class, 'id', 'staff_id')->field('id,name,img,post');
    }



    //创建人
    public function staff() {
        return $this->hasOne(Staff::class, 'id', 'staff_id')->field('id,name,img,post');
    }

    /**
     * 获取
     */
    public function getFilesAttr($value, $data)
    {
        if (empty($data['file_ids'])) {
            return '';
        }
        $paths = File::where(['id' => ['in', $data['file_ids']]])->column('file_path');
        return $paths?implode(',', $paths):'';
    }
}