load->library('session'); $this->load->model('customer_model'); //$this->session->{Customer_model::SESSION_KEY} = 1; $this->checkUser(); } //获取繁育人信息 public function info_get(){ $row = $this->customer_model->get($this->_user_id); //统计数量 $this->load->model('sample_model'); $this->load->model('breeding_plan_model'); $row['total_sample'] = $this->sample_model->getUserTotal($this->_user_id); $row['total_breeding_plan'] = $this->breeding_plan_model->getUserTotal($this->_user_id); $this->success($row); } //获取种猫/犬 public function candidate_post(){ $this->load->model('sample_model'); $this->load->model('report_model'); $data = $this->json_input(); $ids = []; if(!empty($data['tree']) && is_array($data['tree'])){ $ids = $this->_getTreeSampleIds($data['tree']); } $pet_sex = empty($data['pet_sex'])?0:(int)$data['pet_sex']; $pet_species = empty($data['pet_species'])?0:(int)$data['pet_species']; $list = $this->sample_model->getBreeding($this->_user_id,$ids,$pet_species,$pet_sex,100); $ret = []; foreach ($list as $val) { $reportJson = $this->report_model->get_report_json($val['device_id'],$val['pet_species']); $ret[] = $this->_getCandidateItem($val,$reportJson); } $this->success($ret); } //繁育计划保存 public function plan_save_post() { $data = $this->json_input(); if(empty($data['tree'])){ $this->error('参数不完整'); } $tree = $data['tree']; //保存孩子的样本id $children_ids = []; foreach ($tree['children'] as $item) { if(!empty($item['id'])){ $children_ids[] = $item['id']; } } //雌性 $female_id = !empty($tree['female']['self']['id'])?(int)$tree['female']['self']['id']:0; $female_father_id = !empty($tree['female']['father']['id'])?(int)$tree['female']['father']['id']:0; $female_mother_id = !empty($tree['female']['mother']['id'])?(int)$tree['female']['mother']['id']:0; //雄性 $male_id = !empty($tree['male']['self']['id'])?(int)$tree['male']['self']['id']:0; $male_father_id = !empty($tree['male']['father']['id'])?(int)$tree['male']['father']['id']:0; $male_mother_id = !empty($tree['male']['mother']['id'])?(int)$tree['male']['mother']['id']:0; if(empty($female_id) || empty($male_id)){ $this->error('关系不完整'); } $this->load->model('breeding_plan_model'); $entity = [ 'children_ids' => implode(',',$children_ids), 'user_id' => $this->_user_id, 'female_id' => $female_id, 'female_father_id' => $female_father_id, 'female_mother_id' => $female_mother_id, 'male_id' => $male_id, 'male_father_id' => $male_father_id, 'male_mother_id' => $male_mother_id, ]; if(empty($data['id'])){ $this->breeding_plan_model->add($entity); }else{ $this->breeding_plan_model->update($entity,[ 'id'=>$data['id'] ]); } $this->success('保存成功'); } //繁育计划列表 public function list_get() { $this->load->model('breeding_plan_model'); $ret = $this->breeding_plan_model->get($this->_user_id,'user_id',true); $list = []; if($ret){ //获取所有相关样品 $sample_ids = []; foreach ($ret as $item) { $sample_ids[] = $item['male_id']; $sample_ids[] = $item['female_id']; } $this->load->model('sample_model'); //根据id获取样本信息 $samples = $this->sample_model->getByUserIds($sample_ids); $sample_map = []; foreach ($samples as $item) { $this->sample_model->format($item); $sample_map[$item['id']] = $item; } //格式化信息 foreach ($ret as $item) { $male = [ 'name'=>$sample_map[$item['male_id']]['pet_name'], 'pet_img'=>$sample_map[$item['male_id']]['pet_img'], 'device_id'=>$sample_map[$item['male_id']]['device_id'], ]; $female = [ 'name'=>$sample_map[$item['female_id']]['pet_name'], 'pet_img'=>$sample_map[$item['female_id']]['pet_img'], 'device_id'=>$sample_map[$item['female_id']]['device_id'], ]; $tmp = [ 'id'=>$item['id'], 'male'=>$male, 'female'=>$female, ]; $list[] = $tmp; } } $this->success($list); } //繁育计划列表 public function detail_get() { $id = $this->input->get('id'); if(empty($id)){ $this->error('参数错误'); } $this->load->model('breeding_plan_model'); $plan = $this->breeding_plan_model->get($id); if(!$plan){ $this->error('计划不存在'); } $tree = [ 'male'=>[ 'father'=>$this->_getNode($plan['male_father_id']), 'mother'=>$this->_getNode($plan['male_mother_id']), 'self'=>$this->_getNode($plan['male_id']), ], 'female'=>[ 'father'=>$this->_getNode($plan['female_father_id']), 'mother'=>$this->_getNode($plan['female_mother_id']), 'self'=>$this->_getNode($plan['female_id']), ], 'children'=>[] ]; if(!empty($plan['children_ids'])){ $plan['children_ids'] = explode(',',$plan['children_ids']); foreach ($plan['children_ids'] as $val) { $tree['children'][] = $this->_getNode($val); } } //如果没有children,补一个 if(empty($tree['children'])){ $tree['children'][] = [ 'id'=>0, 'pet_name'=>'', 'pet_img'=>'', ]; } $this->success($tree); } //繁育计划节点 protected function _getNode($sample_id) { $this->load->model('sample_model'); $sample = $this->sample_model->get($sample_id); if(!$sample){ return [ 'id'=>0 ]; } return $sample; } //繁育计划候选列表项目 protected function _getCandidateItem($sample,$reportJson) { $lineage = !empty($reportJson['血统分析']['body']['品系纯度'])?$reportJson['血统分析']['body']['品系纯度']:[]; $lineage = implode(',',array_keys($lineage)); $item = [ 'id'=>$sample['id'], 'pet_name'=>$sample['pet_name'], 'pet_img'=>$sample['pet_img'], 'pet_sex'=>$sample['pet_sex'], 'lineage'=>$lineage, ]; return $item; } //繁育计划树的所有id protected function _getTreeSampleIds($tree) { $ids = []; foreach ($tree as $key=>$val) { foreach ($val as $v) { $ids[] = $v['id']; } } return array_filter(array_unique($ids)); } }