初始版本

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,619 @@
<?php
namespace app\adminapi\logic\coach;
use app\common\enum\accountLog\CoachAccountLogEnum;
use app\common\enum\coach\CoachEnum;
use app\common\enum\DefaultEnum;
use app\common\enum\notice\NoticeEnum;
use app\common\enum\shop\ShopEnum;
use app\common\logic\CoachAccountLogLogic;
use app\common\model\coach\Coach;
use app\common\model\coach\CoachGoodsIndex;
use app\common\model\coach\CoachLifePhoto;
use app\common\model\coach\CoachUpdate;
use app\common\model\coach\CoachUser;
use app\common\model\goods\Goods;
use app\common\model\skill\Skill;
use app\common\service\ConfigService;
use app\common\service\FileService;
use think\Exception;
use think\facade\Config;
use think\facade\Db;
/**
* 技师逻辑类
* Class CoachLogic
* @package app\adminapi\logic\coach
*/
class CoachLogic
{
/**
* @notes 技师账号列表
* @return CoachUser[]|array|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author cjhao
* @date 2024/8/20 17:33
*/
public function coachUserLists()
{
$coachUserIds = Coach::where('audit_status','<>',CoachEnum::AUDIT_STATUS_PASS)->column('coach_user_id');
return CoachUser::where('id','not in',$coachUserIds)->field('id,sn,account')->select()->toArray();
}
/**
* @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($shopId = 0)
{
$whereShop = [];
if($shopId){
$whereShop[] = ['G.shop_id','in',[0,$shopId]];
}else{
$whereShop[] = ['G.shop_id','=',0];
}
$skillLists = Skill::field('name,id')->select()->toArray();
$goodsLists = Goods::alias('G')
->where(['G.status'=>1,'G.audit_status'=>1])
->where($whereShop)
->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;
}
/**
* @notes 添加
* @param array $params
* @return string|true
* @throws \Exception
* @author cjhao
* @date 2024/8/21 15:49
*/
public function add(array $params)
{
try {
Db::startTrans();
$number = CoachUser::count()+1;
$sn = sprintf("%03d", $number);
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['mobile'], $passwordSalt);
$avatar = ConfigService::get('default_image', 'user_avatar');
$coachUser = CoachUser::where(['account'=>$params['mobile']])->findOrEmpty();
if(!$coachUser->isEmpty()){
throw new Exception('手机号码已重复');
}
$coachUser = CoachUpdate::where(['mobile'=>$params['mobile'],'audit_status'=>CoachEnum::AUDIT_STATUS_WAIT])->findOrEmpty();
if(!$coachUser->isEmpty()){
throw new Exception('手机号码已重复');
}
$coachUser = CoachUser::create([
'sn' => $sn,
'avatar' => $avatar,
'account' => $params['mobile'],
'password' => $password,
// 'channel' => $params['channel'],
]);
$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 = $params['mobile'];
$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->longitude = $params['longitude'];
$coach->latitude = $params['latitude'];
$coach->address_detail = $params['address_detail'];
$coach->coach_user_id = $coachUser->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->audit_status = CoachEnum::AUDIT_STATUS_PASS;
$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);
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(['id'=>$id])
->append(['province_name','city_name','region_name','region_desc','life_photo'])
->withoutField('create_time,update_time,delete_time')
->findOrEmpty()
->toArray();
$goodsLists = Goods::alias('G')
->where(['CGI.coach_id'=>$id])
->join('coach_goods_index CGI','G.id=CGI.goods_id')
->field('G.id,G.name,G.image')
->select()->toArray();
$coach['life_photo'] = array_column($coach['life_photo']->toArray(),'uri');
$coach['goods_lists'] = $goodsLists;
$coach['goods_ids'] = array_column($goodsLists,'id');
return $coach;
}
/**
* @notes 编辑
* @param array $params
* @return string|true
* @throws \Exception
* @author cjhao
* @date 2024/8/21 15:50
*/
public function edit(array $params)
{
try {
Db::startTrans();
$coach = Coach::find($params['id']);
$coachUser = CoachUser::where('id','<>',$coach['coach_user_id'])
->where('account','=',$params['mobile'])
->findOrEmpty();
if(!$coachUser->isEmpty()){
throw new Exception('手机号码已存在');
}
$coachUser = CoachUpdate::where('mobile','=',$params['mobile'])
->where(['audit_status'=>ShopEnum::AUDIT_STATUS_WAIT])
->where('coach_user_id','<>',$coach['coach_user_id'])
->findOrEmpty();
if(!$coachUser->isEmpty()){
throw new Exception('手机号码已存在');
}
CoachUser::update(['account'=>$params['mobile']],['id'=>$coach['coach_user_id']]);
$coach->name = $params['name'];
$coach->gender = $params['gender'];
$coach->age = $params['age'];
$coach->id_card = $params['id_card'];
$coach->mobile = $params['mobile'];
$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->longitude = $params['longitude'];
$coach->latitude = $params['latitude'];
$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'] ?? '';
$workStatus = $params['work_status'];
if( 0 == $params['server_status']){
$workStatus = 0;
}
$coach->work_status = $workStatus;
$coach->server_status = $params['server_status'];
$coach->save();
(new CoachLifePhoto())->where(['coach_id'=>$params['id']])->delete();
(new CoachGoodsIndex())->where(['coach_id'=>$params['id']])->delete();
//生活照
$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);
Db::commit();
return true;
}catch (Exception $e){
Db::rollback();
return $e->getMessage();
}
}
/**
* @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 15:04
*/
public function adjustMoney(array $params)
{
try {
Db::startTrans();
$coach = Coach::where(['id'=>$params['id']])->findOrEmpty();
if($coach->isEmpty()){
return '技师不存在';
}
$action = $params['action'];
if(1 == $action){
$coach->money = $coach->money+$params['money'];
$coach->save();
CoachAccountLogLogic::add(
$coach->id,
CoachAccountLogEnum::MONEY,
CoachAccountLogEnum::ADMIN_INC_MONEY,
$action,
$params['money'],
'',
$params['admin_id']
);
}else{
if($params['money'] > $coach->money){
return '当前技师佣金仅剩:'.$coach->money;
}
$coach->money = $coach->money - $params['money'];
$coach->save();
CoachAccountLogLogic::add(
$coach->id,
CoachAccountLogEnum::MONEY,
CoachAccountLogEnum::ADMIN_DEC_MONEY,
2,
$params['money'],
'',
$params['admin_id']
);
}
Db::commit();
return true;
}catch (Exception $e){
Db::rollback();
return $e->getMessage();
}
}
/**
* @notes 调整佣金
* @param array $params
* @return string|true
* @author cjhao
* @date 2024/8/23 15:04
*/
public function adjustDeposit(array $params)
{
try {
Db::startTrans();
$coach = Coach::where(['id'=>$params['id']])->findOrEmpty();
if($coach->isEmpty()){
return '技师不存在';
}
$action = $params['action'];
if(1 == $action){
$coach->deposit = $coach->deposit+$params['money'];
$coach->save();
CoachAccountLogLogic::add(
$coach->id,
CoachAccountLogEnum::DEPOSIT,
CoachAccountLogEnum::ADMIN_INC_DEPOSIT,
$action,
$params['money'],
'',
$params['admin_id']
);
}else{
if($params['money'] > $coach->deposit){
return '当前技师保证金仅剩:'.$coach->deposit;
}
$coach->deposit = $coach->deposit - $params['money'];
$coach->save();
CoachAccountLogLogic::add(
$coach->id,
CoachAccountLogEnum::DEPOSIT,
CoachAccountLogEnum::ADMIN_DEC_DEPOSIT,
$action,
$params['money'],
'',
$params['admin_id']
);
}
Db::commit();
return true;
}catch (Exception $e){
Db::rollback();
return $e->getMessage();
}
}
/***
* @notes 技师审核
* @param $params
* @return string|true
* @author cjhao
* @date 2024/8/23 18:47
*/
public function coachAudit($params)
{
try {
// Db::startTrans();
$coach = Coach::where(['id'=>$params['id']])->findOrEmpty();
if($coach->isEmpty()){
return '技师不存在';
}
if(CoachEnum::AUDIT_STATUS_WAIT != $coach->audit_status){
return '技师的审核状态已改变,请刷新页面';
}
$status = CoachEnum::AUDIT_STATUS_PASS;
if(0 == $params['audit_status']){
$status = CoachEnum::AUDIT_STATUS_REFUSE;
}
$coach->audit_status = $status;
$coach->audit_remark = $params['audit_remark'] ?? '';
$coach->save();
if(CoachEnum::AUDIT_STATUS_PASS == $status){
//入驻申请成功通知-师傅
event('Notice', [
'scene_id' => NoticeEnum::APPLY_SUCCESS_NOTICE_STAFF,
'params' => [
'coach_id' => $coach['id'],
]
]);
}else{
//入驻申请失败通知-师傅
event('Notice', [
'scene_id' => NoticeEnum::APPLY_FAIL_NOTICE_STAFF,
'params' => [
'coach_id' => $coach['id'],
]
]);
}
return true;
}catch (Exception $e){
// Db::rollback();
return $e->getMessage();
}
}
/**
* @notes 获取技师详情
* @param int $id
* @return array
* @author cjhao
* @date 2024/8/28 11:28
*/
public function updateDetail(int $id)
{
$updateCoach = (new CoachUpdate())
->where(['id'=>$id])
->append(['goods_lists'])
->withoutField('create_time,update_time,delete_time')
->findOrEmpty()
->toArray();
$coach = (new Coach())
->where(['id'=>$updateCoach['coach_id'],'audit_status'=>CoachEnum::AUDIT_STATUS_PASS])
->append(['life_photo','goods_ids'])
->withoutField('create_time,update_time,delete_time')
->findOrEmpty()
->toArray();
foreach ($updateCoach as $key => $update){
$cancel = ['id','goods_lists','coach_id','coach_user_id'];
if(in_array($key,$cancel)){
continue;
}
$data = $coach[$key] ?? '';
$updateCoach[$key.'_update'] = $update != $data ?: false;
if('goods_ids' == $key && $data){
$goodsIds = array_column($data->toArray(),'goods_id');
sort($goodsIds);
sort($update);
$updateCoach[$key.'_update'] = $update != $goodsIds ?: false;
}
if('life_photo' == $key && $data){
$uriLists = array_column($data->toArray(),'uri');
foreach ($uriLists as $uriKey => $uri){
$uriLists[$uriKey] = FileService::setFileUrl($uri);
}
sort($uriLists);
sort($update);
$updateCoach[$key.'_update'] = $uriLists != $update ?: false;
}
}
return $updateCoach;
}
/**
* @notes 技师资料审核
* @param $params
* @return string|true
* @throws \Exception
* @author cjhao
* @date 2024/8/28 11:31
*/
public function updateAudit($params)
{
try {
Db::startTrans();
$coachUpdate = CoachUpdate::where(['id'=>$params['id']])->findOrEmpty();
if($coachUpdate->isEmpty()){
return '申请记录不存在';
}
if(CoachEnum::AUDIT_STATUS_WAIT != $coachUpdate->audit_status){
return '申请的审核状态已改变,请刷新页面';
}
$status = CoachEnum::AUDIT_STATUS_PASS;
if(0 == $params['audit_status']){
$status = CoachEnum::AUDIT_STATUS_REFUSE;
}
$coachUpdate->audit_status = $status;
$coachUpdate->audit_remark = $params['audit_remark'] ?? '';
$coachUpdate->save();
if(0 == $params['audit_status']){
Db::commit();
return true;
}
$coach = Coach::where(['id'=>$coachUpdate->coach_id])->findOrEmpty();
$coach->name = $coachUpdate->name;
$coach->gender = $coachUpdate->gender;
$coach->age = $coachUpdate->age;
$coach->id_card = $coachUpdate->id_card;
// $coach->mobile = $coachUpdate->mobile;
$coach->education = $coachUpdate->education;
$coach->nation = $coachUpdate->nation;
$coach->province_id = $coachUpdate->province_id;
$coach->city_id = $coachUpdate->city_id;
$coach->region_id = $coachUpdate->region_id;
$coach->latitude = $coachUpdate->latitude;
$coach->longitude = $coachUpdate->longitude;
$coach->address_detail = $coachUpdate->address_detail;
$coach->coach_user_id = $coachUpdate->coach_user_id;
$coach->skill_id = $coachUpdate->skill_id;
$coach->id_card_front = $coachUpdate->id_card_front;
$coach->id_card_back = $coachUpdate->id_card_back;
$coach->portrait_shooting = $coachUpdate->portrait_shooting;
// $coach->work_photo = $coachUpdate->work_photo;
$coach->certification = $coachUpdate->certification;
$coach->health_certificate = $coachUpdate->health_certificate;
// $coach->work_status = $coachUpdate->work_status;
// $coach->server_status = $coachUpdate->server_status;
$coach->save();
(new CoachLifePhoto())->where(['coach_id'=>$coach->id])->delete();
(new CoachGoodsIndex())->where(['coach_id'=>$coach->id])->delete();
//生活照
$lifePhoto = $coachUpdate->life_photo;
$lifePhotoLists = [];
foreach ($lifePhoto as $photo){
$lifePhotoLists[] = [
'uri' => $photo,
'coach_id' => $coach->id
];
}
(new CoachLifePhoto())->saveAll($lifePhotoLists);
//关联服务
$goodsIds = $coachUpdate->goods_ids ;
$goodsLists = [];
foreach ($goodsIds as $goodsId)
{
$goodsLists[] = [
'goods_id' => $goodsId,
'coach_id' => $coach->id,
];
}
(new CoachGoodsIndex())->saveAll($goodsLists);
Db::commit();
return true;
}catch (Exception $e){
Db::rollback();
return $e->getMessage();
}
}
}