Files
2026-09-14 16:32:07 +08:00

184 lines
5.3 KiB
PHP

<?php
class Sample_model extends MY_Model
{
protected $table = 'sample';
protected $join = [
[
'table'=>'series_number as sn',
'on'=>'base.series_id = sn.id',
'type'=>'left'
]
];
protected $keyword_field = ['device_id','base.tel','base.name','base.pet_name'];
protected $list_field = 'base.*,sn.device_id';
const STEP_BIND = 100;
const STEP_SENT = 200;
const STEP_RECEIVE = 300;
const STEP_DNA = 400;
const STEP_DNA_EXTRACTED = 500;
const STEP_UNQUALIFIED = 600;
const STEP_ANALYSIS = 700;
const STEP_COMPLETE = 800;
//作废,这种状态的样本对客户隐藏
const STEP_DISCARD = 900;
//后台的状态
public $step_back = [//
self::STEP_BIND=>'待回寄样本',
self::STEP_SENT=>'已回寄',
self::STEP_RECEIVE=>'样本已收到',
self::STEP_DNA=>'DNA提取中',
self::STEP_DNA_EXTRACTED=>'文库分析中',
self::STEP_UNQUALIFIED=>'样本不合格',
self::STEP_ANALYSIS=>'基因测序中',
self::STEP_COMPLETE=>'报告已完成',
self::STEP_DISCARD=>'作废',
];
public $step = [
self::STEP_BIND=>'待回寄样本',
self::STEP_SENT=>'待回寄样本',
self::STEP_RECEIVE=>'样本已收到',
self::STEP_DNA=>'DNA提取中',
self::STEP_DNA_EXTRACTED=>'文库分析中',
self::STEP_UNQUALIFIED=>'样本不合格',
self::STEP_ANALYSIS=>'基因测序中',
self::STEP_COMPLETE=>'报告已完成',
];
public $step_summary = [
self::STEP_BIND=>'样品回寄',
self::STEP_SENT=>'样品回寄',
self::STEP_RECEIVE=>'实验室已收到样本',
self::STEP_DNA=>'正在提取DNA',
self::STEP_DNA_EXTRACTED=>'文库分析中',
self::STEP_UNQUALIFIED=>'实验室提取DNA失败',
self::STEP_ANALYSIS=>'基因测序中',
self::STEP_COMPLETE=>'报告已完成',
];
//获取用户的总数
public function getUserTotal($user_id)
{
$this->db->select('count(*) as count');
$this->db->from($this->table);
$this->db->where('user_id', $user_id);
$this->db->where('step !=', Sample_model::STEP_DISCARD);
$list = $this->db->get()->row_array();
return $list['count'];
}
//获取种猫种狗
public function getBreeding($user_id,$exclude_ids,$pet_species,$pet_sex,$limit)
{
$this->db->select('base.*,sn.device_id');
$this->db->from('sample as base');
$this->db->where('is_breeding', 1);
$this->db->where('user_id', $user_id);
if(!empty($pet_sex)){
$this->db->where('base.pet_sex', $pet_sex);
}
if(!empty($pet_species)){
$this->db->where('base.pet_species', $pet_species);
}
if(!empty($exclude_ids)){
$this->db->where_not_in('base.id', $exclude_ids);
}
$this->db->order_by('base.id','desc');
$this->db->limit($limit);
//$this->db->where('step <', self::STEP_SENT);
$this->db->join('series_number as sn', 'base.series_id = sn.id','left');
$list = $this->db->get()->result_array();
if($list){
foreach ($list as &$item) {
$this->format($item);
}
}
return $list;
}
//根据id获取样本
public function getByUserIds($ids=[])
{
$this->db->select('base.*,sn.device_id');
$this->db->from('sample as base');
$this->db->join('series_number as sn', 'base.series_id = sn.id','left');
$this->db->where_in('base.id', $ids);
return $this->db->get()->result_array();
}
//获取用户未回寄的样品
public function getNotSentByUserId($user_id)
{
$this->db->select('*');
$this->db->from('sample');
$this->db->where('user_id', $user_id);
$this->db->where('step <', self::STEP_SENT);
return $this->db->get()->result_array();
}
//修改样品状态
public function updateStatus($id,$step)
{
}
public function get($id=false, $field='base.id', $is_list=false, $select=null)
{
$list = parent::get($id, $field, $is_list, $select);
if(!$list){
return $list;
}
if($is_list){
foreach ($list as &$item) {
$this->format($item);
}
}else{
$this->format($list);
}
return $list;
}
//样品处理
public function format(&$entity){
$this->load->library('uploads');
$entity['step_desc'] = $this->step[$entity['step']];
$entity['pet_img'] = empty($entity['pet_img'])?'':base_url().Uploads::DIR.'/'.$entity['pet_img'];
}
//批量发送完成通知
public function complete($ids=[]){
//发送微信通知
$this->load->model('weixin_message_model');
$this->load->model('customer_model');
$this->load->model('series_number_model');
$this->load->model('package_model');
foreach ($ids as $id) {
$sample = $this->sample_model->get($id);
$conf = $this->config->item('allowed_cors_origins');
$user = $this->customer_model->get($sample['user_id']);
$series = $this->series_number_model->get($sample['series_id']);
try{
//如果没关注,会抛出错误,忽略
$this->weixin_message_model->send(
$user['openid'],
Weixin_message_model::TEMPLATE_RESULT,
base_url().'weixin/?url='.urlencode($conf['report'].'/'.$series['device_id']),
[
'first'=>'您的爱宠检测报告已生成,请点击查看结果',
'keyword1'=>$series['device_id'],
'keyword2'=>date('Y-m-d'),
'remark'=>'',
]
);
}catch (Exception $e){
}
}
}
}