什么是动态价格(自命名)?—— 一般来说商品有个固定的价格,随着节假日或者促销活动可能会发生价格变化,结束后又恢复原价,如果每次价格变化都需要修改价格,那么不仅在时间上不好把握,也需要消耗更多的人力。 为了适应这种情况,我们可以给价格在预期的时间段赋予一个新的价格,那么,动态价格就是——给未来的时间段设置一个新的价格,当这个时间到来的时候自动采用新的价格。
要点:
1.优先级:特定时间 > 节假日(不带年份) > 常规价格
2.如何读取数据
3.如何转为图表
效果图:
动态价格添加及列表
核心代码:
1 /** 2 * 获取动态价格节点数据 3 * @param type $pid 4 * @param type $qbdate 5 * @param type $qedate 6 * @return type 7 */ 8 public function get_data($pid,$qbdate="",$qedate="") 9 { 10 //查询时间容错 11 if($qbdate>$qedate){ 12 $tmpdate=$qbdate; 13 $qbdate=$qedate; 14 $qedate=$tmpdate; 15 } 16 $today=mktime(0,0,0,date('m'),date('d'),date('Y')); 17 //is_date 是app functions global.funs.php中的自定义函数 18 if(empty($qbdate) || !is_date($qbdate)) $qbdate=strtotime("-60 days",$today); //默认查询开始年份 19 if(empty($qedate) || !is_date($qedate)) $qedate=strtotime("+60 days",$today); 20 21 //获取景点信息 22 $this->table_name =$this->db_tablepre.'travel_scenic'; 23 $pid = intval($pid); 24 if(!$pid) return false; 25 $r = $this->get_one(array('id'=>$pid)); 26 $data['pid']=$r['id']; 27 $data['title']=$r['title']; 28 $data['price']=$r['price']; 29 $data['cost']=$r['cost']; 30 $data['qbdate']=$qbdate; 31 $data['qedate']=$qedate; 32 33 34 $days=ceil(abs($qbdate-$qedate)/86400); //查询开始日期与结束日期的天数 35 36 //给时间段内的数组赋初值(基本价格) 37 for($i=0;$i<=$days;$i++){ 38 $the_day=strtotime("+".$i." days",$qbdate); 39 $y=intval(date('Y',$the_day)); 40 $m=intval(date('m',$the_day)); 41 $d=intval(date('d',$the_day)); 42 $price_data[$y.'_'.$m.'_'.$d]=$data['price']; 43 $cost_data[$y.'_'.$m.'_'.$d]=$data['cost']; 44 $desc_data[$y.'_'.$m.'_'.$d]=""; 45 } 46 47 //SQL查询准备 48 $qbyear=date('Y',$qbdate); 49 $qbmonth=date('m',$qbdate); 50 $qbday=date('d',$qbdate); 51 52 $qeyear=date('Y',$qedate); 53 $qemonth=date('m',$qedate); 54 $qeday=date('d',$qedate); 55 56 $this->table_name = 'travel_scenic_dynprice'; 57 //此处用循环来解决跨年度查询的问题,每次查询一年的,然后合并; 58 for($qyear=$qbyear;$qyear<=$qeyear;$qyear++){ 59 60 $this->query("select *, 61 DATE_FORMAT(concat(CASE byear WHEN 0 THEN '".$qyear."' ELSE byear END ,'-',bmonth,'-',bday),'%Y-%m-%d') as bdate, 62 DATE_FORMAT(concat(CASE eyear WHEN 0 THEN '".$qyear."' ELSE eyear END ,'-',emonth,'-',eday),'%Y-%m-%d') as edate 63 from phpcms_travel_scenic_dynprice 64 where pid=".$pid." and priority=1 and status=1 65 and DATE_FORMAT(concat(CASE byear WHEN 0 THEN '".$qyear."' ELSE byear END ,'-',bmonth,'-',bday),'%Y-%m-%d') >='".$qbyear."-".$qbmonth."-".$qbday."' 66 and DATE_FORMAT(concat(CASE eyear WHEN 0 THEN '".$qyear."' ELSE eyear END ,'-',emonth,'-',eday),'%Y-%m-%d')<='".$qeyear."-".$qemonth."-".$qeday."' 67 order by id asc"); 68 69 $r2=$this->fetch_array(); 70 71 foreach ($r2 as $key => $value) { 72 $tmp_bdate=strtotime($r2[$key]['bdate']); 73 $tmp_edate=strtotime($r2[$key]['edate']); 74 $days=ceil(abs($tmp_bdate-$tmp_edate)/86400); 75 //echo $days; 76 for($i=0;$i<=$days;$i++){ 77 $the_day=strtotime("+".$i." days",$tmp_bdate); 78 $y=intval(date('Y',$the_day)); 79 $m=intval(date('m',$the_day)); 80 $d=intval(date('d',$the_day)); 81 $price_data[$y.'_'.$m.'_'.$d]=$r2[$key]['dynprice']; 82 $cost_data[$y.'_'.$m.'_'.$d]=$r2[$key]['dyncost']; 83 $desc_data[$y.'_'.$m.'_'.$d]=$r2[$key]['description']; 84 } 85 86 } 87 //合并每年的数组 88 $price_data+=$price_data; 89 $cost_data+=$cost_data; 90 } 91 92 //print_r($price_data); 93 //此处针对特定时间的价格进行处理。 94 $this->query("select *, 95 DATE_FORMAT(concat(byear ,'-',bmonth,'-',bday),'%Y-%m-%d') as bdate, 96 DATE_FORMAT(concat(eyear,'-',emonth,'-',eday),'%Y-%m-%d') as edate 97 from phpcms_travel_scenic_dynprice 98 where pid=".$pid." and priority=2 and status=1 99 and DATE_FORMAT(concat(byear,'-',bmonth,'-',bday),'%Y-%m-%d') >='".$qbyear."-".$qbmonth."-".$qbday."' 100 and DATE_FORMAT(concat(eyear,'-',emonth,'-',eday),'%Y-%m-%d')<='".$qeyear."-".$qemonth."-".$qeday."' 101 order by id asc"); 102 $r3=$this->fetch_array(); 103 104 foreach ($r3 as $key => $value) { 105 $tmp_bdate=strtotime($r3[$key]['bdate']); 106 $tmp_edate=strtotime($r3[$key]['edate']); 107 $days=ceil(abs($tmp_bdate-$tmp_edate)/86400); 108 for($i=0;$i<=$days;$i++){ 109 $the_day=strtotime("+".$i." days",$tmp_bdate); 110 //echo date("Y-m-d",$the_day)." "; 111 $y=intval(date('Y',$the_day)); 112 $m=intval(date('m',$the_day)); 113 $d=intval(date('d',$the_day)); 114 $price_data[$y.'_'.$m.'_'.$d]=$r3[$key]['dynprice']; 115 $cost_data[$y.'_'.$m.'_'.$d]=$r3[$key]['dyncost']; 116 $desc_data[$y.'_'.$m.'_'.$d]=$r3[$key]['description']; 117 } 118 119 } 120 121 122 //去除price_data和cost_data连续都相同的节点 123 $i=1; 124 $count=count($price_data); 125 foreach ($price_data as $key => $value) { 126 if($i==1){ 127 $first_node=$price_data[$key]; 128 $first_node2=$cost_data[$key]; 129 } 130 if($i!=1 && $i!=$count){ 131 if($price_data[$key]==$first_node && $cost_data[$key]==$first_node2 ){ 132 unset($price_data[$key]); 133 unset($cost_data[$key]); 134 unset($desc_data[$key]); 135 }else{ 136 $first_node=$price_data[$key]; 137 $first_node2=$cost_data[$key]; 138 } 139 } 140 $i++; 141 } 142 143 144 $data['price_data']=$price_data; 145 $data['cost_data']=$cost_data; 146 $data['desc_data']=$desc_data; 147 //print_r($data); 148 return $data; 149 150 } 151 152 /** 153 * 根据数据生成chart所需的data数据 154 * @param type $data 155 * @return type 156 */ 157 public function get_chartdata($data) 158 { 159 $price_data=$data['price_data']; 160 $cost_data=$data['cost_data']; 161 $desc_data=$data['desc_data']; 162 $chartdata="["; 163 foreach ($price_data as $key => $value) { 164 $keys=explode("_",$key); 165 $chartdata.="[new Date(".$keys[0].", ".($keys[1]-1).", ".$keys[2]."), ".$price_data[$key].",'".$desc_data[$key]."'],"; 166 } 167 $chartdata=rtrim($chartdata,',').']'; 168 169 $chartdata2="["; 170 foreach ($cost_data as $key => $value) { 171 $keys=explode("_",$key); 172 $chartdata2.="[new Date(".$keys[0].", ".($keys[1]-1).", ".$keys[2]."), ".$cost_data[$key].",'".$desc_data[$key]."'],"; 173 } 174 $chartdata2=rtrim($chartdata2,',').']'; 175 return array('pid'=>$data['pid'], 176 'title'=>$data['title'], 177 'qbdate'=>$data['qbdate'], 178 'qedate'=>$data['qedate'], 179 'chartprice'=>$chartdata, 180 'chartcost'=>$chartdata2); 181 }