<?php
class CageModel extends Orm_Base{
    public $table = 'cage';
    public $field = array(
        'id' => array('type' => "int(11)",'comment' => ''),
        'number' => array('type' => "varchar(45)",'comment' => '笼位编号'),
        'strains' => array('type' => "varchar(45)",'comment' => '品系'),
        'type' => array('type' => "int(11)",'comment' => '笼位类型 '),
        'color' => array('type' => "varchar(255)",'comment' => '笼位颜色'),
        'group' => array('type' => "varchar(255)",'comment' => '实验室对应用户课题组'),
        'user_id' => array('type' => "varchar(255)",'comment' => '负责人id'),
        'c_user_id' => array('type' => "varchar(255)",'comment' => '添加人id'),
        'code' => array('type' => "varchar(255)",'comment' => '二维码信息'),
        'is_delete' => array('type' => "int(11)",'comment' => '是否删除'),
        'created' => array('type' => "varchar(255)",'comment' => '添加时间'),
    );
    public $pk = 'id';
    public function getModelList($params){
        //查询当前笼位一行显示几个
        $cagemodel = new CageSetModel();
        $shownum = $cagemodel->field('line_num')->where("c_user_id='{$params['uid']}'")->fRow();
        $limmit = empty($shownum['line_num'])?20:$shownum['line_num']*5;
        $sql = "select id,number,color,strains,type from cage where c_user_id={$params['uid']}";
        if(!empty($params['keyword'])){
            $sql .=" where number like '%{$params['keyword']}%'";
        }
        if(!empty($params['order'])){
            $sql .=" order by {$params['order']} desc";
        }else{
            $sql .=" order by id desc";
        }
        $data['count'] = count($this->query($sql));
        $offset = ($params['page']-1)*$limmit;
        $sql .=" limit {$offset},{$limmit}";
        $data['data'] = $this->query($sql);
        foreach($data['data'] as $key => &$val){
            //查询公鼠
            $sql = "select count(*) num from squirrel where cage={$val['id']} and is_kill=0 and is_delete=0 and sex=1";
            $mannum = $this->query($sql);
            if(!empty($mannum)){
                $val['mannum'] = $mannum[0]['num'];
            }
            //查询母鼠
            $sql = "select count(*) num from squirrel where cage={$val['id']} and is_kill=0 and is_delete=0 and sex=2";
            $womannum = $this->query($sql);
            if(!empty($womannum)){
                $val['womannum'] = $womannum[0]['num'];
            }
            $val['type_color'] = $val['type']==2?'#FF0000':'';
        }
        $data['page'] = $params['page'];
        return $data;
    }
    public function getOneInfo($id){
        $UserModel = new UserModel();
        $info = $this->field('*')->where("id='{$id}'")->fRow();
        //负责人id
        $info['user'] = $UserModel->field('realname')->fRow()['realname'];
        return $info;
    }
}

