初始版本

This commit is contained in:
贾祥聪
2025-08-19 14:16:51 +08:00
commit f937a1f9b9
4373 changed files with 359728 additions and 0 deletions

View File

@@ -0,0 +1,710 @@
<?php
namespace app\coachapi\logic;
use app\common\enum\coach\CoachEnum;
use app\common\enum\coach\CoachUserEnum;
use app\common\enum\DefaultEnum;
use app\common\enum\notice\NoticeEnum;
use app\common\enum\OrderEnum;
use app\common\enum\PayEnum;
use app\common\enum\shop\ShopEnum;
use app\common\model\coach\Coach;
use app\common\model\coach\CoachGoodsIndex;
use app\common\model\coach\CoachLifePhoto;
use app\common\model\coach\CoachServerTime;
use app\common\model\coach\CoachUpdate;
use app\common\model\coach\CoachUser;
use app\common\model\goods\Goods;
use app\common\model\order\Order;
use app\common\model\shop\ShopCoachApply;
use app\common\model\skill\Skill;
use app\common\service\ConfigService;
use app\common\service\FileService;
use app\common\service\sms\SmsDriver;
use app\common\service\TencentMapKeyService;
use Exception;
use think\facade\Db;
/**
* 技师逻辑类
* Class CoachLogic
* @package app\coachapi\logic
*/
class CoachLogic
{
/**
* @notes 个人中心
* @return array
* @author cjhao
* @date 2024/8/23 16:09
*/
public function center($coachInfo)
{
$coachUser = CoachUser::where(['id'=>$coachInfo['coach_user_id']])->field('sn,avatar')->findOrEmpty();
$coachUser->location = '';
$coachUser->work_status = '';
$coach = Coach::where(['coach_user_id'=>$coachInfo['coach_user_id']])
->order('id desc')
->append(['province_name','city_name','region_name','region_desc'])
->field('id,shop_id,province_id,city_id,region_id,longitude,latitude,longitude_location,latitude_location,name,sn,work_photo,money,deposit,work_status,location,work_photo,audit_status,audit_remark')
->findOrEmpty();
$coachUser->audit_status = $coach->audit_status;
$coachUser->audit_remark = $coach->audit_remark;
$coachUser->avatar = $coach->work_photo ? : $coachUser->avatar;
$todayDate = date('Y-m-d',time());
//今天的时间戳
$todayStart = strtotime($todayDate);
$todayEnd = strtotime($todayDate . ' 23:59:59');
//明天的时间戳
$tomorrowStart = strtotime('+1 day',$todayStart);
$tomorrowEnd = strtotime('+1 day',$todayEnd);
//后天的时间戳
$dayAfterTomorrowStart = strtotime('+2 day',$todayStart);
$dayAfterTomorrowEnd = strtotime('+2 day',$todayEnd);
$todayOrderCount = Order::where(['coach_id'=>$coachInfo['coach_id'],'pay_status'=>PayEnum::ISPAID])
->whereNotIn('order_status',[OrderEnum::ORDER_STATUS_WAIT_PAY,OrderEnum::ORDER_STATUS_WAIT_RECEIVING,OrderEnum::ORDER_STATUS_CLOSE,OrderEnum::ORDER_STATUS_SERVER_FINISH])
->whereBetweenTime('appoint_time',$todayStart,$todayEnd)
->count();
$tomorrowOrderCount = Order::where(['coach_id'=>$coachInfo['coach_id'],'pay_status'=>PayEnum::ISPAID])
->whereNotIn('order_status',[OrderEnum::ORDER_STATUS_WAIT_PAY,OrderEnum::ORDER_STATUS_WAIT_RECEIVING,OrderEnum::ORDER_STATUS_CLOSE,OrderEnum::ORDER_STATUS_SERVER_FINISH])
->whereBetweenTime('appoint_time',$tomorrowStart,$tomorrowEnd)
->count();
$afterTomorrowOrderCount = Order::where(['coach_id'=>$coachInfo['coach_id'],'pay_status'=>PayEnum::ISPAID])
->whereNotIn('order_status',[OrderEnum::ORDER_STATUS_WAIT_PAY,OrderEnum::ORDER_STATUS_WAIT_RECEIVING,OrderEnum::ORDER_STATUS_CLOSE,OrderEnum::ORDER_STATUS_SERVER_FINISH])
->whereBetweenTime('appoint_time',$dayAfterTomorrowStart,$dayAfterTomorrowEnd)
->count();
$location = $coach->location;
$location['longitude'] = $coach->longitude_location;
$location['latitude'] = $coach->latitude_location;
if(CoachEnum::AUDIT_STATUS_PASS == $coach->audit_status){
$coachUser->work_status = $coach->work_status;
$coachUser->work_photo = $coach->work_photo;
$coachUser->location = $location;
}
$coachUser->today_order_count = $todayOrderCount;
$coachUser->tomoroow_order_count = $tomorrowOrderCount;
$coachUser->after_tomoroow_order_count = $afterTomorrowOrderCount;
if(!$coach->isEmpty()){
$coachUser->name = $coach->name;
$coachUser->sn = $coach->sn;
}
$coachUser->money = $coach->money;
$coachUser->deposit = $coach->deposit;
$coachUser->province_id = $coach->province_id;
$coachUser->city_id = $coach->city_id;
$coachUser->region_id = $coach->region_id;
$coachUser->province_name = $coach->province_name;
$coachUser->city_name = $coach->city_name;
$coachUser->region_name = $coach->region_name;
$coachUser->region_desc = $coach->region_desc;
$coachUser->longitude = $coach->longitude;
$coachUser->latitude = $coach->latitude;
$coachUser->id = $coach->id;
$coachUser->shop_id = $coach->shop_id;
if(!$coach->shop_id){
$apply = ShopCoachApply::where(['coach_id'=>$coach['id']])
->order('id desc')
->field('shop_id,id,type,audit_status')
->findOrEmpty();
if(!$apply->isEmpty()){
$shopId = $apply['shop_id'];
//申请商家取消;退出商家同意;退出商家取消
if((1 == $apply['type'] && 3 == $apply['audit_status']) || (2 == $apply['type'] && 1 == $apply['audit_status']) || (2 == $apply['type'] && 1 == $apply['audit_status'])){
$shopId = 0;
}
$coachUser->shop_id = $shopId;
}
}
//服务项目
$coachUser->server_count = CoachGoodsIndex::where(['coach_id'=>$coachInfo['coach_id']])->count();
return $coachUser->toArray();
}
/**
* @notes 获取详情接口
* @param $coachId
* @return Coach|array|\think\Model
* @author cjhao
* @date 2024/11/23 18:15
*/
public function info($coachId)
{
$detail = Coach::where(['id'=>$coachId,'audit_status'=>CoachEnum::AUDIT_STATUS_PASS])
->field('id,name,id_card,id_card_front,portrait_shooting,id_card_back,work_photo')
->findOrEmpty()->toArray();
if(empty($detail)){
$detail = [
'id' => $coachId,
'name' => '',
'id_card' => '',
'id_card_back' => '',
'id_card_front' => '',
'work_photo' => ''
];
}
return $detail;
}
/**
* @notes 技能列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author cjhao
* @date 2024/8/20 18:02
*/
public function skillLists()
{
$skillLists = Skill::field('name,id')->select()->toArray();
// $goodsLists = Goods::alias('G')
// ->where(['G.status'=>1])
// ->append(['category_desc','skill_desc'])
// ->join('goods_skill_index GSI','G.id = GSI.goods_id')
// ->field('G.id,G.price,G.name,G.category_id,G.image,GSI.skill_id')
// ->select()->toArray();
// foreach ($skillLists as $key => $skill){
// $skillGoodsLists = [];
// foreach ($goodsLists as $goods){
// if($skill['id'] == $goods['skill_id']){
// $skillGoodsLists[] = $goods;
// }
// }
// $skillLists[$key]['goods_list'] = $skillGoodsLists;
// }
return $skillLists;
}
public function goodsLists(int $id)
{
$where[] = ['status','=',1];
if($id){
$goodsIds = CoachGoodsIndex::where(['skill_id'=>$id])->column('goods_id');
empty($goodsIds) && $goodsIds = [];
$where[] = ['id','in',implode(',',$goodsIds)];
}
$lists = Goods::where($where)
->field('id,name,price,image')
->select()
->toArray();
return $lists;
}
/**
* @notes 其他数据
* @return array
* @author cjhao
* @date 2024/8/21 18:16
*/
public function otherLists()
{
$educationLists = DefaultEnum::getEducationLists();
$nationLists = DefaultEnum::getNationLists();
return [
'education_lists' => $educationLists,
'nation_lists' => $nationLists,
];
}
/**
* @notes 申请技师
* @param array $params
* @return string|true
* @author cjhao
* @date 2024/8/23 17:24
*/
public function apply(array $params)
{
try {
Db::startTrans();
$coach = Coach::where(['coach_user_id'=>$params['coach_user_id']])
->order('id desc')
->findOrEmpty();
if( !$coach->isEmpty() && CoachEnum::AUDIT_STATUS_PASS == $coach->audit_status ){
return '当前账号已经入驻成功,请勿重复申请';
}
if( !$coach->isEmpty() && CoachEnum::AUDIT_STATUS_WAIT == $coach->audit_status ){
return '当前账号申请正在审核中,请耐心等待';
}
$coachUser = CoachUser::where(['id'=>$params['coach_user_id']])->findOrEmpty();
$coach = (new Coach());
$coach->name = $params['name'];
$coach->sn = $coachUser->sn;
$coach->gender = $params['gender'];
$coach->age = $params['age'];
$coach->id_card = $params['id_card'];
$coach->mobile = $coachUser['account'];
$coach->education = $params['education'];
$coach->nation = $params['nation'];
$coach->province_id = $params['province_id'];
$coach->city_id = $params['city_id'];
$coach->region_id = $params['region_id'];
$coach->address_detail = $params['address_detail'];
$coach->coach_user_id = $params['coach_user_id'];
$coach->skill_id = $params['skill_id'];
$coach->id_card_front = $params['id_card_front'];
$coach->id_card_back = $params['id_card_back'];
$coach->portrait_shooting = $params['portrait_shooting'];
$coach->work_photo = $params['work_photo'];
$coach->certification = $params['certification'] ?? '';
$coach->health_certificate = $params['health_certificate'] ?? '';
$coach->work_status = $params['work_status'];
$coach->server_status = $params['server_status'];
$coach->longitude = $params['longitude'] ?? '';
$coach->latitude = $params['latitude'] ?? '';
$coach->save();
//生活照
$lifePhoto = $params['life_photo'] ?? [];
$lifePhotoLists = [];
foreach ($lifePhoto as $photo){
$lifePhotoLists[] = [
'uri' => $photo,
'coach_id' => $coach->id
];
}
(new CoachLifePhoto())->saveAll($lifePhotoLists);
//关联服务
$goodsIds = $params['goods_ids'] ?? [];
$goodsLists = [];
foreach ($goodsIds as $goodsId)
{
$goodsLists[] = [
'goods_id' => $goodsId,
'coach_id' => $coach->id,
];
}
(new CoachGoodsIndex())->saveAll($goodsLists);
$mobile = ConfigService::get('platform', 'mobile','');
if($mobile){
//入驻申请通知-平台
event('Notice', [
'scene_id' => NoticeEnum::STAFF_APPLY_NOTICE_PLATFORM,
'params' => [
'coach_id' => $coach['id'],
'mobile' => $mobile
]
]);
}
Db::commit();
return true;
}catch (Exception $e){
Db::rollback();
return $e->getMessage();
}
}
/**
* @notes 详情
* @param int $id
* @return array
* @author cjhao
* @date 2024/8/21 15:30
*/
public function detail(int $id)
{
$coach = (new Coach())
->where(['coach_user_id'=>$id])
->append(['life_photo','region_desc','province_name','city_name','region_name'])
->withoutField('create_time,update_time,delete_time')
->order('id desc')
->findOrEmpty()
->toArray();
if($coach){
$coach['skill_name'] = Skill::where(['id'=>$coach['skill_id']])->value('name');
$goodsLists = Goods::alias('G')
->where(['CGI.coach_id'=>$coach['id']])
->join('coach_goods_index CGI','G.id=CGI.goods_id')
->field('G.id,G.name,G.image')
->select()->toArray();
$coach['goods_lists'] = $goodsLists;
}else{
$coach['audit_status'] = '';
}
return $coach;
}
/**
* @notes 获取更新资料接口
* @param $id
* @return array
* @author cjhao
* @date 2024/11/26 17:36
*/
public function updateInfoDetail($coachUserId)
{
$detail = CoachUpdate::where(['coach_user_id'=>$coachUserId])
->append(['province_name','city_name','region_name','region_desc','goods_lists'])
->order('id desc')->findOrEmpty()->toArray();
if(empty($detail)){
$detail = $this->detail($coachUserId);
$detail['audit_status'] = '';
$detail['audit_remark'] = '';
$goodsLists = $detail['goods_lists'] ?? [];
$lifePhoto = $detail['life_photo']->toArray() ?? [];
if($goodsLists) {
$detail['goods_ids'] = array_column($goodsLists,'id');
}
if($lifePhoto) {
$detail['life_photo'] = array_column($lifePhoto,'uri');
}
}
if($detail['goods_ids']){
foreach ($detail['goods_ids'] as $key => $val){
$detail['goods_ids'][$key] = intval($val);
}
}
return $detail;
}
/**
* @notes 技师资料更新
* @param array $params
* @return string|true
* @author cjhao
* @date 2024/8/28 10:16
*/
public function updateInfo(array $params)
{
try {
if(empty($params['coach_user_id'])){
throw new Exception('您技师申请在审核中,暂时不允许修改资料');
}
// Db::startTrans();
$coachUpdate = CoachUpdate::where(['coach_id'=>$params['coach_id'],'audit_status'=>CoachEnum::AUDIT_STATUS_WAIT])
->findOrEmpty();
if(!$coachUpdate->isEmpty()){
throw new Exception( '您提交资料正在审核中,请勿重复提交');
}
$coachUpdate = (new CoachUpdate());
$coachUpdate->coach_id = $params['coach_id'];
$coachUpdate->coach_user_id = $params['coach_user_id'];
$coachUpdate->name = $params['name'];
$coachUpdate->gender = $params['gender'];
$coachUpdate->age = $params['age'];
$coachUpdate->id_card = $params['id_card'];
$coachUpdate->mobile = $params['mobile'];
$coachUpdate->education = $params['education'];
$coachUpdate->nation = $params['nation'];
$coachUpdate->province_id = $params['province_id'];
$coachUpdate->city_id = $params['city_id'];
$coachUpdate->region_id = $params['region_id'];
$coachUpdate->longitude = $params['longitude'];
$coachUpdate->latitude = $params['latitude'];
$coachUpdate->address_detail = $params['address_detail'];
$coachUpdate->skill_id = $params['skill_id'];
$coachUpdate->id_card_front = $params['id_card_front'];
$coachUpdate->id_card_back = $params['id_card_back'];
$coachUpdate->portrait_shooting = $params['portrait_shooting'];
$coachUpdate->work_photo = $params['work_photo'];
$coachUpdate->certification = $params['certification'] ?? '';
$coachUpdate->health_certificate = $params['health_certificate'] ?? '';
// $coachUpdate->work_status = $params['work_status'];
// $coachUpdate->server_status = $params['server_status'];
$lifePhoto = $params['life_photo'] ?? [];
foreach ($lifePhoto as $key => $photo){
$lifePhoto[$key] = FileService::setFileUrl($photo);
}
$lifePhoto = implode(',',$lifePhoto);
$goodsIds = $params['goods_ids'] ?? [];
$goodsIds = implode(',',$goodsIds);
$coachUpdate->life_photo = $lifePhoto;
$coachUpdate->goods_ids = $goodsIds;
$coachUpdate->save();
// Db::commit();
return true;
} catch (Exception $e) {
// Db::rollback();
return $e->getMessage();
}
}
/**
* @notes 地址解析(地址转坐标)
* @param $get
* @return array|mixed
* @author ljj
* @date 2022/10/13 12:06 下午
* 本接口提供由文字地址到经纬度的转换能力,并同时提供结构化的省市区地址信息。
*/
public static function updateLocation($get,$coachId)
{
try {
$coach = Coach::where(['id'=>$coachId])->findOrEmpty();
if($coach->isEmpty()){
return true;
// throw new Exception('请先申请成为技师');
}
$key = (new TencentMapKeyService())->getTencentMapKey();
if ($key == '') {
throw new Exception('请联系管理员检查腾讯地图配置');
}
$data = [
'key' => $key,
'location' => $get['latitude'].','.$get['longitude']
];
$query = http_build_query($data);
$url = 'https://apis.map.qq.com/ws/geocoder/v1/';
$result = json_decode(file_get_contents($url.'?'.$query),true);
if ($result['status'] !== 0) {
$check = (new TencentMapKeyService())->checkResult($result);
while (!$check) {
$data['key'] = (new TencentMapKeyService())->getTencentMapKey(true);
if (empty($data['key'])) {
break;
}
$query = http_build_query($data);
$result = json_decode(file_get_contents($url . '?' . $query), true);
$check = (new TencentMapKeyService())->checkResult($result);
}
}
$data = $result['result']['address_component'];
$coach->longitude_location = $get['longitude'];
$coach->latitude_location = $get['latitude'];
$coach->location = $data;
$coach->save();
return true;
}catch (\Exception $e){
return $e->getMessage();
}
}
/**
* @notes 更新工作状态
* @param $coachId
* @return true
* @author cjhao
* @date 2024/8/29 16:33
*/
public function updateWorkStatus($coachId)
{
$coach = Coach::where(['id'=>$coachId])->findOrEmpty();
$coach->work_status = $coach->work_status ? 0 : 1;
$coach->save();
return true;
}
/**
* @notes 获取技师服务时间
* @param $coachId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author cjhao
* @date 2024/9/2 16:15
*/
public function getServerTime($coachId)
{
$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($coachId);
//师傅空闲时间
$serverTimeLists = [];
//师傅忙碌时间
$serverTimeBusyLists = [];
$coachServerTime = CoachServerTime::where(['coach_id'=>$coachId])
->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;
}
/**
* @notes 设置服务时间
* @param $params
* @param $coachId
* @return string|true
* @author cjhao
* @date 2024/9/3 16:39
*/
public function setServerTime($params,$coachId)
{
try {
Db::startTrans();
$serverTime = [];
foreach ($params['server_time'] as $key => $time){
$times = [];
foreach ($time['time_lists'] as $timeList){
if(in_array($timeList['time'],$times)){
continue;
}
$serverTime[] = [
'coach_id' => $coachId,
'date' => $time['time_date'],
'time' => $timeList['time'],
'status' => $timeList['status']
];
$times[] = $timeList['time'];
}
}
CoachServerTime::where(['coach_id'=>$coachId])->delete();
(new CoachServerTime())->saveAll($serverTime);
Db::commit();
return true;
} catch (Exception $e) {
Db::rollback();
return $e->getMessage();
}
}
/**
* @notes 个人资料
* @param $coachId
* @return array
* @author ljj
* @date 2024/12/3 下午3:47
*/
public function personalData($coachId)
{
$detail = Coach::alias('c')
->join('coach_user cu', 'cu.id = c.coach_user_id')
->where(['c.id'=>$coachId])
->field('c.id,cu.sn,c.work_photo,c.name,c.gender,c.age,c.mobile,c.introduction')
->findOrEmpty()
->toArray();
$detail['gender_desc'] = CoachUserEnum::getSexDesc($detail['gender']);
return $detail;
}
/**
* @notes 设置个人资料
* @param $params
* @return bool
* @author ljj
* @date 2024/12/3 下午3:57
*/
public static function setPersonalData($params): bool
{
Coach::update(['id' => $params['coach_id'], $params['field'] => $params['value']]);
return true;
}
/**
* @notes 绑定手机号
* @param $params
* @return bool
* @author Tab
* @date 2021/8/25 17:55
*/
public static function bindMobile($params)
{
try {
if (empty($params['mobile']) || empty($params['code'])) {
throw new \Exception('请输入手机号和验证码');
}
$smsDriver = new SmsDriver();
$result = $smsDriver->verify($params['mobile'], $params['code']);
if(!$result) {
throw new \Exception('验证码错误');
}
$user = Coach::where(['mobile'=>$params['mobile']])->where('id','<>',$params['coach_id'])->findOrEmpty();
if(!$user->isEmpty()) {
throw new \Exception('该手机号已被其他账号绑定');
}
$user = CoachUpdate::where(['coach_id'=>$params['coach_id']])->where(['mobile'=>$params['mobile'],'audit_status'=>ShopEnum::AUDIT_STATUS_WAIT])->where('id','<>',$params['coach_id'])->findOrEmpty();
if(!$user->isEmpty()) {
throw new \Exception('该手机号已被其他账号绑定');
}
unset($params['code']);
$coach = Coach::findOrEmpty($params['coach_id'])->toArray();
Coach::update(['mobile'=>$params['mobile'],'id'=>$params['coach_id']]);
CoachUser::update(['account'=>$params['mobile'],'id'=>$coach['coach_user_id']]);
return true;
} catch (\Exception $e) {
return $e->getMessage();
}
}
}