<?php  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								/**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								 *
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								 */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								include_once(SERVER_ROOT . "/model/mBase.php");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								class mFormula extends mBase {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    private $obj;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    private $tbl;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    private $formula_herb_tbl;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    private $herb_tbl;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    private $collect_log_tbl;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    private $formula_use_log_tbl;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    private $unit_conv;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    private $user_conv;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function __construct() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $this->obj = new dFormula();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $this->tbl = 'tcm_formula';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $this->formula_herb_tbl = 'tcm_formula_herb';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $this->herb_tbl = 'tcm_herb';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $this->collect_log_tbl = 'tcm_collect_log';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $this->formula_use_log_tbl = 'tcm_formula_use_log';
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $this->unit_conv = 'tcm_unit_conv';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $this->user_conv = 'tcm_user_conv';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function searchFormulaList($condition, $page_num, $page_size) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $offset = ($page_num - 1) * $page_size;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $uid = 0;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $where = "1=1";
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (!empty($condition)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($condition as $key => $val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if ($key == 'name') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and s.{$key} like '%{$val}%'";
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                } elseif ($key == 'uid') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $uid = $val;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and s.{$key} in (0,{$val})";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } elseif (is_array($val)) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    $val = implode(',', $val);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and s.{$key} in ({$val})";
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and s.{$key}={$val}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $sql = "select s.*,COALESCE(u.use_num, 0) AS use_num from {$this->tbl} as s left join {$this->formula_use_log_tbl} as u on s.id=u.formula_id and u.uid={$uid} where {$where} group by s.id order by use_num desc,s.sort desc,s.id asc limit {$offset},{$page_size}";
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $data = $this->obj->execute($sql, true, true);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $formula_ids = array_column($data, 'id');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $formula_arr = $this->getFormulaHerbData($formula_ids);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $unit_conv = array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $user_conv = $this->getUserConvList($uid);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if ($user_conv) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $conv_ids = array_column($user_conv, 'conv_id');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if ($conv_ids) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $unit_conv = $this->getUnitConvByIds($conv_ids);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $unit_conv = array_column($unit_conv, null, 'unit_type');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        foreach ($data as & $da) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            if (isset($formula_arr[$da['id']])) $da['formula'] = $formula_arr[$da['id']];
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            //原方信息
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            if (!empty($da['original'])) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $original = json_decode($da['original'], true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $original = array_column($original, null, 'name');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                foreach ($da['formula'] as & $v) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    if (isset($original[$v['name']])) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                        $num = $this->convByUserChoice($original[$v['name']]['num'], $unit_conv);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                        if ($num) $v['num'] = $num;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                        $v['num_str'] = $original[$v['name']]['num'];
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                        $v['desc'] = $original[$v['name']]['desc'];
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            unset($da['original']);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $data;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function convByUserChoice($num_str, $unit_conv) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$unit_conv) return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $unit_name = preg_replace('/\d/', '', $num_str);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $unit_type = array_search($unit_name, $GLOBALS['weight_convert_list']);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (!$unit_type) return false;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (!isset($unit_conv[$unit_type])) return false;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $unit_num = filter_var($num_str, FILTER_SANITIZE_NUMBER_INT);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $conv_num = $unit_conv[$unit_type]['num'];
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $num = $unit_num * $conv_num;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (!$num) return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $num;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function searchFormulaTotal($condition) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $where = "1=1";
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (!empty($condition)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($condition as $key => $val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if ($key == 'name') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key} like '%{$val}%'";
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                } elseif ($key == 'uid') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key} in (0,{$val})";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } elseif (is_array($val)) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    $val = implode(',', $val);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    $where .= " and {$key} in ({$val})";
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key}={$val}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $sql = "select count(*) as count from $this->tbl where {$where}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $res = $this->obj->execute($sql, true, true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num = $res[0]['count'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $num;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function getUserFormulaList($condition, $page_num, $page_size) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $offset = ($page_num - 1) * $page_size;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $uid = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $where = "1=1";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!empty($condition)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($condition as $key => $val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if ($key == 'uid') $uid = $val;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if ($key == 'name') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and s.{$key} like '%{$val}%'";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } elseif (is_array($val)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $val = implode(',', $val);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and s.{$key} in ({$val})";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and s.{$key}={$val}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $sql = "select s.*,COALESCE(u.use_num, 0) AS use_num from {$this->tbl} as s left join {$this->formula_use_log_tbl} as u on s.id=u.formula_id and u.uid={$uid} where {$where} group by s.id order by use_num desc,s.sort desc,s.id desc limit {$offset},{$page_size}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->execute($sql, true, true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function getUserFormulaTotal($condition) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $where = "1=1";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!empty($condition)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($condition as $key => $val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if ($key == 'name') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key} like '%{$val}%'";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } elseif (is_array($val)) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    $val = implode(',', $val);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    $where .= " and {$key} in ({$val})";
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key}={$val}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $sql = "select count(*) as count from $this->tbl where {$where}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $res = $this->obj->execute($sql, true, true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num = $res[0]['count'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $num;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getFormulaList($condition, $page_num, $page_size) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $offset = ($page_num - 1) * $page_size;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $where = "1=1 ";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!empty($condition)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($condition as $key => $val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if ($key == 'name') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key} like '%{$val}%'";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } elseif (is_array($val)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $val = implode(',', $val);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key} in ({$val})";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key}={$val}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $data = $this->obj->selectAll($this->tbl, array('sql' => $where, 'vals' => array()), 'id desc ', array($offset, $page_size));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $formula_ids = array_column($data, 'id');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $formula_arr = $this->getFormulaHerbData($formula_ids);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        foreach ($data as & $da) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (isset($formula_arr[$da['id']])) $da['formula'] = $formula_arr[$da['id']];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //原方信息
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (!empty($da['original'])) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $original = json_decode($da['original'], true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $original = array_column($original, null, 'name');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                foreach ($da['formula'] as & $v) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    if (isset($original[$v['name']])) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        $v['num_str'] = $original[$v['name']]['num'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        $v['desc'] = $original[$v['name']]['desc'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            unset($da['original']);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $data;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function getFormulaTotal($condition) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $where = "1=1 ";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!empty($condition)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($condition as $key => $val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if ($key == 'name') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key} like '%{$val}%'";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } elseif (is_array($val)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $val = implode(',', $val);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key} in ({$val})";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $where .= " and {$key}={$val}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->count($this->tbl, array('sql' => $where, 'vals' => array()));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getFormulaById($id) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id)));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getHerbs($ids) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->selectIn($this->herb_tbl, array('id' => $ids));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getFormulasHerb($formula_ids) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $data = $this->obj->selectIn($this->formula_herb_tbl, array('formula_id' => $formula_ids));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (empty($data)) return array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return array_column($data, null, 'id');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getFormulaHerbData($formula_ids) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $formula_herbs = $this->getFormulasHerb($formula_ids);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $herb_ids = array_column($formula_herbs, 'herb_id');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $herbs = $this->getHerbs($herb_ids);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $herb_arr = array_column($herbs, null, 'id');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $formula_arr = array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        foreach ($formula_herbs as $formula_herb) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $formula_arr[$formula_herb['formula_id']][] = array(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                'id' => $formula_herb['herb_id'],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                'name' => $herb_arr[$formula_herb['herb_id']]['name'],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                'num' => $formula_herb['num'],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                //'desc' => $formula_herb['desc'],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                'num_str' => $formula_herb['num'] . 'g',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            );
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        return $formula_arr;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getFormulaInfo($id, $uid) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        //药方信息
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $formula = $this->getFormulaById($id);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (empty($formula)) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->setError('抱歉,未找到匹配的药方');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        //自拟药方 判断药方归属
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if ($uid > 0 & &  $formula['uid'] > 0 & &  $formula['uid'] != $uid) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->setError('抱歉,这不是你的药方');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        //获取药方药材信息
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $formula_arr = $this->getFormulaHerbData(array($formula['id']));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $formulaHerb = $formula_arr[$formula['id']];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $original = $formula['original'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        unset($formula['original']);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (empty($original)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $formula['formula'] = $formulaHerb;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return $formula;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $unit_conv = array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $user_conv = $this->getUserConvList($uid);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if ($user_conv) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $conv_ids = array_column($user_conv, 'conv_id');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if ($conv_ids) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $unit_conv = $this->getUnitConvByIds($conv_ids);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $unit_conv = array_column($unit_conv, null, 'unit_type');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        //原方信息
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $original = json_decode($original, true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $original = array_column($original, null, 'name');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        foreach ($formulaHerb as & $v) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (isset($original[$v['name']])) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $num = $this->convByUserChoice($original[$v['name']]['num'], $unit_conv);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                if ($num) $v['num'] = $num;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $v['num_str'] = $original[$v['name']]['num'];
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $v['desc'] = $original[$v['name']]['desc'];
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $formula['formula'] = $formulaHerb;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $formula;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function convOriginal($original) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $herbString_arr = explode(' ', $original);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num_map = $GLOBALS['num_list'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num_list = implode('', array_keys($num_map));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $herb_arr = array();
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        foreach ($herbString_arr as $herbString) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $pattern = '/^(.)(.*?)([' . $num_list . ']+)([^(]*)?((.*?))?/u';
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            preg_match($pattern, $herbString, $matches);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if ($matches) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $herb_name = str_replace(array('(', ')'), '', trim($matches[1] . $matches[2]));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $herb_num = str_replace(array('(', ')'), '', trim($matches[3] . $matches[4]));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $herb_desc = isset($matches[5]) ? str_replace(array('(', ')'), '', trim($matches[5])) : "";
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $herb_arr[] = array(
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    'herb_name' => $herb_name,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    'herb_num' => $herb_num,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    'herb_desc' => $herb_desc,
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $herb_arr[] = array(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    'herb_name' => $herbString,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    'herb_num' => "",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    'herb_desc' => "",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $herb_arr;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function convToNumber($str) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num_map = $GLOBALS['num_list'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num_list = implode('', array_keys($num_map));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        preg_match_all('/[' . $num_list . ']+/u', $str, $matches);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        foreach ($matches[0] as $chinese) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $result = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $current = 0; // 当前的数字
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            for ($i = 0; $i <  mb_strlen ( $ chinese ) ;  $ i + + )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $char = mb_substr($chinese, $i, 1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if (isset($num_map[$char])) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $num = $num_map[$char];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    if ($num == 10) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        // 如果是“十”
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        if ($current == 0) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                            $current = 1; // 处理“十”前没有数字的情况
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        $result += $current * 10; // 将当前数字乘以十
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        $current = 0; // 重置当前数字
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        $current += $num; // 累加当前数字
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $result = $result + $current;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $str = str_replace($chinese, $result, $str);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $str;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function convertToNum($herb_name, $str, $onlyconertNum = true, $from = '汉') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num_list = $GLOBALS['num_list'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num_list_arr = array_merge(array_keys($GLOBALS['num_list']['num']), array_keys($GLOBALS['num_list']['unit']));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $weight_list = $GLOBALS['weight_list'][$from];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $weight_list_arr = array_keys($GLOBALS['weight_list'][$from]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        //计量数字
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num_str = str_replace($weight_list_arr, '', $str);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        //计量单位
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $unit = str_replace($num_list_arr, '', $str);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        //计量单位
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $unit = str_replace($num_str, '', $unit);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        //非数字转换
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $num = $num_str;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!is_numeric($num) & &  $num) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $num = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $temp = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $length = mb_strlen($num_str);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            for ($i = 0; $i <  $length; $i++) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $char = mb_substr($num_str, $i, 1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if (isset($num_list['num'][$char])) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $temp += $num_list['num'][$char];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                } elseif (isset($num_list['unit'][$char])) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $temp = ($temp == 0 ? 1 : $temp) * $num_list['unit'][$char];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $num += $temp; // 将临时结果累加到最终结果中
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $temp = 0; // 重置临时结果
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $num += $temp;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        //只将汉字数字转换成number
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if ($onlyconertNum) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return $num . $unit;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        //单位换算 直接换算成克数
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $weight = isset($weight_list[$unit]) ? $weight_list[$unit] : 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (isset($weight[$herb_name])) $weight = $weight[$herb_name];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!is_numeric($weight)) $weight = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $num * $weight;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function getHerb($name) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->select($this->herb_tbl, array('sql' => '`name`=?', 'vals' => array($name)));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function insertHerb($info) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $herb_id = $this->obj->insert($this->herb_tbl, $info);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$herb_id) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->writeLog('formula', 'save_error_log', '添加药材失败|' . json_encode($info));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        return $herb_id;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function dealForulaHerb($uid, $herbs) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $formula_herb = array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $herb_ids = array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        foreach ($herbs as $key => $item) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $name = trim($item['name']);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $num = trim($item['num']);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $desc = trim($item['desc']);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //自拟药方 克重不能为0
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            if ($uid > 0 & &  $num == 0) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $this->setError("抱歉,药材{$name}重量不能为零");
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $temp = array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            //格式化药材id
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $herb = $this->getHerb($name);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if ($herb) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $temp['herb_id'] = $herb['id'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            } else {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $temp['herb_id'] = $this->insertHerb(array('uid' => $uid, 'name' => $name, 'desc' => $desc));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                if (!$temp['herb_id']) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    $this->setError('添加药材失败');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (isset($herb_ids[$temp['herb_id']])) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $this->setError("抱歉,药方中存在重复的药材");
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $herb_ids[$temp['herb_id']] = $temp['herb_id'];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //录药方上传的原方需要 单位换算
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $temp['num'] = $num;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if ($uid == 0) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                //目前先不换算 默认为0 使用原方用量
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $temp['num'] = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                //$temp['num'] = $this->convertToNum($name, $num, false);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $temp['sort'] = $key;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $formula_herb[] = $temp;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        return $formula_herb;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function insertFormula($info) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $id = $this->obj->insert($this->tbl, $info);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (empty($id)) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->writeLog('formula', 'save_error_log', '添加药方失败|' . json_encode($info, JSON_UNESCAPED_UNICODE));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        return $id;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function insertFormulaHerb($info) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $res = $this->obj->mutiInsert($this->formula_herb_tbl, $info);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->writeLog('formula', 'save_error_log', '批量添加药方药材关联失败|' . json_encode($info, JSON_UNESCAPED_UNICODE));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function updateFormula($id, $info) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $res = $this->obj->update($this->tbl, $info, array('sql' => '`id`=?', 'vals' => array($id)));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->writeLog('formula', 'save_error_log', '更新药方失败|' . $id . '|' . json_encode($info, JSON_UNESCAPED_UNICODE));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getFormulaHerb($formula_id) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->selectAll($this->formula_herb_tbl, array('sql' => '`formula_id`=?', 'vals' => array($formula_id)), 'sort asc ');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function mutiUpdateFormulaHerb($id, $new_data) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $old_data = $this->getFormulaHerb($id);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $old_num = count($old_data);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $new_num = count($new_data);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if ($old_num >= $new_num) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //需要删除药方对应的药材
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($old_data as $key => $value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if (!isset($new_data[$key])) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $res = $this->obj->delete($this->formula_herb_tbl, array('sql' => '`id`=?', 'vals' => array($value['id'])));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                        $this->writeLog('formula', 'save_error_log', '删除药方药材关联失败|' . $value['id']);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    continue;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $res = $this->obj->update($this->formula_herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id'])));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    $this->writeLog('formula', 'save_error_log', '更新药方药材关联失败1|' . $value['id'] . '|' . json_encode($new_data[$key], JSON_UNESCAPED_UNICODE));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //需要增加药方对应的药材
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($new_data as $key => $value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if (!isset($old_data[$key])) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    $res = $this->obj->insert($this->formula_herb_tbl, $value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                        $this->writeLog('formula', 'save_error_log', '添加药方药材关联失败|' . $value['id']);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                        return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    continue;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $res = $this->obj->update($this->formula_herb_tbl, $value, array('sql' => '`id`=?', 'vals' => array($old_data[$key]['id'])));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    $this->writeLog('formula', 'save_error_log', '更新药方药材关联失败2|' . $value['id'] . '|' . json_encode($new_data[$key], JSON_UNESCAPED_UNICODE));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function saveFormula($uid, $id, $name, $source, $method, $herbs, $remark = "", $admin_user = 0) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (empty($name)) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->setError('请填写药方名称');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $herbs = json_decode($herbs, true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (empty($herbs)) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->setError('请填写药方详情');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if ($id) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $formula = $this->getFormulaById($id);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (empty($formula)) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $this->setError('抱歉,未找到匹配的药方');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            if ($formula['uid'] != $uid) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $this->setError('抱歉,这不是你的药方');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $data = array(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            'uid' => $uid,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            'name' => $name,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            'source' => $source,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        //$uid为0 即为后台录入药方 需处理原方数据
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if ($uid == 0) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $org_herb = array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($herbs as $key => $herb) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $org_herb[$key] = array(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                    'name' => trim($herb['name']),
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    'num' => trim($herb['num']),
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                    'desc' => trim($herb['desc']),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                );
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                //如果转义成功 则保存转义后的数据
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $num = $this->convToNumber(trim($herb['num']));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                if ($num != 0) $org_herb[$key]['num'] = $num;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //更新原方和用法
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $data['original'] = json_encode($org_herb, JSON_UNESCAPED_UNICODE);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $data['method'] = $method;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $data['remark'] = $remark;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $data['is_delete'] = 2;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $data['user_id'] = $admin_user;
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        //格式化药方对应的药材数据
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $formula_herb = $this->dealForulaHerb($uid, $herbs);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$formula_herb) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->writeLog('formula', 'save_error_log', '药方药材初始化失败|' . $uid . '|' . json_encode($herbs, JSON_UNESCAPED_UNICODE));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$id) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //添加药方 并添加药方药材关联关系
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $id = $this->insertFormula($data);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (!$id) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $this->setError('添加药材失败');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //创建药方对应的药材
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($formula_herb as & $item) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $item['formula_id'] = $id;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $res = $this->insertFormulaHerb($formula_herb);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $this->setError('添加药方详情失败');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //更新药方 并更新药方药材关联关系
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $res = $this->updateFormula($id, $data);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $this->setError('更新药方失败');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //批量更新药方药材关联关系
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($formula_herb as & $item) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $item['formula_id'] = $id;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $res = $this->mutiUpdateFormulaHerb($id, $formula_herb);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                $this->setError('更新药方详情失败');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $id;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function deleteFormula($uid, $id, $is_delete) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $formula = $this->getFormulaById($id);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (empty($formula)) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->setError('抱歉,未找到匹配的药方');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if ($formula['uid'] != $uid) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->setError('抱歉,这不是你的药方');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $res = $this->updateFormula($id, array('is_delete' => $is_delete));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->setError('删除药方失败');
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getHerbById($id) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->select($this->herb_tbl, array('sql' => '`id`=?', 'vals' => array($id)));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function getFormulaByIds($ids) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->selectIn($this->tbl, array('id' => $ids));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function updateFormulaUseLog($uid, $formula_id) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $res = $this->obj->increase($this->formula_use_log_tbl, array('sql' => '`uid`=? and `formula_id`=?', 'vals' => array($uid, $formula_id)), 'use_num');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$res) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->writeLog('formula', 'update_error_log', '药方使用次数更新失败|' . $uid . '|' . $formula_id);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function getCollectLog($page_num, $page_size) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $offset = ($page_num - 1) * $page_size;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->selectAll($this->collect_log_tbl, array(), 'collect_time desc ', array($offset, $page_size));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function getCollectLogTotal() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->count($this->collect_log_tbl);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getUnitConvByIds($ids) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        return $this->obj->selectIn($this->unit_conv, array('id' => $ids), array('sql' => '`status`=?', 'vals' => array(0)));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getUnitConvById($id) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        return $this->obj->select($this->unit_conv, array('sql' => '`id`=?', 'vals' => array($id)));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getUnitTypeConv($unit_type) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        return $this->obj->selectAll($this->unit_conv, array('sql' => '`unit_type`=? and `status`=?', 'vals' => array($unit_type, 0)));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getUnitConvList($condition, $page_num = 1, $page_size = 500) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $offset = ($page_num - 1) * $page_size;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $where = "1=1 ";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!empty($condition)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            foreach ($condition as $key => $val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $where .= " and {$key}={$val}";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->selectAll($this->unit_conv, array('sql' => $where, 'vals' => array()), 'id desc ', array($offset, $page_size));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function updateUnitConv($id, $info) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->update($this->unit_conv, $info, array('sql' => '`id`=?', 'vals' => array($id)));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function insertUnitConv($info) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $id = $this->obj->insert($this->unit_conv, $info);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (empty($id)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $this->writeLog('formula', 'save_error_log', '添加计量单位转换失败|' . json_encode($info, JSON_UNESCAPED_UNICODE));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $id;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function saveUnitConv($id, $unit_type, $num, $from) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$unit_type) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $this->setError('请选择计量单位');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$num) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $this->setError('请填写转换克重');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (empty($from)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $this->setError('请填写转换根据');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $data = array(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            'unit_type' => $unit_type,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            'num' => $num,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            'from' => $from,
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            'status' => 1,
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$id) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //添加药方 并添加药方药材关联关系
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $id = $this->insertUnitConv($data);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (!$id) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $this->setError('添加失败');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            //更新药方 并更新药方药材关联关系
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $res = $this->updateUnitConv($id, $data);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (!$res) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $this->setError('更新失败');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $id;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getUserConvList($uid) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->selectAll($this->user_conv, array('sql' => '`uid`=?', 'vals' => array($uid)));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function getUserConv($uid, $unit_type) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return $this->obj->select($this->user_conv, array('sql' => '`uid`=? and `unit_type`=?', 'vals' => array($uid, $unit_type)));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								    public function saveUserConv($uid, $unit_type, $conv_id) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$unit_type || !$conv_id) {
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								            $this->setError('参数错误');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $user_conv = $this->getUserConv($uid, $unit_type);
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if ($user_conv) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $res = $this->updateUserConv($user_conv['id'], array('conv_id' => $conv_id));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            if (!$res) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                $this->setError('更新失败');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								                return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        $res = $this->insertUserConv(array('uid' => $uid, 'unit_type' => $unit_type, 'conv_id' => $conv_id));
							 
						 
					
						
							
								
							 
							
								
									
										 
								
							 
							
								 
							
								        if (!$res) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $this->setError('设置失败');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function updateUserConv($id, $info) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $res = $this->obj->update($this->user_conv, $info, array('sql' => '`id`=?', 'vals' => array($id)));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$res) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $this->writeLog('formula', 'save_error_log', '更新用户计量单位转换设置失败|' . $id . '|' . json_encode($info, JSON_UNESCAPED_UNICODE));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    public function insertUserConv($info) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        $res = $this->obj->insert($this->user_conv, $info);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        if (!$res) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            $this->writeLog('formula', 'save_error_log', '添加用户计量单位转换设置失败|' . json_encode($info));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								            return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								        return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								}