Files
anmo/server/app/shopapi/logic/CoachLogic.php
2025-08-19 14:16:51 +08:00

254 lines
9.3 KiB
PHP
Executable File

<?php
namespace app\shopapi\logic;
use app\common\enum\coach\CoachEnum;
use app\common\enum\GoodsEnum;
use app\common\enum\shop\ShopEnum;
use app\common\logic\BaseLogic;
use app\common\model\coach\Coach;
use app\common\model\coach\CoachGoodsIndex;
use app\common\model\coach\CoachServerTime;
use app\common\model\deposit\DepositPackage;
use app\common\model\goods\Goods;
use app\common\model\shop\Shop;
use app\common\model\shop\ShopCoachApply;
use app\common\service\ConfigService;
use think\Exception;
use think\facade\Db;
/**
* 技师逻辑类
* Class CoachLogic
* @package app\shopapi\logic
*/
class CoachLogic extends BaseLogic
{
/**
* @notes 获取技师信息
* @param $id
* @return array|false
* @author cjhao
* @date 2024/12/8 22:26
*/
public function info($id,$shopId)
{
try {
if(empty($id)){
throw new Exception('请选择技师');
}
$coach = Coach::where(['id'=>$id,'shop_id'=>$shopId])
->field('id,sn,mobile,name,work_photo,work_status,gender,age,shop_id')
->findOrEmpty()->toArray();
return $coach;
}catch (Exception $e){
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 申请详情
* @param int $id
* @return false
* @author cjhao
* @date 2024/10/16 02:10
*/
public function applyDetail(int $id)
{
try {
$shopCoachApply = ShopCoachApply::where(['id'=>$id])->findOrEmpty();
if($shopCoachApply->isEmpty()){
throw new Exception('记录不存在');
}
$coach = Coach::alias('C')
->join('skill S','C.skill_id = S.id')
->where(['C.id'=>$shopCoachApply['coach_id']])
->field('C.id,C.name,mobile,work_photo,order_num,good_comment,S.name as skill_name,C.audit_status,introduction')
->findOrEmpty()->toArray();
$coach['good_comment'] = $coach['good_comment'].'%';
return $coach;
}catch (Exception $e){
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 门店申请审核
* @param array $params
* @return string|true
* @author cjhao
* @date 2024/10/16 01:59
*/
public function applyAudit(array $params)
{
try {
$id = $params['id'] ?? '';
$auditStatus = $params['audit_status'] ?? '';
$auditRemark = $params['audit_remark'] ?? '';
if(empty($id)){
throw new Exception('请选择审核记录');
}
if(empty($auditStatus)){
throw new Exception('请选择审核状态');
}
Db::startTrans();
$shopCoachApply = ShopCoachApply::where(['id'=>$params['id']])
->findOrEmpty();
if($shopCoachApply->isEmpty()){
throw new Exception('记录不存在');
}
if(ShopEnum::AUDIT_STATUS_WAIT != $shopCoachApply->audit_status){
throw new Exception('当前记录状态已改变');
}
$shopCoachApply->audit_status = $auditStatus;
$shopCoachApply->audit_remark = $auditRemark;
$shopCoachApply->save();
if(ShopEnum::AUDIT_STATUS_PASS == $shopCoachApply->audit_status){
if(1 == $shopCoachApply['type']){
$deposit = Shop::where(['id'=>$params['shop_id'],'audit_status'=>ShopEnum::AUDIT_STATUS_PASS])->value('deposit');
$depositPackageLists = DepositPackage::where(['type'=>2])->order('money desc')->select()->toArray();
//套餐列表
$coachNum = ConfigService::get('server_setting', 'shop_coach_limit');
foreach ($depositPackageLists as $depositPackage){
if($deposit >= $depositPackage['money']){
$coachNum = $depositPackage['coach_num'];
break;
}
}
$nowCoachNum = Coach::where(['shop_id'=>$params['shop_id'],'audit_status'=>CoachEnum::AUDIT_STATUS_PASS])->count();
if($nowCoachNum >= $coachNum){
throw new Exception('服务人员已达到上限');
}
Coach::where(['id'=>$shopCoachApply['coach_id']])->update([
'shop_id' => $shopCoachApply['shop_id']
]);
}else{
//退出申请
Coach::where(['id'=>$shopCoachApply['coach_id']])->update([
'shop_id' => 0
]);
//清理技师管理的商家项目
$goodsIds = Goods::where(['shop_id'=>$shopCoachApply['shop_id'],'audit_status'=>GoodsEnum::AUDIT_STATUS_PASS])
->column('id');
if($goodsIds){
CoachGoodsIndex::where(['coach_id'=>$shopCoachApply['coach_id'],'goods_id'=>$goodsIds])->delete();
}
}
}
Db::commit();
return true;
}catch (Exception $e){
Db::rollback();
return $e->getMessage();
}
}
/**
* @notes 获取师傅服务
* @param $id
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author cjhao
* @date 2024/10/21 00:25
*/
public function getServerTime($id)
{
try {
if(empty($id)){
throw new Exception('请选择技师');
}
$advanceAppoint = ConfigService::get('server_setting', 'advance_appoint');
$timestampLists[date('m-d',time())] = time()+60*60;
//现在的时间戳和明天,后天零点的时间戳
for ($i = 1;$i < $advanceAppoint;$i++){
$timestampLists[date('m-d',strtotime('+ '.$i.' days'))] = strtotime('+ '.$i.' days midnight');
}
$intervalsLists = [];
foreach ($timestampLists as $date => $timestamp){
$intervalsLists[$date] = get_hour_to_midnight($timestamp);
}
//订单预约时间
$orderAppointLists = \app\common\logic\CoachLogic::getCoachOrderAppoint($id);
//师傅空闲时间
$serverTimeLists = [];
//师傅忙碌时间
$serverTimeBusyLists = [];
$coachServerTime = CoachServerTime::where(['coach_id'=>$id])
->field('date,time,status')
->select();
foreach ($coachServerTime as $time)
{
if(1 == $time['status']){
$serverTimeLists[$time['date']][] = $time['time'];
}else{
$serverTimeBusyLists[$time['date']][] = $time['time'];
}
}
// 获取当前日期的时间戳
$currentDate = strtotime(date('Y-m-d'));
// 获取明天和后天的时间戳
$tomorrowDate = strtotime('tomorrow');
$afterTomorrowDate = strtotime('+2 days',$currentDate);
foreach ($intervalsLists as $date => $intervals){
$timeTips = '';
$timestamp = strtotime(date('Y-'.$date));
if ($timestamp >=$currentDate && $timestamp <$tomorrowDate) {
$timeTips = '今天';
} elseif ($timestamp >=$tomorrowDate && $timestamp <$afterTomorrowDate) {
$timeTips = '明天';
} elseif ($timestamp >=$afterTomorrowDate && $timestamp < strtotime('+3 days',$currentDate)) {
$timeTips = '后天';
} else {
$weekdays = array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
$weekdayIndex = date('w',$timestamp);
$timeTips = $weekdays[$weekdayIndex];
}
foreach ($intervals as $key => $interval){
$orderAppoint = $orderAppointLists[$date] ?? [];
$serverTime = $serverTimeLists[$date] ?? [];
$serverTimeBusy = $serverTimeBusyLists[$date]?? [];
// $intervalsLists[$date][$key]['status'] = 0;
//空闲时间
if(in_array($interval['time'],$serverTime) || empty($serverTime)){
$intervals[$key]['status'] = 1;
}
//忙
if(in_array($interval['time'],$serverTimeBusy)){
$intervals[$key]['status'] = 2;
}
//已预约
if(in_array($interval['time'],$orderAppoint)){
$intervals[$key]['status'] = 3;
}
}
$timeLists[] = [
'time_date' => $date,
'time_tips' => $timeTips,
'time_lists' => $intervals
];
}
return $timeLists;
}catch (\think\Exception $e){
return $e->getMessage();
}
}
}