Files
park/application/api/controller/YqEnterprise.php
MeSHard b22d09bd39 init
2025-12-01 11:19:23 +08:00

511 lines
21 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | Author: 企业端
// +----------------------------------------------------------------------
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
class YqEnterprise extends Api {
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
// 表名定义
protected $api_perimeter = 'perimeter';
protected $api_enterprise_record = 'enterprise_record';
protected $api_vehicle = 'vehicle';
protected $api_waybill = 'waybill';
protected $api_check_record = 'check_record';
//保存登录
public function save_login($param){
if (empty($param['code'])){
$this->alertMsg(300,'异常操作!');
}
$map['login_time'] = array('elt',date("Y-m-d H:i:s", strtotime("+7 day")));
$map['code'] = $param['code'];
$enterprise_id = Db::name($this->api_enterprise_record)->where($map)->value('enterprise_id');
$data['login_status'] = false;
$data['check_uid'] = '';
if ($enterprise_id){
$data['login_status'] = true;
$data['check_uid'] = $enterprise_id;
}
$this->alertMsg(200,'成功!',$data);
}
//登录
public function login($param = null){
// 如果没有传递参数,从 input() 获取
if ($param === null) {
$param = input();
}
if (empty($param['account']) || empty($param['password'])){
$this->alertMsg(300,'异常操作!');
}
$info = Db::name($this->api_perimeter)->where('account',$param['account'])->field('id,password')->find();
if (!$info['id']) {
$this->alertMsg(300,'用户不存在或被禁用!');
} else {
if (md5($param['password']) !== $info['password']) {
$this->alertMsg(300,'密码错误!');
} else {
// code 参数可选,如果没有提供则生成一个
$code = isset($param['code']) ? $param['code'] : 'web_' . time() . '_' . uniqid();
$arr = [
'enterprise_id' => $info['id'],
'code' => $code,
'login_time' => date('Y-m-d H:i:s'),
];
$enterprise_id = Db::name($this->api_enterprise_record)->where('code',$code)->value('enterprise_id');
if (!$enterprise_id){
Db::name($this->api_enterprise_record)->insert($arr);
}else{
Db::name($this->api_enterprise_record)->where('enterprise_id',$enterprise_id)->update($arr);
}
$this->alertMsg(200,'登录成功!',$info['id']);
}
}
}
//修改密码
public function updata_pwd($param){
if (empty($param['check_uid']) || empty($param['old_pwd']) || empty($param['pwd']) ||empty($param['secondary_pwd'])){
$this->alertMsg(300,'异常操作!');
}
$password = Db::name($this->api_perimeter)->where('id',$param['check_uid'])->value('password');
if (md5($param['old_pwd']) !== $password) {
$this->alertMsg(300,'旧密码错误!');
} else {
if ($param['pwd'] != $param['secondary_pwd']){
$this->alertMsg(300,'两次新密码不匹配!');
}
$rse = Db::name($this->api_perimeter)->where('id',$param['check_uid'])->update(['password'=>md5($param['pwd'])]);
if($rse) {
$this->alertMsg(200,'修改成功!');
}else{
$this->alertMsg(300,'网络异常,请稍后再试!');
}
}
}
//查验车辆码
public function check_vehicle_no($param){
if (empty($param['info'])){
$this->alertMsg(300,'异常操作!');
}
$info = explode(',',$param['info']);
$vehicle_info = Db::name($this->api_vehicle)->where('vehicleNo',$info[0])->field('id,plateColor,qr_color')->find();
if (empty($vehicle_info['qr_color'])){
$this->alertMsg(300,'园区内未检测到该车信息,请先绑定后再试!');
}
if ($vehicle_info['qr_color'] != 1){
if ($vehicle_info['qr_color'] == 2){
$mag = '未查到该车有最新电子运单信息,请进行电子运单预约上报!';
}else{
$mag = '该车有违章,请到停车场或检查站进行红码消除后再进行电子运单预约上报!';
}
$this->alertMsg(201,$mag,$vehicle_info['qr_color']);
}else{
$where['tow_license'] = $info[0];
$where['is_del'] = 1;
$field = 'id,tow_license,mount_license,driver_tel,sale_product,create_time,waybill_date,waybill_loading,waybill_unloading,check_status';
$waybill = Db::name($this->api_waybill)->whereTime('waybill_date','d')->where($where)->order('id desc')->field($field)->find();//(当天内有效)
if (empty($waybill)){
$this->alertMsg(202,'未查到该车有最新电子运单信息,请进行电子运单预约上报!');
}else{
$vehicle_status = '正常已获取到车辆GPS定位';
$vehicle_id = true;
if (!$vehicle_info['plateColor']){//车辆表是否存在该车GPS定位
$vehicle_status = '异常未获取到车辆GPS定位';
$vehicle_id = false;
$waybill['plateColor'] = '';
}else{
$waybill['plateColor'] = $vehicle_info['plateColor'];
}
$waybill['vehicle_status'] = $vehicle_status;
$waybill['vehicle_id'] = $vehicle_id;
}
$data['waybill'] = $waybill;
$this->alertMsg(200,'操作成功!',$data);
}
}
//查验结果提交
public function check_submit($param){
if (empty($param['waybill_id']) || empty($param['check_uid']) || empty($param['check_status'])){
$this->alertMsg(300,'异常操作!');
}
$id = Db::name($this->api_check_record)->where('waybill_id',$param['waybill_id'])->value('id');
if ($id){
$this->alertMsg(300,'该车辆信息已审核,请勿重复操作!');
}
$arr = [
'waybill_id' => $param['waybill_id'],
'check_uid' => $param['check_uid'],
'check_status' => $param['check_status'],
'check_where' => $param['check_where']?$param['check_where']:'',
'check_time' => date('Y-m-d H:i:s'),
];
Db::startTrans();
try{
Db::name($this->api_waybill)->where('id',$param['waybill_id'])->setField(['check_status'=>$param['check_status']]);
Db::name($this->api_check_record)->insert($arr);
$see = true;
Db::commit();
} catch (\Exception $e) {
$see = false;
Db::rollback();
}
if($see) {
$this->alertMsg(200,'提交成功!');
}else{
$this->alertMsg(300,'网络异常,请稍后再试!');
}
}
//查验列表
public function check_list($param){
$page = input('page',$this->pageNum);
$limit = input('limit',$this->pernumber);
$map['check_uid'] = $param['check_uid'];
$map['is_del'] = 1;
//模糊搜索
if (input('key')) {
$where['tow_license'] = array('like', "%" . input('key') . "%");
$ids = Db::name($this->api_waybill)->where($where)->field('id')->select();
if (!empty($ids)){
foreach ($ids as $v) {
$v = join(',',$v);
$temp[] = $v;
}
$t = '';
foreach($temp as $v){
$t .= $v.',';
}
$t = substr($t,0,-1);
$map['waybill_id'] = array('in',$t);
}else{
$map['waybill_id'] = '';
}
}
//时间筛选
$sldate = input('reservation','');
$arr = explode(" - ",$sldate);
if(count($arr) == 2){
$map['check_time'] = array(array('egt',$arr[0]),array('elt',$arr[1]),'AND');
}
$field = 'id,waybill_id,check_uid,check_status,check_time';
$list = Db::name($this->api_check_record)->where($map)->field($field)->order('check_time desc')->page($page)->limit($limit)->select();
$conut = Db::name($this->api_check_record)->where($map)->field('id')->count();
if ($list){
foreach ($list as $k => $v){
$list[$k]['check_name'] = Db::name($this->api_perimeter)->where('id',$v['check_uid'])->value('name');
$waybill = Db::name($this->api_waybill)->where('id',$v['waybill_id'])->field('tow_license,mount_license,waybill_date')->find();
$list[$k]['tow_license'] = $waybill['tow_license'];
$list[$k]['mount_license'] = $waybill['mount_license'];
$list[$k]['waybill_date'] = date('Y-m-d',strtotime($waybill['waybill_date']));
}
}
$data['pages'] = ceil($conut/$limit);
$data['list'] = $list;
$this->alertMsg(300,'请求成功!',$data);
}
//查验详情
public function check_detail($param){
if (empty($param['waybill_id'])){
$this->alertMsg(300,'异常操作!');
}
$field = 'id,tow_license,mount_license,driver_tel,sale_product,create_time,waybill_date,waybill_loading,waybill_unloading,check_status';
$waybill = Db::name($this->api_waybill)->where('id',$param['waybill_id'])->field($field)->find();
$vehicle_status = '正常已获取到车辆GPS定位';
$vehicle_id = true;
$vehicle_info = Db::name($this->api_vehicle)->where('vehicleNo',$waybill['tow_license'])->field('id,plateColor')->find();
if (!$vehicle_info['plateColor']){//车辆表是否存在该车GPS定位
$vehicle_status = '异常未获取到车辆GPS定位';
$vehicle_id = false;
$waybill['plateColor'] = '';
}else{
$waybill['plateColor'] = $vehicle_info['plateColor'];
}
$waybill['vehicle_status'] = $vehicle_status;
$waybill['vehicle_id'] = $vehicle_id;
$data['waybill'] = $waybill;
$this->alertMsg(200,'操作成功!',$data);
}
//车辆信息
public function vehicle_info($param){
if (empty($param['vehicleNo'])){
$this->alertMsg(300,'异常操作!');
}
if (empty($param['plateColor'])){
$this->alertMsg(300,'该车暂未获取到GPS车辆信息无法显示');
}
$url = 'http://47.108.219.88:5000/api/httpserver/vehicleInfo?vehicleNo='.rawurlencode($param['vehicleNo']).'&plateColor='.$param['plateColor'];
$header = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$json = json_decode($output,true);
if (empty($json['body'])){
$list = [
'vehicleNo' => '',
'ownerName' => '',
'licenseIssueOrganCode' => '',
'transCertificateCode' => '',
'transCertificateWord' => '',
'businessScopeName' => '',
];
$data['list'] = $list;
}else{
$data['list'] = $json['body'];
}
$this->alertMsg(200,'操作成功!',$data);
}
//获取统计数据(用于移动端首页展示)
public function statistics($param = null){
if ($param === null) {
$param = input();
}
if (empty($param['check_uid'])){
$this->alertMsg(300,'异常操作!');
}
$check_uid = $param['check_uid'];
$today = date('Y-m-d');
// 先检查是否有该企业的查验记录(不限制 is_del因为可能数据中 is_del 的值不同)
$hasRecords = Db::name($this->api_check_record)
->where('check_uid', $check_uid)
->count();
// 1. 园区车辆总数:统计该企业查验过的车辆总数(去重)
// 先获取所有去重的车牌号,然后统计数量
// 注意:如果关联失败,可能是因为 waybill_id 不存在,需要先检查
$vehicleList = [];
if ($hasRecords > 0) {
try {
$vehicleList = Db::name($this->api_check_record)
->alias('cr')
->join($this->api_waybill . ' w', 'cr.waybill_id = w.id', 'LEFT')
->where('cr.check_uid', $check_uid)
->where('cr.is_del', 1)
->where('w.tow_license', '<>', '')
->where('w.tow_license', 'not null')
->group('w.tow_license')
->column('w.tow_license');
} catch (\Exception $e) {
// 如果关联查询失败,尝试直接查询
$vehicleList = [];
}
}
$vehicleCount = count(array_filter($vehicleList)); // 过滤空值
// 2. 今日入园申请数:今日该企业的查验记录数
$todayApplications = Db::name($this->api_check_record)
->where('check_uid', $check_uid)
->where('is_del', 1)
->whereTime('check_time', 'today')
->count();
// 如果 todayApplications 为 0尝试不限制 is_del
if ($todayApplications == 0) {
$todayApplications = Db::name($this->api_check_record)
->where('check_uid', $check_uid)
->whereTime('check_time', 'today')
->count();
}
// 3. 今日已批准数:今日已通过的查验记录数
$todayApproved = Db::name($this->api_check_record)
->where('check_uid', $check_uid)
->where('check_status', 1)
->where('is_del', 1)
->whereTime('check_time', 'today')
->count();
// 4. 今日待审批数:今日未通过的查验记录数
$todayPending = Db::name($this->api_check_record)
->where('check_uid', $check_uid)
->where('check_status', '<>', 1)
->where('is_del', 1)
->whereTime('check_time', 'today')
->count();
// 5. 有运单车辆数:统计有运单的车辆数(今日有运单的车辆)
$waybillVehicleList = [];
if ($hasRecords > 0) {
try {
$waybillVehicleList = Db::name($this->api_waybill)
->alias('w')
->join($this->api_check_record . ' cr', 'w.id = cr.waybill_id', 'LEFT')
->where('cr.check_uid', $check_uid)
->where('cr.is_del', 1)
->whereTime('w.waybill_date', 'today')
->where('w.tow_license', '<>', '')
->where('w.tow_license', 'not null')
->group('w.tow_license')
->column('w.tow_license');
} catch (\Exception $e) {
$waybillVehicleList = [];
}
}
$waybillVehicles = count(array_filter($waybillVehicleList));
// 6. 运输中车辆数:今日有运单且状态为运输中的车辆(可以根据实际情况调整判断条件)
$transportingVehicles = $waybillVehicles; // 简化处理,实际可以根据运单状态判断
// 7. 待装卸车辆数:可以根据运单状态判断,这里简化处理
$loadingVehicles = 0;
// 8. 今日新增车辆数:今日首次查验的车辆数(该车辆在今天之前没有查验记录)
$allVehicles = [];
$todayVehicles = [];
if ($hasRecords > 0) {
try {
// 先获取所有该企业查验过的车辆(今天之前的)
$allVehicles = Db::name($this->api_check_record)
->alias('cr')
->join($this->api_waybill . ' w', 'cr.waybill_id = w.id', 'LEFT')
->where('cr.check_uid', $check_uid)
->where('cr.is_del', 1)
->where('cr.check_time', '<', date('Y-m-d 00:00:00'))
->where('w.tow_license', '<>', '')
->where('w.tow_license', 'not null')
->group('w.tow_license')
->column('w.tow_license');
// 获取今日查验的所有车辆
$todayVehicles = Db::name($this->api_check_record)
->alias('cr')
->join($this->api_waybill . ' w', 'cr.waybill_id = w.id', 'LEFT')
->where('cr.check_uid', $check_uid)
->where('cr.is_del', 1)
->whereTime('cr.check_time', 'today')
->where('w.tow_license', '<>', '')
->where('w.tow_license', 'not null')
->group('w.tow_license')
->column('w.tow_license');
} catch (\Exception $e) {
$allVehicles = [];
$todayVehicles = [];
}
}
// 今日新增 = 今日查验的车辆中,不在历史车辆列表中的
$todayNewVehicles = 0;
if (!empty($todayVehicles)) {
$allVehicles = array_filter($allVehicles);
$todayVehicles = array_filter($todayVehicles);
foreach ($todayVehicles as $vehicle) {
if ($vehicle && !in_array($vehicle, $allVehicles)) {
$todayNewVehicles++;
}
}
}
$data = [
'total_vehicles' => intval($vehicleCount),
'today_applications' => intval($todayApplications),
'today_approved' => intval($todayApproved),
'today_pending' => intval($todayPending),
'active_orders' => intval($waybillVehicles),
'transporting' => intval($transportingVehicles),
'loading' => intval($loadingVehicles),
'today_new' => intval($todayNewVehicles)
];
$this->alertMsg(200,'请求成功!',$data);
}
//获取当前账户的园区记录列表(简化版,用于移动端首页展示)
public function park_records($param = null){
if ($param === null) {
$param = input();
}
if (empty($param['check_uid'])){
$this->alertMsg(300,'异常操作!');
}
$page = isset($param['page']) ? intval($param['page']) : 1;
$limit = isset($param['limit']) ? intval($param['limit']) : 10;
$map['check_uid'] = $param['check_uid'];
$map['is_del'] = 1;
// 获取总数
$count = Db::name($this->api_check_record)->where($map)->count();
// 获取列表数据
$field = 'id,waybill_id,check_uid,check_status,check_time,check_where';
$list = Db::name($this->api_check_record)
->where($map)
->field($field)
->order('check_time desc')
->page($page, $limit)
->select();
// 补充关联信息
if ($list){
foreach ($list as $k => $v){
// 获取运单信息
$waybill = Db::name($this->api_waybill)
->where('id',$v['waybill_id'])
->field('tow_license,mount_license,waybill_date,waybill_loading,waybill_unloading')
->find();
if ($waybill) {
$list[$k]['tow_license'] = $waybill['tow_license'];
$list[$k]['mount_license'] = $waybill['mount_license'];
$list[$k]['waybill_date'] = $waybill['waybill_date'];
$list[$k]['waybill_loading'] = $waybill['waybill_loading'];
$list[$k]['waybill_unloading'] = $waybill['waybill_unloading'];
} else {
$list[$k]['tow_license'] = '';
$list[$k]['mount_license'] = '';
$list[$k]['waybill_date'] = '';
$list[$k]['waybill_loading'] = '';
$list[$k]['waybill_unloading'] = '';
}
// 格式化时间
$list[$k]['check_time_formatted'] = date('Y-m-d H:i:s', strtotime($v['check_time']));
$list[$k]['check_date'] = date('Y-m-d', strtotime($v['check_time']));
}
}
$data = [
'list' => $list ? $list : [],
'total' => $count,
'page' => $page,
'limit' => $limit,
'pages' => ceil($count / $limit)
];
$this->alertMsg(200,'请求成功!',$data);
}
}