<?php

/**
 * 鼠笼笼位相关接口
 * @author   YS
 * @version  1.0
 */
class Api_CageController extends Ctrl_Api{
    public $Cagemodel;
    public $POST;
    public $Cagecolormodel;
    public $SquirrelModel;
    public $Usermodel;
    public function init() {
        $this->Cagemodel = new CageModel();
        $this->POST =json_decode(file_get_contents('php://input'),true);
        $this->Cagecolormodel = new CagecolorModel();
        $this->Usermodel = new UserModel();
        $this->SquirrelModel = new SquirrelModel();
    }
    /**
     * 鼠笼列表接口
     * @param $strains  品系
     * @param $number   笼位号
     * @param $type   笼位属性
     * @return
     *  {
     *  "errorcode": "200",
     *  "message": "登陆成功",
     *  "data": {
     *          "username": "用户名",
     *          "type": "1",用户类型
     *          "realname": "真实姓名",
     *          "faculties": "1",院系
     *          "specialized": "1",专业
     *          "group": "1",课题组
     *          "teacher": "1",导师
     *          "phone": "13566987478",手机号
     *          "status": "1",审核状态
     *          "is_delete": "0"是否删除
     *      }
     *  }
     */
    public function listAction(){
        $params['order'] = empty($this->POST['order'])?'':$this->POST['order'];
        $params['uid'] = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $params['keyword'] = empty($this->POST['keyword'])?'':$this->POST['keyword'];
        $params['page'] = empty($this->POST['page'])?1:$this->POST['page'];
        $data = $this->Cagemodel->getModelList($params);
        Tool_Fnc::apiMsg('笼位获取成功', '200',$data);
    }
    /**
     * 鼠笼添加接口
     * @param $strains  笼位品系
     * @param $number   笼位号
     * @param $type   笼位类型
     * @param $color  笼位颜色
     * @param $group   实验室
     * @param $user_id   负责人
     * @return
     *  {
     *  "errorcode": "200",
     *  "message": "添加成功",
     *  "data": []
     *  }
     */
    public function addAction(){
        //查询笼位号
        $data['c_user_id'] = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $CageNum = $this->Cagemodel->field('count(*) as num')->where("user_id={$data['c_user_id']}")->fRow();
        $CageNum = empty($CageNum)?1:(int) $CageNum['num']+1;
        $data['type'] = $this->POST['type'];
        $data['number'] = "NO.0000".$CageNum;
        if($CageNum>9) $data['number'] = "NO.000".$CageNum;
        if($CageNum>99) $data['number'] = "NO.00".$CageNum;
        if($CageNum>999) $data['number'] = "NO.0".$CageNum;
        $data['strains'] =empty($this->POST['strains'])||!isset($this->POST['strains'])?Tool_Fnc::apiMsg('请选择笼位品系', '500'):Tool_Fnc::safe_string($this->POST['strains']);
        $data['color'] =empty($this->POST['color'])||!isset($this->POST['color'])?Tool_Fnc::apiMsg('请选择笼位颜色', '500'):Tool_Fnc::safe_string($this->POST['color']);
        $data['group'] = empty($this->POST['group'])||!isset($this->POST['group'])?Tool_Fnc::apiMsg('请选择实验室', '500'):Tool_Fnc::safe_string($this->POST['group']);
        $data['user_id'] =empty($this->POST['user_id'])||!isset($this->POST['user_id'])?Tool_Fnc::apiMsg('请选择负责人', '500'):Tool_Fnc::safe_string($this->POST['user_id']);
        $data['created'] = date('Y-m-d H:i:s',time());
        $ret = $this->Cagemodel->insert($data);
        if(!$ret) Tool_Fnc::apiMsg('笼位建设失败', '500');
        Tool_Fnc::apiMsg('笼位建设成功', '200');
    }
    /**
     * 获取笼位号
     */
    public function getCagenumAction(){
        $uid = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $CageNum = $this->Cagemodel->field('count(*) as num')->where("user_id={$uid}")->fRow();
        $CageNum = empty($CageNum)?1:(int) $CageNum['num']+1;
        $data['number'] = "NO.0000".$CageNum;
        if($CageNum>9) $data['number'] = "NO.000".$CageNum;
        if($CageNum>99) $data['number'] = "NO.00".$CageNum;
        if($CageNum>999) $data['number'] = "NO.0".$CageNum;
        Tool_Fnc::apiMsg('获取笼位号成功', '200',$data);
    }
    /**
     * 获取笼位类型接口
     */
    public function getCagetypeAction(){
        $data = array(['id'=>1,'title'=>'库存笼'],['id'=>2,'title'=>'繁殖笼']);
        Tool_Fnc::apiMsg('获取笼位类型成功', '200',$data);
    }
    /**
     * 获取笼位颜色接口
     */
    public function getCagecolorAction(){
        $params['field'] = 'color';
        $data = $this->Cagecolormodel->getModelList($params);
        Tool_Fnc::apiMsg('获取成功', '200',$data);
    }
    /**
     * 获取负责人接口
     */
    public function getUserAction(){
        $uid = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $data = $this->Usermodel->getUser($uid);
        Tool_Fnc::apiMsg('获取成功', '200',$data);
    }
    public function getCageinfoACtion(){
        $id = empty($this->POST['id'])?Tool_Fnc::apiMsg('缺少笼位ID', '500'):$this->POST['id'];
        $params['where'] = "and cage = {$id}";
        $data['cage'] = $this->Cagemodel->getOneInfo($id);
        $data['cage']['code'] = Yaf_Registry::get("config")->web->url->img;
        $data['squirrel'] = $this->SquirrelModel->getModelList($params);
        Tool_Fnc::apiMsg('获取成功', '200',$data);
    }
}

