<?php

/**
 * 小鼠相关接口
 * @author   YS
 * @version  1.0
 */
class Api_SquirrelController extends Ctrl_Api{
    public $Squirrelmodel;
    public $NewSquirrelmodel;
    public $CageModel;
    public $CageSetModel;
    public $SquirrelSetModel;
    public $POST;
    public $ExcelSetModel;
    public function init() {
        $this->Squirrelmodel = new SquirrelModel();
        $this->NewSquirrelmodel = new NewSquirrelModel();
        $this->CageModel = new CageModel();
        $this->CageSetModel=new CageSetModel();
        $this->SquirrelSetModel=new SquirrelSetModel();
        $this->ExcelSetModel=new ExcelSetModel();
        $this->POST =json_decode(file_get_contents('php://input'),true);
    }
    /**
     * 鼠笼列表接口
     * @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(){
        $uid = empty($this->POST['uid'])||!isset($this->POST['cage_id'])?Tool_Fnc::apiMsg('请先登录', '500'):Tool_Fnc::safe_string($this->POST['uid']);
        $cage = empty($this->POST['cage_id'])||!isset($this->POST['cage_id'])?Tool_Fnc::apiMsg('缺少重要参数', '500'):Tool_Fnc::safe_string($this->POST['cage_id']);
        $type = empty($this->POST['type'])||!isset($this->POST['type'])?1:Tool_Fnc::safe_string($this->POST['type']);
        $params['where'] = "and cage = {$cage}";
        $params['type'] =$type;
        $data = $this->Squirrelmodel->getModelList($params);
        Tool_Fnc::apiMsg('获取成功', '200',$data);
    }
    /**
     * 小鼠添加接口
     * @param $sex  性别
     * @param $birthday   生日
     * @param $father   父亲id
     * @param $mather  母亲id
     * @param $strains_id   品系id
     * @param $gene   基因id
     * @param $hair_color   毛色
     * @param $remark   备注
     * @param $cage   笼位号
     * @return
     *  {
     *  "errorcode": "200",
     *  "message": "添加成功",
     *  "data": {
     *          "sex": "用户名",性别
     *          "birthday": "1",生日
     *          "father": "1",母亲id
     *          "mather": "2",母亲id
     *          "strains_id": "1",品系id
     *          "gene": "1",基因id
     *          "hair_color": "1",毛色
     *          "remark": "备注",备注
     *          "cage": "1",笼位号id
     *      }
     *  }
     */
    public function addAction(){
        //查询笼位号
        $CageNum = $this->Squirrelmodel->field('count(*) as num')->fRow();
        $CageNum = empty($CageNum)?1:(int) $CageNum['num']+1;
        $data['c_user_id'] = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $data['type'] = $this->POST['type'];
        $data['number'] = "SN00000".$CageNum;
        if($CageNum>9) $data['number'] = "SN0000".$CageNum;
        if($CageNum>99) $data['number'] = "SN000".$CageNum;
        if($CageNum>999) $data['number'] = "SN00".$CageNum;
        if($CageNum>9999) $data['number'] = "SN0".$CageNum;
        $data['sex'] =empty($this->POST['sex'])||!isset($this->POST['sex'])?Tool_Fnc::apiMsg('请选择性别', '500'):Tool_Fnc::safe_string($this->POST['sex']);
        $data['birthday'] =empty($this->POST['birthday'])||!isset($this->POST['birthday'])?Tool_Fnc::apiMsg('请填写生日', '500'):Tool_Fnc::safe_string($this->POST['birthday']);
        $data['father'] = empty($this->POST['father'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['father']);
        $data['mother'] = empty($this->POST['mother'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['mother']);
        $data['strains_id'] =empty($this->POST['strains_id'])||!isset($this->POST['strains_id'])?Tool_Fnc::apiMsg('请选择品系', '500'):Tool_Fnc::safe_string($this->POST['strains_id']);
        $data['gene'] =empty($this->POST['gene'])||!isset($this->POST['gene'])?Tool_Fnc::apiMsg('请填写基因', '500'):Tool_Fnc::safe_string($this->POST['gene']);
        $data['hair_color'] =empty($this->POST['hair_color'])||!isset($this->POST['hair_color'])?Tool_Fnc::apiMsg('请选择毛色', '500'):Tool_Fnc::safe_string($this->POST['hair_color']);
        $data['remark'] = empty($this->POST['remark'])||!isset($this->POST['remark'])?'':Tool_Fnc::safe_string($this->POST['remark']);
        $data['cage'] =empty($this->POST['cage'])||!isset($this->POST['cage'])?Tool_Fnc::apiMsg('未找到当前笼位ID', '500'):Tool_Fnc::safe_string($this->POST['cage']);
        $data['created'] = date('Y-m-d H:i:s',time());
        //判断鼠笼鼠的数量是否可以容纳
        if(!$this->Squirrelmodel->isFull($data['cage'],$this->POST['uid'])) Tool_Fnc::apiMsg('当前笼位已满，请重新选择笼位添加', '500');
        //获取当前用户仓鼠的配置
        $params['user_id'] = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $squirrelSet = $this->SquirrelSetModel->getsetinfo($params);
        //根据生日获取各个提醒时间
        $data['weaning_time'] = date("Y-m-d",strtotime("+{$squirrelSet['weaning_time']} day",strtotime($data['birthday'])));
        $data['nursing_time'] = date("Y-m-d",strtotime("+{$squirrelSet['nursing_time']} day",strtotime($data['birthday'])));
        $data['grow_time'] =$data['sex']==1? date("Y-m-d",strtotime("+{$squirrelSet['male_grow_time']} days",strtotime($data['birthday']))):date("Y-m-d",strtotime("+{$squirrelSet['female_grow_time']} days",strtotime($data['birthday'])));
        $data['infertility_time'] = date("Y-m-d",strtotime("+{$squirrelSet['infertility_time']} month",strtotime($data['birthday'])));
        $data['dose_time'] = date("Y-m-d",strtotime("+{$squirrelSet['dose_time']} day",strtotime($data['birthday'])));
        $ret = $this->Squirrelmodel->insert($data);
        if(!$ret) Tool_Fnc::apiMsg('小鼠建档失败', '500');
        Tool_Fnc::apiMsg('小鼠建档成功', '200');
    }
    /**
     * 小鼠编辑接口
     * @param $sex  性别
     * @param $birthday   生日
     * @param $father   父亲id
     * @param $mather  母亲id
     * @param $strains_id   品系id
     * @param $gene   基因id
     * @param $hair_color   毛色
     * @param $remark   备注
     * @param $cage   笼位号
     * @return
     *  {
     *  "errorcode": "200",
     *  "message": "添加成功",
     *  "data": {
     *          "sex": "用户名",性别
     *          "birthday": "1",生日
     *          "father": "1",母亲id
     *          "mather": "2",母亲id
     *          "strains_id": "1",品系id
     *          "gene": "1",基因id
     *          "hair_color": "1",毛色
     *          "remark": "备注",备注
     *          "cage": "1",笼位号id
     *      }
     *  }
     */
    public function editAction(){
        $data['uid'] = empty($this->POST['uid'])||!isset($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):Tool_Fnc::safe_string($this->POST['id']);
        $data['id'] = empty($this->POST['id'])||!isset($this->POST['id'])?Tool_Fnc::apiMsg('系统错误', '500'):Tool_Fnc::safe_string($this->POST['id']);
        $data['sex'] =empty($this->POST['sex'])||!isset($this->POST['sex'])?Tool_Fnc::apiMsg('请选择性别', '500'):Tool_Fnc::safe_string($this->POST['sex']);
        $data['birthday'] =empty($this->POST['birthday'])||!isset($this->POST['birthday'])?Tool_Fnc::apiMsg('请填写生日', '500'):Tool_Fnc::safe_string($this->POST['birthday']);
        $data['father'] = empty($this->POST['father'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['father']);
        $data['mother'] = empty($this->POST['mother'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['mother']);
        $data['strains_id'] =empty($this->POST['strains_id'])||!isset($this->POST['strains_id'])?Tool_Fnc::apiMsg('请选择品系', '500'):Tool_Fnc::safe_string($this->POST['strains_id']);
        $data['gene'] =empty($this->POST['gene'])||!isset($this->POST['gene'])?Tool_Fnc::apiMsg('请填写基因', '500'):Tool_Fnc::safe_string($this->POST['gene']);
        $data['hair_color'] =empty($this->POST['hair_color'])||!isset($this->POST['hair_color'])?Tool_Fnc::apiMsg('请选择毛色', '500'):Tool_Fnc::safe_string($this->POST['hair_color']);
        $data['remark'] = empty($this->POST['remark'])||!isset($this->POST['remark'])?'':Tool_Fnc::safe_string($this->POST['remark']);
        $data['created'] = date('Y-m-d H:i:s',time());
        //获取当前用户仓鼠的配置
        $params['user_id'] = $data['uid'];
        $squirrelSet = $this->SquirrelSetModel->getsetinfo($params);
        //根据生日获取各个提醒时间
        $data['weaning_time'] = date("Y-m-d",strtotime("+{$squirrelSet['weaning_time']} day",strtotime($data['birthday'])));
        $data['nursing_time'] = date("Y-m-d",strtotime("+{$squirrelSet['nursing_time']} day",strtotime($data['birthday'])));
        $data['grow_time'] =$data['sex']==1? date("Y-m-d",strtotime("+{$squirrelSet['male_grow_time']} days",strtotime($data['birthday']))):date("Y-m-d",strtotime("+{$squirrelSet['female_grow_time']} days",strtotime($data['birthday'])));
        $data['infertility_time'] = date("Y-m-d",strtotime("+{$squirrelSet['infertility_time']} month",strtotime($data['birthday'])));
        $data['dose_time'] = date("Y-m-d",strtotime("+{$squirrelSet['dose_time']} day",strtotime($data['birthday'])));
        $ret = $this->Squirrelmodel->update($data);
        if(!$ret) Tool_Fnc::apiMsg('信息未变更', '500');
        Tool_Fnc::apiMsg('信息更新成功', '200');
    }
    /**
     * 获取小鼠详情接口
     * @param $id  小鼠id
     * @return
     *  {
     *  "errorcode": "200",
     *  "message": "添加成功",
     *  "data": {
     *          "sex": "用户名",性别
     *          "birthday": "1",生日
     *          "father": "1",母亲id
     *          "mather": "2",母亲id
     *          "strains_id": "1",品系id
     *          "gene": "1",基因id
     *          "hair_color": "1",毛色
     *          "remark": "备注",备注
     *          "cage": "1",笼位号id
     *      }
     *  }
     */
    public function getInfosAction(){
        $id = empty($this->POST['id'])||!isset($this->POST['id'])?Tool_Fnc::apiMsg('小鼠ID缺失', '500'):Tool_Fnc::safe_string($this->POST['id']);
        $data = $this->Squirrelmodel->getOneInfo($id);
        Tool_Fnc::apiMsg('获取成功', '200',$data);

    }
    /**
     * 幼崽信息添加接口
     * @param $sex  性别
     * @param $birthday   生日
     * @param $father   父亲id
     * @param $mather  母亲id
     * @param $strains_id   品系id
     * @param $gene   基因id
     * @param $hair_color   毛色
     * @param $remark   备注
     * @param $cage   笼位号
     * @return
     *  {
     *  "errorcode": "200",
     *  "message": "添加成功",
     *  "data": {
     *          "sex": "用户名",性别
     *          "birthday": "1",生日
     *          "father": "1",母亲id
     *          "mather": "2",母亲id
     *          "strains_id": "1",品系id
     *          "gene": "1",基因id
     *          "hair_color": "1",毛色
     *          "remark": "备注",备注
     *          "cage": "1",笼位号id
     *      }
     *  }
     */
    public function addNewSquirrelAction(){
        //查询笼位号
        $data['c_user_id'] = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $data['count'] =empty($this->POST['count'])||!isset($this->POST['birthday'])?Tool_Fnc::apiMsg('填写幼崽数量', '500'):Tool_Fnc::safe_string($this->POST['count']);
        $data['birthday'] =empty($this->POST['birthday'])||!isset($this->POST['birthday'])?Tool_Fnc::apiMsg('请选择生日', '500'):Tool_Fnc::safe_string($this->POST['birthday']);
        $data['father'] = empty($this->POST['father'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['father']);
        $data['mother'] = empty($this->POST['mother'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['mother']);
        $data['strains'] =empty($this->POST['strains'])||!isset($this->POST['strains'])?Tool_Fnc::apiMsg('请填写品系', '500'):Tool_Fnc::safe_string($this->POST['strains']);
        $data['gene'] =empty($this->POST['gene'])||!isset($this->POST['gene'])?Tool_Fnc::apiMsg('请填写基因', '500'):Tool_Fnc::safe_string($this->POST['gene']);
        $data['remark'] = empty($this->POST['remark'])||!isset($this->POST['remark'])?'':Tool_Fnc::safe_string($this->POST['remark']);
        $data['cage'] =empty($this->POST['cage'])||!isset($this->POST['cage'])?Tool_Fnc::apiMsg('未找到当前笼位ID', '500'):Tool_Fnc::safe_string($this->POST['cage']);
        $data['created'] = date('Y-m-d H:i:s',time());
        //获取当前用户仓鼠的配置
        $params['user_id'] = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $squirrelSet = $this->SquirrelSetModel->getsetinfo($params);
        //根据生日获取各个提醒时间
        $data['weaning_time'] = date("Y-m-d",strtotime("+{$squirrelSet['weaning_time']} day",strtotime($data['birthday'])));
        $data['nursing_time'] = date("Y-m-d",strtotime("+{$squirrelSet['nursing_time']} day",strtotime($data['birthday'])));
        $data['grow_time'] =$data['sex']==1? date("Y-m-d",strtotime("+{$squirrelSet['male_grow_time']} days",strtotime($data['birthday']))):date("Y-m-d",strtotime("+{$squirrelSet['female_grow_time']} days",strtotime($data['birthday'])));
        $data['infertility_time'] = date("Y-m-d",strtotime("+{$squirrelSet['infertility_time']} month",strtotime($data['birthday'])));
        $data['dose_time'] = date("Y-m-d",strtotime("+{$squirrelSet['dose_time']} day",strtotime($data['birthday'])));
        $ret = $this->NewSquirrelmodel->insert($data);
        if(!$ret) Tool_Fnc::apiMsg('添加失败', '500');
        Tool_Fnc::apiMsg('添加成功', '200');
    }
    public function editNewSquirrelAction(){
        //查询笼位号
        $data['c_user_id'] = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $data['id'] = empty($this->POST['id'])||!isset($this->POST['id'])?Tool_Fnc::apiMsg('系统错误', '500'):Tool_Fnc::safe_string($this->POST['id']);
        $data['count'] =empty($this->POST['count'])||!isset($this->POST['birthday'])?Tool_Fnc::apiMsg('填写幼崽数量', '500'):Tool_Fnc::safe_string($this->POST['count']);
        $data['birthday'] =empty($this->POST['birthday'])||!isset($this->POST['birthday'])?Tool_Fnc::apiMsg('请选择生日', '500'):Tool_Fnc::safe_string($this->POST['birthday']);
        $data['father'] = empty($this->POST['father'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['father']);
        $data['mother'] = empty($this->POST['mother'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['mother']);
        $data['strains'] =empty($this->POST['strains'])||!isset($this->POST['strains'])?Tool_Fnc::apiMsg('请填写品系', '500'):Tool_Fnc::safe_string($this->POST['strains']);
        $data['gene'] =empty($this->POST['gene'])||!isset($this->POST['gene'])?Tool_Fnc::apiMsg('请填写基因', '500'):Tool_Fnc::safe_string($this->POST['gene']);
        $data['remark'] = empty($this->POST['remark'])||!isset($this->POST['remark'])?'':Tool_Fnc::safe_string($this->POST['remark']);
        $data['cage'] =empty($this->POST['cage'])||!isset($this->POST['cage'])?Tool_Fnc::apiMsg('未找到当前笼位ID', '500'):Tool_Fnc::safe_string($this->POST['cage']);
        $data['created'] = date('Y-m-d H:i:s',time());
        //获取当前用户仓鼠的配置
        $params['user_id'] = empty($this->POST['uid'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['uid'];
        $squirrelSet = $this->SquirrelSetModel->getsetinfo($params);
        //根据生日获取各个提醒时间
        $data['weaning_time'] = date("Y-m-d",strtotime("+{$squirrelSet['weaning_time']} day",strtotime($data['birthday'])));
        $data['nursing_time'] = date("Y-m-d",strtotime("+{$squirrelSet['nursing_time']} day",strtotime($data['birthday'])));
        $data['grow_time'] =$data['sex']==1? date("Y-m-d",strtotime("+{$squirrelSet['male_grow_time']} days",strtotime($data['birthday']))):date("Y-m-d",strtotime("+{$squirrelSet['female_grow_time']} days",strtotime($data['birthday'])));
        $data['infertility_time'] = date("Y-m-d",strtotime("+{$squirrelSet['infertility_time']} month",strtotime($data['birthday'])));
        $data['dose_time'] = date("Y-m-d",strtotime("+{$squirrelSet['dose_time']} day",strtotime($data['birthday'])));
        $ret = $this->NewSquirrelmodel->update($data);
        if(!$ret) Tool_Fnc::apiMsg('信息未变更', '500');
        Tool_Fnc::apiMsg('编辑成功', '200');
    }
    public function getNewSquirrelAction(){
        $id = empty($this->POST['id'])||!isset($this->POST['id'])?Tool_Fnc::apiMsg('幼崽ID缺失', '500'):Tool_Fnc::safe_string($this->POST['id']);
        $data = $this->NewSquirrelmodel->getOneInfo($id);
        Tool_Fnc::apiMsg('获取成功', '200',$data);
    }
    public function getparentsAction(){
        $return = $this->Squirrelmodel->partent();
        Tool_Fnc::apiMsg('获取成功', '200',$return);
    }
    public function getIconAction(){
        $id= empty($this->POST['id'])?Tool_Fnc::apiMsg('缺少小鼠id', '500'):$this->POST['id'];
        $data = $this->Squirrelmodel->getIcon($id);
        Tool_Fnc::apiMsg('获取成功', '200',$data);
    }
    public function operateAction(){
        $type = empty($this->POST['type'])?Tool_Fnc::apiMsg('操作类型缺失', '500'):$this->POST['type'];
        $data['id'] = empty($this->POST['id'])?Tool_Fnc::apiMsg('小鼠ID缺失', '500'):$this->POST['id'];
        $data['test_time'] = $this->POST['test_time'];
        $data['jianwei_time'] = $this->POST['jianwei_time'];
        $data['kill_time'] = $this->POST['kill_time'];
        $data['del_time'] = $this->POST['del_time'];
        $data['jianwei_show'] = $this->POST['jianwei_show'];
        $data['test_show'] = $this->POST['test_show'];
        $data['del_show'] = $this->POST['del_show'];
        $data['kill_show'] = $this->POST['kill_show'];
        $excemodel = $type==1?$this->Squirrelmodel:$this->NewSquirrelmodel;
        $excemodel->operate($data);
        Tool_Fnc::apiMsg('操作已更新', '200');
    }
    public function getoperateAction(){
        $type = empty($this->POST['type'])?Tool_Fnc::apiMsg('操作类型缺失', '500'):$this->POST['type'];
        $id = empty($this->POST['id'])?Tool_Fnc::apiMsg('小鼠ID缺失', '500'):$this->POST['id'];
        $excemodel = $type==1?$this->Squirrelmodel:$this->NewSquirrelmodel;
        $data = $excemodel->field('id,jianwei_time,jianwei_show,kill_time,kill_show,test_time,test_show,del_time,del_show')->where("id={$id}")->fRow();
        foreach($data as $key => &$val){
            $val = empty($val)?'':$val;
        }
        Tool_Fnc::apiMsg('获取成功', '200',$data);
    }
}

