85 lines
3.5 KiB
PHP
85 lines
3.5 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use think\Db;
|
||
|
||
class Calendar extends ApiBase
|
||
{
|
||
public function index()
|
||
{
|
||
$month = $this->request->post("month");
|
||
if (!$month) {
|
||
return $this->_error("月份不能为空");
|
||
}
|
||
|
||
// 验证月份格式:2026-01
|
||
if (!preg_match('/^\d{4}-\d{2}$/', $month)) {
|
||
return $this->_error("月份格式错误,请使用格式:2026-01");
|
||
}
|
||
|
||
// 计算该月的开始和结束日期
|
||
$startDate = $month . '-01';
|
||
$endDate = date('Y-m-t', strtotime($startDate)); // 获取该月最后一天
|
||
|
||
// 查询该月份的所有服务数据,关联商品表和员工表
|
||
$service = Db::name('order_exe')
|
||
->alias('oe')
|
||
->join('goods g', 'oe.goods_id = g.id', 'LEFT')
|
||
->join('staff s', 'oe.staff_id = s.id', 'LEFT')
|
||
->where('oe.date', '>=', $startDate)
|
||
->where('oe.date', '<=', $endDate)
|
||
->where('oe.abnormal', 0)
|
||
->field('oe.staff_status,oe.date,oe.addtime,oe.start_time,oe.timeout,oe.xq,oe.staff_id,s.name as staff_name,s.onwork,s.leave,g.name as goods_name')
|
||
->order('oe.date asc, oe.addtime asc')
|
||
->select();
|
||
|
||
$serviceDate = [];
|
||
if ($service) {
|
||
foreach ($service as $v) {
|
||
// 如果有绑定员工,但员工状态不符合要求,则跳过
|
||
if (!empty($v['staff_id']) && ($v['onwork'] != 1 || $v['leave'] != 1)) {
|
||
continue;
|
||
}
|
||
|
||
// 处理服务时间
|
||
$timeRange = '';
|
||
if (!empty($v['start_time']) && !empty($v['timeout'])) {
|
||
// 判断是否为时间戳(数字)
|
||
$startTimestamp = is_numeric($v['start_time']) ? $v['start_time'] : strtotime($v['start_time']);
|
||
$timeoutTimestamp = is_numeric($v['timeout']) ? $v['timeout'] : strtotime($v['timeout']);
|
||
$startTime = date('H:i', $startTimestamp);
|
||
$timeoutTime = date('H:i', $timeoutTimestamp);
|
||
$timeRange = $startTime . '-' . $timeoutTime;
|
||
} elseif (!empty($v['start_time'])) {
|
||
$startTimestamp = is_numeric($v['start_time']) ? $v['start_time'] : strtotime($v['start_time']);
|
||
$startTime = date('H:i', $startTimestamp);
|
||
$timeRange = $startTime;
|
||
}
|
||
|
||
$serviceDate[] = [
|
||
'staff_status' => $v['staff_status'],
|
||
'date' => $v['date'],
|
||
'staff_name' => $v['staff_name'] ?? '',
|
||
'goods_name' => $v['goods_name'] ?? '',
|
||
'addtime' => $v['addtime'] == 1 ? '上午' : ($v['addtime'] == 2 ? '下午' : ''), //1表示上午,2表示下午
|
||
'serviceTime' => $timeRange,
|
||
'xq' => $v['xq'] ?? $this->getChineseWeekDay($v['date']), // 如果没有xq字段,则根据日期计算
|
||
];
|
||
}
|
||
}
|
||
return $this->_success("成功", $serviceDate);
|
||
}
|
||
|
||
/**
|
||
* 获取中文星期
|
||
* @param string $date 日期格式:Y-m-d
|
||
* @return string
|
||
*/
|
||
private function getChineseWeekDay($date)
|
||
{
|
||
$weekDay = date('w', strtotime($date));
|
||
$weekList = ['日', '一', '二', '三', '四', '五', '六'];
|
||
return '星期' . $weekList[$weekDay];
|
||
}
|
||
} |