2024-04-01 15:54:27 +08:00
< ? php
header ( " Content-type:text/html;charset=utf-8 " );
$gdbname = " yzms " ;
require_once ( dirname ( __FILE__ ) . " /common.php " );
require_once ( dirname ( __FILE__ ) . " /push_sms.php " );
require_once ( dirname ( __FILE__ ) . " /include/pay.inc.php " );
// 参数过滤
function check_form ( $str ){
$farr = array (
" / \ s+/ " , //过滤多余空白
//过滤 <script>等可能引入恶意内容或恶意改变显示布局的代码,如果不需要插入flash等,还可以加入<object>的过滤
" /<( \ /?)(script|i?frame|style|html|body|title|link|meta| \ ?| \ %)([^>]*?)>/isU " ,
" /(<[^>]*)on[a-zA-Z]+ \ s*=([^>]*>)/isU " , //过滤javascript的on事件
);
$tarr = array (
" " ,
" < \1 \2 \3 > " , //如果要直接清除不安全的标签,这里可以留空
" \1 \2 " ,
);
$str = preg_replace ( $farr , $tarr , $str );
return $str ;
}
// 根据id获取菜式投票的详情
function get_dish_vote_date_info ( $db , $vote_date_id ){
return $db -> get_one ( " select
a .* ,
b . name meal_type_name ,
c . name dining_hall_name
from tb_dish_vote_date a
left join tb_meal_type b on a . dc_type = b . id
left join tb_dining_hall c on a . dining_hall_id = c . id
where a . id = '{$vote_date_id}' and a . status = '1' " );
}
function dc_type_info ( $dc_type ){
return $db -> get_one ( " select * from tb_meal_type where id=' { $dc_type } ' and status='1' " );
}
function dish_vote_foot_type ( $db , $dining_hall_id , $dc_type = '' ){
$where = " " ;
if ( $dc_type ){
$where = " and a.meal_type_id=' { $dc_type } ' " ;
}
return $db -> get_all ( " select
a .* ,
b . name meal_type_name
from tb_dish_vote_foot_type a
left join tb_meal_type b on a . meal_type_id = b . id
where a . dining_hall_id = '{$dining_hall_id}' { $where } and b . status = '1'
order by b . idx desc , a . id asc " );
}
function vote_dish_list_day ( $db , $date , $dc_type , $dining_hall_id , $generate_dish_ids ){
$where = '' ;
if ( $generate_dish_ids ){
$where .= " and a.dish_id not in ( { $generate_dish_ids } ) " ;
}
return $db -> get_all ( " select * from (
select
a .* ,
b . dish_name ,
b . dish_price ,
c . type_name ,
count ( a . id ) dish_vote_num
from tb_dish_vote_user a
left join tb_dish b on a . dish_id = b . id
left join tb_dish_type c on b . dish_type = c . id
where a . status = '2' and a . dining_hall_id = '{$dining_hall_id}' and a . dc_type = '{$dc_type}' and a . date = '{$date}' { $where }
group by a . dish_id ) a
order by a . dish_vote_num desc
" );
}
// 生成菜谱
function add_date_dish ( $db , $vote_date_id , $current_date , $dc_type ){
// 根据id获取菜式投票的详情
$vote_date_info = get_dish_vote_date_info ( $db , $vote_date_id );
// 判断这个日期是否已经存在菜谱
$check_date = $db -> get_one ( " select * from tb_date where dc_date=' { $current_date } ' and dc_type=' { $dc_type } ' " );
if ( $check_date ){
return array (
'status' => - 1 ,
'msg' => " 【 { $current_date } 】该日期已存在菜谱。 "
);
}
// 获取已经生成的菜谱,以及获取已选取的菜品
$dish_ids = array ();
for ( $i = strtotime ( $vote_date_info [ 'start_day' ]); $i <= strtotime ( $vote_date_info [ 'end_day' ]); $i += 86400 ) {
$date_info = $db -> get_one ( " select
a .* ,
GROUP_CONCAT ( b . dish_id ) dish_ids
from tb_date a
left join tb_date_dish b on a . id = b . date_id
where a . dc_date = '".date(' Y - m - d ')."' and a . dc_type = '{$vote_date_info[' dc_type ']}' and a . dining_hall_id = '{$vote_date_info[' dining_hall_id ']}'
GROUP BY a . id
" );
if ( empty ( $date_info [ 'dish_ids' ])){
continue ;
}
$dish_ids = array_merge ( $dish_ids , explode ( " , " , $date_info [ 'dish_ids' ]));
}
// 已经生成菜谱的菜品id
// $data_dish_list_arr = $dish_ids;
// 获取荤素规则
$dish_vote_foot_type = dish_vote_foot_type ( $db , $vote_date_info [ 'dining_hall_id' ], $vote_date_info [ 'dc_type' ]);
// tb_dish_vote_foot_type 根据该表 获取当天票数最多的菜式(荤,素)
$vote_dish_list = vote_dish_list_day ( $db , $current_date , $vote_date_info [ 'dc_type' ], $vote_date_info [ 'dining_hall_id' ], implode ( " , " , $dish_ids ));
// 记录每个规则的选中菜式的个人, 方便判断all_book_limit
$good_type_max_vote = array ();
$dish_arr = array ();
foreach ( $vote_dish_list as $k => $v ){
// 循环foot_type
foreach ( $dish_vote_foot_type as $foot_type_key => $foot_type_val ){
// 排除不需要的规则
if ( $v [ 'dc_type' ] != $foot_type_val [ 'meal_type_id' ]){
continue ;
}
$dish_type_id_arr = explode ( " , " , substr ( $foot_type_val [ 'dish_type_id' ], 1 , strlen ( $foot_type_val [ 'dish_type_id' ]) - 2 ));
// 判断菜式类型是否存在foot_type里
if ( in_array ( $v [ 'dish_type' ], $dish_type_id_arr )){
// 判断在这个周期里,是否已经被安排过菜谱
if ( ! in_array ( $v [ 'dish_id' ], $dish_arr )){
// 超出类型可以max_vote的范围
$good_type_max_vote_num = count ( $good_type_max_vote ) != 0 && isset ( $good_type_max_vote [ $foot_type_val [ 'id' ]]) ? $good_type_max_vote [ $foot_type_val [ 'id' ]] : 0 ;
if ( $foot_type_val [ 'max_vote' ] != 0 && $good_type_max_vote_num >= $foot_type_val [ 'max_vote' ]){
continue ;
}
$good_type_max_vote [ $foot_type_val [ 'id' ]] = $good_type_max_vote_num + 1 ;
$dish_arr [] = array (
'dish_id' => $v [ 'dish_id' ],
'type' => $v [ 'type_name' ],
'name' => $v [ 'dish_name' ],
'price' => $v [ 'dish_price' ],
'num' => 0
);
break ;
}
}
}
}
if ( empty ( $dish_arr )){
// 生成成功1次都改变状态, 添加成功, 更新is_create_dishes状态
$db -> query ( " update tb_dish_vote_date set is_create_dishes='1' where id=' { $vote_date_id } ' " );
// 如果没有投票,则不生成菜谱
return array (
'status' => 0 ,
'msg' => " { $vote_date_info [ 'dining_hall_name' ] } 饭堂 " . date ( 'Y年m月d日' , strtotime ( $current_date )) . " , { $vote_date_info [ 'meal_type_name' ] } 没有生产; "
// 'msg' => "【{$vote_date_info['start_day']} ~ {$vote_date_info['end_day']}】({$vote_date_info['meal_type_name']}) {$current_date},这天没有足够的投票菜品信息,不能生成菜谱,请手动添加对应菜谱。"
);
}
// $dish_arr 获取 $current_date 对应投票最多且排除以生成菜谱的 菜品列表
// 生成 $current_date 这天的菜谱
// $date_data = array(
// 'dc_date' => $current_date,
// 'dc_type' => $vote_date_info['dc_type'],
// 'is_free' => 0,
// 'dining_hall_id' => $vote_date_info['dining_hall_id']
// );
$db -> query ( " insert into tb_date set dc_date=' { $current_date } ', dc_type=' { $vote_date_info [ 'dc_type' ] } ', is_free='0', dining_hall_id=' { $vote_date_info [ 'dining_hall_id' ] } ' " );
$date_id = $db -> insert_id ();
if ( ! $date_id ){
return array (
'status' => 0 ,
'msg' => " { $vote_date_info [ 'dining_hall_name' ] } 饭堂 " . date ( 'Y年m月d日' , strtotime ( $current_date )) . " , { $vote_date_info [ 'meal_type_name' ] } 没有生产(生成菜谱失败); "
);
}
$dish_str = '' ;
foreach ( $dish_arr as $k => $v ){
$dish_str .= " , { $v [ 'name' ] } " ;
$date_dish_data = array (
'date_id' => $date_id ,
'dish_id' => $v [ 'dish_id' ],
'max_num' => 2 ,
'is_normal_dish' => 1 ,
'total_max_num' => 0 ,
'book_num' => 0 ,
'idx' => 0 ,
'book_type' => 1 ,
);
$date_dish_sql = " " ;
foreach ( $date_dish_data as $k2 => $v2 ){
$date_dish_sql .= " , { $k2 } =' { $v2 } ' " ;
}
$date_dish_sql = substr ( $date_dish_sql , 1 , strlen ( $date_dish_sql ) - 1 );
$db -> query ( " insert into tb_date_dish set { $date_dish_sql } " );
$date_dish_id = $db -> insert_id ();
if ( ! $date_dish_id ){
// 生成失败, 删除tb_date_dish 和 tb_date
$db -> query ( " delete from tb_date_dish where date_id=' { $date_id } ' " );
$db -> query ( " delete from tb_date where id=' { $date_id } ' " );
return array (
'status' => 0 ,
'msg' => " { $vote_date_info [ 'dining_hall_name' ] } " . date ( 'Y年m月d日' , strtotime ( $current_date )) . " - { $vote_date_info [ 'meal_type_name' ] } 没有生产(生成菜谱存在错误); "
);
}
}
// 生成成功1次都改变状态, 添加成功, 更新is_create_dishes状态
$db -> query ( " update tb_dish_vote_date set is_create_dishes='1' where id=' { $vote_date_id } ' " );
// 的订餐数据:".count($suc_type_val)." 份;
$dish_str = substr ( $dish_str , 1 , strlen ( $dish_str ) - 1 );
return array (
'status' => 1 ,
'msg' => " { $vote_date_info [ 'dining_hall_name' ] } , " . date ( 'Y年m月d日' , strtotime ( $current_date )) . $vote_date_info [ 'meal_type_name' ] . " 的订餐数据: " . $dish_str . " ,共 " . count ( $dish_arr ) . " 份。 " ,
'date_id' => $date_id ,
'dish_arr' => $dish_arr ,
);
// $get_dish = $this->db->query("");
}
if ( isset ( $argv )){
$act = check_form ( $argv [ 1 ]);
$dining_hall_id = check_form ( $argv [ 2 ]); // 饭堂ID
$company_id = check_form ( $argv [ 3 ]); // 公司ID
$admin_id_suc = check_form ( $argv [ 4 ]); // 生成菜谱成功后, 对改admin_id进行短信通知
$admin_id_err = check_form ( $argv [ 5 ]); // 生成菜谱失败后, 对改admin_id进行短信通知
} else {
$act = check_form ( $_GET [ 'act' ]);
$dining_hall_id = check_form ( $_GET [ 'dining_hall_id' ]);
$company_id = check_form ( $_GET [ 'company_id' ]);
$admin_id_suc = check_form ( $_GET [ 'admin_id_suc' ]);
$admin_id_err = check_form ( $_GET [ 'admin_id_err' ]);
}
if ( $act == 'create_dishes' ){
$fp = fopen ( dirname ( __FILE__ ) . " /time_task_lock.txt " , " w+ " );
if ( ! flock ( $fp , LOCK_EX | LOCK_NB )) {
fclose ( $fp );
exit ;
}
$log_type = " create_dishes " ;
$res [ 'status' ] = 0 ;
// $dining_hall_id = check_form($_GET['dining_hall_id']);
// $admin_id_suc = check_form($_GET['admin_id_suc']);
// $admin_id_err = check_form($_GET['admin_id_err']);
if ( empty ( $dining_hall_id ) || empty ( $admin_id_suc ) || empty ( $admin_id_err )){
// $res['msg'] = "必要参数不能为空";
// echo json_encode($res);
echo " 必要参数不能为空 " ;
exit ;
}
// 获取还没生成的菜式投票列表
$get_not_generate_list = $db -> get_all ( " select
a .* ,
b . name meal_type_name
from tb_dish_vote_date a
left join tb_meal_type b on a . dc_type = b . id
where a . is_create_dishes = '0' and a . status = '1' and a . dining_hall_id = '{$dining_hall_id}' " );
if ( empty ( $get_not_generate_list )){
echo " 没有可生成菜谱的日期 " ;
exit ;
}
$now_time = date ( " Y-m-d H:i:s " );
$generate_num = 0 ;
$generate_suc_num = 0 ;
$create_date_ids = array ();
$vote_date_ids = array ();
$msg = array ();
// $msg_suc = '';
// $msg_err = '';
// 循环还没生成菜谱的周期
foreach ( $get_not_generate_list as $k => $v ){
if ( $now_time >= $v [ 'deadline' ]){
$vote_date_ids [] = $v [ 'id' ];
// 生成菜谱
for ( $i = strtotime ( $v [ 'start_day' ]); $i <= strtotime ( $v [ 'end_day' ]); $i += 86400 ){
// 循环每一天 逐个添加
$current_date = date ( " Y-m-d " , $i );
// $add_date = $this->Dishes_model->add_date($current_date, $v['dc_type'], $v['dining_hall_id'], 1);
// if($add_date == -1){
// $error_msg .= "【".date("Y-m-d", $i)."】 已存在菜谱,生成失败;";
// continue;
// }
$rs = add_date_dish ( $db , $v [ 'id' ], $current_date , $v [ 'dc_type' ]);
$generate_num += 1 ;
if ( $rs [ 'status' ] == 0 ){
$msg [ $current_date ][ 'err' ][] = $rs [ 'msg' ];
// echo $rs['msg'];
// $msg_err .= $rs['msg'];
// pushSms($rs['msg']);
// echo json_encode($rs);
}
if ( $rs [ 'status' ] == 1 ){
$msg [ $current_date ][ 'suc' ][ $v [ 'meal_type_name' ]] .= $rs [ 'msg' ];
// $msg[$current_date]['suc_type'][$v['meal_type_name']] +=1 ;
// $msg[$current_date]['suc_type'][$v['meal_type_name']] +=1 ;
// $msg_suc .= $rs['msg'];
$create_date_ids [] = $rs [ 'date_id' ];
$generate_suc_num += 1 ;
}
}
}
}
// $res['status'] = 1;
// $msg = "【{$get_not_generate_list['start_day']} ~ {$get_not_generate_list['end_day']}】({$get_not_generate_list['meal_type_name']})生成菜谱成功,共生成 {$generate_num} 餐次";
// $msg = "生成菜谱成功,共生成次数 {$generate_num} 餐次,成功生成 {$generate_suc_num} 餐次";
// pushSms($res['msg']);
// 判断是否推送过
if ( $generate_suc_num == 0 ){
echo " 没有可生成的菜谱,生成次数 { $generate_num } ,成功生成次数 { $generate_suc_num } " ;
}
// 获取推送用户的信息
$admin_info = $db -> get_all ( " select * from tb_admin where id in ( { $admin_id_suc } ) and enabled='1' " );
if ( empty ( $admin_info )){
echo " 没有可推送的用户 " ;
exit ;
}
// 获取手机号列表
$cellphone_suc_list = array ();
$cellphone_err_list = array ();
$admin_suc_list = $db -> get_all ( " select * from tb_admin where id in ( { $admin_id_suc } ) and enabled='1' " );
$admin_err_list = $db -> get_all ( " select * from tb_admin where id in ( { $admin_id_err } ) and enabled='1' " );
$admin_suc_ids = array ();
$admin_err_ids = array ();
foreach ( $admin_suc_list as $admin_key => $admin_val ){
$check_create_dish = $db -> get_one ( " select * from tb_jtj_ts_log where type=' { $log_type } ' and admin_id=' { $admin_val [ 'id' ] } ' and vote_date_id in ( " . implode ( " , " , $vote_date_ids ) . " ) " );
// if($check_create_dish){
// // 这些周期都推送过
// continue;
// }
$cellphone_suc_list [] = $admin_val [ 'cellphone' ];
$admin_suc_ids [] = $admin_val [ 'id' ];
}
foreach ( $admin_err_list as $admin_key => $admin_val ){
$check_create_dish = $db -> get_one ( " select * from tb_jtj_ts_log where type=' { $log_type } ' and admin_id=' { $admin_val [ 'id' ] } ' and vote_date_id in ( " . implode ( " , " , $vote_date_ids ) . " ) " );
// if($check_create_dish){
// // 这些周期都推送过
// continue;
// }
$cellphone_err_list [] = $admin_val [ 'cellphone' ];
$admin_err_ids [] = $admin_val [ 'id' ];
}
// , {$vote_date_info['meal_type_name']}的订餐数据;
foreach ( $msg as $msg_key => $msg_val ){
foreach ( $msg_val as $k2 => $v2 ){
$send_msg = " " ;
if ( $k2 == 'suc' && ! empty ( $v2 )){
// 成功的
foreach ( $v2 as $suc_type_key => $suc_type_val ){
$send_msg .= $suc_type_val ;
}
foreach ( $cellphone_suc_list as $k => $v ){
// 过滤
// $check_username = $db->get_all("select * from tb_user where username in ('向熊林','霍瑞兴','张桂香','王建美','蒙秀荣','杜春梅','陆伟玲','谭婷芳','郑新浓','马绿花','莫火燕','覃金兰','张淼开','甘文盛','谢卫连','黎伟玲','黄玉娇','李亚炳','罗庆坚')");
// $check_tel = array_column($check_username, 'cellphone');
// if(in_array($v, $check_tel)){
// 暂时停止推送
// $send_res = sendTs($v, "自动生成菜谱提示", $send_msg);
// $sms_res = sendSms($v, $send_msg);
// }
// $sms_res = sendSms($v, $send_msg);
}
} elseif ( $k2 == 'err' && ! empty ( $msg_val )){
// 失败的
$send_msg = implode ( '' , $msg_val [ 'err' ]);
foreach ( $cellphone_err_list as $k => $v ) {
// 过滤
// $check_username = $db->get_all("select * from tb_user where username in ('向熊林','霍瑞兴','张桂香','王建美','蒙秀荣','杜春梅','陆伟玲','谭婷芳','郑新浓','马绿花','莫火燕','覃金兰','张淼开','甘文盛','谢卫连','黎伟玲','黄玉娇','李亚炳','罗庆坚')");
// $check_tel = array_column($check_username, 'cellphone');
// if(in_array($v, $check_tel)){
// 暂时停止推送
// $send_res = sendTs($v, "自动生成菜谱提示", $send_msg);
// $sms_res = sendSms($v, $send_msg);
// }
// $sms_res = sendSms($v, $send_msg);
}
} else {
continue ;
}
}
}
// ------------ 记录log
$admin_id_arr = array_merge ( $admin_suc_ids , $admin_err_ids );
$admin_id_arr = array_unique ( $admin_id_arr );
foreach ( $admin_id_arr as $admin_id_arr_key => $admin_id_arr_val ){
foreach ( $vote_date_ids as $vote_date_ids_key => $vote_date_ids_val ){
// $data['cellphone'] = $v['cellphone'];
$data [ 'content' ] = $sms_res ;
$data [ 'type' ] = $log_type ;
$data [ 'admin_id' ] = $admin_id_arr_val ;
// $data['user_id'] = $v['id'];
$data [ 'vote_date_id' ] = $vote_date_ids_val ;
$data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
// 推送log表记录
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
}
}
}
// 投票截止推送
if ( $act == 'vote_close_ts' ){
$log_type = " vote_close " ;
// $company_id = check_form($_GET['company_id']);
// $dining_hall_id = check_form($_GET['dining_hall_id']);
$where = " " ;
if ( $company_id ){
$where .= " and a.company_id=' { $company_id } ' " ;
}
if ( $dining_hall_id ){
$where .= " and a.dining_hall_id=' { $dining_hall_id } ' " ;
}
// 获取配置 (投票截止提前的时间)
$config = $db -> get_one ( " select * from tb_jtj_excitation_config where name='VOTE_BEFORE_DEADLINE_HOUR' " );
$vote_close_second = $config [ 'value' ] * 60 * 60 ; // 提前n秒提醒
// 获取投票截止时间 -------------
// 当前时间
$now_time = date ( " Y-m-d H:i:s " );
// 在现在时间 + $vote_close_second 提前 $vote_close_second 小时
$remind_deadline = date ( " Y-m-d H:i:s " , strtotime ( $now_time ) + $vote_close_second );
// 需要提醒的投票范围列表
$dish_vote_date_info = $db -> get_all ( " select a.*,b.name meal_type_name from tb_dish_vote_date a
left join tb_meal_type b on a . dc_type = b . id
where a . status = '1' and a . deadline >= '{$now_time}' and a . deadline <= '{$remind_deadline}' " );
if ( empty ( $dish_vote_date_info )){
echo " 没有需要推送的内容 " ;
exit ;
}
// 获取交通局的用户列表 (没被提醒过的人,或可以被提醒的人)(记录时间 < 当前时间-提前通知时间 = 允许再次提醒)
$ts_time = date ( " Y-m-d H:i:s " , strtotime ( " - { $vote_close_second } seconds " ));
$jtj_user_list = $db -> get_all ( " select a.* from tb_user a
left join tb_jtj_ts_log b on a . id = b . user_id
where a . enabled = '1' { $where }
" );
$jtj_user = array ();
foreach ( $jtj_user_list as $k => $v ){
if ( $v [ 'add_time' ] < $ts_time ){
// 允许再次记录的用户
$jtj_user [] = $v [ 'id' ];
}
}
if ( empty ( $jtj_user )){
echo " 没有需要推送的用户 " ;
exit ;
}
$ts_msg = array ();
// 循环每个投票 周期
foreach ( $dish_vote_date_info as $k => $v ){
// if($v['id'] == 34){
// echo "-------------";
// }
// 获取规则
$dish_vote_foot_type = $db -> get_all ( " select * from tb_dish_vote_foot_type where dining_hall_id=' { $v [ 'dining_hall_id' ] } ' and meal_type_id=' { $v [ 'dc_type' ] } ' " );
if ( empty ( $dish_vote_foot_type )){
continue ;
}
// 获取这个周期 有几天 (查找每天是否都投票满)
$days = round ( strtotime ( $v [ 'end_day' ]) - strtotime ( $v [ 'start_day' ]) / 3600 / 24 );
for ( $i = strtotime ( $v [ 'start_day' ]); $i <= strtotime ( $v [ 'end_day' ]); $i += 86400 ){
// 获取已投票,用户投票的内容
$get_dish_type_num = $db -> get_all ( " select a.*,
a . user_id , GROUP_CONCAT ( b . dish_type ) dish_type_id
from tb_dish_vote_user a
, tb_dish b
where
a . vote_date_id = '{$v[' id ']}' and
a . dish_id = b . id and
a . dining_hall_id = '{$v[' dining_hall_id ']}' and
a . status = '2' and
a . dc_type = '{$v[' dc_type ']}' and
a . date = '".date(' Y - m - d ',$i)."' and
a . user_id in ( " .implode( " , " , $jtj_user ). " )
group by a . user_id
" );
// 获取 用户投票后的 菜品类型数组
$user_dish_id_arr = array ();
$user_dish_id_arr2 = array ();
foreach ( $get_dish_type_num as $k2 => $v2 ){
$user_dish_id_arr [ $v2 [ 'user_id' ]] = $v2 [ 'dish_type_id' ];
$user_dish_id_arr2 [] = $v2 [ 'user_id' ];
}
// 判断这天有没有没参与投票的人,直接推送通知
if ( count ( $user_dish_id_arr2 ) != count ( $jtj_user )){
$diff = array_diff ( $jtj_user , $user_dish_id_arr2 );
foreach ( $diff as $diff_key => $diff_val ){
$user_dish_id_arr [ $diff_val ] = '' ;
}
}
// 提炼每个规则
foreach ( $dish_vote_foot_type as $foot_type_key => $foot_type_val ){
// 需要投票的票数
$max_vote = $foot_type_val [ 'max_vote' ];
// 规则 对应的菜品类型
$dish_type_ids = substr ( $foot_type_val [ 'dish_type_id' ], 1 , strlen ( $foot_type_val [ 'dish_type_id' ]) - 2 );
$dish_type_ids = explode ( " , " , $dish_type_ids );
// 循环多个规则
foreach ( $user_dish_id_arr as $user_id_key => $user_dish_val ){
$vote_num = 0 ;
// 循环单个用户 投票的菜品,匹配规则
if ( ! empty ( $user_dish_val )){
$user_dish_type_arr = explode ( " , " , $user_dish_val );
foreach ( $user_dish_type_arr as $vote_dish_type_key => $vote_dish_type_val ){
if ( in_array ( $vote_dish_type_val , $dish_type_ids )){
$vote_num += 1 ;
}
}
}
if ( $vote_num < $max_vote ){
// 投票少于规则,需要推送提醒
// 日期_周期ID_用户ID
$ts_msg [ $user_id_key ][ $v [ 'id' ]][] = " 【 { $v [ 'start_day' ] } ~ { $v [ 'end_day' ] } 】( { $v [ 'meal_type_name' ] } : { $foot_type_val [ 'name' ] } )投票准备截止,请尽快投票! " ;
// $ts_msg[date('Y-m-d',$i).'_'.$v['id'].'_'.$user_id_key][] = "【{$v['start_day']} ~ {$v['end_day']}】({$v['meal_type_name']}: {$foot_type_val['name']})投票准备截止,请尽快投票!";
// $ts_msg[date('Y-m-d',$i).'_'.$foot_type_val['name'].'_'.$v['id']][$user_id_key][] = "【{$v['start_day']} ~ {$v['end_day']}】({$v['meal_type_name']})投票准备截止,请尽快投票!{$foot_type_val['name']}-".date('Y-m-d',$i)."-{$vote_num}-{$max_vote}-{$user_id_key}";
// pushSms();
}
}
}
}
}
foreach ( $ts_msg as $k => $v ){
// 判断推送记录
$vote_date_ids = array_keys ( $v );
$check_vote_ts = $db -> get_all ( " select * from tb_jtj_ts_log where type=' { $log_type } ' and user_id=' { $k } ' and vote_date_id in ( " . implode ( ',' , $vote_date_ids ) . " ) " );
if ( $check_vote_ts ){
// 这些周期都推送过
echo " 已有推送记录,不重复推送 " ;
exit ;
}
$user_info = $db -> get_one ( " select * from tb_user where enabled='1' and id=' { $k } ' " );
if ( empty ( $check_reminder )){
// 发短信提醒 用户订餐
$cellphone = $user_info [ 'cellphone' ];
$title = " 投票通知 " ;
$content = " 您还有没投票的菜品,请及时投票 " ;
// 暂时停止推送
// $send_res = sendTs($cellphone, $title, $content);
// sendSms($cellphone, $content);
// 暂时屏蔽推送消息
$send_res = 0 ;
if ( $send_res ){
foreach ( $vote_date_ids as $vote_date_ids_key => $vote_date_ids_val ){
$data [ 'cellphone' ] = $user_info [ 'cellphone' ];
$data [ 'title' ] = $title ;
$data [ 'content' ] = $content ;
$data [ 'type' ] = $log_type ;
$data [ 'user_id' ] = $k ;
$data [ 'vote_date_id' ] = $vote_date_ids_val ;
$data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
// 推送log表记录
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
}
echo " “ { $user_info [ 'cellphone' ] } ” 用户推送消息成功。 " ;
}
} else {
echo " “ { $user_info [ 'cellphone' ] } ” 用户已推送过消息。 " ;
exit ;
}
}
}
// 订餐截止推送
if ( $act == 'dc_close_ts' ){
$log_type = " dc_close " ;
// $company_id = check_form($_GET['company_id']);
// $dining_hall_id = check_form($_GET['dining_hall_id']);
if ( empty ( $dining_hall_id )){
echo " 参数不能为空 " ;
exit ;
}
$where = " " ;
if ( $company_id ){
$where .= " and company_id=' { $company_id } ' " ;
}
if ( $dining_hall_id ){
$where .= " and dining_hall_id=' { $dining_hall_id } ' " ;
}
$config = $db -> get_one ( " select * from tb_jtj_excitation_config where name='MEAL_BEFORE_DEADLINE_HOUR' " );
$second = $config [ 'value' ] * 60 * 60 ;
// 获取当前饭堂所有用户
$user_list = $db -> get_one ( " select *,group_concat(id) ids from tb_user where enabled='1' { $where } group by dining_hall_id " );
$now_day = date ( 'Y-m-d' );
$s_day = date ( 'Y-m-d' , strtotime ( '-2 day' ));
// 获取今天订餐的dc_type
$dc_type_arr = $db -> get_all ( " select * from tb_date where dc_date>=' { $s_day } ' and dining_hall_id=' { $dining_hall_id } ' " );
foreach ( $dc_type_arr as $k => $v ){
$dc_date = $v [ 'dc_date' ];
$order_rule_info = $db -> get_one ( " select a.name dc_type_name,b.* from tb_meal_type a,tb_order_rule b where a.id = b.for_dc_type and b.rule_type = 1 and for_action = 1 AND a.id=' { $v [ 'dc_type' ] } ' and b.status='1' and b.dining_hall_id=' { $dining_hall_id } ' " );
$reminder_time = strtotime ( $v [ 'dc_date' ] . " " . $order_rule_info [ 'time' ]) - ( $order_rule_info [ 'relative_day' ] * 86400 ) - $second ;
$reminder_end_time = strtotime ( $v [ 'dc_date' ] . " " . $order_rule_info [ 'time' ]) - ( $order_rule_info [ 'relative_day' ] * 86400 );
if ( strtotime ( " now " ) >= $reminder_time && strtotime ( " now " ) <= $reminder_end_time ){
// 提醒为订餐用户
$dc_user = $db -> get_one ( " select *,group_concat(uid) uids from tb_order where date_id=' { $v [ 'id' ] } ' and type='1' and is_delete='0' and uid in ( { $user_list [ 'ids' ] } ) group by date_id " );
$reminder_user_ids = array_diff ( explode ( " , " , $user_list [ 'ids' ]), explode ( " , " , $dc_user [ 'uid' ]));
// 这些人还没订餐
if ( ! empty ( $reminder_user_ids )){
foreach ( $reminder_user_ids as $ids_key => $ids_val ){
// 判断是否通知过
// $check_reminder = $db->get_one("select * from tb_jtj_ts_log where type='{$log_type}' and user_id='{$ids_val}' and date_id='{$v['id']}'");
$check_reminder = $db -> get_one ( " select * from tb_jtj_ts_log where type=' { $log_type } ' and user_id=' { $ids_val } ' and date=' { $dc_date } ' " );
$user_info = $db -> get_one ( " select * from tb_user where enabled='1' and id=' { $ids_val } ' " );
if ( empty ( $check_reminder )){
// 发短信提醒 用户订餐
$cellphone = $user_info [ 'cellphone' ];
$title = " 订餐通知 " ;
// $content = "您还没预定 ".$v['dc_date']." 的{$order_rule_info['dc_type_name']},订餐即将结束,过时系统默认订餐,请及时订餐,如已订餐,请忽略此信息,谢谢。";
$content = " { $dc_date } 订餐即将结束,请及时订餐,过时系统将自动下单,如已订餐,请忽略此信息,谢谢。 " ;
// 暂时停止推送
// $send_res = sendTs($cellphone, $title, $content);
// sendSms($cellphone, $content);
$send_res = 0 ;
if ( $send_res ){
$data [ 'cellphone' ] = $user_info [ 'cellphone' ];
$data [ 'title' ] = $title ;
$data [ 'content' ] = $content ;
$data [ 'type' ] = $log_type ;
$data [ 'user_id' ] = $ids_val ;
$data [ 'date_id' ] = $v [ 'id' ];
$data [ 'date' ] = $dc_date ;
$data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
// 推送log表记录
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
echo " “ { $user_info [ 'cellphone' ] } ” 用户推送消息成功。 " ;
}
} else {
echo " “ { $user_info [ 'cellphone' ] } ” 用户已推送过消息。 " ;
continue ;
}
}
}
} else {
echo " 还没到通知时间。 " ;
}
}
}
// 余额不足推送
if ( $act == 'insufficient_balance_ts' ){
$log_type = " insufficient_balance " ;
// $company_id = check_form($_GET['company_id']);
// $dining_hall_id = check_form($_GET['dining_hall_id']);
$config = $db -> get_one ( " select * from tb_jtj_excitation_config where name='INSUFFICIENT_BALANCE' " );
$price = $config [ 'value' ];
$where = " " ;
if ( $company_id ){
$where .= " and a.company_id=' { $company_id } ' " ;
}
if ( $dining_hall_id ){
$where .= " and a.dining_hall_id=' { $dining_hall_id } ' " ;
}
// 获取钱包剩余金额
// $je_list = $db->get_all("select a.user_id, b.cellphone, b.username, sum(a.account) account_total
// from tb_account a
// left join tb_user b on a.user_id=b.id
// where a.type_id in (1,2) and b.enabled='1' {$where}
// GROUP BY a.user_id");
// $je_list = $db->get_all("select a.id, a.cellphone, a.username,sum(a.account)
// from (select a.id, a.cellphone, a.username,b.account,b.type_id from tb_user a left join tb_account b on a.id=b.user_id) a
// where a.type_id in (1,2) or a.type_id is null
// GROUP BY a.id");
$je_list = $db -> get_all ( " select
a . id , a . cellphone , a . username , sum ( b . account ) account_total
from tb_user a left join tb_account b on a . id = b . user_id
where a . enabled = '1' and ( b . type_id in ( 1 , 2 ) or b . type_id is null ) { $where }
GROUP BY a . id " );
if ( ! empty ( $je_list )){
foreach ( $je_list as $k => $v ){
if ( $v [ 'account_total' ] < $price ){
// 推送规则
$check_ye = $db -> get_one ( " select *
from tb_jtj_ts_log
where type = '{$log_type}' and user_id = '{$v[' id ']}' and CURDATE () <= date ( add_time )
order by id desc " );
if ( $check_ye ){
// 今天内不重复推送
echo " { $v [ 'cellphone' ] } 号码余额不足 { $price } 元 已推送过。 \n " ;
continue ;
}
// 提示余额不足推送
// 推送log表记录
$cellphone = $v [ 'cellphone' ];
$title = " 余额不足 " ;
$content = " 您的余额已不足 { $price } 元,请及时充值。 " ;
// $check_username = $db->get_all("select * from tb_user where username in ('向熊林','霍瑞兴','张桂香','王建美','蒙秀荣','杜春梅','陆伟玲','谭婷芳','郑新浓','马绿花','莫火燕','覃金兰','张淼开','甘文盛','谢卫连','黎伟玲','黄玉娇','李亚炳','罗庆坚')");
// $check_tel = array_column($check_username, 'cellphone');
// if(in_array($cellphone, $check_tel)){
$send_res = sendTs ( $cellphone , $title , $content );
// $sms_res = sendSms($cellphone, $content);
// }
// $send_res = sendTs($cellphone, $title, $content);
// $sms_res = sendSms($cellphone, $content);
if ( $send_res ){
$data [ 'cellphone' ] = $cellphone ;
$data [ 'title' ] = $title ;
$data [ 'content' ] = $content ;
$data [ 'type' ] = $log_type ;
$data [ 'user_id' ] = $v [ 'id' ];
$data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
}
}
}
} else {
echo " 找不到对应用户 " ;
exit ;
}
}
// 截止时间后,自动生成进行订餐(星期日订下周的餐,订菜谱所有的菜品)
if ( $act == 'default_dish' ){
$fp = fopen ( dirname ( __FILE__ ) . " /time_task_lock.txt " , " w+ " );
if ( ! flock ( $fp , LOCK_EX | LOCK_NB )) {
fclose ( $fp );
exit ;
}
header ( " Content-type:text/html;charset=utf-8 " );
// 测试数据
$dc_types = check_form ( $argv [ 2 ]); // dc_type
$company_id = check_form ( $argv [ 3 ]); // 公司ID
$cls_ids = check_form ( $argv [ 4 ]); // 荤素汤 1,2,3
// 自动订餐提前几天
$relative_day_tq = 1 ;
if ( empty ( $dc_types )){
$dc_types = trim ( $_GET [ 'dc_types' ]);
}
if ( empty ( $company_id )){
$company_id = trim ( $_GET [ 'company_id' ]);
}
if ( empty ( $cls_ids )){
$cls_ids = trim ( $_GET [ 'cls_ids' ]);
}
if ( empty ( $cls_ids )){
$cls_ids = '4,5,6' ; // 荤素汤 1,2,3
}
$cls_ids_arr = explode ( ',' , $cls_ids );
// $dc_types = "27,33,25"; // 早,午,晚
// if(empty($dc_types) || empty($company_id)){
// exit;
// }
// 记录异常问题提示
$err_msg = array ();
$suc_msg = array ();
// 查找几天内
$other_day = strtotime ( " -5 day " ); // 当前时间往前5天
$to_day = date ( 'Y-m-d' );
$time = date ( 'H:i:s' );
$relative_datetime = date ( 'Y-m-d H:i:s' );
$current_date = date ( 'Y-m-d H:i:s' );
// 判断明天是否有可定餐
$dc_date_list = $db -> get_all ( "
select
a . id date_id ,
a . dc_date ,
a . update_time ,
b . name dc_type_name ,
b . id dc_type ,
b . start_time ,
c .*
from tb_date a
left join tb_meal_type b on a . dc_type = b . id
left join tb_order_rule c on ( a . dc_type = c . for_dc_type and c . `status` = 1 and c . dining_hall_id = a . dining_hall_id and c . rule_type = 1 and c . for_action = 1 )
where
a . dc_date >= '{$to_day}' and
b . company_id = '{$company_id}' and
b . mealtype = '1' and
a . dc_type in ({ $dc_types })
" );
if ( empty ( $dc_date_list )){
$err_msg [] = " 没有安排可订餐时间 " ;
echo " 没有安排可订餐时间 " ;
exit ;
}
// 获取所有可以自动进行订餐的 用户
$user_list = $db -> get_all ( " select * from tb_user where enabled in (1,-1) and company_id=' { $company_id } ' and default_dc_type!='' " );
// 进行订餐
foreach ( $dc_date_list as $k => $v ){
// 判断当前菜谱的更新时间,更新时间+1小时才能执行自动订餐
if ( ! empty ( $v [ 'update_time' ])){
$update_time = strtotime ( $v [ 'update_time' ]) + 3600 ;
if ( $update_time > time ()){
echo " 创建菜谱后1小时才能自动订餐 " ;
continue ;
}
}
$log_type = 'default_dish' ;
$dc_date = $v [ 'dc_date' ];
$date_id = $v [ 'date_id' ];
$dc_type_name = $v [ 'dc_type_name' ];
$dc_type = $v [ 'for_dc_type' ];
// 1预定, 2退订
$for_action = $v [ 'for_action' ];
// 提前多少天
if ( $relative_day_tq ){
$relative_day = $relative_day_tq ;
} else {
$relative_day = $v [ 'relative_day' ];
}
// 截止时间
$deadline = $v [ 'time' ];
// 获取下一周的日期段
$next_week_arr = get_next_week_day ( 1 );
$next_week_start_day = $next_week_arr [ 'start_day' ];
// 临时改周日也可以订餐
// $z_ri = strtotime($next_week_arr['start_day']) - (60*60*24);
// $next_week_start_day = date('Y-m-d', $z_ri);
// // 前提2天, 星期6自动订
// $relative_day = 2;
$next_week_start_end = $next_week_arr [ 'end_day' ];
// 订餐周期的起始日(的$relative_day, 表示提前多少天开始订餐)
$current_date = strtotime ( $next_week_start_day ) - ( 60 * 60 * 24 * $relative_day );
$current_date = date ( 'Y-m-d' , $current_date ) . " 09:00:00 " ;
// 非下周的餐次,都跳过
if ( $dc_date < $next_week_start_day || $dc_date > $next_week_start_end ){
continue ;
}
// 截止具体时间
$deadline_time = strtotime ( $v [ 'dc_date' ]) - ( 60 * 60 * 24 * $relative_day );
$deadline_time = date ( 'Y-m-d' , $deadline_time ) . " { $deadline } " ;
// 判断时间段 用餐开始时间
$meal_time = $dc_date . ' ' . $v [ 'start_time' ];
if ( strtotime ( $current_date ) > time ()){
echo " 还没到周期开始日前 { $relative_day } 天的09:00:00自动订餐时间 { $dc_date } { $dc_type_name } <br> " ;
continue ;
}
// 今天的餐都不订
if ( $dc_date <= date ( 'Y-m-d' )){
echo " 今天的餐都不订 { $dc_date } { $dc_type_name } <br> " ;
continue ;
}
// 每天下午09:00 对未订餐开始订餐
if ( date ( " H:i " ) <= " 09:00 " ){
echo " 还没到09:00订餐时间 { $dc_date } { $dc_type_name } <br> " ;
continue ;
}
// 循环用户
foreach ( $user_list as $user_k => $user_v ){
$cellphone = $user_v [ 'cellphone' ];
// 用户设置自定义 自动订餐
if ( empty ( $user_v [ 'default_dc_type' ])){
// 用户没有设置自动订餐
echo " 【用户: { $user_v [ 'username' ] } 】用户没有设置自动订餐<br> " ;
continue ;
}
$default_dc_type = explode ( ',' , $user_v [ 'default_dc_type' ]);
if ( ! in_array ( $v [ 'dc_type' ], $default_dc_type )){
// 当前dc_type 用户没有设置自动订餐
echo " 当前dc_type=' { $v [ 'dc_type' ] } ' 用户没有设置自动订餐<br> " ;
continue ;
}
$dish = array ();
$user_id = $user_v [ 'id' ];
$dining_hall_id = $user_v [ 'dining_hall_id' ];
$user_type_id = $user_v [ 'type_id' ];
// 判断该用户是否订餐(已订过餐 或 已取消过)
$check_dish = $db -> get_one ( " select * from tb_order where uid=' { $user_id } ' and type='1' and state_id in (3,4,6) and date_id=' { $date_id } ' and is_delete=0 " );
if ( $check_dish ){
continue ;
}
// if($dc_type == '229'){
// 当菜品类型 是 早餐, 固定从tb_date 找3个dish_type的一个菜品 生成 $dish
$dish = array ();
$get_dc_date_info = $db -> get_all ( " select * from tb_date a ,tb_date_dish b where a.id=b.date_id and a.dc_date=' { $dc_date } ' and a.dc_type=' { $dc_type } ' and a.dining_hall_id=' { $dining_hall_id } ' " );
$dish_list_id = array_column ( $get_dc_date_info , 'dish_id' );
$dish_list_id_str = join ( ',' , $dish_list_id );
$dish = $db -> get_all ( " select * from tb_dish where id in ( { $dish_list_id_str } ) and enabled='1' " );
if ( count ( $dish ) <= 0 ){
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_date } { $dc_type_name } 菜品类型不足, 必须大于等于1个菜品 " ;
echo $log_msg ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
continue ;
}
// }
// 下单
$total_price = 0 ;
// 计算金额
foreach ( $dish as $dk => $dv ){
$total_price += $dv [ 'dish_price' ];
}
//考虑tb_meal_type_guding_price固定金额
$row = $db -> get_one ( " select * from tb_meal_type_guding_price where company_id = ' " . $company_id . " ' and meal_type_id = ' " . $dc_type . " ' and (user_type_id=0 or user_type_id= " . $user_type_id . " ) and (dining_hall_id=0 or dining_hall_id = " . $dining_hall_id . " ) order by idx " );
if ( $row ) {
$total_price = $row [ 'price' ];
}
// 判断该用户余额是否充足
$user_balance = $db -> get_one ( " select sum(account) account from tb_account where user_id=' { $user_id } ' group by user_id " );
if ( $user_balance [ 'account' ] < $total_price ){
// 判断是否今天已经记录过
$check_yu_e = $db -> get_one ( " select * from tb_jtj_ts_log where DAY(add_time) = DAY(NOW()) and type='default_dish' and user_id=' { $user_v [ 'id' ] } ' and content like '%余额不足%' " );
if ( $check_yu_e ){
echo " 【用户: { $user_v [ 'username' ] } 】 余额不足,自动订餐失败,今天已记录过,跳过。<br> " ;
continue ;
}
$err_msg [] = $log_msg = " 用户: { $user_v [ 'username' ] } 余额不足,自动订餐失败。 " ;
echo " 【用户: { $user_v [ 'username' ] } 】 余额不足,自动订餐失败。<br> " ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
continue ;
}
$total_num = count ( $dish );
$parent_code = md5 ( uniqid ( rand (), true ));
// 生成code
do {
$code = date ( 'Ymd' ) . $dc_type . substr ( preg_replace ( " /[a-z]/ " , " " , md5 ( rand () . " _1 " . microtime ()) . " 0000 " ), 0 , 4 );
$t_order = $db -> get_one ( " select a.* from tb_order a where code = ' { $code } ' " );
} while ( $t_order );
// 生成take_food_code
do {
$take_food_code = substr ( preg_replace ( " /[a-z]/ " , " " , md5 ( rand () . " _ " . microtime ()) . " 00000000 " ), 0 , 8 );
$t_order = $db -> get_one ( " select a.* from tb_order a where take_food_code=' { $take_food_code } ' " );
} while ( $t_order );
$qrcode_url = " /show/get.php?act=getQRCode&takecode= " . $take_food_code ;
$total = array ( " total_price " => $total_price , " total_num " => $total_num );
if ( $total_num <= 0 ){
// if($total_num < $default_dish_num){
$log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_date } { $dc_type_name } 没有可订的菜品,自定订餐失败。 " ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
echo $log_msg ;
continue ;
}
$order = array (
'code' => $code ,
" parent_code " => $parent_code ,
" state_id " => 6 ,
" uid " => $user_id ,
" date_id " => $date_id ,
" type " => 1 ,
" create_time " => date ( 'Y-m-d H:i:s' ),
" book_num " => 1 ,
" take_num " => 0 ,
" reason " => " 自动订餐,订餐消费支付 { $total [ 'total_price' ] } 元 " ,
" take_food_code " => $take_food_code ,
'total_num' => $total [ 'total_num' ],
'total_price' => $total [ 'total_price' ],
" qrcode_url " => $qrcode_url ,
" qrcode_path " => '' ,
" book_fee " => 0
);
// 延迟0.01-0.6秒
$suijishu = mt_rand ( 1 , 60 );
usleep ( $suijishu * 10000 );
// 在判断是否已经有订单
$check_dish = $db -> get_one ( " select * from tb_order where uid=' { $user_id } ' and type='1' and state_id in (3,4,6) and date_id=' { $date_id } ' and is_delete=0 " );
if ( $check_dish ){
continue ;
}
$db -> insert ( " tb_order " , $order );
$oid = $db -> insert_id ();
if ( $oid > 0 ){
$rList = array ( array ( 'ac' => $total [ 'total_price' ], 'rtype' => 2 , 'ftid' => $dining_hall_id , 'oid' => $oid ));
foreach ( $dish as $dk => $dv ){
$order_detail = array ( " order_id " => $oid , " dish_id " => $dv [ 'id' ], " dish_amount " => 1 , " dish_price " => $dv [ 'dish_price' ]);
// $this->Dishes_model->add_date_dish_book_num(1, $date_id, $dv['dish_id']);
$db -> update ( " tb_date_dish " , array ( 'book_num' => 'book_num+1' ), " date_id=' { $date_id } ' and dish_id=' { $dv [ 'id' ] } ' " );
$db -> insert ( " tb_order_detail " , $order_detail );
$odid = $db -> insert_id ();
}
$date = strtotime ( $dc_date );
$month = date ( 'n' , $date );
$day = date ( 'j' , $date );
$date_text = $month . '月' . $day . '日' . '(周' . cnWeek ( $date ) . ')' . $dc_type_name ;
$result [] = array ( 'order_id' => $oid , 'meal_text' => $date_text , 'take_food_code' => $order [ 'take_food_code' ], 'qr_code' => $qrcode_url , " total_price " => $total [ 'total_price' ], " total_num " => $total [ 'total_num' ]);
}
// 结账
if ( $rList ) { //扣费
$r = gpay_pay_user_account ( $user_id , $rList , $updateInfo );
if ( $r != 'suc' ) {
// 支付失败,删除记录
foreach ( $dish as $dk => $dv ){
$db -> update ( " tb_date_dish " , array ( 'book_num' => 'book_num-1' ), " date_id=' { $date_id } ' and dish_id=' { $dv [ 'id' ] } ' " );
}
$db -> delete ( " tb_order_detail " , " order_id=' { $oid } ' " );
$db -> delete ( " tb_order " , " id=' { $oid } ' " );
if ( $r == 'yebz' ) {
// echo "【用户:{$user_v['username']}】余额不足,下单失败,请先充值";
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】余额不足,下单失败,请先充值。 " ;
} else {
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】下单失败,请重试! " ;
// echo "【用户:{$user_v['username']}】下单失败,请重试!";
}
} else {
$suc_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_type_name } 订餐成功, 订单ID: { $oid } <br> " ;
// 发推送和短信
// $check_username = $db->get_all("select * from tb_user where username in ('向熊林','霍瑞兴','张桂香','王建美','蒙秀荣','杜春梅','陆伟玲','谭婷芳','郑新浓','马绿花','莫火燕','覃金兰','张淼开','甘文盛','谢卫连','黎伟玲','黄玉娇','李亚炳','罗庆坚')");
// $check_tel = array_column($check_username, 'cellphone');
// if(in_array($cellphone, $check_tel)){
$content = " 已成功为您订餐: { $dc_date } { $dc_type_name } , 您可进入“云中美食app-我的订单”查看。" ;
// 暂时停止推送
// $send_res = sendTs($cellphone, $title, $content);
// $sms_res = sendSms($cellphone, $content);
// }
}
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
echo $log_msg ;
}
}
}
}
// 今天订明天的餐
if ( $act == 'default_dish3' ){
$fp = fopen ( dirname ( __FILE__ ) . " /time_task_lock.txt " , " w+ " );
if ( ! flock ( $fp , LOCK_EX | LOCK_NB )) {
fclose ( $fp );
exit ;
}
header ( " Content-type:text/html;charset=utf-8 " );
// 测试数据
$dc_types = check_form ( $argv [ 2 ]); // dc_type
$company_id = check_form ( $argv [ 3 ]); // 公司ID
$cls_ids = check_form ( $argv [ 4 ]); // 荤素汤 1,2,3
// 自动订餐提前几天
$relative_day_tq = 1 ;
if ( empty ( $dc_types )){
$dc_types = trim ( $_GET [ 'dc_types' ]);
}
if ( empty ( $company_id )){
$company_id = trim ( $_GET [ 'company_id' ]);
}
if ( empty ( $cls_ids )){
$cls_ids = trim ( $_GET [ 'cls_ids' ]);
}
if ( empty ( $cls_ids )){
$cls_ids = '4,5,6' ; // 荤素汤 1,2,3
}
$cls_ids_arr = explode ( ',' , $cls_ids );
// $dc_types = "27,33,25"; // 早,午,晚
// if(empty($dc_types) || empty($company_id)){
// exit;
// }
// 记录异常问题提示
$err_msg = array ();
$suc_msg = array ();
$to_day = date ( 'Y-m-d' );
$time = date ( 'H:i:s' );
$current_date = date ( 'Y-m-d H:i:s' );
// 明天
$tomorrow_date = date ( 'Y-m-d' , strtotime ( " +1 days " ));
// 判断明天是否有可定餐
$dc_date_list = $db -> get_all ( "
select
a . id date_id ,
a . dc_date ,
b . name dc_type_name ,
b . id dc_type ,
b . start_time ,
c .*
from tb_date a
left join tb_meal_type b on a . dc_type = b . id
left join tb_order_rule c on ( a . dc_type = c . for_dc_type and c . `status` = 1 and c . dining_hall_id = a . dining_hall_id and c . rule_type = 1 and c . for_action = 1 )
where
a . dc_date = '{$tomorrow_date}' and
b . company_id = '{$company_id}' and
b . mealtype = '1' and
a . dc_type in ({ $dc_types })
" );
if ( empty ( $dc_date_list )){
$err_msg [] = " 没有安排可订餐时间 " ;
echo " 没有安排可订餐时间 " ;
exit ;
}
// 获取所有可以自动进行订餐的 用户
$user_list = $db -> get_all ( " select * from tb_user where enabled in (1,-1) and company_id=' { $company_id } ' and default_dc_type!='' " );
// 进行订餐
foreach ( $dc_date_list as $k => $v ){
$log_type = 'default_dish' ;
$dc_date = $v [ 'dc_date' ];
$date_id = $v [ 'date_id' ];
$dc_type_name = $v [ 'dc_type_name' ];
$dc_type = $v [ 'for_dc_type' ];
// 1预定, 2退订
$for_action = $v [ 'for_action' ];
// 提前多少天
if ( $relative_day_tq ){
$relative_day = $relative_day_tq ;
} else {
$relative_day = $v [ 'relative_day' ];
}
// 截止时间
$deadline = $v [ 'time' ];
//非明天的餐 都不订
if ( $dc_date != $tomorrow_date ){
echo " 非明天的餐,都不订 " ;
continue ;
}
// 判断时间段 用餐开始时间
$meal_time = $dc_date . ' ' . $v [ 'start_time' ];
// 每天下午09:00 对未订餐开始订餐
if ( date ( " H:i " ) <= " 09:00 " ){
echo " 还没到09:00订餐时间 { $dc_date } { $dc_type_name } <br> " ;
continue ;
}
// 循环用户
foreach ( $user_list as $user_k => $user_v ){
$cellphone = $user_v [ 'cellphone' ];
// 用户设置自定义 自动订餐
if ( empty ( $user_v [ 'default_dc_type' ])){
// 用户没有设置自动订餐
echo " 【用户: { $user_v [ 'username' ] } 】用户没有设置自动订餐<br> " ;
continue ;
}
$default_dc_type = explode ( ',' , $user_v [ 'default_dc_type' ]);
if ( ! in_array ( $v [ 'dc_type' ], $default_dc_type )){
// 当前dc_type 用户没有设置自动订餐
echo " 当前dc_type=' { $v [ 'dc_type' ] } ' 用户没有设置自动订餐<br> " ;
continue ;
}
$dish = array ();
$user_id = $user_v [ 'id' ];
$dining_hall_id = $user_v [ 'dining_hall_id' ];
$user_type_id = $user_v [ 'type_id' ];
// 判断该用户是否订餐(已订过餐 或 已取消过)
$check_dish = $db -> get_one ( " select * from tb_order where uid=' { $user_id } ' and type='1' and state_id in (3,4,6) and date_id=' { $date_id } ' and is_delete=0 " );
if ( $check_dish ){
continue ;
}
// if($dc_type == '229'){
// 当菜品类型 是 早餐, 固定从tb_date 找3个dish_type的一个菜品 生成 $dish
$dish = array ();
$get_dc_date_info = $db -> get_all ( " select * from tb_date a ,tb_date_dish b where a.id=b.date_id and a.dc_date=' { $dc_date } ' and a.dc_type=' { $dc_type } ' and a.dining_hall_id=' { $dining_hall_id } ' " );
$dish_list_id = array_column ( $get_dc_date_info , 'dish_id' );
$dish_list_id_str = join ( ',' , $dish_list_id );
$dish = $db -> get_all ( " select * from tb_dish where id in ( { $dish_list_id_str } ) and enabled='1' " );
if ( count ( $dish ) <= 0 ){
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_date } { $dc_type_name } 菜品类型不足, 必须大于等于1个菜品 " ;
echo $log_msg ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
continue ;
}
// }
// 下单
$total_price = 0 ;
// 计算金额
foreach ( $dish as $dk => $dv ){
$total_price += $dv [ 'dish_price' ];
}
//考虑tb_meal_type_guding_price固定金额
$row = $db -> get_one ( " select * from tb_meal_type_guding_price where company_id = ' " . $company_id . " ' and meal_type_id = ' " . $dc_type . " ' and (user_type_id=0 or user_type_id= " . $user_type_id . " ) and (dining_hall_id=0 or dining_hall_id = " . $dining_hall_id . " ) order by idx " );
if ( $row ) {
$total_price = $row [ 'price' ];
}
// 判断该用户余额是否充足
$user_balance = $db -> get_one ( " select sum(account) account from tb_account where user_id=' { $user_id } ' group by user_id " );
if ( $user_balance [ 'account' ] < $total_price ){
// 判断是否今天已经记录过
$check_yu_e = $db -> get_one ( " select * from tb_jtj_ts_log where DAY(add_time) = DAY(NOW()) and type='default_dish' and user_id=' { $user_v [ 'id' ] } ' and content like '%余额不足%' " );
if ( $check_yu_e ){
echo " 【用户: { $user_v [ 'username' ] } 】 余额不足,自动订餐失败,今天已记录过,跳过。<br> " ;
continue ;
}
$err_msg [] = $log_msg = " 用户: { $user_v [ 'username' ] } 余额不足,自动订餐失败。 " ;
echo " 【用户: { $user_v [ 'username' ] } 】 余额不足,自动订餐失败。<br> " ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
continue ;
}
$total_num = count ( $dish );
$parent_code = md5 ( uniqid ( rand (), true ));
// 生成code
do {
$code = date ( 'Ymd' ) . $dc_type . substr ( preg_replace ( " /[a-z]/ " , " " , md5 ( rand () . " _1 " . microtime ()) . " 0000 " ), 0 , 4 );
$t_order = $db -> get_one ( " select a.* from tb_order a where code = ' { $code } ' " );
} while ( $t_order );
// 生成take_food_code
do {
$take_food_code = substr ( preg_replace ( " /[a-z]/ " , " " , md5 ( rand () . " _ " . microtime ()) . " 00000000 " ), 0 , 8 );
$t_order = $db -> get_one ( " select a.* from tb_order a where take_food_code=' { $take_food_code } ' " );
} while ( $t_order );
$qrcode_url = " /show/get.php?act=getQRCode&takecode= " . $take_food_code ;
$total = array ( " total_price " => $total_price , " total_num " => $total_num );
if ( $total_num <= 0 ){
// if($total_num < $default_dish_num){
$log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_date } { $dc_type_name } 没有可订的菜品,自定订餐失败。 " ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
echo $log_msg ;
continue ;
}
$order = array (
'code' => $code ,
" parent_code " => $parent_code ,
" state_id " => 6 ,
" uid " => $user_id ,
" date_id " => $date_id ,
" type " => 1 ,
" create_time " => date ( 'Y-m-d H:i:s' ),
" book_num " => 1 ,
" take_num " => 0 ,
" reason " => " 自动订餐,订餐消费支付 { $total [ 'total_price' ] } 元 " ,
" take_food_code " => $take_food_code ,
'total_num' => $total [ 'total_num' ],
'total_price' => $total [ 'total_price' ],
" qrcode_url " => $qrcode_url ,
" qrcode_path " => '' ,
" book_fee " => 0
);
// 延迟0.01-0.6秒
$suijishu = mt_rand ( 1 , 60 );
usleep ( $suijishu * 10000 );
// 在判断是否已经有订单
$check_dish = $db -> get_one ( " select * from tb_order where uid=' { $user_id } ' and type='1' and state_id in (3,4,6) and date_id=' { $date_id } ' and is_delete=0 " );
if ( $check_dish ){
continue ;
}
$db -> insert ( " tb_order " , $order );
$oid = $db -> insert_id ();
if ( $oid > 0 ){
$rList = array ( array ( 'ac' => $total [ 'total_price' ], 'rtype' => 2 , 'ftid' => $dining_hall_id , 'oid' => $oid ));
foreach ( $dish as $dk => $dv ){
$order_detail = array ( " order_id " => $oid , " dish_id " => $dv [ 'id' ], " dish_amount " => 1 , " dish_price " => $dv [ 'dish_price' ]);
// $this->Dishes_model->add_date_dish_book_num(1, $date_id, $dv['dish_id']);
$db -> update ( " tb_date_dish " , array ( 'book_num' => 'book_num+1' ), " date_id=' { $date_id } ' and dish_id=' { $dv [ 'id' ] } ' " );
$db -> insert ( " tb_order_detail " , $order_detail );
$odid = $db -> insert_id ();
}
$date = strtotime ( $dc_date );
$month = date ( 'n' , $date );
$day = date ( 'j' , $date );
$date_text = $month . '月' . $day . '日' . '(周' . cnWeek ( $date ) . ')' . $dc_type_name ;
$result [] = array ( 'order_id' => $oid , 'meal_text' => $date_text , 'take_food_code' => $order [ 'take_food_code' ], 'qr_code' => $qrcode_url , " total_price " => $total [ 'total_price' ], " total_num " => $total [ 'total_num' ]);
}
// 结账
if ( $rList ) { //扣费
$r = gpay_pay_user_account ( $user_id , $rList , $updateInfo );
if ( $r != 'suc' ) {
// 支付失败,删除记录
foreach ( $dish as $dk => $dv ){
$db -> update ( " tb_date_dish " , array ( 'book_num' => 'book_num-1' ), " date_id=' { $date_id } ' and dish_id=' { $dv [ 'id' ] } ' " );
}
$db -> delete ( " tb_order_detail " , " order_id=' { $oid } ' " );
$db -> delete ( " tb_order " , " id=' { $oid } ' " );
if ( $r == 'yebz' ) {
// echo "【用户:{$user_v['username']}】余额不足,下单失败,请先充值";
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】余额不足,下单失败,请先充值。 " ;
} else {
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】下单失败,请重试! " ;
// echo "【用户:{$user_v['username']}】下单失败,请重试!";
}
} else {
$suc_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_type_name } 订餐成功, 订单ID: { $oid } <br> " ;
// 发推送和短信
// $check_username = $db->get_all("select * from tb_user where username in ('向熊林','霍瑞兴','张桂香','王建美','蒙秀荣','杜春梅','陆伟玲','谭婷芳','郑新浓','马绿花','莫火燕','覃金兰','张淼开','甘文盛','谢卫连','黎伟玲','黄玉娇','李亚炳','罗庆坚')");
// $check_tel = array_column($check_username, 'cellphone');
// if(in_array($cellphone, $check_tel)){
$content = " 已成功为您订餐: { $dc_date } { $dc_type_name } , 您可进入“云中美食app-我的订单”查看。" ;
// 暂时停止推送
// $send_res = sendTs($cellphone, $title, $content);
// $sms_res = sendSms($cellphone, $content);
// }
}
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
echo $log_msg ;
}
}
}
}
// 星期日 订下周的餐,根据投票表
if ( $act == 'default_dish2' ){
$fp = fopen ( dirname ( __FILE__ ) . " /time_task_lock.txt " , " w+ " );
if ( ! flock ( $fp , LOCK_EX | LOCK_NB )) {
fclose ( $fp );
exit ;
}
header ( " Content-type:text/html;charset=utf-8 " );
// 测试数据
$dc_types = check_form ( $argv [ 2 ]); // dc_type
$company_id = check_form ( $argv [ 3 ]); // 公司ID
$cls_ids = check_form ( $argv [ 4 ]); // 荤素汤 1,2,3
// 自动订餐提前几天
$relative_day_tq = 1 ;
if ( empty ( $dc_types )){
$dc_types = trim ( $_GET [ 'dc_types' ]);
}
if ( empty ( $company_id )){
$company_id = trim ( $_GET [ 'company_id' ]);
}
if ( empty ( $cls_ids )){
$cls_ids = trim ( $_GET [ 'cls_ids' ]);
}
if ( empty ( $cls_ids )){
$cls_ids = '4,5,6' ; // 荤素汤 1,2,3
}
$cls_ids_arr = explode ( ',' , $cls_ids );
// $dc_types = "27,33,25"; // 早,午,晚
if ( empty ( $dc_types ) || empty ( $company_id )){
exit ;
}
// 记录异常问题提示
$err_msg = array ();
$suc_msg = array ();
// 查找几天内
$other_day = strtotime ( " -5 day " ); // 当前时间往前5天
// $to_day = date('Y-m-d', $other_day);
// $time = date('H:i:s', $other_day);
// $relative_datetime = date('Y-m-d H:i:s', $other_day);
$to_day = date ( 'Y-m-d' );
$time = date ( 'H:i:s' );
$relative_datetime = date ( 'Y-m-d H:i:s' );
$current_date = date ( 'Y-m-d H:i:s' );
// 判断今天是否有 可订餐
$dc_date_list = $db -> get_all ( "
select
a . id date_id ,
a . dc_date ,
b . name dc_type_name ,
b . id dc_type ,
b . start_time ,
c .*
from tb_date a
left join tb_meal_type b on a . dc_type = b . id
left join tb_order_rule c on ( a . dc_type = c . for_dc_type and c . `status` = 1 and c . dining_hall_id = a . dining_hall_id and c . rule_type = 1 and c . for_action = 1 )
where a . dc_date >= '{$to_day}' and b . company_id = '{$company_id}' and b . mealtype = '1' and a . dc_type in ({ $dc_types })
" );
if ( empty ( $dc_date_list )){
$err_msg [] = " 没有安排可订餐时间 " ;
echo " 没有安排可订餐时间 " ;
exit ;
}
// 获取所有可以自动进行订餐的 用户
$user_list = $db -> get_all ( " select * from tb_user where enabled='1' and company_id=' { $company_id } ' and default_dc_type!='' " );
// 进行订餐
foreach ( $dc_date_list as $k => $v ){
$log_type = 'default_dish' ;
$dc_date = $v [ 'dc_date' ];
$date_id = $v [ 'date_id' ];
$dc_type_name = $v [ 'dc_type_name' ];
$dc_type = $v [ 'for_dc_type' ];
// 1预定, 2退订
$for_action = $v [ 'for_action' ];
// 提前多少天
if ( $relative_day_tq ){
$relative_day = $relative_day_tq ;
} else {
$relative_day = $v [ 'relative_day' ];
}
// 截止时间
$deadline = $v [ 'time' ];
/*
// 获取投票开始时间, 前一天开始( 暂时不判断is_create_dishes=1, 因为生成菜谱还没成功update 1, 待修复)
$vote_info = $db -> get_one ( " select a.* from tb_dish_vote_date a left join tb_dining_hall b on a.dining_hall_id=b.id where a.start_day<=' { $dc_date } ' and a.end_day>=' { $dc_date } ' and a.dc_type=' { $v [ 'dc_type' ] } ' and a.status=1 and b.enabled=1 and b.company_id=' { $company_id } ' " );
if ( $vote_info ){
// 订餐周期的起始日
$current_date = strtotime ( $vote_info [ 'start_day' ]) - ( 60 * 60 * 24 * $relative_day );
$current_date = date ( 'Y-m-d' , $current_date ) . " 17:30:00 " ;
// $current_date_end = date('Y-m-d', $current_date)." 23:59:59";
} else {
echo " 找不到投票周期的信息 { $dc_date } { $dc_type_name } <br> " ;
continue ;
} */
// 需求修改,只是用自动订餐
// 获取下一周的日期段
$next_week_arr = get_next_week_day ( 1 );
$next_week_start_day = $next_week_arr [ 'start_day' ];
$next_week_start_end = $next_week_arr [ 'end_day' ];
// 订餐周期的起始日(的$relative_day, 表示提前多少天开始订餐)
$current_date = strtotime ( $next_week_start_day ) - ( 60 * 60 * 24 * $relative_day );
$current_date = date ( 'Y-m-d' , $current_date ) . " 09:00:00 " ;
// 非下周的餐次,都跳过
if ( $dc_date < $next_week_start_day || $dc_date > $next_week_start_end ){
continue ;
}
// 截止具体时间
$deadline_time = strtotime ( $v [ 'dc_date' ]) - ( 60 * 60 * 24 * $relative_day );
$deadline_time = date ( 'Y-m-d' , $deadline_time ) . " { $deadline } " ;
// 判断时间段 用餐开始时间
$meal_time = $dc_date . ' ' . $v [ 'start_time' ];
if ( strtotime ( $current_date ) > time ()){
echo " 还没到周期开始日前 { $relative_day } 天的09:00:00自动订餐时间 { $dc_date } { $dc_type_name } <br> " ;
continue ;
}
// 今天的餐都不订
if ( $dc_date <= date ( 'Y-m-d' )){
echo " 今天的餐都不订 { $dc_date } { $dc_type_name } <br> " ;
continue ;
}
// 每天下午09:00 对未订餐开始订餐
if ( date ( " H:i " ) <= " 09:00 " ){
echo " 还没到09:00订餐时间 { $dc_date } { $dc_type_name } <br> " ;
continue ;
}
// 已经开始用餐就不自动订餐
if ( time () > strtotime ( $meal_time )){
echo " 已经开始用餐,不进行自动订餐 { $dc_date } { $dc_type_name } <br> " ;
continue ;
}
// 如果中途需要订餐,则
// if(strtotime($current_date) < time() && strtotime($current_date_end) > time()){
// // 只订当前时间还没开始用餐 往后的餐次 (提前$relative_day日)
// // $tiqian = $relative_day;
// // 提前 $relative_day 日 到 $relative_day日的晚上12点 允许自动订餐
// $tiqian = strtotime($v['dc_date']) - (60 * 60 * 24 * $relative_day);
// $tiqian_start = date('Y-m-d', $tiqian) ." 17:30:00";
// $tiqian_end = date('Y-m-d', $tiqian) ." 23:59:59";
// if(strtotime($tiqian_start) > time()){
// echo "还没到自动订餐时间 {$dc_date} {$dc_type}<br>";
// continue;
// }
// }
// echo $dc_date.' '.$dc_type."<br>";
// continue;
// if($current_date < $deadline_time || $current_date > $meal_time){
// 当前时间 < 截止时间 || 当前时间 > 开始取餐时间
// 还没到预定截止时间
/*
$err_msg [] = $log_msg = " { $dc_date } - { $dc_type_name } (截止时间: { $deadline_time } / 就餐时间: { $meal_time } ),还没到截止时间,或者已开始就餐,不进行自动订餐<br> " ;
echo $log_msg ;
continue ;
*/
// }
// 循环用户
foreach ( $user_list as $user_k => $user_v ){
$cellphone = $user_v [ 'cellphone' ];
// 用户设置自定义 自动订餐
if ( empty ( $user_v [ 'default_dc_type' ])){
// 用户没有设置自动订餐
echo " 【用户: { $user_v [ 'username' ] } 】用户没有设置自动订餐<br> " ;
continue ;
}
$default_dc_type = explode ( ',' , $user_v [ 'default_dc_type' ]);
if ( ! in_array ( $v [ 'dc_type' ], $default_dc_type )){
// 当前dc_type 用户没有设置自动订餐
echo " 当前dc_type 用户没有设置自动订餐<br> " ;
continue ;
}
$dish = array ();
$user_id = $user_v [ 'id' ];
$dining_hall_id = $user_v [ 'dining_hall_id' ];
$user_type_id = $user_v [ 'type_id' ];
// 判断该用户是否订餐(已订过餐 或 已取消过)
$check_dish = $db -> get_one ( " select * from tb_order where uid=' { $user_id } ' and type='1' and state_id in (3,4,6) and date_id=' { $date_id } ' and is_delete=0 " );
if ( $check_dish ){
continue ;
}
if ( $dc_type == '229' ){
// 当菜品类型 是 早餐, 固定从tb_date 找3个dish_type的一个菜品 生成 $dish
$dish = array ();
$get_dc_date_info = $db -> get_all ( " select * from tb_date a ,tb_date_dish b where a.id=b.date_id and a.dc_date=' { $dc_date } ' and a.dc_type=' { $dc_type } ' and a.dining_hall_id=' { $dining_hall_id } ' " );
$dish_list_id = array_column ( $get_dc_date_info , 'dish_id' );
$dish_list_id_str = join ( ',' , $dish_list_id );
$dish = $db -> get_all ( " select * from tb_dish where id in ( { $dish_list_id_str } ) and enabled='1' group by dish_type LIMIT 3 " );
if ( count ( $dish ) < 3 ){
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_date } { $dc_type_name } 菜品类型不足, 必须大于等于3个菜品 " ;
echo $log_msg ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
continue ;
}
} else {
// 进行自动订餐
$foot_type = $db -> get_all ( " select a.*,b.default_dish_num from tb_dish_vote_foot_type a left join tb_dish_vote_foot_type_cls b on a.cls_id=b.id and a.cls_id>0 where meal_type_id=' { $dc_type } ' and dining_hall_id=' { $dining_hall_id } ' and b.id in ( { $cls_ids } ) " );
// 筛选出 两荤一素一汤
$dish_ids_arr = array ();
// 已录入自定订餐菜式的份数
$default_dish_num = 0 ;
foreach ( $foot_type as $ft_k => $ft_v ){
// if($ft_v['cls'] == 1 && count($dish_ids['hun']) >= 2){
// continue;
// }elseif($ft_v['cls'] == 2 && count($dish_ids['su']) >= 1){
// continue;
// }elseif($ft_v['cls'] == 3 && count($dish_ids['tang']) >= 1){
// continue;
// }
if ( count ( $dish_ids_arr [ $ft_v [ 'cls_id' ]]) >= $ft_v [ 'default_dish_num' ]){
// 数组的个数大于等于 限定个数
continue ;
}
$dish_type_ids = substr ( $ft_v [ 'dish_type_id' ], 1 , strlen ( $ft_v [ 'dish_type_id' ]) - 2 );
$dish_type_ids_arr = explode ( ',' , $dish_type_ids );
// 查找对应类型的 dish
// $dish_list = $db->get_all("select * from tb_dish where enabled='1' and dining_hall_id='{$dining_hall_id}' and dish_type in ({$dish_type_ids})");
$dish_list = $db -> get_all ( " select a.total_max_num, a.book_num,b.* from tb_date_dish a
left join tb_dish b on a . dish_id = b . id
where b . enabled = '1' and b . dining_hall_id = '{$dining_hall_id}' and b . dish_type in ({ $dish_type_ids }) and a . date_id = '{$date_id}' " );
// $count = $ft_v['default_dish_num'] - count($dish_ids_arr[$ft_v['cls_id']]);
// for ($i=0;$i < $count; $i++){
// 有问题
for ( $i = 0 ; $i < count ( $dish_list ); $i ++ ){
// 判断菜式是否已经存满
if ( $ft_v [ 'default_dish_num' ] - count ( $dish_ids_arr [ $ft_v [ 'cls_id' ]]) <= 0 ){
break ;
}
// 判断该菜品最多可点分数 是否已点完
if ( $dish_list [ $i ][ 'total_max_num' ] != 0 && $dish_list [ $i ][ 'total_max_num' ] <= $dish_list [ $i ][ 'book_num' ]){
break ;
}
if ( empty ( $dish_list [ $i ])){
break ;
}
$dish_ids_arr [ $ft_v [ 'cls_id' ]][] = $dish_list [ $i ];
// $default_dish_num += 1;
}
$cls_ids_arr = array_diff ( $cls_ids_arr , [ $ft_v [ 'cls_id' ]]);
$default_dish_num += $ft_v [ 'default_dish_num' ];
}
if ( count ( $cls_ids_arr ) > 0 ){
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_date } { $dc_type_name } 菜品类型不足, 必须满足2荤1素1汤<br> " ;
echo $log_msg ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
continue ;
}
// 把dish_ids数组变成一维数组
$dish = array_reduce ( $dish_ids_arr , function ( $result , $value ) {
// foreach ($value as $reduce_k => $reduce_v){
// // 费用翻倍
// $value[$reduce_k]['dish_price'] = $reduce_v['dish_price'] * 2;
// }
return array_merge ( $result , array_values ( $value ));
}, array ());
}
// 下单
$total_price = 0 ;
// 计算金额
foreach ( $dish as $dk => $dv ){
$total_price += $dv [ 'dish_price' ];
}
//考虑tb_meal_type_guding_price固定金额
$row = $db -> get_one ( " select * from tb_meal_type_guding_price where company_id = ' " . $company_id . " ' and meal_type_id = ' " . $dc_type . " ' and (user_type_id=0 or user_type_id= " . $user_type_id . " ) and (dining_hall_id=0 or dining_hall_id = " . $dining_hall_id . " ) order by idx " );
if ( $row ) {
$total_price = $row [ 'price' ];
}
// 判断该用户余额是否充足
$user_balance = $db -> get_one ( " select sum(account) account from tb_account where user_id=' { $user_id } ' group by user_id " );
if ( $user_balance [ 'account' ] < $total_price ){
// 判断是否今天已经记录过
$check_yu_e = $db -> get_one ( " select * from tb_jtj_ts_log where DAY(add_time) = DAY(NOW()) and type='default_dish' and user_id=' { $user_v [ 'id' ] } ' and content like '%余额不足%' " );
if ( $check_yu_e ){
echo " 【用户: { $user_v [ 'username' ] } 】 余额不足,自动订餐失败,今天已记录过,跳过。<br> " ;
continue ;
}
$err_msg [] = $log_msg = " 用户: { $user_v [ 'username' ] } 余额不足,自动订餐失败。 " ;
echo " 【用户: { $user_v [ 'username' ] } 】 余额不足,自动订餐失败。<br> " ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
continue ;
}
$total_num = count ( $dish );
$parent_code = md5 ( uniqid ( rand (), true ));
// 生成code
do {
$code = date ( 'Ymd' ) . $dc_type . substr ( preg_replace ( " /[a-z]/ " , " " , md5 ( rand () . " _1 " . microtime ()) . " 0000 " ), 0 , 4 );
$t_order = $db -> get_one ( " select a.* from tb_order a where code = ' { $code } ' " );
} while ( $t_order );
// 生成take_food_code
do {
$take_food_code = substr ( preg_replace ( " /[a-z]/ " , " " , md5 ( rand () . " _ " . microtime ()) . " 00000000 " ), 0 , 8 );
$t_order = $db -> get_one ( " select a.* from tb_order a where take_food_code=' { $take_food_code } ' " );
} while ( $t_order );
$qrcode_url = " /show/get.php?act=getQRCode&takecode= " . $take_food_code ;
$total = array ( " total_price " => $total_price , " total_num " => $total_num );
if ( $total_num <= 0 ){
// if($total_num < $default_dish_num){
$log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_date } { $dc_type_name } 没有可订的菜品,自定订餐失败。 " ;
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
echo $log_msg ;
continue ;
}
$order = array (
'code' => $code ,
" parent_code " => $parent_code ,
" state_id " => 6 ,
" uid " => $user_id ,
" date_id " => $date_id ,
" type " => 1 ,
" create_time " => date ( 'Y-m-d H:i:s' ),
" book_num " => 1 ,
" take_num " => 0 ,
" reason " => " 自动订餐,订餐消费支付 { $total [ 'total_price' ] } 元 " ,
" take_food_code " => $take_food_code ,
'total_num' => $total [ 'total_num' ],
'total_price' => $total [ 'total_price' ],
" qrcode_url " => $qrcode_url ,
" qrcode_path " => '' ,
" book_fee " => 0
);
// 延迟0.01-0.6秒
$suijishu = mt_rand ( 1 , 60 );
usleep ( $suijishu * 10000 );
// 在判断是否已经有订单
$check_dish = $db -> get_one ( " select * from tb_order where uid=' { $user_id } ' and type='1' and state_id in (3,4,6) and date_id=' { $date_id } ' and is_delete=0 " );
if ( $check_dish ){
continue ;
}
$db -> insert ( " tb_order " , $order );
$oid = $db -> insert_id ();
if ( $oid > 0 ){
$rList = array ( array ( 'ac' => $total [ 'total_price' ], 'rtype' => 2 , 'ftid' => $dining_hall_id , 'oid' => $oid ));
foreach ( $dish as $dk => $dv ){
$order_detail = array ( " order_id " => $oid , " dish_id " => $dv [ 'id' ], " dish_amount " => 1 , " dish_price " => $dv [ 'dish_price' ]);
// $this->Dishes_model->add_date_dish_book_num(1, $date_id, $dv['dish_id']);
$db -> update ( " tb_date_dish " , array ( 'book_num' => 'book_num+1' ), " date_id=' { $date_id } ' and dish_id=' { $dv [ 'id' ] } ' " );
$db -> insert ( " tb_order_detail " , $order_detail );
$odid = $db -> insert_id ();
}
$date = strtotime ( $dc_date );
$month = date ( 'n' , $date );
$day = date ( 'j' , $date );
$date_text = $month . '月' . $day . '日' . '(周' . cnWeek ( $date ) . ')' . $dc_type_name ;
$result [] = array ( 'order_id' => $oid , 'meal_text' => $date_text , 'take_food_code' => $order [ 'take_food_code' ], 'qr_code' => $qrcode_url , " total_price " => $total [ 'total_price' ], " total_num " => $total [ 'total_num' ]);
}
// 结账
if ( $rList ) { //扣费
$r = gpay_pay_user_account ( $user_id , $rList , $updateInfo );
if ( $r != 'suc' ) {
// 支付失败,删除记录
foreach ( $dish as $dk => $dv ){
$db -> update ( " tb_date_dish " , array ( 'book_num' => 'book_num-1' ), " date_id=' { $date_id } ' and dish_id=' { $dv [ 'id' ] } ' " );
}
$db -> delete ( " tb_order_detail " , " order_id=' { $oid } ' " );
$db -> delete ( " tb_order " , " id=' { $oid } ' " );
if ( $r == 'yebz' ) {
// echo "【用户:{$user_v['username']}】余额不足,下单失败,请先充值";
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】余额不足,下单失败,请先充值。 " ;
} else {
$err_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】下单失败,请重试! " ;
// echo "【用户:{$user_v['username']}】下单失败,请重试!";
}
} else {
$suc_msg [] = $log_msg = " 【用户: { $user_v [ 'username' ] } 】 { $dc_type_name } 订餐成功, 订单ID: { $oid } <br> " ;
// 发推送和短信
// $check_username = $db->get_all("select * from tb_user where username in ('向熊林','霍瑞兴','张桂香','王建美','蒙秀荣','杜春梅','陆伟玲','谭婷芳','郑新浓','马绿花','莫火燕','覃金兰','张淼开','甘文盛','谢卫连','黎伟玲','黄玉娇','李亚炳','罗庆坚')");
// $check_tel = array_column($check_username, 'cellphone');
// if(in_array($cellphone, $check_tel)){
$content = " 已成功为您订餐: { $dc_date } { $dc_type_name } , 您可进入“云中美食app-我的订单”查看。" ;
// 暂时停止推送
// $send_res = sendTs($cellphone, $title, $content);
// $sms_res = sendSms($cellphone, $content);
// }
}
$log_data [ 'cellphone' ] = $user_v [ 'cellphone' ];
$log_data [ 'title' ] = '自动订餐' ;
$log_data [ 'content' ] = $log_msg ;
$log_data [ 'type' ] = $log_type ;
$log_data [ 'user_id' ] = $user_v [ 'id' ];
$log_data [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$sql_data = " " ;
foreach ( $log_data as $data_key => $data_val ){
$sql_data .= " , { $data_key } =' { $data_val } ' " ;
}
$sql_data = substr ( $sql_data , 1 , strlen ( $sql_data ) - 1 );
$db -> query ( " insert into tb_jtj_ts_log set { $sql_data } " );
echo $log_msg ;
}
}
}
}
function get_next_work_day ( $next_week_num = 1 , $current_date = '' ){
// 获取当前星期几
$week_num = date ( 'w' );
// 按照星期1-日 = 1-7
$week_num = $week_num != 0 ? $week_num : 7 ;
// 获取相差多少天到下星期1
$differ_day = 7 - $week_num + 1 ;
$n = 7 * ( $next_week_num - 1 );
$start_day = date ( 'Y-m-d' , strtotime ( $differ_day + $n . ' day' ));
$end_day = date ( 'Y-m-d' , strtotime ( $differ_day + $n + 6 . ' day' ));
$list = array ();
$day_list = array (); // 这周工作日所有日期的数组
for ( $i = 0 ; $i < 5 ; $i ++ ){
$date_name = date ( 'Y-m-d' , strtotime ( $differ_day + $n + $i . ' day' ));
$week_name = get_week_name ( $date_name );
$current = false ;
if ( $date_name == date ( 'Y' ) . '-' . $current_date ){
$current = true ;
}
if ( empty ( $current_date ) && $i == 0 ){
$current = true ;
}
$day_list [] = $date_name ;
$list [] = array (
'exact_date' => $date_name ,
'date' => substr ( $date_name , 5 ),
'weekday' => $week_name ,
'name' => substr ( $date_name . '( ' . $week_name . ') ' , 5 ),
'current' => $current
);
}
return array (
'start_day' => $start_day ,
'end_day' => $end_day ,
'list' => $list ,
'day_list' => $day_list
);
}
function get_next_week_day ( $next_week_num = 1 , $current_date = '' ){
// 获取当前星期几
$week_num = date ( 'w' );
// 按照星期1-日 = 1-7
$week_num = $week_num != 0 ? $week_num : 7 ;
// 获取相差多少天到下星期1
$differ_day = 7 - $week_num + 1 ;
$n = 7 * ( $next_week_num - 1 );
// $start_day = date('Y-m-d', strtotime($differ_day+$n .' day'));
$start_day = date ( 'Y-m-d' , strtotime ( '+1 day' ));
$end_day = date ( 'Y-m-d' , strtotime ( $differ_day + $n + 6 . ' day' ));
$list = array ();
$day_list = array (); // 这周工作日所有日期的数组
for ( $i = 0 ; $i < 14 ; $i ++ ){
$date_name = date ( 'Y-m-d' , strtotime ( $differ_day + $n + $i . ' day' ));
$week_name = get_week_name ( $date_name );
$current = false ;
if ( $date_name == date ( 'Y' ) . '-' . $current_date ){
$current = true ;
}
if ( empty ( $current_date ) && $i == 0 ){
$current = true ;
}
$day_list [] = $date_name ;
$list [] = array (
'exact_date' => $date_name ,
'date' => substr ( $date_name , 5 ),
'weekday' => $week_name ,
'name' => substr ( $date_name . '( ' . $week_name . ') ' , 5 ),
'current' => $current
);
}
return array (
'start_day' => $start_day ,
'end_day' => $end_day ,
'list' => $list ,
'day_list' => $day_list
);
}
function get_week_name ( $day = '' ){
$num = ! empty ( $day ) ? date ( 'w' , strtotime ( $day )) : date ( 'w' );
$arr = array ( " 日 " , " 一 " , " 二 " , " 三 " , " 四 " , " 五 " , " 六 " );
return '周' . $arr [ $num ];
}
// 投票提醒
if ( $act == 'vote_remind' ) {
$log_type = " vote_remind " ;
$date_time = date ( " Y-m-d H:i:s " );
// 获取今天 往后的一个星期
$next_week_arr = get_next_work_day ( 1 );
$vote_date_list = $db -> get_all ( " select * from tb_dish_vote_date where status=1 and is_create_dishes=0 and deadline>' { $date_time } ' and start_day<=' { $next_week_arr [ 'end_day' ] } ' " );
// 获取该投票菜式已经生成(截止之前)
// $vote_date_list = $db->get_all("select * from tb_dish_vote_date where status=1 and is_create_dishes=1 and deadline>'{$date_time}'");
foreach ( $vote_date_list as $k => $v ){
$start_day = $v [ 'start_day' ];
$end_day = $v [ 'end_day' ];
$deadline = $v [ 'deadline' ];
$add_time = $v [ 'add_time' ];
// 一小时后推送
$push_time = strtotime ( $add_time ) + ( 60 * 60 * 1 );
if ( time () < $push_time ){
// 还没到时间
continue ;
}
// 判断是否已经推送过
//$check_push = $db->get_one("select * from tb_jtj_ts_log where type='{$log_type}' and vote_date_id='{$v['id']}'");
//if($check_push){
// 已经推送过
// continue;
//}
$dc_type = $v [ 'dc_type' ];
$dc_type_name = '' ;
if ( $dc_type ){
$dc_type_info = $db -> get_one ( " select * from tb_meal_type where id=' { $dc_type } ' " );
$dc_type_name = " ( { $dc_type_info [ 'name' ] } ) " ;
}
// 获取可以推送的用户
$user_list = $db -> get_all ( " select * from tb_user where enabled='1' and company_id='54' " );
foreach ( $user_list as $user_k => $user_v ){
// 判断是否已经推送过
$check_push = $db -> get_one ( " select * from tb_jtj_ts_log where type=' { $log_type } ' and vote_date_id=' { $v [ 'id' ] } ' and user_id = " . $user_v [ 'id' ]);
if ( $check_push ){
// 已经推送过
continue ;
}
// 暂时停止推送
// sendTs($user_v['cellphone'], "通知", "投票菜单{$dc_type_name}已更新,请积极参与投票(如已投票,请忽略)。");
// sendSms($user_v['cellphone'], "投票菜单{$dc_type_name}已更新,请积极参与投票(如已投票,请忽略)。");
//记录推送记录
$push_arr = array (
'user_id' => $user_v [ 'id' ],
'cellphone' => $user_v [ 'cellphone' ],
'title' => '投票提醒' ,
'type' => $log_type ,
'vote_date_id' => $v [ 'id' ],
'content' => " 【 { $start_day } ~ { $end_day } 】用户: { $user_v [ 'cellphone' ] } ,投票菜单 { $dc_type_name } 已推送完成, vote_date_id= { $v [ 'id' ] } " ,
'add_time' => date ( 'Y-m-d H:i:s' )
);
// $db->insert("tb_jtj_ts_log", $push_arr);
// $push_id = $db->insert_id();
}
}
}
// 生成菜谱后 1小时推送提醒订餐
if ( $act == 'buffet_order_remind' ){
$log_type = " buffet_order_remind " ;
$date_time = date ( " Y-m-d H:i:s " );
// 判断是否已经生成了菜谱 (截止之后)
$vote_date_list = $db -> get_all ( " select * from tb_dish_vote_date where status=1 and is_create_dishes=1 and deadline<=' { $date_time } ' " );
foreach ( $vote_date_list as $k => $v ){
// 判断是否已经推送了
$start_day = $v [ 'start_day' ];
$end_day = $v [ 'end_day' ];
$deadline = $v [ 'deadline' ];
// 超过7天的不提示
$last_push_time = strtotime ( $deadline ) + ( 60 * 60 * 24 * 7 );
if ( time () > $last_push_time ){
continue ;
}
// 判断是否已经推送过
$check_push = $db -> get_one ( " select * from tb_jtj_ts_log where type=' { $log_type } ' and vote_date_id=' { $v [ 'id' ] } ' " );
// 已经推送过
if ( $check_push ){ continue ;}
// 判断投票截止时间 + 1小时
$push_time = strtotime ( $deadline ) + ( 60 * 60 * 1 );
if ( time () < $push_time ){
// 还没到时间
continue ;
}
$dc_type = $v [ 'dc_type' ];
$dc_type_name = '' ;
if ( $dc_type ){
$dc_type_info = $db -> get_one ( " select * from tb_meal_type where id=' { $dc_type } ' " );
$dc_type_name = " ( { $dc_type_info [ 'name' ] } ) " ;
}
// 获取可以推送的用户
$user_list = $db -> get_all ( " select * from tb_user where enabled='1' and company_id='54' " );
foreach ( $user_list as $user_k => $user_v ){
// 暂时停止推送
// sendTs($user_v['cellphone'], "通知", "菜单{$dc_type_name}已更新,请及时订餐(如已订餐,请忽略)。");
// sendSms($user_v['cellphone'], "菜单{$dc_type_name}已更新,请及时订餐(如已订餐,请忽略)。");
//记录推送记录
$push_arr = array (
'cellphone' => $user_v [ 'cellphone' ],
'title' => '提醒订餐' ,
'type' => $log_type ,
'vote_date_id' => $v [ 'id' ],
'content' => " 【 { $start_day } ~ { $end_day } 】用户: { $user_v [ 'username' ] } ,生成菜单 { $dc_type_name } 后提醒订餐已完成 " ,
'add_time' => date ( 'Y-m-d H:i:s' )
);
// $db->insert("tb_jtj_ts_log", $push_arr);
// $push_id = $db->insert_id();
}
}
}
// 生日餐提醒
if ( $act == 'birth_meal_remind' ){
$log_type = 'birth_meal_remind' ;
$date_time = date ( " Y-m-d H:i:s " );
$year = date ( 'Y' );
// 获取所有用户
$user_list = $db -> get_all ( " select * from tb_user where enabled='1' and company_id='54' and (birthday is not null or birthday!='') " );
foreach ( $user_list as $k => $v ){
if ( empty ( trim ( $v [ 'birthday' ]))){
// 防止设置生日是空格
continue ;
}
// 判断这个人今年是否提醒了
$check_push = $db -> get_one ( " select * from tb_jtj_ts_log where type=' { $log_type } ' and user_id=' { $v [ 'id' ] } ' and DATE_FORMAT(add_time,'%Y') = ' { $year } ' " );
if ( $check_push ){
continue ;
}
// 今年生日时间 - 48小时 提醒
$birth_remind_time = strtotime ( $year . '-' . $v [ 'birthday' ]) - ( 60 * 60 * 48 );
if ( time () < $birth_remind_time ){
// 还没到生日提醒
continue ;
}
// 生日提醒结束时间
$birth_remind_end_time = strtotime ( $year . '-' . $v [ 'birthday' ]) - ( 60 * 60 * 15 );
if ( time () > $birth_remind_end_time ){
continue ;
}
// 生日提醒
// 暂时停止推送
// sendTs($v['cellphone'], "通知", "您有生日餐可订,请及时进入【云中美食】订餐(如已订餐,请忽略)。");
// sendSms($v['cellphone'], "您有生日餐可订,请及时进入【云中美食】订餐(如已订餐,请忽略)。");
//记录推送记录
$push_arr = array (
'cellphone' => $v [ 'cellphone' ],
'title' => '生日餐提醒' ,
'type' => $log_type ,
'user_id' => $v [ 'id' ],
'content' => " 【用户: { $v [ 'username' ] } 】生日餐已提醒 " ,
'add_time' => date ( 'Y-m-d H:i:s' )
);
// $db->insert("tb_jtj_ts_log", $push_arr);
// $push_id = $db->insert_id();
}
}
//header("Content-type:text/html;charset=utf-8");
if ( $act == 'tuikuan' ){
$dining_hall_id = $_GET [ 'dining_hall_id' ];
$date_id = $_GET [ 'date_id' ];
$user_id = $_GET [ 'user_id' ];
if ( empty ( $dining_hall_id ) || empty ( $date_id )){
echo " 参数不能为空 " ;
exit ;
}
function get_user_by_phone2 ( $cellphone )
{
global $db ;
$row = $db -> get_one ( " select a.*,b.name type_name from tb_user a, tb_user_type b where a.type_id = b.id and (cellphone = ' { $cellphone } ' or username=' { $cellphone } ') and a.enabled not in (0, -99) " );
if ( ! $row ) return null ;
if ( is_jdpw2 ( $row [ 'password' ])) {
$row [ 'jdmm' ] = 1 ;
} else {
$row [ 'jdmm' ] = 0 ;
}
$row1 = $db -> get_one ( " select * from tb_config where class = 'ONLY_CASH_AREA' " );
$cash_fund = 0 ;
if ( $row1 ) {
$v = trim ( $row1 [ 'value' ], " , " );
if ( in_array ( $row [ 'area_id' ], explode ( ',' , $v ))) $cash_fund = 1 ;
}
$row [ 'cash_fund' ] = $cash_fund ;
return $row ;
}
// $sql = "select a.* from tb_order a left join tb_date b on a.date_id=b.id where b.dining_hall_id='{$dining_hall_id}' and a.date_id='{$date_id}' and a.state_id=6 and a.type=1";
$where_user_id = " " ;
if ( $user_id ){
$where_user_id = " and a.uid=' { $user_id } ' " ;
} else {
$where_user_id = " and DATE(a.create_time) = DATE(now()) " ;
}
$sql = " select a.* from tb_order a left join tb_date b on a.date_id=b.id where b.dining_hall_id=' { $dining_hall_id } ' and a.date_id=' { $date_id } ' and a.state_id=6 and a.type=1 { $where_user_id } " ;
$info = $db -> get_all ( $sql );
if ( empty ( $info )){
echo " 暂没有可退订单<br> " ;
exit ;
}
$host = $_SERVER [ 'HTTP_HOST' ];
$protocol = ( ! empty ( $_SERVER [ 'HTTPS' ]) && $_SERVER [ 'HTTPS' ] !== 'off' || $_SERVER [ 'SERVER_PORT' ] == 443 ) ? " https:// " : " http:// " ;
foreach ( $info as $k => $v ){
$user_id = $v [ 'uid' ];
if ( empty ( $user_id )){
echo " order_id=' { $v [ 'id' ] } ', 用户ID: ' { $user_id } ' 的uid为空, 无法退餐<br> " ;
continue ;
}
// 获取用户token
$getToken = $db -> get_one ( " select * from tb_session where uid=' { $user_id } ' order by expire_time desc " );
if ( empty ( $getToken )){
// 没有token信息, 自己生成
$token = md5 ( uniqid ());
$user_info = $db -> get_one ( " select * from tb_user where id=' { $user_id } ' " );
$user = get_user_by_phone2 ( $user_info [ 'cellphone' ]);
$set = " token=' { $token } ', ip=' " . $_SERVER [ 'REMOTE_ADDR' ] . " ', login_time=' " . time () . " ', data=' " . addslashes ( json_encode ( $user )) . " ', expire_time=' " . ( time () + 86400 ) . " ' " ;
$row = $db -> get_one ( " select * from tb_session where uid= " . $user_id );
if ( $row ) {
$db -> query ( " update tb_session set { $set } where uid= " . $user_id );
} else {
$db -> query ( " insert into tb_session set { $set } , uid= " . $user_id );
}
$getToken = $db -> get_one ( " select * from tb_session where uid=' { $user_id } ' order by expire_time desc " );
// echo "order_id='{$v['id']}', 用户ID: '{$user_id}' 的token获取失败, 无法退餐<br>";
// continue;
}
if ( $getToken [ 'expire_time' ] < time ()){
// token过期
$expire_time = time () + 60 ;
$db -> query ( " update tb_session set expire_time=' { $expire_time } ' where uid=' { $getToken [ 'uid' ] } ' and token=' { $getToken [ 'token' ] } ' " );
}
$post_data = array (
'token' => $getToken [ 'token' ],
'order_id' => $v [ 'id' ],
'cancel_price' => $v [ 'total_price' ],
);
$res = gquery ( " { $protocol } { $host } /api/order/cancel/ " , $post_data );
$res = json_decode ( $res , 1 );
echo " order_id=' { $v [ 'id' ] } ', 用户ID: ' { $user_id } ' { $res [ 'message' ] } <br> " ;
}
}
if ( $act == 'repeat_tc' ) {
// $user_id = $_GET['user_id'];
$check = $_GET [ 'check' ];
$dining_hall_id = 174 ;
if ( $check ) {
$user_dc_all = $db -> get_all ( " select * from
( select GROUP_CONCAT ( a . id ) order_ids , COUNT ( a . code ) total_code , a . uid
from tb_order a , tb_date b
where a . date_id = b . id and b . dining_hall_id = { $dining_hall_id } and a . state_id = 6 and a . date_id > 0 and a . is_delete = 0 and a . uid > 0 and a . create_time BETWEEN '2021-08-08 00:00:00' and '2021-08-10 23:00:00'
GROUP BY a . uid , a . date_id )
as c where total_code >= 2 " );
echo " <pre> " ;
print_r ( $user_dc_all );
echo " </pre> " ;
exit ;
}
// if (empty($user_id)) {
// echo "参数不能为空";
// exit;
// }
function get_user_by_phone2 ( $cellphone )
{
global $db ;
$row = $db -> get_one ( " select a.*,b.name type_name from tb_user a, tb_user_type b where a.type_id = b.id and (cellphone = ' { $cellphone } ' or username=' { $cellphone } ') and a.enabled not in (0, -99) " );
if ( ! $row ) return null ;
if ( is_jdpw2 ( $row [ 'password' ])) {
$row [ 'jdmm' ] = 1 ;
} else {
$row [ 'jdmm' ] = 0 ;
}
$row1 = $db -> get_one ( " select * from tb_config where class = 'ONLY_CASH_AREA' " );
$cash_fund = 0 ;
if ( $row1 ) {
$v = trim ( $row1 [ 'value' ], " , " );
if ( in_array ( $row [ 'area_id' ], explode ( ',' , $v ))) $cash_fund = 1 ;
}
$row [ 'cash_fund' ] = $cash_fund ;
return $row ;
}
$user_dc_all_arr = $db -> get_all ( "
select * from
( select GROUP_CONCAT ( a . id ) order_ids , COUNT ( a . code ) total_code , a . uid
from tb_order a , tb_date b
where a . date_id = b . id and b . dining_hall_id = { $dining_hall_id } and a . state_id = 6 and a . date_id > 0 and a . is_delete = 0 and a . uid > 0 and a . create_time BETWEEN '2021-08-08 00:00:00' and '2021-08-10 23:00:00'
GROUP BY a . uid , a . date_id )
as c where total_code >= 2 " );
// foreach ($user_dc_all_arr as $dc_k => $user_dc_all) {
foreach ( $user_dc_all_arr as $dc_k => $info ) {
$order_ids = $info [ 'order_ids' ];
$user_id = $info [ 'uid' ];
echo " 当前用户今天有order_id数量超过1个: " . $order_ids . " <br> " ;
$order_id_arr = explode ( ',' , $order_ids );
foreach ( $order_id_arr as $k => $v ) {
if ( $k == 0 ) {
echo " order_id: { $v } ,保留。<br> " ;
continue ;
}
echo " 退餐: order_id: { $v } 。<br> " ;
// 获取用户token
$getToken = $db -> get_one ( " select * from tb_session where uid=' { $user_id } ' order by expire_time desc " );
if ( empty ( $getToken )) {
// 没有token信息, 自己生成
$token = md5 ( uniqid ());
$user_info = $db -> get_one ( " select * from tb_user where id=' { $user_id } ' " );
$user = get_user_by_phone2 ( $user_info [ 'cellphone' ]);
$set = " token=' { $token } ', ip=' " . $_SERVER [ 'REMOTE_ADDR' ] . " ', login_time=' " . time () . " ', data=' " . addslashes ( json_encode ( $user )) . " ', expire_time=' " . ( time () + 86400 ) . " ' " ;
$row = $db -> get_one ( " select * from tb_session where uid= " . $user_id );
if ( $row ) {
$db -> query ( " update tb_session set { $set } where uid= " . $user_id );
} else {
$db -> query ( " insert into tb_session set { $set } , uid= " . $user_id );
}
$getToken = $db -> get_one ( " select * from tb_session where uid=' { $user_id } ' order by expire_time desc " );
// echo "order_id='{$v['id']}', 用户ID: '{$user_id}' 的token获取失败, 无法退餐<br>";
// continue;
}
if ( $getToken [ 'expire_time' ] < time ()) {
// token过期
$expire_time = time () + 60 ;
$db -> query ( " update tb_session set expire_time=' { $expire_time } ' where uid=' { $getToken [ 'uid' ] } ' and token=' { $getToken [ 'token' ] } ' " );
}
$order_info = $db -> get_one ( " select * from tb_order where id=' { $v } ' " );
$post_data = array (
'token' => $getToken [ 'token' ],
'order_id' => $order_info [ 'id' ],
'cancel_price' => $order_info [ 'total_price' ],
);
$host = $_SERVER [ 'HTTP_HOST' ];
$protocol = ( ! empty ( $_SERVER [ 'HTTPS' ]) && $_SERVER [ 'HTTPS' ] !== 'off' || $_SERVER [ 'SERVER_PORT' ] == 443 ) ? " https:// " : " http:// " ;
$res = gquery ( " { $protocol } { $host } /api/order/cancel/ " , $post_data );
$res = json_decode ( $res , 1 );
echo " order_id=' { $order_info [ 'id' ] } ', 用户ID: ' { $user_id } ' { $res [ 'message' ] } <br> " ;
echo " --------------------------------------------------<br> " ;
}
}
}
2024-05-10 09:21:36 +08:00
// 定时脚本,当天围餐的菜品都撤掉,然后自动插入餐标菜式,以便实现:当日预定按照人头固定金额, 20, 30, 40这三个金额
2024-05-10 17:39:04 +08:00
if ( $act == 'baojian' ){
2024-05-10 09:21:36 +08:00
$company_id = '4151' ;
// 固定餐标的类型
$meal_label_type = " 898 " ;
$dining_hall_id = " 305 " ;
// 获取今天围餐的所有订单
2024-05-20 08:52:12 +08:00
// $today = date('Y-m-d');
$today = date ( 'Y-m-d' , strtotime ( date ( 'Y-m-d' ) . ' +1 day' ));
2024-05-10 09:21:36 +08:00
$dateInfo = $db -> get_all ( " select a.*, b.name as meal_type_name from tb_date a,tb_meal_type b where a.dc_type=b.id and a.dc_date=' { $today } ' and a.dining_hall_id=' { $dining_hall_id } ' and b.mealtype=6 and b.status=1 order by b.idx desc " );
if ( empty ( $dateInfo )){
2024-05-10 17:39:04 +08:00
echo " 今天没有包间 " ;
2024-05-10 09:21:36 +08:00
exit ;
}
foreach ( $dateInfo as $k => $v ){
// 把tb_date_dish的菜品数量改为0, 然后插入餐标菜式
$date_id = $v [ 'id' ];
// 获取当前菜谱的所有菜品(不含固定餐标)
$dish_list = $db -> get_all ( " select a.*, b.dish_type from tb_date_dish a, tb_dish b where a.dish_id=b.id and b.dish_type!=' { $meal_label_type } ' and a.date_id=' { $date_id } ' and b.enabled=1 " );
// 获取需要更新的菜品id
$dish_ids = array_column ( $dish_list , 'dish_id' );
$dish_ids = join ( ',' , $dish_ids );
$db -> query ( " update tb_date_dish set total_max_num = book_num where date_id= " . $date_id . " and dish_id in ( { $dish_ids } ) " );
//对于book_num = 0的, 也更新为不可以订了
$db -> query ( " update tb_date_dish set total_max_num = 1,book_num = 1 where date_id= " . $date_id . " and book_num = 0 and total_max_num = 0 and dish_id in ( { $dish_ids } ) " );
// 然后添加餐标菜式
// 判断是否已经有餐标菜式
$has_meal_label = $db -> get_one ( " select * from tb_date_dish a, tb_dish b where a.dish_id=b.id and b.dish_type=' { $meal_label_type } ' and a.date_id=' { $date_id } ' and b.enabled=1 " );
2024-05-10 17:37:16 +08:00
if ( empty ( $has_meal_label )){
2024-05-10 09:21:36 +08:00
// 当前菜谱没有餐标菜式,添加
$meal_label_list = $db -> get_all ( " select * from tb_dish where enabled=1 and dish_type=' { $meal_label_type } ' and dining_hall_id=' { $dining_hall_id } ' order by id asc " );
foreach ( $meal_label_list as $mk => $mv ){
$insert_data = array (
'date_id' => $date_id ,
'dish_id' => $mv [ 'id' ],
'max_num' => 1000 ,
'is_normal_dish' => 1 ,
'total_max_num' => 0 ,
'idx' => 0 ,
'book_type' => 1 ,
'book_num' => 0 ,
);
$db -> insert ( " tb_date_dish " , $insert_data );
}
}
}
}