| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362 |
- <?php
- // +----------------------------------------------------------------------
- // | likeadmin快速开发前后端分离管理后台(PHP版)
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
- // | github下载:https://github.com/likeshop-github/likeadmin
- // | 访问官网:https://www.likeadmin.cn
- // | likeadmin团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeadminTeam
- // +----------------------------------------------------------------------
- namespace app\adminapi\logic\works;
- use app\adminapi\logic\spare_part\SparePartLogic;
- use app\adminapi\logic\user\UserLogic;
- use app\api\logic\ServiceOrderLogic;
- use app\api\logic\UserCouponLogic;
- use app\common\enum\ThirdTypeEnum;
- use app\common\enum\worker\WorkerAccountLogEnum;
- use app\common\logic\ThirdOrderLogic;
- use app\common\logic\WorkerAccountLogLogic;
- use app\common\model\third\ThirdOrders;
- use app\common\model\works\ServiceWorkDeterminedPrice;
- use app\workerapi\logic\MasterWorkerLogic;
- use think\Exception;
- use think\db\Query;
- use think\facade\Db;
- use think\facade\Log;
- use app\common\logic\BaseLogic;
- use app\api\logic\PerformanceLogic;
- use app\common\model\dict\DictData;
- use app\common\model\coupon\UserCoupon;
- use app\common\model\works\ServiceWork;
- use app\common\model\recharge\OrderGoods;
- use app\common\model\spare_part\SparePart;
- use app\common\model\works\ServiceWorkLog;
- use app\common\model\coupon\CouponCategory;
- use app\common\model\recharge\RechargeOrder;
- use app\common\model\works\ServiceWorkSpare;
- use app\workerapi\logic\ServiceWorkLogLogic;
- use app\common\model\orders\OrderEffectiveLog;
- use app\common\model\master_worker\MasterWorker;
- use app\common\model\training\TrainingWorkerTask;
- use app\common\model\performance\PerformanceRules;
- use app\common\model\works\ServiceWorkCustomerLog;
- use app\workerapi\logic\ServiceWorkReceiveLogLogic;
- use app\common\model\master_worker\MasterWorkerTeam;
- use app\common\model\works\ServiceWorkAppointmentLog;
- use app\workerapi\logic\ServiceWorkerAllocateWorkerLogic;
- use app\common\model\master_worker\MasterWorkerAccountLog;
- use app\common\model\master_worker\MasterWorkerRetentionMoneyLog;
- use app\common\model\master_commission\MasterWorkerCommissionRatio;
- use app\common\model\master_commission\MasterWorkerCommissionConfig;
- use app\common\model\service_area\ServiceArea;
- /**
- * ServiceWork逻辑
- * Class ServiceWorkLogic
- * @package app\adminapi\logic\works
- */
- class ServiceWorkLogic extends BaseLogic
- {
- /**
- * @notes 编辑
- * @param array $params
- * @return bool
- * @author likeadmin
- * @date 2024/07/10 18:17
- */
- public static function edit(array $params): bool
- {
- Db::startTrans();
- try {
- $userId = UserLogic::getUserIdByMobile($params['mobile']);
- $service_area_id = ServiceArea::serviceAreaId(['lon' => $params['lon'], 'lat' => $params['lat']]);
- if($params['address'] && (empty($params['lon']) || empty($params['lat']))){
- $lon_lat = get_address_lat_lng($params['address']);
- $params['lon'] = $lon_lat['lon'];
- $params['lat'] = $lon_lat['lat'];
- }
- ServiceWork::where('id', $params['id'])->update([
- 'real_name' => $params['real_name'],
- 'mobile' => $params['mobile'],
- 'user_id' => $userId,
- 'address' => $params['address'],
- 'appointment_time' => strtotime($params['appointment_time']),
- 'estimated_finish_time' => $params['estimated_finish_time'],
- 'lon' => $params['lon'],
- 'lat' => $params['lat'],
- 'service_area_id' => $service_area_id,
- ]);
- \app\common\model\orders\RechargeOrder::where('work_id',$params['id'])->update(['user_id' => $userId]);
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- *
- * @return false|void
- */
- public static function pickWork($params)
- {
- Db::startTrans();
- try {
- $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- $receive_time = time();
- $work->work_status = 2;//待联系
- $work->service_status = 1;//服务中
- $work->receive_time = $receive_time;
- $work->save();
- //添加变更日志
- $work_log = [
- 'work_id'=>$work->id,
- 'master_worker_id'=>$work->master_worker_id,
- 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',$receive_time).'领取了工单',
- ];
- ServiceWorkLogLogic::add($work_log);
- //添加领单日志
- ServiceWorkReceiveLogLogic::add($work_log);
- Db::commit();
- // 工单状态通知外部平台 即工程师接单成功
- if($work->external_platform_id > 0){
- event('ExternalPlatform', [
- 'send_code' => 1001,
- 'params' => [
- 'work_id' => $work->id,
- ]
- ]);
- }
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 预约成功,等待上门
- * @return false|void
- */
- public static function appointWork($params)
- {
- Db::startTrans();
- try {
- $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- if($work->work_status != 2){
- throw new Exception('请勿重复点击');
- }
- //验证更改的预约时间必须是在领单时间内的半小内修改,否则不允许修改
- if(strtotime($work->appointment_time) != strtotime($params['appointment_time']) && (time()-strtotime($work->receive_time))>1800){
- throw new Exception('距离领单时间已超过半小时,无法修改预约时间,请联系客服');
- }
- $work->work_status = 3;//待上门
- $work->appointment_time = strtotime($params['appointment_time']);
- $work->save();
- //添加变更日志
- $work_log = [
- 'work_id'=>$work->id,
- 'master_worker_id'=>$work->master_worker_id,
- 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'联系了客户,确认了于'.$params['appointment_time'].$params['address'].'预约上门',
- ];
- ServiceWorkLogLogic::add($work_log);
- Db::commit();
- }
- catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 工程师确认上门
- * @param $params
- * @return false|void
- */
- public static function confirmDoor($params)
- {
- Db::startTrans();
- try {
- $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- if(!in_array($work->work_status,[2,3])){
- throw new Exception('禁止操作上门');
- }
- $work->finally_door_time = time();//最后上门时间
- $work->work_status = 4;//已上门
- $work->save();
- //添加变更日志
- $work_log = [
- 'work_id'=>$work->id,
- 'master_worker_id'=>$work->master_worker_id,
- 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'已上门',
- ];
- ServiceWorkLogLogic::add($work_log);
- Db::commit();
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 工程师确认报价单
- * @param $params
- * @return false|void
- */
- public static function confirmPrice($params)
- {
- Db::startTrans();
- try {
- $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- //搜索待支付订单
- $paid_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>1])->findOrEmpty()->toArray();
- if(empty($paid_order)){
- throw new Exception('订单错误');
- }
- if($work->work_status != 4){
- throw new Exception('请勿重复操作');
- }
- // 商城配件.计算配件总价 id company_price original_price offering_price number
- $spare_total = 0;
- $work->spare_total = 0;
- isset($params['spare_parts']) && $params['spare_parts'] && $params['spare_parts'] = json_decode($params['spare_parts'], true);
- $spare_parts_ids = ServiceWorkSpare::where('service_work_id',$work['id'])->column('id');
-
- if(isset($params['spare_parts']) && $params['spare_parts']){
- if (count($params['spare_parts']) > 10) {
- throw new Exception('商城配件最多10个');
- }
- $ids = array_column($params['spare_parts'],'id');
- $spare_parts = SparePart::where('id','in',$ids)->column('id as spare_part_id,spare_name,spare_image,company_price,original_price,offering_price,spare_number,spare_unit,brand');
- $spare_parts = array_column($spare_parts,null,'spare_part_id');
- foreach ($params['spare_parts'] as $k => $item){
- if (!isset($spare_parts[$item['id']])){
- throw new Exception('配件信息错误');
- }
- $spare_part = $spare_parts[$item['id']];
- $spare_total += $spare_part['company_price']*$item['spare_number'];
- $data = array_merge($spare_part, ['service_work_id' => $work['id'], 'spare_number' => $item['spare_number'] * $spare_part['spare_number']]);
- if (current($spare_parts_ids)) {
- //修改
- ServiceWorkSpare::where("id",current($spare_parts_ids))->update($data);
- } else {
- //新增
- ServiceWorkSpare::create($data);
- }
- array_shift($spare_parts_ids);
- }
- $work->spare_total += $spare_total;
- }
- //自选配件
- isset($params['self_spare_parts']) && $params['self_spare_parts'] && $params['self_spare_parts'] = json_decode($params['self_spare_parts'], true);
- if(isset($params['self_spare_parts']) && $params['self_spare_parts']){
- if (count($params['self_spare_parts']) > 10) {
- throw new Exception('自选配件最多10个');
- }
- foreach ($params['self_spare_parts'] as $item){
- $spare_total += $item['offering_price']*$item['spare_number'];
- $data = [
- 'service_work_id'=>$work['id'],
- 'spare_name' => $item['spare_name'],
- 'spare_image' => empty($item['spare_image']) ? '' : $item['spare_image'],
- 'offering_price' => $item['offering_price'],
- 'company_price' => 0,
- 'original_price' => 0,
- 'spare_number' => $item['spare_number'],
- 'spare_part_id' => 0,
- 'brand' => $item['brand'],
- 'status' => 0,
- ];
- if (current($spare_parts_ids)) {
- //修改
- ServiceWorkSpare::where("id",current($spare_parts_ids))->update($data);
- } else {
- //新增
- ServiceWorkSpare::create($data);
- }
- array_shift($spare_parts_ids);
- }
- $work->spare_total += $spare_total;
- }
- //删除多余的配件数据
- if ($spare_parts_ids) {
- ServiceWorkSpare::where('id','in',$spare_parts_ids)->delete();
- }
- // 添加工单尾款报价记录 - 即配件费用
- if(isset($params['price_content'])){
- !is_array($params['price_content']) && $params['price_content'] = json_decode($params['price_content'], true);
- ServiceWorkDeterminedPrice::where('work_id',$work->id)->delete();
- ServiceWorkDeterminedPrice::create([
- 'work_id'=>$work->id,
- 'content'=>$params['price_content']??[],
- ]);
- $work->spare_total += array_sum('price');
- }
- // order_amount 原 = $params['amount'] 修改为 = 配件总价 + 服务尾款
- $order_amount = $params['amount'] + $spare_total;
- //定金存在尾款结算功能,全款直接提交
- if($paid_order['payment_type']==1){
- $un_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>0])->findOrEmpty();
- if($un_order->isEmpty()){
- //新增待支付尾款
- $order_data = [
- 'order_type'=>$paid_order['order_type'],
- 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
- 'work_id'=>$paid_order['work_id'],
- 'user_id'=>$paid_order['user_id'],
- 'payment_type'=>2,
- 'order_total'=>$order_amount,
- //'order_amount'=>$params['amount'],
- 'order_amount'=>$order_amount,
- 'order_terminal'=>$paid_order['order_terminal']
- ];
- RechargeOrder::create($order_data);
- }else{
- //修改尾款信息
- $un_order->order_total = $order_amount;
- //$un_order->order_amount = $params['amount'];
- $un_order->order_amount = $order_amount;
- $un_order->save();
- }
- //更新服务费用
- $work->service_fee = $paid_order['paid_amount']+$params['amount'];
- }
- //总工单费用
- $work->work_total = $work->service_fee+$spare_total;
- $work->work_images = $params['work_images'];
- $work->explanation = $params['explanation']??'';
- //判断是否是第三个订单,如果是美团订单直接滤过用户确认环节
- if($work->third_type==1){
- $work->work_status = 5;
- $work->user_confirm_status = 2;
- }else{
- $work->user_confirm_status = 1;//待确认报价
- }
- if($work->external_platform_id > 0){
- $work->work_status = 5;
- $work->user_confirm_status = 2;
- }
- $work->price_approval = 0;
- $work->save();
- //添加变更日志
- $work_log = [
- 'work_id'=>$work->id,
- 'master_worker_id'=>$work->master_worker_id,
- 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'提交了报价单',
- ];
- ServiceWorkLogLogic::add($work_log);
- Db::commit();
- // 商家报价通知外部平台 即报价
- if($work->external_platform_id > 0){
- event('ExternalPlatform', [
- 'send_code' => 1006,
- 'params' => [
- 'work_id' => $work->id,
- ]
- ]);
- }
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 工程师修改自选配件的图片
- * @param $params
- * @return false|void
- */
- public static function selfSparePart($params)
- {
- Db::startTrans();
- try {
- $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- $audit = ServiceWorkSpare::where(['id' => $params['id'],'service_work_id'=>$work['id']])->findOrEmpty();
- if(!$audit->isEmpty() && ($audit['status']!=0 && $audit['status']!=2) ){
- throw new Exception('当前工单配件不允许修改');
- }
-
- if (empty($params['spare_image'])){
- throw new Exception('请上传配件图片');
- }
- //修改
- ServiceWorkSpare::where("id",$params['id'])->where('service_worker_id', $work['id'])->update(['spare_image' => $params['spare_image']]);
-
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 工程师确认服务完成
- * @param $params
- * @return false|void
- */
- public static function confirmServiceFinish($params)
- {
- Db::startTrans();
- try {
- $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- if($work->user_confirm_status !=2){
- throw new Exception('请勿重复操作');
- }
- $work->finished_images = $params['finished_images'];
- $work->user_confirm_status = 3;//待确认服务完成
- $work->save();
- //添加变更日志
- $work_log = [
- 'work_id'=>$work->id,
- 'master_worker_id'=>$work->master_worker_id,
- 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'提交了待用户确认服务完成',
- ];
- ServiceWorkLogLogic::add($work_log);
- Db::commit();
- // 外部平台工单 - 通知外边平台
- if($work->external_platform_id > 0){
- /*event('ExternalPlatform', [
- 'scene' => 'service_finish',
- 'params' => [
- 'work_id' => $work->id,
- ]
- ]);*/
- }
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function allocateWorker($params,$userInfo){
- Db::startTrans();
- try {
- $work = ServiceWork::findOrEmpty($params['id']);
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- if($work->work_status >=6 ){
- throw new \Exception('工单状态只能修改待结算之前的');
- }
- if($work->master_worker_id == $params['master_worker_id'] && !isset($params['is_grab_order'])){
- throw new \Exception('分配的工程师相同');
- }
- $worker = MasterWorker::where(['id'=>$params['master_worker_id'],'is_disable' =>0])->findOrEmpty();
- if($worker->isEmpty()){
- throw new \Exception('工程师不存在或被禁用');
- }
- if($worker->master_worker_id && !isset($params['is_grab_order'])){
- MasterWorker::setWorktotal('dec',$worker->master_worker_id);
- }
- $work->master_worker_id = $params['master_worker_id'];
- $work->work_status = 1;
- $work->dispatch_time = time();
- MasterWorker::setWorktotal('inc',$params['master_worker_id']);
- $work->save();
- $work_log = [
- 'work_id'=>$work->id,
- 'master_worker_id'=>$work->master_worker_id,
- 'type' => isset($params['is_grab_order'])?3:0,
- 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'分配了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
- ];
- ServiceWorkerAllocateWorkerLogic::add($work_log);
- Db::commit();
- //美团的订单更新履约状态为已分配
- if($work->third_type == 1){
- ThirdOrderLogic::updateorderfulfillinfo($work,ThirdTypeEnum::MEITUAN_MEITUAN);
- }
- // 工程师派单通知【给工程师的通知】【公众号通知,不发短信】
- $workDetail = ServiceWorkLogic::detail($params);
- $res = event('Notice', [
- 'scene_id' => 113,
- 'params' => [
- 'user_id' => $params['master_worker_id'],
- 'order_id' => $params['id'],
- 'thing9' => $workDetail['title'],
- 'time7' => $workDetail['appointment_time'],
- 'thing8' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
- 'phone_number6' => asteriskString($workDetail['mobile']),
- ]
- ]);
- return true;
- }catch(\Exception $e){
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 工单详情
- * @param $params
- * @return array|false
- */
- public static function detail($params){
- $work_where = !empty($params['work_sn'])?['work_sn'=>$params['work_sn']]:['id'=>$params['id']];
- $result = ServiceWork::with([
- 'worker'=> function(Query $query) {
- $query->field('id,worker_number,real_name,mobile');
- },
- 'allocateWorkerLog' =>function(Query $query){
- $query->field('id,work_id,opera_log,create_time');
- },
- 'serviceWorkLog' =>function(Query $query){
- $query->field('id,work_id,opera_log,create_time');
- }
- ])->append(['id','work_status_text','service_status_text'])
- ->where($work_where)
- ->findOrEmpty()->toArray();
- $result['is_operate_prohibit'] = 0;
- if(isset($params['user_id']) && $result['master_worker_id'] && $params['user_id'] && ($params['user_id'] != $result['master_worker_id']) ){
- // 带徒师傅的工单
- $lead_master_worker_id = TrainingWorkerTask::where('master_worker_id',$params['user_id'])->where('training_status',2)
- ->where('operate_status',0)->value('lead_master_worker_id');
- // 团队负责人编辑 工单池(包括已派队员) 工单
- if($params['user_info']['team_role'] == 1 && $params['user_info']['tenant_id'] == $result['tenant_id'] ){
- $result['is_operate_prohibit'] = 0;
- }elseif(!empty($lead_master_worker_id) && !empty($result['master_worker_id']) && $lead_master_worker_id == $result['master_worker_id']){
- // 徒查看师父的工单 不能编辑可查看
- $result['is_operate_prohibit'] = 1;
- }else{
- throw new \Exception('您没有权限操作该工单');
- }
- }
- //工程师工单按钮状态
- $work_service_status = 0;
- $work_service_status_text = '待派单';
- //工单状态
- if($result['work_status'] == 1){
- $work_service_status = 1;
- $work_service_status_text = '待领单';
- }
- if($result['work_status'] == 2){
- $work_service_status = 2;
- $work_service_status_text = '预约上门';
- }
- if($result['work_status'] == 3){
- $work_service_status = 3;
- $work_service_status_text = '等待上门';
- if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
- $work_service_status = 4;
- $work_service_status_text = '确认上门';
- }
- }
- if($result['work_status'] == 4 and $result['user_confirm_status']==0){
- $work_service_status = 5;
- $work_service_status_text = '确认报价';
- }
- if($result['work_status'] == 4 and $result['user_confirm_status']==1){
- $work_service_status = 6;
- $work_service_status_text = '用户确认报价中';
- }
- if($result['work_status'] == 5 and $result['user_confirm_status']==2){
- $work_service_status = 7;
- $work_service_status_text = '完成服务';
- }
- if($result['work_status'] == 5 and $result['user_confirm_status']==3){
- $work_service_status = 8;
- $work_service_status_text = '用户确认完成服务中';
- }
- if($result['work_status'] ==6){
- $work_service_status = 9;
- $work_service_status_text = '待结算';
- }
- if($result['work_status'] ==7){
- $work_service_status = 10;
- $work_service_status_text = '已完结';
- }
- if($result['work_status'] ==8){
- $work_service_status = 11;
- $work_service_status_text = '已评价';
- }
- if($result['work_status'] ==9){
- $work_service_status = 12;
- $work_service_status_text = '已退费';
- }
- $result['work_service_status'] = $work_service_status;
- $result['work_service_status_text'] = $work_service_status_text;
- //搜索当前工单下的所有订单记录
- $result['pay_orders'] = RechargeOrder::with(['orderGoods'=>function(Query $query){
- $query->field('id,sn,goods_id,goods_name,goods_image,goods_number,good_unit,goods_size,goods_payment_type,goods_type,goods_brand,base_service_fee,service_total,service_fee')->order(['id'=>'desc']);
- }])->where(['work_id'=>$result['id']])->field('id as order_id,sn,order_type,pay_status,payment_type,pay_way,pay_time,order_amount,order_total,coupon_price,create_time')->order('id asc')->select()->toArray();
- $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
- $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
- $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
- $order_type_data = DictData::where('type_value','order_type')->column('name','value');
- $coupon_price = 0;
- foreach ($result['pay_orders'] as $k=>&$v){
- $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
- $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
- $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
- $v['order_type_name'] = $order_type_data[$v['order_type']];
- $v['pay_time'] = $v['pay_time'] && is_numeric($v['pay_time']) ? date('Y-m-d H:i:s',$v['pay_time']):'';
- if($v['payment_type']!=1 and !empty($result['spare_total'])){
- $v['order_total'] = $v['order_total'] - $result['spare_total'];
- $v['order_amount'] = $v['order_amount'] - $result['spare_total'];
- }
- $coupon_price += $v['coupon_price'];
- //服务支付类别
- if(!empty($v['orderGoods'][0]['goods_payment_type'])){
- $goods_payment_type = $v['orderGoods'][0]['goods_payment_type'];
- }
- }
- $result['goods_payment_type'] = !empty($goods_payment_type)?$goods_payment_type:1;
- //汇总优惠卷额度
- $result['coupon_price'] = $coupon_price;
- //工单总支付金额
- $result['worker_account'] = $result['work_amount'];
- // 配件信息
- $result['spare_parts'] = [];
- $result['self_spare_parts'] = [];
- if($result['spare_total'] > 0){
- $result['spare_parts'] = ServiceWorkSpare::getLists($result['id'], 1);
- $result['self_spare_parts'] = ServiceWorkSpare::getLists($result['id'], 2);
- }
- // 保修卡信息
- $result['order_effectives'] = OrderEffectiveLog::with(['goods'=>function ($query) {
- $query->with(['goodsCategory'=>function ($query1) {
- $query1->field(['name','picture']);
- }]);
- }])->where('work_id',$result['id'])
- ->field(['id','goods_id','sn','effective_unit','effective_num','remark','end_effective_time'])
- ->append(['effective_unit_text'])
- ->order('create_time desc')
- ->select()
- ->toArray();
- //查收工程师提成金额
- $change_amount = MasterWorkerAccountLog::where(['work_sn'=>$result['work_sn'],'action'=>1])->value('change_amount');
- $result['change_amount'] = !empty($change_amount)?$change_amount:0;
- //质保金相关金额
- $result['retention'] = MasterWorkerRetentionMoneyLog::where('work_id',$result['id'])->visible(['action','amount'])->select()
- ->each(function ($item){
- $item['amount'] = $item['action'] == 1 ? '+'.$item['amount'] : '-'.$item['amount'];
- })->toArray();
- //保修工单时的信息
- $result['effective_income_amount'] = 0;
- if(!empty($result['order_effective_id'])){
- $order_effective = OrderEffectiveLog:: findOrEmpty($result['order_effective_id']);
- $serviceWork = ServiceWork::findOrEmpty($order_effective->work_id);
- if($serviceWork->master_worker_id != $result['master_worker_id']){
- $result['effective_income_amount'] = \app\adminapi\logic\effective\OrderEffectiveLogLogic::commissionAndAssuranceDeposit($serviceWork);
- }
- }
- //获取美团订单信息
- $result['meituan_order'] = [];
- if($result['third_type']==1){
- $result['meituan_order'] = ThirdOrders::where(['work_id'=>$result['id']])->order('create_time desc')->findOrEmpty();
- }
- return $result;
- }
- public static function getUserCouponDetails($params)
- {
- try{
- $work = ServiceWork::where('id',$params['id'])->findOrEmpty();
- if(!$work){
- throw new \Exception('工单不存在');
- }
- $coupon_all_ids =UserCoupon::where('user_id',$work['user_id'])
- ->where('voucher_count','>',0)
- ->where('voucher_status',0)
- ->where('expire_time','>',time())
- ->column('coupon_id');
- $coupon_ids = CouponCategory::where('goods_category_id',$work['goods_category_id'])->whereIn('coupon_id',$coupon_all_ids)->column('coupon_id');
- $data = UserCoupon::where('user_id',$work['user_id'])
- ->where('voucher_count','>',0)
- ->where('voucher_status',0)
- ->whereIn('coupon_id',$coupon_ids)
- ->append(['discount_ratio_text'])
- ->where('expire_time','>',time())
- ->visible(['id','coupon_id','amount','amount_require','begin_use','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
- ->select()->toArray();
- foreach($data as $k => $v){
- $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
- $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
- $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
- }
- return $data;
- } catch(\Exception $e){
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function getDetailWorkServiceStatus($params)
- {
- $result = ServiceWork::where('id',$params['id'])->field('work_status,user_confirm_status,appointment_time,price_approval,appoint_approval')->findOrEmpty()->toArray();
- //工程师工单按钮状态
- $work_service_status = 0;
- //工单状态
- if($result['work_status'] == 1){
- $work_service_status = 1;
- }
- if($result['work_status'] == 2){
- $work_service_status = 2;
- }
- if($result['work_status'] == 3){
- $work_service_status = 3;
- if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
- $work_service_status = 4;
- }
- }
- if($result['work_status'] == 4 and $result['user_confirm_status']==0){
- $work_service_status = 5;
- }
- if($result['work_status'] == 4 and $result['user_confirm_status']==1){
- $work_service_status = 6;
- }
- if($result['work_status'] == 5 and $result['user_confirm_status']==2){
- $work_service_status = 7;
- }
- if($result['work_status'] == 5 and $result['user_confirm_status']==3){
- $work_service_status = 8;
- }
- if($result['work_status'] ==6){
- $work_service_status = 9;
- }
- if($result['work_status'] ==7){
- $work_service_status = 10;
- }
- if($result['work_status'] ==8){
- $work_service_status = 11;
- }
- if($result['work_status'] ==9){
- $work_service_status = 12;
- }
- return ['work_service_status'=>$work_service_status,'price_approval'=>$result['price_approval'],'appoint_approval'=>$result['appoint_approval']];
- }
- /**
- * @notes 取消操作
- * @param array $params
- * @return bool
- * @author likeadmin
- * @date 2024/09/19 10:48
- */
- public static function cancel(array $params): bool
- {
- Db::startTrans();
- try {
- // 04-11-12 不做任何限制强制取消,已支付的费用给工程师余额
- $serviceWorkInfo = ServiceWork::find($params['id']);
- if((int)$serviceWorkInfo['user_confirm_status'] === 5 || (int)$serviceWorkInfo['service_status'] > 2){
- throw new \Exception('用户已完结该工单或已取消,已退款,不可执行取消');
- }
- //工单如果存在费用情况,不允许取消
- $paid_amount = RechargeOrder::where('work_id', $params['id'])->where('pay_status', 1)->value('paid_amount');
- if($paid_amount > 0 and ($serviceWorkInfo['third_type'] == 0 && $serviceWorkInfo['external_platform_id'] == 0)){
- // 4=已上门,5=服务中,6=待结算, 即工程师已上门服务过
- throw new Exception('工单存在费用情况,不允许取消,请走退费流程');
- }
- ServiceWork::where('id', $params['id'])->update([
- 'work_status' => 9,
- 'service_status' => 4
- ]);
- ServiceWorkLog::create([
- 'work_id' => $params['id'],
- 'master_worker_id' => $serviceWorkInfo['master_worker_id'],
- 'opera_log' => "工单:{$serviceWorkInfo['work_sn']}已取消".($params['remark']??'')
- ]);
- Db::commit();
- Log::info('取消工单'.json_encode([$serviceWorkInfo]));
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- Log::info('取消工单-Error'.json_encode([$e->getMessage()]));
- return false;
- }
- }
- /**
- * @notes 工程师结算操作
- * @param array $params
- * @return bool
- * @author likeadmin
- * @date 2024/09/19 10:48
- */
- public static function settlement(array $params): bool
- {
- Db::startTrans();
- try {
- $serviceWorkInfo = ServiceWork::find($params['id']);
- if((int)$serviceWorkInfo['work_pay_status'] !== 3){
- throw new \Exception('该工单非待结算状态');
- }
- if(ServiceWorkSpareAudit::where('service_work_id',$serviceWorkInfo->id)->findOrEmpty()->isEmpty()){
- $serviceWorkInfo->user_confirm_status = 5;
- $serviceWorkInfo->work_status = 7;
- $serviceWorkInfo->work_pay_status = 1;
- $serviceWorkInfo->save();
- }else{
- throw new \Exception('请完成配件审核');
- }
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 工程师配件审核结算操作
- * @param array $params
- * @return bool
- * @author liugc <466014217@qq.com>
- * @date 2025/5/9 9:14
- */
- public static function settlementSpare(array $params): bool
- {
- Db::startTrans();
- try {
- $spare = ServiceWorkSpare::where('id',$params['id'])->findOrEmpty();
- if($spare->isEmpty()){
- throw new \Exception('无配件审核');
- }else{
- $serviceWorkInfo = ServiceWork::find($spare->service_work_id);
- if((int)$serviceWorkInfo['work_pay_status'] !== 3){
- throw new \Exception('该工单非待结算状态');
- }
- $spare->status = $params['status']??0;
- $spare->remark = $params['remark']??'';
- $spare->save();
- }
- Db::commit();
- // 是否所有配件审核完成
- SparePartLogic::isExamineOk($spare->service_work_id);
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 获取所有改约通知
- * @param $userId
- * @return array|false
- */
- public static function getAppointmentNotice($userId)
- {
- return ServiceWork::where(['master_worker_id'=>$userId,'appoint_approval'=>1])
- ->where('work_status','>',1)
- ->where('work_status','<',7)
- ->field(['id', 'work_sn','real_name','mobile', 'address', 'title', 'appointment_time','work_status'])
- ->order(['appointment_time' => 'asc'])//上门时间排序
- ->select()->each(function (&$item){
- $item['last_appointment_time'] = date('Y-m-d H:i:s',ServiceWorkAppointmentLog::where('work_id',$item['id'])->order('id desc')->value('last_appointment_time'));
- })
- ->toArray();
- }
- /**
- * @param $params
- * @return bool
- */
- public static function submitAppointment($params)
- {
- Db::startTrans();
- try {
- $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
- if($serviceWork->isEmpty()){
- throw new \Exception('工单不存在');
- }
- $serviceWork->appoint_approval = 2;
- $serviceWork->save();
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @param $params
- * @return bool
- */
- public static function submitChangePrice($params)
- {
- Db::startTrans();
- try {
- $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
- if($serviceWork->isEmpty()){
- throw new \Exception('工单不存在');
- }
- $serviceWork->work_status = 4;
- $serviceWork->user_confirm_status = 0;
- $serviceWork->price_approval = 2;
- $serviceWork->save();
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function notApproved($params)
- {
- Db::startTrans();
- try {
- $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
- if($serviceWork->isEmpty()){
- throw new \Exception('工单不存在');
- }
- $serviceWork->refund_approval = 3;
- $serviceWork->save();
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function againDoor($params)
- {
- Db::startTrans();
- try {
- $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
- if($serviceWork->isEmpty()){
- throw new \Exception('工单不存在');
- }
- //更新预约日志
- //更新工单未确认上门的状态
- $serviceWork->work_status = 3;
- $serviceWork->user_confirm_status = 0;
- $appoint = ServiceWorkAppointmentLog::where(['work_id'=>$serviceWork->id,'worker_id'=>$params['user_id']])->count();
- if($appoint > 2){
- throw new Exception('您的修改预约时间次数已到上限,请联系客服处理');
- }
- ServiceWorkAppointmentLog::create([
- 'work_id'=>$serviceWork->id,
- 'worker_id'=>$params['user_id'],
- 'last_appointment_time'=>strtotime($serviceWork->appointment_time),
- 'this_appointment_time'=>strtotime($params['appointment_time']),
- ]);
- $serviceWork->appointment_time = strtotime($params['appointment_time']);
- $serviceWork->save();
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function cancelAllocation($params,$userInfo){
- Db::startTrans();
- try {
- $work = ServiceWork::findOrEmpty($params['id']);
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- if($work->work_status >=6 ){
- throw new \Exception('工单状态只能修改待结算之前的');
- }
- $worker = MasterWorker::where(['id'=>$work->master_worker_id])->findOrEmpty();
- if($worker->isEmpty()){
- throw new \Exception('工程师不存在');
- }
- if ($work->tenant_id > 0) {
- //团队订单取消时,派单数量减1
- $updateData = date("H",strtotime($work->appointment_time)) < 12 ? ['am_order' => Db::raw('am_order - 1')] : ['pm_order' => Db::raw('pm_order - 1')];
- MasterWorkerTeam::where('master_worker_id',$work->master_worker_id)->where('tenant_id',$work->tenant_id)->update($updateData);
- }
- $params['master_worker_id'] = (isset($params['master_worker_id']) && !empty($params['master_worker_id']))?$params['master_worker_id']:$work->master_worker_id;
- MasterWorker::setWorktotal('dec',$work->master_worker_id);
- $work->master_worker_id = 0;
- $work->tenant_id = 0;
- $work->work_status = 0;
- $work->dispatch_time = 0;
- $work->first_contact_time = 0;
- $work->estimated_finish_time = 0;
- $work->save();
- $work_log = [
- 'work_id'=>$work->id,
- 'master_worker_id'=>$params['master_worker_id'],
- 'type' => 1,
- 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'取消了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
- ];
- ServiceWorkerAllocateWorkerLogic::add($work_log);
- Db::commit();
- return true;
- }catch(\Exception $e){
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function addCustomerLog($params)
- {
- Db::startTrans();
- try {
- $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
- if($serviceWork->isEmpty()){
- throw new \Exception('工单不存在');
- }
- ServiceWorkCustomerLog::create([
- 'work_id'=>$serviceWork->id,
- 'type'=>$params['type']??0,
- 'content'=>$params['content']??'',
- 'admin_id'=>$params['admin_id']??0,
- 'create_time'=>time(),
- ]);
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function distributeTenants($params,$userInfo)
- {
- Db::startTrans();
- try {
- if(empty($params['id']) || empty($params['tenant_id'])){
- throw new \Exception('参数错误'.json_encode($params));
- }
- if (is_array($params['id'])) {
- $ids = $params['id'];
- } else{
- $ids = [$params['id']];
- }
- // 取消工程师分配
- foreach ($ids as $id) {
- $res = self::cancelAllocation(['id'=>$id,'master_worker_id'=>''],$userInfo);
- if($res === false){
- Log::info('分配log'.self::getError());
- }
- }
- ServiceWork::whereIn('id',$ids)->update(['tenant_id'=>$params['tenant_id']]);
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- *
- * @return false|void
- */
- public static function contactCustomer($params)
- {
- try {
- $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
- if($work->isEmpty()){
- throw new \Exception('工单不存在');
- }
- if (empty($work->first_contact_time)) {
- $work->first_contact_time = time();
- $work->save();
- }
- return true;
- }
- catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 添加第三方平台订单
- * @return false|void
- */
- public static function addThirdPlatformsOrders($params)
- {
- try {
- if($params['userAddress']){
- $lon_lat = get_address_lat_lng($params['userAddress']);
- $params['lon'] = $lon_lat['lon'];
- $params['lat'] = $lon_lat['lat'];
- }
- return ThirdOrderLogic::submitOrders($params);
- }catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 上门码和完成码
- * @param $params
- * @return false|array
- */
- public static function confirmDoorCode($params)
- {
- try {
- $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
- if($work->isEmpty()){
- throw new Exception('工单不存在');
- }
- $encryptedData = encrypt($params['work_sn'], \think\facade\Config::get('project.work_sn_key'));
- $user_website = \think\facade\Config::get('project.user_website');
- $type = $params['type']??1;
- if($type == 1){
- $user_website .= '?code='.$encryptedData;
- }elseif($type == 2){
- $user_website .= 'pages/quoted_price?code='.$encryptedData;
- }
- return ['url'=>$user_website];
- }catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 终止结束服务操作
- */
- public static function terminateService($params): bool
- {
- Db::startTrans();
- try {
- $serviceWork = ServiceWork::where('work_pay_status',1)
- ->where('master_worker_id','>',0)
- ->whereIn('work_status',[0,1,2,3,4,5,6])
- ->where('id',$params['id'])->findOrEmpty();
- if($serviceWork->isEmpty()){
- throw new \Exception('该工单状态不可执行结束服务');
- }
- if($params['isearnest'] == 1){
- $serviceWork->work_status = 7;
- $serviceWork->user_confirm_status = 5;
- $serviceWork->service_status = 3;
- $serviceWork->work_pay_status = 1;
- $serviceWork->finished_time = time();
- $serviceWork->remark = ($serviceWork->remark?:'')." | 终止结束服务:常规流程";
- $serviceWork->save();
- }else{
- $paid_amount = RechargeOrder::where('work_id', $params['id'])->where('payment_type', 1)->value('paid_amount');
- if($paid_amount > 0){
- // 存在上门费给工程师
- Log::info('终止结束服务工单'.$serviceWork->id.',上门费:'.$paid_amount);
- WorkerAccountLogLogic::addAccountLog($serviceWork,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC);
- }
- ServiceWork::where('id', $params['id'])->update([
- 'work_status' => 7,
- 'user_confirm_status' => 5,
- 'service_status' => 3,
- 'finished_time' => time(),
- 'work_pay_status' => 2, // 已结算则不执行 onAfterUpdate
- 'settlement_amount' => $paid_amount??0,
- 'worker_price' => $paid_amount??0,
- 'remark' => ($serviceWork->remark?:'')." | 终止结束服务:上门费-{$paid_amount}"
- ]);
- }
- ServiceWorkLog::create([
- 'work_id' => $params['id'],
- 'master_worker_id' => $serviceWork->master_worker_id,
- 'opera_log' => "工单:{$serviceWork->work_sn} 终止结束服务"
- ]);
- Db::commit();
- Log::info('终止结束服务'.json_encode([$serviceWork]));
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- Log::info('终止结束服务-Error'.json_encode([$e->getMessage()]));
- return false;
- }
- }
- /**
- * 工程师抢单
- */
- public static function grabOrder($params): bool
- {
- if(!MasterWorkerLogic::isReceivingOrders($params['master_worker_id'])){
- Log::info('抢单失败:工程师不能接单:'.$params['id'].'--'.$params['master_worker_id']);
- self::setError('未达到接单条件');
- return false;
- }
- $serviceWork = 0;
- Db::startTrans();
- try {
- // 抢单
- $serviceWork = ServiceWork::where(['work_pay_status'=>1,'work_status'=>0,'service_status'=>0,'master_worker_id'=>0])
- ->where('id',$params['id'])->update(['master_worker_id'=>$params['master_worker_id'],'work_status'=>1]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- Log::info('抢单失败:'.$params['id'].'--'.$params['master_worker_id'].':'.$e->getMessage());
- self::setError('抢单失败');
- return false;
- }
- if($serviceWork == 1){
- Log::info('抢单成功:'.$params['id'].'--'.$params['master_worker_id']);
- $params['is_grab_order'] = 1;
- self::allocateWorker($params,['admin_id'=>0,'name'=>'抢单工程师ID:'.$params['master_worker_id']]);
- return true;
- }else{
- Log::info('抢单失败:'.$params['id'].'--'.$params['master_worker_id']);
- self::setError('抢单失败');
- return false;
- }
- }
- /**
- * 给用户发券
- */
- public static function userAddVoucher($params): bool
- {
- try {
- $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
- if(!$serviceWork->isEmpty()){
- $codes = [];
- foreach ($params['coupon_data'] as $coupon_data) {
- $coupon_data['code'] && $codes[] = $coupon_data['code'];
- }
- if($codes){
- UserCouponLogic::grant($codes,$serviceWork->user_id,$serviceWork->id);
- }
- }
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 给用户加单
- */
- public static function userExtendOrder($params): bool
- {
- try {
- $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
- if(!$serviceWork->isEmpty()){
- //goods_id ['user_info']['mobile'] user_id coupon_id=0 worker=1 terminal=1 pay_way=2 address contact_people=real_name contact_number=mobile appointment_time lon lat property_activity_id=0
- ServiceOrderLogic::submitOrder([
- 'goods_id' => $serviceWork->goods_id,
- 'user_info' => [
- 'mobile' => $serviceWork->mobile
- ],
- 'user_id' => $serviceWork->user_id,
- 'coupon_id' => 0,
- 'worker' => 1,
- 'terminal' => 1,
- 'pay_way' => 2,
- 'appointment_time' => $serviceWork->appointment_time,
- 'address' => $serviceWork->address,
- 'contact_people' => $serviceWork->real_name,
- 'contact_number' => $serviceWork->mobile,
- 'lon' => $serviceWork->lon,
- 'lat' => $serviceWork->lat,
- 'property_activity_id' => 0
- ]);
- }
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- }
|