<?php
namespace app\common\model;

use app\hander\HelpHander;
use think\Db;
use think\Exception;
use think\Model;

class AttendanceClass extends Model
{

    public function add(){
        $data = [
            'id' => input('id/d',0),
            'name' => input('name','','trim'),
            'content' => input('content','','trim'),
            'org_id' => input('orgId/d',0),
        ];

        $result = validate('AttendanceClass')->check($data,[],'');
        if(true !== $result){
            HelpHander::error(validate('AttendanceClass')->getError());
        }
        $id = $data['id'];
        unset($data['id']);
        Db::startTrans();
        try{
            if($id > 0){
                $data['update_time'] = date('Y-m-d H:i:s');
                $ret = $this->allowField(true)->save($data,['id'=>$id]);
            }else{
                $data['create_time'] = date('Y-m-d H:i:s');
                $ret = $this->allowField(true)->save($data);
            }
            if(!$ret){
                \exception('操作失败');
            }

            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            HelpHander::error($e->getMessage());
        }

        return true;
    }

    public function info($id){
        $info = $this->where('id',$id)->find();
        if(!$info){
            HelpHander::error('数据不存在');
        }
        $data = $info->toArray();
        $data['content'] = json_decode($data['content'],true);
        return $data;
    }

    public function getNameById($id){
        $info = $this->where('id',$id)->find();
        if(!$info){
            HelpHander::error('数据不存在');
        }
        $data = $info->toArray();
        return $this->formatClassName($data);
    }

    public function formatClassName($info){
        $info['content'] = json_decode($info['content'],true);
        $names = [];
        foreach ($info['content']['dates'] as $k=>$v){
            $str = '';
            if ($v['snext'] == 1) {
                $str .= '次日'.$v['stime'];
            } else {
                $str .= $v['stime'];
            }
            $str .= '-';
            if ($v['enext'] === 1) {
                $str .= '次日'.$v['etime'];
            } else {
                $str .= $v['etime'];
            }
            $names[] = $str;
        }
        return $info['name'].' '.implode(',',$names);
    }

    public function lists($page,$size,$name,$orgId){
        $map[] = ['del','=',0];
        $map[] = ['org_id','=',$orgId];
        if($name != ''){
            $map[] = ['name','like','%'.$name.'%'];
        }
        $lists = $this
            ->where($map)
            ->page($page,$size)
            ->order('id desc')
            ->select();
        $lists = $lists?$lists->toArray():[];
        foreach ($lists as $k=>$v){
            $lists[$k]['content'] = json_decode($v['content'],true);
        }
        $total = $this->where($map)->count();
        $data = [
            'total' => $total,
            'list' => $lists
        ];
        return $data;
    }

    public function del($id){
        $ret = $this->where('id',$id)->setField('del',1);
        if(!$ret){
            HelpHander::error('删除失败');
        }
        return true;
    }

    public function all($orgId){
        $map[] = ['del','=',0];
        $map[] = ['org_id','=',$orgId];
        $lists = $this
            ->where($map)
            ->order('id desc')
            ->select();
        $lists = $lists?$lists->toArray():[];
        foreach ($lists as $k=>$v){
            $lists[$k]['content'] = json_decode($v['content'],true);
        }
        return $lists;
    }

}