12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338 |
- <?php
- namespace app\common\model;
- use app\common\util\AppMsg;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- class Orders extends Base {
- public $priority = [
- 1 => '可延缓的',
- 2 => '常规',
- 3 => '紧急',
- 4 => '预约',
- ];
- public $source_type = [
- 1 => '一键呼叫',
- 2 => '隐患',
- 3 => '投诉',
- ];
- public $table = 'orders';
- protected $validateName = 'Orders';
- public function addSave($data, $type = 0) {//type 是否是调度发单1是
- $scene = 'dispatchAdd';
- if ($type == 0) {
- $scene = 'addShow';
- }
- $data['userType'] = isset($data['userType']) ? $data['userType'] : 0;
- $data['isSpontaneous'] = isset($data['isSpontaneous']) ? $data['isSpontaneous'] : 0;
- $data['quality_type'] = isset($data['quality_type']) ? $data['quality_type'] : 0;
- if(isset($data['source_type']) && $data['source_type']==4 && $data['isSpontaneous']==0){//一键呼叫派发
- if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
- $this->error = '请选择转单类型';
- return false;
- }
- $config = Db::name('config')
- ->where('name','web_order_transfer_type')
- ->value('value');
- if(empty($config)){
- $workType = [];
- }else{
- $workType = explode('|',$config);
- }
- if(!in_array($data['work_type_mode'],$workType)){
- $this->error = '当前类型不能进行转单';
- return false;
- }
- if($type==1){
- if($data['userType']==0){
- $data['to_user_id'] = !isset($data['to_user_id'])?$data['to_user_id'.$data['work_type_mode']]:$data['to_user_id'];
- }else{
- $data['to_roles_id'] =!isset($data['to_roles_id'])? $data['to_roles_id'.$data['work_type_mode']]:$data['to_roles_id'];
- }
- }
- }
- $result = validate($this->validateName)->check($data, [], $scene);
- if (true !== $result) {
- $this->error = validate($this->validateName)->getError();
- return false;
- }
- if($data['work_type_mode']!=3){
- if(empty($data['content'])){
- $images = isset($data['images']) ? $data['images'] : '';
- $videos = isset($data['videos']) ? $data['videos'] : '';
- $voices = isset($data['voices']) ? $data['voices'] : '';
- if(empty($images) && empty($videos) && empty($voices)){
- $this->error = '内容不能为空';
- return false;
- }
- }
- }
- if ($data['work_type_mode'] == 3) {
- $result = validate('OrdersConvey')->check($data, []);
- if (true !== $result) {
- $this->error = validate('OrdersConvey')->getError();
- return false;
- }
- }
- if($type == 1 && $data['work_type_mode'] == 15) { // 品质整改
- if(!isset($data['informer_user']) || empty($data['informer_user'])){
- $this->error = '知会人不能为空';
- return false;
- }
- }
- $sData = array(
- 'dep_id' => isset($data['dep_id']) && !empty($data['dep_id']) ? $data['dep_id'] : 0,
- 'org_id' => $data['org_id'],
- 'user_id' => $data['user_id'],
- 'order_mode' => $type == 0 ? 1 : 4,
- 'create_time' => getTime(),
- 'create_yyyy' => date('Y'),
- 'create_yyyymm' => date('Ym'),
- 'create_yyyymmdd' => date('Ymd'),
- 'sn' => get_unique_sn(get_config('sn_prefix')),
- 'content' => $data['content'],
- 'images' => isset($data['images']) ? $data['images'] : '',
- 'videos' => isset($data['videos']) ? $data['videos'] : '',
- 'voices' => isset($data['voices']) ? $data['voices'] : '',
- 'work_type_mode' => $data['work_type_mode'],
- 'quality_type' => isset($data['quality_type'])?$data['quality_type']:0,
- 'quality_cate' => isset($data['quality_cate'])?$data['quality_cate']:0,
- 'informer_user' => isset($data['informer_user'])?$data['informer_user']:0,
- 'source_type' => $type == 0 ? 1 : 2,
- 'name' => isset($data['name']) ? $data['name'] : '',
- 'phone' => isset($data['phone']) ? $data['phone'] : '',
- );
- if(isset($data['source_type']) && !empty($data['source_type'])){
- $sData['source_type'] = $data['source_type'];
- }
- //普通订单自发
- if (isset($data['source_type']) && $data['source_type'] == 3) {
- $sData['source_type'] = 3;
- $sData['order_mode'] = 4;
- $sData['send_time'] = getTime();
- if ($data['work_type_mode'] == 1) {
- if (!isset($data['type_id']) || empty($data['type_id'])) {
- $this->error = '请选择报修事项';
- return false;
- }
- }
- }
- //一键呼叫订单自发
- if (isset($data['source_type']) && $data['source_type'] == 4 && $data['isSpontaneous']==1) {
- $sData['source_type'] = 3;
- $sData['order_mode'] = 4;
- $sData['send_time'] = getTime();
- }
- if ($type == 1) {
- if ($data['userType'] == 0) {
- $sData['send_time'] = getTime();
- if (!isset($data['to_user_id']) || empty($data['to_user_id'])) {
- $this->error = '请选择执行人';
- return false;
- }
- if ($data['work_type_mode'] == 1) {
- if (!isset($data['type_id']) || empty($data['type_id'])) {
- $this->error = '请选择报修事项';
- return false;
- }
- }
- }
- else {
- if(two_dispatch_off($data['org_id'])!=1){
- $this->error = '二级调度开关未开启';
- return false;
- }
- $sData['order_mode'] = 1;
- $sData['is_deal'] = 1;
- if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) {
- $this->error = '请选择二级调度';
- return false;
- }
- }
- }
- if(isset($data['sxType'] ) && !empty($data['sxType']) && $data['sxType'] == 1){
- $sData['name']=isset($data['name'])?$data['name']:'';
- $sData['phone']=isset($data['phone'])?$data['phone']:'';
- }
- // if($type == 0){
- // $sData['from'] = 1;
- // }
- $this->startTrans();
- try {
- $pusharr = []; // 需要推送给工人
- $pushdis = []; // 需要推送给二级调度
- $orderId = $this->insertGetId($sData);
- if (!$orderId) {
- \exception('保存订单失败');
- }
- if(isset($data['id']) && $data['id'] > 0){ // 电话保单
- Db::name('phone_monitor_record')->where('id',$data['id'])->update([
- 'order_id' => $orderId,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- }
- if ($data['work_type_mode'] == 1) {//报修订单
- $rData = [];
- if (isset($data['type_id']) && !empty($data['type_id'])) {
- $rData['type_id'] = $data['type_id'];
- }
- if (isset($data['address_id']) && !empty($data['address_id'])) {
- $rData['address_id'] = $data['address_id'];
- }
- if (isset($data['repair_priority']) && !empty($data['repair_priority'])) {
- $rData['repair_priority'] = $data['repair_priority'];
- }
- if (!empty($rData)) {
- $rData['order_id'] = $orderId;
- $res = Db::name('order_repair')
- ->insert($rData);
- if (!$res) {
- \exception('保存维修扩展失败');
- }
- }
- }
- if ($data['work_type_mode'] == 3) {
- if (!$data['start'] || !$data['end']) {
- $this->error = '取件与送达地点不能为空';
- return false;
- }
- if ($data['start'] == $data['end']) {
- $this->error = '取件与送达地点不能是同一地点';
- return false;
- }
- if (!$data['type']) {
- $this->error = '运送类型不能为空';
- return false;
- }
- $conveyCate = Db::name('convey_cate')
- ->where('id', $data['type'])
- ->find();
- if($conveyCate['cate']==4){
- if (!$data['xq_time'] || !$data['ywc_time']) {
- $this->error = '应完成时间应和需求时间不能为空';
- return false;
- }
- if ($data['xq_time'] >= $data['ywc_time']) {
- $this->error = '应完成时间应大于需求时间';
- return false;
- }
- if (strtotime($data['xq_time']) < time() - 10 * 60) {
- $this->error = '需求时间已过时';
- return false;
- }
- }else{
- $data['xq_time'] = date('Y-m-d H:i:s');
- $time = Db::name('time')
- ->where('id',$conveyCate['time_id'])
- ->find();
- $data['ywc_time'] = date('Y-m-d H:i',strtotime($data['xq_time']) + 60*$time['bz_time']);
- }
- $ysData = array(
- 'order_id' => $orderId,
- 'type' => $data['type'],
- 'start' => $data['start'],
- 'end' => $data['end'],
- 'xq_time' => isset($data['xq_time'])?$data['xq_time']:"",
- 'ywc_time' => isset($data['ywc_time'])?$data['ywc_time']:"",
- 'name' => isset($data['name'])?$data['name']:"",
- 'phone' => isset($data['phone'])?$data['phone']:"",
- 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
- 'priority' => empty($data['priority']) ? 0 : $data['priority'],
- 'score'=>$conveyCate['score']
- );
- if(empty($ysData['xq_time'])){
- unset($ysData['xq_time']);
- }
- if(empty($ysData['ywc_time'])){
- unset($ysData['ywc_time']);
- }
- $oCid = Db::name('order_convey')
- ->insertGetId($ysData);
- if (!$oCid) {
- exception('运送信息保存失败');
- }
- if(isset($data['way_addr']) && !empty($data['way_addr'])){
- $wayAddr = explode(',',$data['way_addr']);
- foreach ($wayAddr as $k=>$v){
- if($v){
- $cdata = [
- 'order_id'=>$orderId,
- 'addr'=>$v,
- 'create_time'=>date('Y-m-d H:i:s'),
- 'order_convey_id'=>$oCid,
- ];
- $oce = Db::name('order_convey_end')->insert($cdata);
- if(!$oce){
- exception('途径地点信息保存失败');
- }
- }
- }
- }
- model('UserConveyCate')->updateCate($data['user_id'],$data['type']);
- if ($conveyCate['cate'] == 1) {
- $patient = [
- 'order_id' => $orderId,
- 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
- 'name' => isset($data['p_name']) ? $data['p_name'] : '',
- 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
- 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
- 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
- ];
- $ocPid = Db::name('order_convey_patient')
- ->insertGetId($patient);
- if (!$ocPid) {
- exception('保存运送病人扩展失败');
- }
- }
- }
- if ($type == 1 || $sData['source_type']==3 || ($sData['source_type']==4 && $data['isSpontaneous']==1)) {
- if ($data['userType'] == 0) {
- if($sData['source_type']==3 || ($sData['source_type']==4 && $data['isSpontaneous']==1)){
- $data['to_user_id'] = $data['user_id'];
- }
- $send_user_num = $this->sendUserNum($data['work_type_mode'],$data['org_id']);
- $users = explode(',', $data['to_user_id']);
- if($send_user_num==1 && count($users) >1){
- exception('执行人只能选择单人');
- }
- $to_user_id = array_unique($users);
- $todoData = [
- 'order_id' => $orderId,
- 'todo_content' => isset($data['todo_content'])?$data['todo_content']:"",
- 'create_user_id' => $data['user_id'],
- 'org_id' => $data['org_id'],
- 'create_time' => getTime(),
- 'todo_mode' => $sData['source_type']==3?2:1,
- 'work_type_mode' => $data['work_type_mode'],
- 'create_yyyy' => date('Y'),
- 'create_yyyymm' => date('Ym'),
- 'create_yyyymmdd' => date('Ymd'),
- 'quality_type' => $data['quality_type'],
- ];
- if($sData['source_type']==3){
- $todoData['confirm_time'] = getTime();
- }
- $taskData = [
- 'org_id' => $data['org_id'],
- 'type' => 1,
- 'start_time' => getTime(),
- 'create_time' => getTime(),
- ];
- if($data['work_type_mode']==1){
- if (isset($data['repair_priority']) && $data['repair_priority']==3) {
- $taskData['priority'] = 1;
- }
- }elseif ($data['work_type_mode']==3){
- if (isset($data['priority']) && $data['priority']==3) {
- $taskData['priority'] = 2;
- }
- }
- $sns = [];
- foreach ($to_user_id as $k => $v) {
- if(empty($v)){
- continue;
- }
- $todoData['to_user_id'] = $v;
- $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
- while (true){
- if(in_array($todoData['sn'],$sns)){
- $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
- }else{
- $sns[] = $todoData['sn'];
- break;
- }
- }
- if($type == 1 && $data['work_type_mode'] == 15){ // 品质整改
- $todoData['todo_mode'] = 2;
- $todoData['confirm_time'] = getTime();
- }
- $res = Db::name('todo')
- ->insertGetId($todoData);
- if (!$res) {
- \exception('执行人:' . $v . '派单失败');
- }
- if($sData['source_type']!=3){
- $pusharr[] = [
- 'user_id' => $v,
- 'todo_id' => $res
- ];
- }
- $taskData['user_id'] = $v;
- $taskData['bus_id'] = $res;
- $res = Db::name('task')
- ->insert($taskData);
- if (!$res) {
- \exception('执行人:' . $v . '任务保存失败');
- }
- }
- if(check_two_dispatch($data['user_id'])==1){
- $dLog = [
- 'user_id'=>$data['user_id'],
- 'to_user_id'=>$data['user_id'],
- 'create_time' => getTime(),
- 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
- 'order_id' => $orderId,
- 'roles_id' => 0
- ];
- $r = Db::name('orders')
- ->where('id',$orderId)
- ->update(['is_deal'=>1]);
- if(!$r){
- \exception('订单更新失败');
- }
- $res = Db::name('dispatch_log')
- ->insert($dLog);
- if (!$res) {
- \exception('二级调度记录保存失败');
- }
- }
- }
- else {
- $pRolesId = explode(',',$data['to_roles_id']);
- $pp = [];
- foreach ($pRolesId as $k=>$v){
- $logData = [
- 'user_id' => $data['user_id'],
- 'to_user_id' =>0,
- 'create_time' => getTime(),
- 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
- 'order_id' => $orderId,
- 'roles_id' => $v
- ];
- $pp[] = $logData;
- }
- $pushdis = Db::name('user_roles')
- ->where('roles_id','in',$pRolesId)
- ->column('user_id');
- $res = Db::name('dispatch_log')
- ->insertAll($pp);
- if (!$res) {
- \exception('派发失败');
- }
- }
- }
- if($type == 1 && $data['work_type_mode'] == 15){ // 品质整改
- if($data['quality_type']==0){
- Db::name('daily_record')->where('id',$data['daily_record_id'])->update(['order_id' => $orderId]);
- }elseif ($data['quality_type']==1){
- Db::name('device_record')->where('id',$data['daily_record_id'])->update(['order_id' => $orderId]);
- }
- elseif (in_array($data['quality_type'],[2,3,4,5])){
- Db::name('patrol_record')->where('id',$data['daily_record_id'])->update(['order_id' => $orderId]);
- }
- }
- $this->commit();
- if($pusharr){
- foreach ($pusharr as $k=>$v){
- // 极光推送
- send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
- }
- }
- if(empty($sData['informer_user']) && $type == 1 && $data['work_type_mode'] == 15){
- send_jpush([$sData['informer_user']],AppMsg::PUSH_INFORMER_USER,'您有一个品控订单,请知晓',['id'=>$orderId]);
- }
- if($pushdis){
- foreach ($pushdis as $k=>$v){
- // 极光推送
- send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$orderId]);
- }
- }
- if($type == 0 && $sData['source_type'] != 3){ // 客户发单,需要检查是否有抢单
- $off = model('Config')->getConfig('org_grab_order'.$sData['work_type_mode'],$sData['org_id']);
- if($off){
- $workers = model('WorkTypeMode')->getRolesUserApp($sData['work_type_mode'],$sData['org_id']);
- $uids = [];
- foreach ($workers as $v){
- $uids[] = $v['id'];
- }
- if($uids){
- send_jpush($uids,AppMsg::PUSH_ORDER_ROB,'',['id'=>$orderId]);
- }
- }
- }
- $dispatchOff = model('Config')->getConfig('org_dispatch_off',$sData['org_id']);
- if($dispatchOff==1){
- //提醒调度
- $dUser = model('Roles')->getDispatchOrder($sData['work_type_mode'],$sData['org_id']);
- if(!empty($dUser)){
- send_jpush($dUser,AppMsg::PUSH_WORKER_ORDER_SEND,'您有一个新的订单需要及时处理',['id'=>$orderId]);
- }
- }
- return true;
- } catch (Exception $e) {
- $this->error = $e->getMessage();
- $this->rollback();
- return false;
- }
- }
- //后台格式化订单详情
- public function formatOrder($v, $is_list = 0) {
- $v['order_type'] = '';
- $v['order_type_matter'] = '';
- $v['address_title'] = '';
- $v['repair_priority'] = '';
- $v['task_priority'] = 0;
- $v['cons'] = [];
- if ($v['work_type_mode'] == 1) {
- $typeInfo = (new \app\common\model\OrderType())
- ->getTypeByOrderId($v['id']);
- $v['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : '';
- $v['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : '';
- $v['address_title'] = $typeInfo ? $typeInfo['address_title'] : '';
- $v['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : '';
- $v['task_priority'] = $typeInfo?($typeInfo['repair_priority_index']==3?1:0):0;
- $todo_mate = Db::name('todo_mate')
- ->where('order_id',$v['id'])
- ->select();
- if(!empty($todo_mate)){
- $cons = Db::name('todo_mate_item')
- ->alias('a')
- ->join('mate_goods b','b.id = a.items_id')
- ->where('a.todo_mate_id','in',array_column($todo_mate,'id'))
- ->field('a.*,b.title')
- ->select();
- foreach ($cons as $k1=>$v1){
- $v['cons'][] = [
- 'title' =>$v1['title'],
- 'total' =>$v1['total'],
- 'money' =>$v1['money'],
- 'total_money' =>$v1['total_money'],
- 'realName' =>$this->getTableField('user',['id'=>$v1['user_id']],'real_name'),
- ];
- }
- }
- }
- $v['type_name'] = '';
- $v['start_name'] = '';
- $v['end_name'] = '';
- $v['device_name'] = '';
- if ($v['work_type_mode'] == 3) {
- $oc = Db::name('order_convey')
- ->where('order_id', $v['id'])
- ->find();
- $v['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
- $v['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
- $v['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
- $v['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
- $v['task_priority'] = $oc['priority']==3?2:0;
- }
- $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
- $v['source_type_text'] =$v['from']>0? isset($this->source_type[$v['from']])?$this->source_type[$v['from']]:'':$this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
- $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
- $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content');
- $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content');
- $v['order_mode_color'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'color');
- $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name');
- $v['informer_user_name'] = $this->getTableField('user', ['id' => $v['informer_user']], 'real_name');
- $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile');
- $v['schedulContent'] = '';
- $v['to_user_name'] = '';
- $v['quality_cate_name'] = '';
- if($v['work_type_mode']==15){
- $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']);
- }
- $todo = Db::name('todo')
- ->where('order_id', $v['id'])
- ->where('del',0)
- ->select();
- if($todo){
- $toUserInfo = Db::name('user')
- ->where('id','in',array_column($todo,'to_user_id'))
- ->column('real_name');
- $v['to_user_name'] = implode(',',$toUserInfo);
- }
- $dispatchLog = Db::name('dispatch_log')
- ->where('order_id',$v['id'])
- ->find();
- if($dispatchLog && $dispatchLog['content']){
- $v['schedulContent'] = $dispatchLog['content'];
- }
- $pDep = '';
- $depInfo = Db::name('dep')
- ->where('id',$v['dep_id'])
- ->find();
- if($depInfo){
- $pDep = Db::name('dep_cate')
- ->where('id',$depInfo['cate_id'])
- ->value('title');
- }
- $v['dep_cate_name'] = $pDep;
- if ($is_list > 0) {
- $v['todo_list'] = [];
- $todo = Db::name('todo')
- ->where('order_id', $v['id'])
- ->where('del',0)
- ->select();
- if (!empty($todo)) {
- foreach ($todo as $k => $v1) {
- $todo[$k]['to_real_name'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'real_name');
- $todo[$k]['todo_mode_text'] = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'in_content');
- $tx = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'out_content');
- $todo[$k]['to_ok_images'] = !empty($v1['images'])?explode(',',$v1['images']):'';
- $todo[$k]['todo_mode_out_text'] =$tx;
- $todoConvey = Db::name('todo_convey')->where('todo_id',$v1['id'])->find();
- $todo[$k]['start_img'] = $todoConvey?$todoConvey['start_img'] :'';
- $todo[$k]['end_img'] = $todoConvey?$todoConvey['end_img'] :'';
- }
- $v['todo_list'] = $todo;
- }
- $v['comment'] = [];
- if ($v['order_mode'] == 6) {
- $v['comment'] = Db::name('comment')
- ->where('type', $v['work_type_mode'])
- ->where('from_id', $v['id'])
- ->where('org_id', $v['org_id'])
- ->find();
- }
- $v['oc'] = [];
- $v['ocp'] = [];
- if ($v['work_type_mode'] == 3) {
- $oc = Db::name('order_convey')
- ->where('order_id', $v['id'])
- ->find();
- $oc['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
- $oc['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
- $oc['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
- $oc['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
- $conveyEndCount = Db::name('order_convey_end')
- ->where('order_id',$v['id'])
- ->where('del',0)
- ->count();
- $ceNums = $conveyEndCount+1;
- $oc['typeScore'] = round($oc['score']*$ceNums,1);
- $v['oc'] = $oc;
- $cate = $this->getTableField('convey_cate', ['id' => $oc['type']], 'cate');
- if ($cate == 1) {
- $ocp = Db::name('order_convey_patient')
- ->where('order_id', $v['id'])
- ->find();
- $v['ocp'] = $ocp;
- }
- }
- }
- return $v;
- }
- public function newFormatOrder($v, $is_list = 0) {
- $v['order_type'] = '';
- $v['order_type_matter'] = '';
- $v['address_title'] = '';
- $v['repair_priority'] = '';
- $v['task_priority'] = 0;
- $v['cons'] = [];
- if ($v['work_type_mode'] == 1) {
- $typeInfo = (new \app\common\model\OrderType())
- ->getTypeByOrderId($v['id']);
- $v['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : '';
- $v['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : '';
- $v['address_title'] = $typeInfo ? $typeInfo['address_title'] : '';
- $v['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : '';
- $v['task_priority'] = $typeInfo?($typeInfo['repair_priority_index']==3?1:0):0;
- }
- $v['type_name'] = '';
- $v['start_name'] = '';
- $v['end_name'] = '';
- $v['device_name'] = '';
- if ($v['work_type_mode'] == 3) {
- $oc = Db::name('order_convey')
- ->where('order_id', $v['id'])
- ->find();
- $v['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
- $v['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
- $v['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
- $v['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
- $v['task_priority'] = $oc['priority']==3?2:0;
- }
- $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
- $v['source_type_text'] =$v['from']>0? isset($this->source_type[$v['from']])?$this->source_type[$v['from']]:'':$this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
- $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
- $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content');
- $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content');
- $v['order_mode_color'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'color');
- $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name');
- $v['informer_user_name'] = $this->getTableField('user', ['id' => $v['informer_user']], 'real_name');
- $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile');
- $v['schedulContent'] = '';
- $v['to_user_name'] = '';
- $v['quality_cate_name'] = '';
- if($v['work_type_mode']==15){
- $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']);
- }
- $todo = Db::name('todo')
- ->where('order_id', $v['id'])
- ->where('del',0)
- ->select();
- if($todo){
- $toUserInfo = Db::name('user')
- ->where('id','in',array_column($todo,'to_user_id'))
- ->column('real_name');
- $v['to_user_name'] = implode(',',$toUserInfo);
- }
- $dispatchLog = Db::name('dispatch_log')
- ->where('order_id',$v['id'])
- ->find();
- if($dispatchLog && $dispatchLog['content']){
- $v['schedulContent'] = $dispatchLog['content'];
- }
- $pDep = '';
- $depInfo = Db::name('dep')
- ->where('id',$v['dep_id'])
- ->find();
- if($depInfo){
- $pDep = Db::name('dep_cate')
- ->where('id',$depInfo['cate_id'])
- ->value('title');
- }
- $v['dep_cate_name'] = $pDep;
- return $v;
- }
- //api格式化订单详情
- public function apiFormatOrder($v, $is_list = 0,$todo_id=0) {
- $v['delayReason'] = '';
- $v['videos'] = $v['videos']?$v['videos']:'';
- $v['voices'] = $v['voices']?$v['voices']:'';
- $v['images'] = $v['images']?$v['images']:'';
- $v['repair'] = null;
- if ($v['work_type_mode'] == 1) {
- $v['repair'] = [];
- $typeInfo = (new \app\common\model\OrderType())
- ->getTypeByOrderId($v['id']);
- $v['repair']['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : '';
- $v['repair']['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : '';
- $v['repair']['address_title'] = $typeInfo ? $typeInfo['address_title'] : '';
- $v['repair']['type_id'] = $typeInfo ? $typeInfo['type_id'] : 0;
- $v['repair']['address_id'] = $typeInfo ? $typeInfo['address_id'] : 0;
- $v['repair']['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : "";
- }
- $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
- $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
- $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content');
- $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content');
- $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name');
- $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile');
- $v['schedulContent'] = '';
- $v['send_time'] = $v['send_time']?$v['send_time']:"";
- $v['cancel_time'] = $v['cancel_time']?$v['cancel_time']:"";
- $v['finish_time'] = $v['finish_time']?$v['finish_time']:"";
- $v['quality_cate_name'] = '';
- if($v['work_type_mode']==15){
- $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']);
- }
- $dispatchLog = Db::name('dispatch_log')
- ->where('order_id',$v['id'])
- ->find();
- if($dispatchLog && $dispatchLog['content']){
- $v['schedulContent'] = $dispatchLog['content'];
- }
- if ($is_list > 0) {
- $v['todo_list'] = [];
- $t = [];
- if($todo_id >0){
- $t[] = ['id','=',$todo_id];
- }
- $todo = Db::name('todo')
- ->where('order_id', $v['id'])
- ->where($t)
- ->select();
- if (!empty($todo)) {
- $ysdign = model("Config")->getConfig("order_ys_sign",$v['org_id']);
- foreach ($todo as $k => $v1) {
- $todo[$k]['s_num'] = $v['s_num'];
- $todo[$k]['yssign'] = $ysdign == 1?1:0;
- $todo[$k]['to_real_name'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'real_name');
- $todo[$k]['todo_mode_text'] = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'in_content');
- $tx = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'out_content');
- $todo[$k]['startStatus'] = 0;
- $todo[$k]['mobile'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'mobile');;
- $todo[$k]['images'] = $v1['images']?$v1['images']:"";
- $todo[$k]['reject_time'] = $v1['reject_time']?$v1['reject_time']:"";
- $todo[$k]['confirm_time'] = $v1['confirm_time']?$v1['confirm_time']:"";
- $todo[$k]['done_time'] = $v1['done_time']?$v1['done_time']:"";
- $todo[$k]['sign'] = $v1['sign']?$v1['sign']:"";
- $todo[$k]['nodo_reason'] = $v1['nodo_reason']?$v1['nodo_reason']:'';
- $pause_name = '';
- $pause_reason = '';
- $pause_time = '';
- $pauseList = [];
- $pause_reason = $v1['pause_reason'];
- $pause_time = $v1['pause_time'];
- if($v1['todo_mode']==2 && $v1['pause']==1){
- $pause_name = '已挂起';
- $tx = $tx.'(已挂起)';
- }
- $pauseList = Db::name('todo_puase')
- ->where('todo_id',$v1['id'])
- ->select();
- $pauseList = $pauseList?$pauseList:[];
- $todo[$k]['pauseList'] = $pauseList;
- $todo[$k]['todo_mode_out_text'] = $tx;
- $todo[$k]['pause_name'] = $pause_name;
- $todo[$k]['pause_time'] = $pause_time;
- $todo[$k]['pause_reason'] = $pause_reason;
- if($v['work_type_mode'] == 3){
- $todo_convey = Db::name('todo_convey')
- ->where('todo_id',$v1['id'])
- ->find();
- $todo[$k]['startStatus'] = isset($todo_convey['start_time'])&& !empty($todo_convey['start_time'])?1:0;
- $todo[$k]['start_img'] = isset($todo_convey['start_img'])&& !empty($todo_convey['start_img'])?$todo_convey['start_img']:'';
- $todo[$k]['end_img'] = isset($todo_convey['end_img'])&& !empty($todo_convey['end_img'])?$todo_convey['end_img']:'';
- }
- $todo_mate = Db::name('todo_mate')
- ->where('todo_id',$v1['id'])
- ->select();
- $todo[$k]['cons'] = [];
- if(!empty($todo_mate)){
- $cons = Db::name('todo_mate_item')
- ->alias('a')
- ->join('mate_goods b','b.id = a.items_id')
- ->where('a.todo_mate_id','in',array_column($todo_mate,'id'))
- ->field('a.*,b.title')
- ->select();
- foreach ($cons as $k2=>$v2){
- $todo[$k]['cons'][] = [
- 'title' =>$v2['title'],
- 'total' =>$v2['total'],
- 'money' =>$v2['money'],
- 'total_money' =>$v2['total_money'],
- 'realName' =>$this->getTableField('user',['id'=>$v2['user_id']],'real_name'),
- ];
- }
- }
- }
- $v['todo_list'] = $todo;
- }
- $v['comment'] = null;
- if ($v['order_mode'] == 6) {
- $v['comment'] = [];
- $v['comment'] = Db::name('comment')
- ->where('type', $v['work_type_mode'])
- ->where('from_id', $v['id'])
- ->where('org_id', $v['org_id'])
- ->find();
- }
- $v['oc'] = null;
- if ($v['work_type_mode'] == 3) {
- $v['oc'] = [];
- $oc = Db::name('order_convey')
- ->where('order_id', $v['id'])
- ->find();
- $oc['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
- $oc['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
- $oc['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
- $oc['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
- $oc['priority'] = isset($this->priority[$oc['priority']]) ? $this->priority[$oc['priority']] : "";
- $sample = Db::name('order_convey_lis')
- ->where('order_id', $v['id'])
- ->column('barcode');
- $oc['sample'] = implode(',', $sample);
- $cate = $this->getTableField('convey_cate', ['id' => $oc['type']], 'cate');
- $oc['conveyTypeCate'] = $cate;
- $conveyEndCount = Db::name('order_convey_end')
- ->where('order_id',$v['id'])
- ->where('del',0)
- ->count();
- $ceNums = $conveyEndCount+1;
- $oc['typeScore'] = round($oc['score']*$ceNums,1);
- $v['oc'] = $oc;
- if ($cate == 1) {
- $ocp = Db::name('order_convey_patient')
- ->where('order_id', $v['id'])
- ->find();
- $v['oc']['ocp'] = $ocp;
- }
- }
- }
- $order_delay = Db::name('order_delay')
- ->alias('a')
- ->join('delay_reason b','a.delay_reason_id=b.id')
- ->where('a.order_id',$v['id'])
- ->field('a.*,b.title')
- ->find();
- if($order_delay && $order_delay['status']==0){
- $v['delayReason'] = $order_delay['title'];
- }
- if($v['name']){
- $v['real_name'] = $v['name'];
- }
- if($v['phone']){
- $v['mobile'] = $v['phone'];
- }
- return $v;
- }
- public function cancel($id, $userId, $type = 1, $reason = '') {//type=1用户取消2调度取消
- $info = $this
- ->where('id', $id)
- ->find();
- if (!in_array($info['order_mode'], [1, 4])) {
- $this->error = '当前订单不可取消';
- }
- $data = [
- 'cancel_reason' => $reason,
- 'cancel_user_id' => $userId,
- 'cancel_time' => getTime(),
- 'order_mode' => $type == 1 ? 2 : 3,
- ];
- $this->startTrans();
- try {
- $res = $this->where('id', $id)->update($data);
- if (!$res) {
- \exception('更新订单失败');
- }
- if ($type == 2) {
- $todo = Db::name('todo')
- ->where('order_id', $id)
- ->select();
- $tData = [
- 'cancel_reason' => $reason,
- 'todo_mode' => 6,
- ];
- if (!empty($todo)) {
- $res = Db::name('todo')
- ->where('order_id', $id)
- ->update($tData);
- if (!$res) {
- \exception('更新工单失败');
- }
- Db::name('task')
- ->where('bus_id', 'in', array_column($todo, 'id'))
- ->where('type', 1)
- ->delete();
- }
- }
- $this->commit();
- return true;
- } catch (Exception $e) {
- $this->error = $e->getMessage();
- $this->rollback();
- return false;
- }
- }
- public function del($orderId, $userId) {
- $this->startTrans();
- try {
- $res = $this
- ->where('id', $orderId)->update([
- 'del' => 1,
- 'del_user_id' => $userId,
- 'del_time' => getTime(),
- ]);
- if (!$res) {
- \exception('订单删除失败');
- }
- $todo = Db::name('todo')
- ->where('order_id', $orderId)
- ->select();
- if (!empty($todo)) {
- $res = Db::name('todo')
- ->where('order_id', $orderId)
- ->update(['del' => 1]);
- if (!$res) {
- \exception('工单删除失败');
- }
- Db::name('task')
- ->where('bus_id', 'in', array_column($todo, 'id'))
- ->where('type', 1)
- ->delete();
- }
- $this->commit();
- return true;
- } catch (Exception $e) {
- Db::rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- //获取订单派单人数
- public function sendUserNum($mode = 1,$orgId=0) {
- $name = 'org_send_order' . $mode;
- $value = (new \app\common\model\Config())
- ->getConfig($name,$orgId);
- return (!$value || $value == 0) ? 0 : 1;//0多人1单人
- }
- //派单
- public function send($id, $userId, $data) {
- $info = Db::name('orders')->where('id', $id)->find();
- $oldInfo = $info;
- if ($info['order_mode'] != 1) {
- $this->error = '当前订单不可派发';
- return false;
- }
- $data['userType'] = isset($data['userType']) ? $data['userType'] : 0;
- if($info['source_type']==4 && $oldInfo['work_type_mode'] ==0 && $info['is_deal']==0){//一键呼叫派发
- if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
- $this->error = '请选择转单类型';
- return false;
- }
- $config = Db::name('config')
- ->where('name','web_order_transfer_type')
- ->value('value');
- if(empty($config)){
- $workType = [];
- }else{
- $workType = explode('|',$config);
- }
- if(!in_array($data['work_type_mode'],$workType)){
- $this->error = '当前类型不能进行转单';
- return false;
- }
- if($data['userType']==0){
- $data['to_user_id'] = !isset($data['to_user_id'])?$data['to_user_id'.$data['work_type_mode']]:$data['to_user_id'];
- }else{
- $data['to_roles_id'] =!isset($data['to_roles_id'])? $data['to_roles_id'.$data['work_type_mode']]:$data['to_roles_id'];
- }
- $info['work_type_mode'] = $data['work_type_mode'];
- if ($data['work_type_mode'] == 3) {
- $result = validate('OrdersConvey')->check($data, []);
- if (true !== $result) {
- $this->error = validate('OrdersConvey')->getError();
- return false;
- }
- }
- }
- $pusharr = []; // 需要推送给工人
- $pushdis = []; // 需要推送给二级调度
- if ($data['userType'] == 0) {
- if (!isset($data['to_user_id']) || empty($data['to_user_id'])) {
- $this->error = '请选择执行人';
- return false;
- }
- if ($info['work_type_mode'] == 1) {
- if (!isset($data['type_id']) || empty($data['type_id'])) {
- $this->error = '请选择报修事项';
- return false;
- }
- }
- }
- else {
- if(two_dispatch_off($info['org_id'])!=1){
- $this->error = '二级调度开关未开启';
- return false;
- }
- if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) {
- $this->error = '请选择二级调度';
- return false;
- }
- }
- $this->startTrans();
- try {
- if ($data['userType'] == 0) {
- $send_user_num = $this->sendUserNum($info['work_type_mode'],$data['org_id']);
- $users = explode(',', $data['to_user_id']);
- if($send_user_num==1 && count($users) >1){
- $this->error = '执行人只能选择单人';
- return false;
- }
- $to_user_id = array_unique($users);
- $todoData = [
- 'order_id' => $id,
- 'todo_content' => $data['todo_content'],
- 'create_user_id' => $userId,
- 'org_id' => $data['org_id'],
- 'create_time' => getTime(),
- 'todo_mode' => 1,
- 'work_type_mode' => $info['work_type_mode'],
- 'create_yyyy' => date('Y'),
- 'create_yyyymm' => date('Ym'),
- 'create_yyyymmdd' => date('Ymd'),
- ];
- $taskData = [
- 'org_id' => $data['org_id'],
- 'type' => 1,
- 'start_time' => getTime(),
- 'create_time' => getTime(),
- ];
- if($info['work_type_mode']==1){
- if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) {
- $taskData['priority'] = isset($data['repair_priority']) && $data['repair_priority']==3?1:0;
- }else{
- $orderRepair = Db::name('order_repair')
- ->where('order_id',$id)
- ->find();
- $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0;
- }
- }else if ($info['work_type_mode']==3){
- if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) {
- $taskData['priority'] = isset($data['repair_priority']) && $data['repair_priority']==3?2:0;
- }else{
- $order_convey = Db::name('order_convey')
- ->where('order_id',$id)
- ->find();
- $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0;
- }
- }
- $sns = [];
- foreach ($to_user_id as $k => $v) {
- if(empty($v)){
- continue;
- }
- $todoData['to_user_id'] = $v;
- $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
- while (true){
- if(in_array($todoData['sn'],$sns)){
- $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
- }else{
- $sns[] = $todoData['sn'];
- break;
- }
- }
- $res = Db::name('todo')
- ->insertGetId($todoData);
- if (!$res) {
- \exception('执行人:' . $v . '派单失败');
- }
- $pusharr[] = [
- 'user_id' => $v,
- 'todo_id' => $res
- ];
- $taskData['user_id'] = $v;
- $taskData['bus_id'] = $res;
- $res = Db::name('task')
- ->insert($taskData);
- if (!$res) {
- \exception('执行人:' . $v . '任务保存失败');
- }
- }
- if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){
- $rr = ['order_mode' => 4, 'work_type_mode'=>$data['work_type_mode'],'send_time' => getTime(),'from'=>1];
- }else{
- $rr = ['order_mode' => 4, 'send_time' => getTime()];
- }
- $res = $this->where('id', $id)
- ->update($rr);
- if (!$res) {
- \exception('订单更新失败');
- }
- }
- else {
- $pRolesId = explode(',',$data['to_roles_id']);
- $pp = [];
- foreach ($pRolesId as $k=>$v){
- $logData = [
- 'user_id' => $userId,
- 'to_user_id' =>0,
- 'create_time' => getTime(),
- 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
- 'order_id' => $id,
- 'roles_id' => $v
- ];
- $pp[] = $logData;
- }
- $pushdis = Db::name('user_roles')
- ->where('roles_id','in',$pRolesId)
- ->column('user_id');
- $res = Db::name('dispatch_log')
- ->insertAll($pp);
- if (!$res) {
- \exception('派发失败');
- }
- if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){
- $rrs = ['is_deal' => 1, 'work_type_mode'=>$data['work_type_mode']];
- }else{
- $rrs = ['is_deal' => 1];
- }
- $res = $this->where('id', $id)
- ->update($rrs);
- if (!$res) {
- \exception('订单更新失败');
- }
- }
- if ($info['work_type_mode'] == 1) {//报修订单
- $rData = [];
- if (isset($data['type_id']) && !empty($data['type_id'])) {
- $rData['type_id'] = $data['type_id'];
- }
- if (isset($data['address_id']) && !empty($data['address_id'])) {
- $rData['address_id'] = $data['address_id'];
- }
- if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) {
- if (isset($data['repair_priority']) && !empty($data['repair_priority'])) {
- $rData['repair_priority'] = $data['repair_priority'];
- }
- }
- if (!empty($rData)) {
- $old = Db::name('order_repair')
- ->where('order_id', $id)
- ->find();
- if ($old) {
- $rData['update_time'] = getTime();
- $res = Db::name('order_repair')
- ->where('order_id', $id)
- ->update($rData);
- }
- else {
- $rData['order_id'] = $id;
- $res = Db::name('order_repair')
- ->insert($rData);
- }
- if (!$res) {
- \exception('保存维修扩展失败');
- }
- }
- }else{
- $old = Db::name('order_repair')
- ->where('order_id', $id)
- ->find();
- if ($old) {
- $res = Db::name('order_repair')
- ->where('order_id', $id)
- ->delete();
- if (!$res) {
- \exception('删除维修扩展失败');
- }
- }
- }
- if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){//一键呼叫 转单运送
- if ($data['work_type_mode'] == 3) {
- if (!$data['start'] || !$data['end']) {
- $this->error = '取件与送达地点不能为空';
- return false;
- }
- if ($data['start'] == $data['end']) {
- $this->error = '取件与送达地点不能是同一地点';
- return false;
- }
- if (!$data['xq_time'] || !$data['ywc_time']) {
- $this->error = '应完成时间应和需求时间不能为空';
- return false;
- }
- if ($data['xq_time'] >= $data['ywc_time']) {
- $this->error = '应完成时间应大于需求时间';
- return false;
- }
- if (strtotime($data['xq_time']) < time() - 10 * 60) {
- $this->error = '需求时间已过时';
- return false;
- }
- if (!$data['type']) {
- $this->error = '运送类型不能为空';
- return false;
- }
- // if(!$data['name']){
- // $this->error = '联系人不能为空';
- // return false;
- // }
- // if(!$data['phone']){
- // $this->error = '联系人电话不能为空';
- // return false;
- // }
- $ysData = array(
- 'order_id' => $id,
- 'type' => $data['type'],
- 'start' => $data['start'],
- 'end' => $data['end'],
- 'xq_time' => $data['xq_time'],
- 'ywc_time' => $data['ywc_time'],
- 'name' => $data['name'],
- 'phone' => $data['phone'],
- 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
- 'priority' => empty($data['priority']) ? 0 : $data['priority']
- );
- $check = Db::name('order_convey')
- ->where('order_id',$id)
- ->find();
- if($check){
- Db::name('order_convey')
- ->where('order_id',$id)
- ->update($ysData);
- }else{
- $oCid = Db::name('order_convey')
- ->insertGetId($ysData);
- if (!$oCid) {
- exception('运送信息保存失败');
- }
- }
- $conveyCate = Db::name('convey_cate')
- ->where('id', $data['type'])
- ->find();
- if ($conveyCate['cate'] == 1) {
- $patient = [
- 'order_id' => $id,
- 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
- 'name' => isset($data['p_name']) ? $data['p_name'] : '',
- 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
- 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
- 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
- ];
- $cp = Db::name('order_convey_patient')
- ->where('order_id',$id)
- ->find();
- if($cp){
- Db::name('order_convey_patient')
- ->where('order_id',$id)
- ->update($patient);
- }else{
- $ocPid = Db::name('order_convey_patient')
- ->insertGetId($patient);
- if (!$ocPid) {
- exception('保存运送病人扩展失败');
- }
- }
- }
- }else{
- Db::name('order_convey')
- ->where('order_id',$id)
- ->delete();
- Db::name('order_convey_patient')
- ->where('order_id',$id)
- ->delete();
- }
- }
- $this->commit();
- if($pusharr){
- foreach ($pusharr as $k=>$v){
- // 极光推送
- send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
- }
- }
- if($pushdis){
- foreach ($pushdis as $k=>$v){
- // 极光推送
- send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$info['id']]);
- }
- }
- // 极光推送
- send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]);
- return true;
- } catch (Exception $e) {
- $this->rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- //转单
- public function zd_send($id, $data) {
- $info = $this->where('id', $id)->find();
- if ($info['order_mode'] != 1) {
- $this->error = '当前订单不可转单';
- return false;
- }
- if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
- $this->error = '请选择转单类型';
- return false;
- }
- $config = Db::name('config')
- ->where('name','web_order_transfer_type')
- ->value('value');
- if(empty($config)){
- $workType = [];
- }else{
- $workType = explode('|',$config);
- }
- if(!in_array($data['work_type_mode'],[98,99])){
- if(!in_array($data['work_type_mode'],$workType)){
- $this->error = '当前类型不能进行转单';
- return false;
- }
- }
- $info['work_type_mode'] = $data['work_type_mode'];
- if ($data['work_type_mode'] == 3) {
- $result = validate('OrdersConvey')->check($data, []);
- if (true !== $result) {
- $this->error = validate('OrdersConvey')->getError();
- return false;
- }
- }
- if ($info['work_type_mode'] == 1) {
- if (!isset($data['type_id']) || empty($data['type_id'])) {
- $this->error = '请选择报修事项';
- return false;
- }
- }
- $this->startTrans();
- try {
- $rr['work_type_mode'] = $data['work_type_mode'];
- $rr['update_time'] = getTime();
- if($info['work_type_mode'] == 0 && $info['source_type'] == 4){
- $rr['from'] = 1;
- }
- $res = $this->where('id', $id)
- ->update($rr);
- if (!$res) {
- \exception('订单更新失败');
- }
- // if($info['work_type_mode'] == 3){
- // $score = empty($data['score'])?0:floatval($data['score']);
- // Db::name('order_convey')->where('order_id',$id)->update(['score'=>$score]);
- // }
- if ($info['work_type_mode'] == 1) {//报修订单
- $rData = [];
- if (isset($data['type_id']) && !empty($data['type_id'])) {
- $rData['type_id'] = $data['type_id'];
- }
- if (isset($data['address_id']) && !empty($data['address_id'])) {
- $rData['address_id'] = $data['address_id'];
- }
- if (isset($data['repair_priority']) && !empty($data['repair_priority'])) {
- $rData['repair_priority'] = $data['repair_priority'];
- }
- if (!empty($rData)) {
- $old = Db::name('order_repair')
- ->where('order_id', $id)
- ->find();
- if ($old) {
- $rData['update_time'] = getTime();
- $res = Db::name('order_repair')
- ->where('order_id', $id)
- ->update($rData);
- }
- else {
- $rData['order_id'] = $id;
- $res = Db::name('order_repair')
- ->insert($rData);
- }
- if (!$res) {
- \exception('保存维修扩展失败');
- }
- }
- }else{
- $old = Db::name('order_repair')
- ->where('order_id', $id)
- ->find();
- if ($old) {
- $res = Db::name('order_repair')
- ->where('order_id', $id)
- ->delete();
- if (!$res) {
- \exception('删除维修扩展失败');
- }
- }
- }
- if ($data['work_type_mode'] == 3) {
- if (!$data['start'] || !$data['end']) {
- $this->error = '取件与送达地点不能为空';
- return false;
- }
- if ($data['start'] == $data['end']) {
- $this->error = '取件与送达地点不能是同一地点';
- return false;
- }
- if (!$data['xq_time'] || !$data['ywc_time']) {
- $this->error = '应完成时间应和需求时间不能为空';
- return false;
- }
- if ($data['xq_time'] >= $data['ywc_time']) {
- $this->error = '应完成时间应大于需求时间';
- return false;
- }
- if (strtotime($data['xq_time']) < time() - 10 * 60) {
- $this->error = '需求时间已过时';
- return false;
- }
- if (!$data['type']) {
- $this->error = '运送类型不能为空';
- return false;
- }
- // if(!$data['name']){
- // $this->error = '联系人不能为空';
- // return false;
- // }
- // if(!$data['phone']){
- // $this->error = '联系人电话不能为空';
- // return false;
- // }
- $ysData = array(
- 'order_id' => $id,
- 'type' => $data['type'],
- 'start' => $data['start'],
- 'end' => $data['end'],
- 'xq_time' => $data['xq_time'],
- 'ywc_time' => $data['ywc_time'],
- 'name' => $data['name'],
- 'phone' => $data['phone'],
- 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
- 'priority' => empty($data['priority']) ? 0 : $data['priority']
- );
- $check = Db::name('order_convey')
- ->where('order_id',$id)
- ->find();
- if($check){
- Db::name('order_convey')
- ->where('order_id',$id)
- ->update($ysData);
- }else{
- $oCid = Db::name('order_convey')
- ->insertGetId($ysData);
- if (!$oCid) {
- exception('运送信息保存失败');
- }
- }
- $conveyCate = Db::name('convey_cate')
- ->where('id', $data['type'])
- ->find();
- if ($conveyCate['cate'] == 1) {
- $patient = [
- 'order_id' => $id,
- 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
- 'name' => isset($data['p_name']) ? $data['p_name'] : '',
- 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
- 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
- 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
- ];
- $cp = Db::name('order_convey_patient')
- ->where('order_id',$id)
- ->find();
- if($cp){
- Db::name('order_convey_patient')
- ->where('order_id',$id)
- ->update($patient);
- }else{
- $ocPid = Db::name('order_convey_patient')
- ->insertGetId($patient);
- if (!$ocPid) {
- exception('保存运送病人扩展失败');
- }
- }
- }
- }else{
- Db::name('order_convey')
- ->where('order_id',$id)
- ->delete();
- Db::name('order_convey_patient')
- ->where('order_id',$id)
- ->delete();
- }
- //转投诉
- if($data['work_type_mode']==98){
- $complain=[
- 'org_id'=>$info['org_id'],
- 'dep_id'=>$info['dep_id'],
- 'create_user_id'=>$info['user_id'],
- 'content'=>$info['content'],
- 'images'=>isset($info['images'])?$info['images']:'',
- 'voices'=>isset($info['voices'])?$info['voices']:'',
- 'videos'=>isset($info['videos'])?$info['videos']:'',
- 'create_time'=>getTime(),
- 'from'=>1,
- 'create_yyyy'=>date('Y'),
- 'create_yyyymm'=>date('Ym'),
- 'create_yyyymmdd'=>date('Ymd'),
- ];
- $ret = Db::name('complain')->insert($complain);
- if (!$ret) {
- exception('转投诉失败');
- }
- }
- //转隐患
- if($data['work_type_mode']==99){
- $complain=[
- 'org_id'=>$info['org_id'],
- 'dep_id'=>$info['dep_id'],
- 'create_user_id'=>$info['user_id'],
- 'content'=>$info['content'],
- 'images'=>isset($info['images'])?$info['images']:'',
- 'voices'=>isset($info['voices'])?$info['voices']:'',
- 'videos'=>isset($info['videos'])?$info['videos']:'',
- 'create_time'=>getTime(),
- 'from'=>1,
- 'create_yyyy'=>date('Y'),
- 'create_yyyymm'=>date('Ym'),
- 'create_yyyymmdd'=>date('Ymd'),
- ];
- $ret = Db::name('hiddendanger')->insert($complain);
- if (!$ret) {
- exception('转投诉失败');
- }
- }
- if(in_array($data['work_type_mode'],[98,99])){
- $res = $this
- ->where('id', $id)->update([
- 'del' => 1,
- 'work_type_mode' => 0,
- 'del_user_id' => $info['user_id'],
- 'del_time' => getTime(),
- ]);
- if (!$res) {
- \exception('订单删除失败');
- }
- }
- $this->commit();
- return true;
- } catch (Exception $e) {
- $this->rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- //批量派单
- public function batchsend($id, $userId, $data) {
- $ids = explode(',',$id);
- $infos = $this->where('id', 'in',$ids)->select();
- $pusharr = []; // 需要推送给工人
- foreach ($infos as $k=>$info){
- if ($info['order_mode'] != 1) {
- $this->error = '当前选择订单中包含已派发订单,请重新选择';
- return false;
- }
- $data['userType'] = isset($data['userType']) ? $data['userType'] : 0;
- if ($data['userType'] == 0) {
- if (!isset($data['to_user_id']) || empty($data['to_user_id'])) {
- $this->error = '请选择执行人';
- return false;
- }
- if ($info['work_type_mode'] == 1) {
- if (!isset($data['type_id']) || empty($data['type_id'])) {
- $this->error = '请选择报修事项';
- return false;
- }
- }
- }
- else {
- if(two_dispatch_off($info['org_id'])!=1){
- $this->error = '二级调度开关未开启';
- return false;
- }
- if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) {
- $this->error = '请选择二级调度';
- return false;
- }
- }
- $this->startTrans();
- try {
- if ($data['userType'] == 0) {
- $send_user_num = $this->sendUserNum($info['work_type_mode'],$data['org_id']);
- $users = explode(',', $data['to_user_id']);
- if($send_user_num==1 && count($users) >1){
- $this->error = '执行人只能选择单人';
- return false;
- }
- $to_user_id = array_unique($users);
- $todoData = [
- 'order_id' => $info['id'],
- 'todo_content' => $data['todo_content'],
- 'create_user_id' => $userId,
- 'org_id' => $data['org_id'],
- 'create_time' => getTime(),
- 'todo_mode' => 1,
- 'work_type_mode' => $info['work_type_mode'],
- 'create_yyyy' => date('Y'),
- 'create_yyyymm' => date('Ym'),
- 'create_yyyymmdd' => date('Ymd'),
- ];
- $taskData = [
- 'org_id' => $data['org_id'],
- 'type' => 1,
- 'start_time' => getTime(),
- 'create_time' => getTime(),
- ];
- $sns = [];
- foreach ($to_user_id as $k => $v) {
- if(empty($v)){
- continue;
- }
- $todoData['to_user_id'] = $v;
- $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
- while (true){
- if(in_array($todoData['sn'],$sns)){
- $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
- }else{
- $sns[] = $todoData['sn'];
- break;
- }
- }
- $res = Db::name('todo')
- ->insertGetId($todoData);
- if (!$res) {
- \exception('执行人:' . $v . '派单失败');
- }
- $pusharr[] = [
- 'user_id' => $v,
- 'todo_id' => $res
- ];
- $taskData['user_id'] = $v;
- $taskData['bus_id'] = $res;
- $res = Db::name('task')
- ->insert($taskData);
- if (!$res) {
- \exception('执行人:' . $v . '任务保存失败');
- }
- }
- $res = $this->where('id', $info['id'])
- ->update(['order_mode' => 4, 'send_time' => getTime()]);
- if (!$res) {
- \exception('订单更新失败');
- }
- }
- else {
- $logData = [
- 'user_id' => $userId,
- 'to_user_id' => 0,
- 'create_time' => getTime(),
- 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
- 'order_id' => $info['id'],
- 'roles_id' => $data['to_roles_id']
- ];
- $res = Db::name('dispatch_log')
- ->insertGetId($logData);
- if (!$res) {
- \exception('派发失败');
- }
- $res = $this->where('id', $info['id'])
- ->update(['is_deal' => 1]);
- if (!$res) {
- \exception('订单更新失败');
- }
- }
- if ($info['work_type_mode'] == 1) {//报修订单
- $rData = [];
- if (isset($data['type_id']) && !empty($data['type_id'])) {
- $rData['type_id'] = $data['type_id'];
- }
- if (isset($data['address_id']) && !empty($data['address_id'])) {
- $rData['address_id'] = $data['address_id'];
- }
- if (!empty($rData)) {
- $old = Db::name('order_repair')
- ->where('order_id', $info['id'])
- ->find();
- if ($old) {
- $rData['update_time'] = getTime();
- $res = Db::name('order_repair')
- ->where('order_id', $info['id'])
- ->update($rData);
- }
- else {
- $rData['order_id'] = $info['id'];
- $res = Db::name('order_repair')
- ->insert($rData);
- }
- if (!$res) {
- \exception('保存维修扩展失败');
- }
- }
- }
- $this->commit();
- } catch (Exception $e) {
- $this->rollback();
- $this->error = $e->getMessage();
- return false;
- }
- // 极光推送
- send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]);
- }
- if($pusharr){
- foreach ($pusharr as $k=>$v){
- // 极光推送
- send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
- }
- }
- return true;
- }
- //完成订单
- public function finish($id) {
- $info = $this->where('id', $id)->find();
- if ($info['order_mode'] != 4) {
- $this->error = '当前订单不可完成';
- return false;
- }
- $this->startTrans();
- try {
- $res = $this->where('id', $id)
- ->update([
- 'order_mode' => 5,
- 'finish_time' => getTime(),
- ]);
- if (!$res) {
- \exception('订单更新失败');
- }
- $todo = Db::name('todo')
- ->where('order_id', $id)
- ->select();
- if (!empty($todo)) {
- foreach ($todo as $kk=>$vv){
- $wh = [
- 'todo_mode' => 3,
- 'done_time' => getTime(),
- 'wc_time'=>time() - strtotime($vv['confirm_time'])
- ];
- if($vv['todo_mode']==1){
- $wh['confirm_time'] = $vv['create_time'];
- $wh['wc_time'] = time() - strtotime($vv['create_time']);
- }
- $res = Db::name('todo')
- ->where('id', $vv['id'])
- ->update($wh);
- if (!$res) {
- \exception('工单更新失败');
- }
- }
- $res = Db::name('task')
- ->where('bus_id', 'in', array_column($todo, 'id'))
- ->where('type', 1)
- ->delete();
- if (!$res) {
- \exception('任务删除失败');
- }
- }
- $this->commit();
- // 极光推送
- send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_FINISH,'',['id'=>$info['id']]);
- return true;
- } catch (Exception $e) {
- $this->rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- //批量完成订单
- public function batchfinish($id) {
- $ids = explode(',',$id);
- $infos = $this->where('id', 'in',$ids)->select();
- $pusharr = []; // 需要推送
- foreach ($infos as $k=>$info){
- if ($info['order_mode'] != 4) {
- $this->error = '当前所选订单有不可完成的订单';
- return false;
- }
- $this->startTrans();
- try {
- $res = $this->where('id', $info['id'])
- ->update([
- 'order_mode' => 5,
- 'finish_time' => getTime(),
- ]);
- if (!$res) {
- \exception('订单更新失败');
- }
- $todo = Db::name('todo')
- ->where('order_id', $info['id'])
- ->select();
- if (!empty($todo)) {
- foreach ($todo as $kk=>$vv){
- $wh = [
- 'todo_mode' => 3,
- 'done_time' => getTime(),
- 'wc_time'=>time() - strtotime($vv['confirm_time'])
- ];
- if($vv['todo_mode']==1){
- $wh['confirm_time'] = $vv['create_time'];
- $wh['wc_time'] = time() - strtotime($vv['create_time']);
- }
- $res = Db::name('todo')
- ->where('id', $vv['id'])
- ->update($wh);
- if (!$res) {
- \exception('工单更新失败');
- }
- }
- Db::name('task')
- ->where('bus_id', 'in', array_column($todo, 'id'))
- ->where('type', 1)
- ->delete();
- }
- $this->commit();
- $pusharr[] = [
- 'user_id' => $info['user_id'],
- 'id' => $info['id']
- ];
- } catch (Exception $e) {
- $this->rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- if($pusharr){
- foreach ($pusharr as $k=>$v){
- // 极光推送
- send_jpush([$v['user_id']],AppMsg::PUSH_USER_ORDER_FINISH,'',['id'=>$v['id']]);
- }
- }
- return true;
- }
- //评价订单
- public function addComment($id, $score, $content, $userId, $org_id) {
- $info = $this->where('id', $id)->find();
- if (!$info) {
- $this->error = '订单不存在';
- return false;
- }
- if ($info['user_id'] != $userId) {
- $this->error = '订单只可由发起人评价';
- return false;
- }
- if ($info['order_mode'] != 5) {
- $this->error = '此状态不能评价';
- return false;
- }
- $jsonArray = [
- 'score'=>$score,
- 'content'=>isset($content)?$content:'',
- 'userId'=>$userId,
- 'type'=>$info['work_type_mode'],
- 'fromId'=>$id,
- 'orgId'=>$org_id,
- ];
- $ret = model('Comment')->addSave($jsonArray);
- if(!$ret){
- $this->error = model('Comment')->getError();
- return false;
- }
- return true;
- }
- //api 调度订单列表
- public function lists($page, $size, $cur_order_mode, $userId, $orgId) {
- $map[] = ['org_id', '=', $orgId];
- $map[] = ['del', '=', 0];
- $map2[] = ['todo_mode', '=', 4];
- $map1 = [];
- $auth = get_dispatch_auth($userId);
- if (empty($auth)) {
- $map[] = ['work_type_mode', '=', -1];
- }
- else {
- $map[] = ['work_type_mode', 'in', $auth];
- }
- $org_two_dispatch = (new Config())->getConfig('org_two_dispatch',$orgId);
- if($org_two_dispatch==0){//二级调度开关关闭
- if ($cur_order_mode == 0) {//待处理
- $map[] = ['order_mode', 'in', [1]];
- $map[] = ['is_deal', '=', 0];
- }
- else {
- if ($cur_order_mode == 1) {//已处理
- $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
- }
- }
- }else{
- if (check_two_dispatch($userId) == 1) {//二级调度角色
- $roles_id = Db::name('user_roles')
- ->where('user_id',$userId)
- ->value('roles_id');
- $ids = Db::name('dispatch_log')
- ->where('roles_id', $roles_id)
- ->whereOr('to_user_id', $userId)
- ->column('order_id');
- if ($cur_order_mode == 2) {//驳回
- if (empty($ids)) {
- $map2[] = ['order_id', '=', -1];
- }
- else {
- $map2[] = ['order_id', 'in', $ids];
- }
- }
- else {
- if (empty($ids)) {
- $map[] = ['id', '=', -1];
- }
- else {
- $map[] = ['id', 'in', $ids];
- }
- }
- if ($cur_order_mode == 0) {//待处理
- $map[] = ['order_mode', 'in', [1]];
- }
- else {
- if ($cur_order_mode == 1) {//已处理
- $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
- }
- }
- }
- else {
- if ($cur_order_mode == 0) {//待处理
- $map[] = ['order_mode', 'in', [1]];
- $map[] = ['is_deal', '=', 0];
- }
- else {
- if ($cur_order_mode == 1) {//已处理
- $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
- $map1[] = ['is_deal', '=', 1];
- }
- }
- }
- }
- $offset = ($page - 1) * $size;
- if ($cur_order_mode == 2) { //驳回订单
- $list = Db::name('todo_view')
- ->where($map)
- ->where($map2)
- ->limit($offset, $size)
- ->order('id', 'desc')
- ->select();
- $data = [];
- foreach ($list as $k => $v) {
- $order = $this->where('id', $v['order_id'])
- ->find()->toArray();
- $v1 = $this->formatOrder($order);
- $v1['is_transfer'] = ($order['source_type']==4 && $order['order_mode']==1 && $order['is_deal']==0)?1:0;
- $ll = formatArray(
- [
- 'id',
- 'order_mode',
- 'create_time',
- 'source_type',
- 'work_type_mode',
- 'send_time',
- 'work_type_mode_text',
- 'dep',
- 'order_mode_text',
- 'order_mode_out_text',
- 'real_name',
- 'is_transfer',
- 'task_priority',
- 'sn'
- ]
- , $v1);
- if($order['from'] > 0){
- $from = '';
- if($order['from'] == 1){
- $from = '一键呼叫';
- }else if($order['from'] == 2){
- $from = '隐患预警';
- }else if($order['from'] == 3){
- $from = '投诉';
- }
- $ll['work_type_mode_text'] = $ll['work_type_mode_text'].'('.$from.')';
- }
- $ll['todo_id'] = $v['id'];
- $data[] = $ll;
- }
- return $data;
- }
- $list = $this->where($map)
- ->whereOr($map1)
- ->limit($offset, $size)
- ->order('id', 'desc')
- ->select();
- $list = $list ? $list->toArray() : [];
- $data = [];
- foreach ($list as $k => $v) {
- $v = $this->formatOrder($v);
- $v['is_transfer'] = ($v['source_type']==4 && $v['order_mode']==1 && $v['is_deal']==0)?1:0;
- $ll = formatArray(
- [
- 'id',
- 'order_mode',
- 'create_time',
- 'source_type',
- 'work_type_mode',
- 'send_time',
- 'work_type_mode_text',
- 'dep',
- 'order_mode_text',
- 'order_mode_out_text',
- 'real_name',
- 'is_transfer',
- 'task_priority',
- 'sn'
- ]
- , $v);
- if($v['from'] > 0){
- $from = '';
- if($v['from'] == 1){
- $from = '一键呼叫';
- }else if($v['from'] == 2){
- $from = '隐患预警';
- }else if($v['from'] == 3){
- $from = '投诉';
- }
- $ll['work_type_mode_text'] = $ll['work_type_mode_text'].'('.$from.')';
- }
- $data[] = $ll;
- }
- return $data;
- }
- //api 发起人品控订单列表
- public function userQualityLists($page, $size, $userId, $orgId) {
- $map[] = ['org_id', '=', $orgId];
- $map[] = ['del', '=', 0];
- $map[] = ['user_id', '=', $userId];
- $map[] = ['work_type_mode', '=', 15];
- $offset = ($page - 1) * $size;
- $list = $this->where($map)
- ->limit($offset, $size)
- ->order('id', 'desc')
- ->select();
- $list = $list ? $list->toArray() : [];
- $data = [];
- foreach ($list as $k => $v) {
- $v = $this->formatOrder($v);
- $ll = formatArray(
- [
- 'id',
- 'create_time',
- 'informer_user_name',
- 'order_mode',
- 'order_mode_out_text',
- 'to_user_name',
- 'real_name',
- 'sn'
- ]
- , $v);
- $data[] = $ll;
- }
- return $data;
- }
- //api 知会人品控订单列表
- public function informerQualityLists($page, $size, $userId, $orgId) {
- $map[] = ['org_id', '=', $orgId];
- $map[] = ['del', '=', 0];
- $map[] = ['informer_user', '=', $userId];
- $map[] = ['work_type_mode', '=', 15];
- $offset = ($page - 1) * $size;
- $list = $this->where($map)
- ->limit($offset, $size)
- ->order('id', 'desc')
- ->select();
- $list = $list ? $list->toArray() : [];
- $data = [];
- foreach ($list as $k => $v) {
- $v = $this->formatOrder($v);
- $ll = formatArray(
- [
- 'id',
- 'create_time',
- 'informer_user_name',
- 'order_mode',
- 'order_mode_out_text',
- 'to_user_name',
- 'real_name',
- 'sn'
- ]
- , $v);
- $data[] = $ll;
- }
- return $data;
- }
- //api 订单详情
- public function detail($orderId) {
- $info = $this->where('id', $orderId)
- ->find();
- if (empty($info)) {
- HelpHander::error('订单信息不存在');
- }
- $is_ch = 0;
- if($info['order_mode'] == 4){
- $todoInfo = Db::name('todo')->where('order_id',$info['id'])->where('del',0)->where('todo_mode','>',1)->find();
- if(!$todoInfo){
- $is_ch = 1;
- }
- }
- $info['is_ch'] = $is_ch;
- $info = $info->toArray();
- $info = $this->apiFormatOrder($info, 1);
- if($info['work_type_mode'] == 3){
- // 地点路径
- $conveyends = Db::name('order_convey_end')
- ->alias('a')
- ->join('address b','b.id = a.addr')
- ->where('a.order_id',$orderId)
- ->order('a.id asc')
- ->field('a.*,b.title')
- ->select();
- $info['ends'] = $conveyends?$conveyends:[];
- $payinfo = [
- 'is_pay' => 0,
- 'id' => 0,
- 'url' => '',
- 'money' => 0,
- 'remark' => '',
- 'type' => 1,
- 'pay_time' => '',
- ];
- // 是否需要支付
- $pay = Db::name('order_convey_pay')->where('order_id',$orderId)->find();
- if($pay){
- $payinfo['is_pay'] = $pay['status'] == 0?0:1;
- $payinfo['url'] = url('h5/Index/pay',['id'=>$pay['id']],false,true);
- $payinfo['money'] = round($pay['money'],2);
- $payinfo['remark'] = $pay['remark'];
- $payinfo['pay_time'] = $pay['pay_time']?$pay['pay_time']:'';
- $payinfo['id'] = $pay['id'];
- }
- $info['pay'] = $payinfo;
- }
- return $info;
- }
- //抢单列表
- public function queryGrabOrders($page, $size, $userId, $orgId) {
- $rolesId = Db::name('user_roles')
- ->where('user_id', $userId)
- ->value('roles_id');
- $rolesInfo = Db::name('roles')
- ->where('id', $rolesId)
- ->find();
- if ($rolesInfo['parent_id'] > 0) {
- $rolesId = $rolesInfo['parent_id'];
- }
- $workType = Db::name('work_type_mode')
- ->where('type', 1)
- ->where('roles', 'like', '%' . $rolesId . '%')
- ->column('id');
- if (empty($workType)) {
- return [];
- }
- $n = [];
- foreach ($workType as $k=>$v){
- $orgGrabOrder = (new Config())->getConfig('org_grab_order'.$v,$orgId);
- if($orgGrabOrder && $orgGrabOrder>0){
- $n[] = $v;
- }
- }
- if(empty($n)){
- return [];
- }
- $workType = $n;
- $map[] = ['org_id', '=', $orgId];
- $map[] = ['del', '=', 0];
- $map[] = ['order_mode', 'in', [1]];
- $map[] = ['work_type_mode', 'in', $workType];
- $offset = ($page - 1) * $size;
- $list = $this->where($map)
- // ->limit($offset, $size)
- ->order('id', 'desc')
- ->select();
- $list = $list ? $list->toArray() : [];
- $data = [];
- // 预约类型-取15分钟后之前的订单
- $curtime15 = time()+60*15;
- foreach ($list as $k => $v) {
- $rr = formatArray(
- ['id', 'content', 'create_time', 'work_type_mode']
- , $v);
- $rr['depName'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
- $convey = Db::name('order_convey')->where('order_id',$v['id'])->find();
- $rr['start_address'] = '';
- $rr['end_address'] = '';
- $rr['convey_title'] = '';
- if($v['work_type_mode'] == 3){
- if($convey){
- $rr['start_address'] = Db::name('address')->where('id',$convey['start'])->value('title');
- $rr['end_address'] = Db::name('address')->where('id',$convey['end'])->value('title');
- $cate = Db::name('convey_cate')->where('id',$convey['type'])->find();
- $rr['convey_title'] = $cate?$cate['title']:'';
- $conveyCate = Db::name('convey_cate')->where('id',$convey['type'])->find();
- if($conveyCate && $conveyCate['priority'] != 4 && $convey['xq_time'] < date('Y-m-d H:i:s',$curtime15)){
- $data[] = $rr;
- }
- }
- }else{
- $data[] = $rr;
- }
- }
- $newData = array_page_list($data,$page,$size);
- return $newData;
- }
- //抢单
- public function grabOrdersEdit($order, $userId,$orderType,$addressId) {
- $this->startTrans();
- try {
- $data = [
- 'order_mode' => 4,
- 'send_time' => getTime(),
- ];
- $res = $this->where('id', $order['id'])->update($data);
- if (!$res) {
- \exception('订单修改失败');
- }
- $orders = Db::name('orders')
- ->where('id',$order['id'])
- ->find();
- if($orders['work_type_mode']==1){
- $p = [
- 'type_id'=>$orderType,
- 'address_id'=>$addressId,
- ];
- $repair = Db::name('order_repair')
- ->where('order_id', $order['id'])
- ->find();
- if($repair){
- $r = Db::name('order_repair')
- ->where('id', $repair['id'])
- ->update($p);
- }else{
- $p['order_id'] = $order['id'];
- $r = Db::name('order_repair')->insert($p);
- }
- if (!$r) {
- \exception('订单扩展修改失败');
- }
- }
- $todoData = [
- 'order_id' => $order['id'],
- 'todo_content' => '',
- 'to_user_id' => $userId,
- 'create_user_id' => $userId,
- 'org_id' => $order['org_id'],
- 'create_time' => getTime(),
- 'xy_time' => time()-strtotime($orders['create_time']),
- 'confirm_time' => getTime(),
- 'todo_mode' => 2,
- 'work_type_mode' => $order['work_type_mode'],
- 'create_yyyy' => date('Y'),
- 'create_yyyymm' => date('Ym'),
- 'create_yyyymmdd' => date('Ymd'),
- 'sn' => get_unique_sn(get_config('sn_prefix')),
- ];
- $res = Db::name('todo')
- ->insertGetId($todoData);
- if (!$res) {
- \exception('抢单失败');
- }
- $taskData = [
- 'org_id' => $order['org_id'],
- 'type' => 1,
- 'start_time' => getTime(),
- 'create_time' => getTime(),
- ];
- $taskData['user_id'] = $userId;
- $taskData['bus_id'] = $res;
- if($orders['work_type_mode']==1){
- $orderRepair = Db::name('order_repair')
- ->where('order_id',$orders['id'])
- ->find();
- $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0;
- }else if ($orders['work_type_mode']==3){
- $order_convey = Db::name('order_convey')
- ->where('order_id',$orders['id'])
- ->find();
- $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0;
- }
- $res = Db::name('task')
- ->insert($taskData);
- if (!$res) {
- \exception('任务保存失败');
- }
- $this->commit();
- return true;
- } catch (Exception $e) {
- $this->error = $e->getMessage();
- $this->rollback();
- return false;
- }
- }
- //用户订单列表
- public function userOrdersByMode($page,$size,$userId,$mode,$type,$orgId){
- $map[] = ['del', '=', 0];
- $map[] = ['user_id', '=', $userId];
- if($mode >0){
- $map[] = ['work_type_mode', '=', $mode];
- }
- if($type >0 && in_array($type,[1,5,6])){
- $map[] = ['order_mode', '=', $type];
- }elseif ($type==7){//处理中
- $map[] = ['order_mode', 'in', [1,4]];
- }elseif ($type==8){//已挂起
- $ids = Db::name('todo')
- ->where('org_id',$orgId)
- ->where('del',0)
- ->where('work_type_mode',1)
- ->where('todo_mode',2)
- ->where('pause',1)
- ->column('order_id');
- if(empty($ids)){
- $map[] = ['id', '=', -1];
- }else{
- $map[] = ['id', 'in', $ids];
- }
- }elseif ($type==9){//已取消
- $map[] = ['order_mode', 'in', [2,3,7]];
- }
- $offset = ($page - 1) * $size;
- $list = $this->where($map)
- ->limit($offset, $size)
- ->order('id', 'desc')
- ->select();
- $list = $list ? $list->toArray() : [];
- $data = [];
- foreach ($list as $k => $v) {
- $v = $this->formatOrder($v);
- $order_delay = Db::name('order_delay')
- ->alias('a')
- ->join('delay_reason b','a.delay_reason_id=b.id')
- ->where('a.order_id',$v['id'])
- ->field('a.*,b.title')
- ->find();
- if($order_delay && $order_delay['status']==0){
- $v['order_mode_out_text'] = '延迟中';
- $v['delay_reason'] = $order_delay['title'];
- }
- if($type==8){
- $v['order_mode_out_text'] = $v['order_mode_out_text'].'(已挂起)';
- }
- $ll = formatArray(
- [
- 'id',
- 'order_mode',
- 'create_time',
- 'source_type',
- 'work_type_mode',
- 'send_time',
- 'work_type_mode_text',
- 'dep',
- 'order_mode_text',
- 'order_mode_out_text',
- 'real_name',
- 'sn'
- ]
- , $v);
- $data[] = $ll;
- }
- return $data;
- }
- //所有订单列表
- public function queryAllOrder($orgId,$page,$size,$workTypeMode,$currOrderMode,$beginTime,$endTime){
- if(in_array($workTypeMode,[0,1,2,3,4,15])){
- $map[] = ['work_type_mode','=',$workTypeMode];
- }
- if($currOrderMode && $currOrderMode > 0){
- $map[] = ['order_mode','=',$currOrderMode];
- }
- if($beginTime){
- $map[] = ['create_time','>=',$beginTime.' 00:00:00'];
- }
- if($endTime){
- $map[] = ['create_time','<=',$endTime.' 23:59:59'];
- }
- $map[] = ['del', '=', 0];
- $map[] = ['org_id', '=', $orgId];
- $offset = ($page - 1) * $size;
- $list = $this->where($map)
- ->limit($offset, $size)
- ->order('id', 'desc')
- ->select();
- $list = $list ? $list->toArray() : [];
- $data = [];
- foreach ($list as $k => $v) {
- $v = $this->apiFormatOrder($v);
- $ll = formatArray(
- [
- 'id',
- 'order_mode',
- 'create_time',
- 'source_type',
- 'work_type_mode',
- 'send_time',
- 'work_type_mode_text',
- 'dep',
- 'order_mode_text',
- 'order_mode_out_text',
- 'real_name',
- 'sn'
- ]
- , $v);
- $data[] = $ll;
- }
- return $data;
- }
- //延迟任务
- public function delay_reason($id,$delayReasonId){
- $ids = explode(',',$id);
- $this->startTrans();
- try{
- foreach ($ids as $id){
- $info = $this->where('id',$id)->find();
- if (!in_array($info['order_mode'],[1,4])) {
- \exception('选择的订单状态不能延迟');
- }
- $res = Db::name('order_delay')
- ->where('order_id',$id)
- ->where('status',0)
- ->find();
- if($res){
- \exception('选择的订单已存在延时任务');
- }
- $curTime = date('Y-m-d H:i:s');
- $res =Db::name('order_delay')
- ->insert([
- 'order_id' => $id,
- 'delay_reason_id' => $delayReasonId,
- 'start_time' => $curTime,
- 'status' => 0
- ]);
- if(!$res){
- \exception('添加失败');
- }
- }
- $this->commit();
- return true;
- }catch (Exception $e){
- $this->rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- //取消延迟
- public function batchcanceldelay($id){
- $ids = explode(',',$id);
- $this->startTrans();
- try{
- foreach ($ids as $id){
- $info = Db::name('order_delay')
- ->where('order_id',$id)
- ->where('status',0)
- ->find();
- if(!$info){
- \exception('选择的订单不存在延时任务');
- }
- $curTime = date('Y-m-d H:i:s');
- $res =Db::name('order_delay')
- ->where('order_id',$id)
- ->update([
- 'end_time' => $curTime,
- 'diff' => strtotime($curTime) - strtotime($info['start_time']),
- 'status' => 1
- ]);
- if(!$res){
- \exception('添加失败');
- }
- }
- $this->commit();
- return true;
- }catch (Exception $e){
- $this->rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- public function addConveyEnd($orgId,$orderId,$addrId){
- $order = Db::name('orders')
- ->where('org_id',$orgId)
- ->where('id',$orderId)
- ->where('order_mode',4)
- ->where('del',0)
- ->where('work_type_mode',3)
- ->find();
- if(!$order){
- $this->error = '订单不存在';
- return false;
- }
- // 检查订单是不是病人运送
- $convey = Db::name('order_convey')
- ->alias('a')
- ->join('convey_cate b','a.type = b.id')
- ->where('a.order_id',$orderId)
- ->where('b.cate',1)
- ->field('a.*')
- ->find();
- if(!$convey){
- $this->error = '订单不存在';
- return false;
- }
- Db::startTrans();
- try{
- $ret = Db::name('order_convey_end')->insert([
- 'order_id' => $orderId,
- 'addr' => $convey['end'],
- 'order_convey_id' => $convey['id'],
- 'create_time' => date('Y-m-d H:i:s')
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- // 更换目的地
- if($addrId != $convey['end']){
- $res = Db::name('order_convey')->where('id', $convey['id'])->update([
- 'end' => $addrId,
- ]);
- if(!$res){
- \exception('操作失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- // 途经点点扫码
- public function sweepCode($code,$id,$num=0,$img='',$orgId=0,$userId=0){
- $addrs = model('address')->checkAddrCode($code,2,$orgId);
- if(!$addrs){
- HelpHander::error('二维码不正确');
- }
- $info = Db::name('order_convey_end')->where('id',$id)->find();
- if(!$info){
- $this->error = '数据不存在';
- return false;
- }
- if($info['addr'] != $addrs['id']){
- $this->error = '地点不正确';
- return false;
- }
- $totalNum = Db::name('orders')->where('id',$info['order_id'])->value('total_num');
- $totalNum2 = Db::name('order_convey_end')->where('order_id',$info['order_id'])->where('del',0)->where('scan',1)->sum('num');
- $t2 = $totalNum2+$num;
- if(intval($t2) > intval($totalNum) ){
- $this->error = '送达数量不能超过领取总数量';
- return false;
- }
- $res = Db::name('order_convey_end')->where('id',$id)->update([
- 'scan' => 1,
- 'update_time' => date('Y-m-d H:i:s'),
- 'num'=>$num,
- 'img'=>$img,
- 'user_id'=>$userId
- ]);
- if(!$res){
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- //api获取调度,待处理,已处理,驳回数量
- public function orderCount($userId, $orgId,$cur_order_mode=0) {
- $map[] = ['org_id', '=', $orgId];
- $map[] = ['del', '=', 0];
- $map2[] = ['todo_mode', '=', 4];
- $map1 = [];
- $auth = get_dispatch_auth($userId);
- if (empty($auth)) {
- $map[] = ['work_type_mode', '=', -1];
- }
- else {
- $map[] = ['work_type_mode', 'in', $auth];
- }
- $org_two_dispatch = (new Config())->getConfig('org_two_dispatch',$orgId);
- if($org_two_dispatch==0){//二级调度开关关闭
- if ($cur_order_mode == 0) {//待处理
- $map[] = ['order_mode', 'in', [1]];
- $map[] = ['is_deal', '=', 0];
- }
- else {
- if ($cur_order_mode == 1) {//已处理
- $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
- }
- }
- }else{
- if (check_two_dispatch($userId) == 1) {//二级调度角色
- $roles_id = Db::name('user_roles')
- ->where('user_id',$userId)
- ->value('roles_id');
- $ids = Db::name('dispatch_log')
- ->where('roles_id', $roles_id)
- ->whereOr('to_user_id', $userId)
- ->column('order_id');
- if ($cur_order_mode == 2) {//驳回
- if (empty($ids)) {
- $map2[] = ['order_id', '=', -1];
- }
- else {
- $map2[] = ['order_id', 'in', $ids];
- }
- }
- else {
- if (empty($ids)) {
- $map[] = ['id', '=', -1];
- }
- else {
- $map[] = ['id', 'in', $ids];
- }
- }
- if ($cur_order_mode == 0) {//待处理
- $map[] = ['order_mode', 'in', [1]];
- }
- else {
- if ($cur_order_mode == 1) {//已处理
- $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
- }
- }
- }
- else {
- if ($cur_order_mode == 0) {//待处理
- $map[] = ['order_mode', 'in', [1]];
- $map[] = ['is_deal', '=', 0];
- }
- else {
- if ($cur_order_mode == 1) {//已处理
- $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
- $map1[] = ['is_deal', '=', 1];
- }
- }
- }
- }
- if ($cur_order_mode == 2) { //驳回订单
- $count = Db::name('todo_view')
- ->where($map)
- ->where($map2)
- ->count();
- return $count;
- }
- $count = $this->where($map)
- ->whereOr($map1)
- ->count();
- return $count;
- }
- public function addBxOrders($data){
- if($data['type_id'] < 1){
- $this->error = '报修事项不能为空';
- return false;
- }
- if($data['repair_priority'] < 1){
- $this->error = '请选择紧急程度';
- return false;
- }
- if(empty($data['images']) && empty($data['voices']) && empty($data['content'])){
- $this->error = '内容不能为空';
- return false;
- }
- $odata = [
- 'user_id'=>$data['user_id'],
- 'org_id'=> $data['org_id'],
- 'dep_id'=>$data['dep_id'],
- 'images'=>$data['images'],
- 'videos'=>$data['videos'],
- 'voices'=>$data['voices'],
- 'content'=>$data['content'],
- 'work_type_mode'=>$data['work_type_mode'],
- 'source_type'=>$data['source_type'],
- 'sn'=>get_unique_sn(get_config('sn_prefix')),
- 'order_mode'=>4,
- 'create_time'=>date('Y-m-d H:i:s'),
- 'send_time'=>date('Y-m-d H:i:s'),
- 'create_yyyy'=>date('Y'),
- 'create_yyyymm'=>date('Ym'),
- 'create_yyyymmdd'=>date('Ymd'),
- 'from'=>1,
- ];
- Db::startTrans();
- try {
- $orderId = Db::name('orders')->insertGetId($odata);
- if(!$orderId){
- \exception('操作失败');
- }
- $orData = [
- 'order_id'=>$orderId,
- 'type_id'=>$data['type_id'],
- 'address_id'=>$data['address_id'],
- 'repair_priority'=>$data['repair_priority'],
- 'update_time'=>date('Y-m-d H:i:s'),
- ];
- $or = Db::name('order_repair')->insert($orData);
- if(!$or){
- \exception('操作失败');
- }
- $tData = [
- 'order_id'=>$orderId,
- 'to_user_id'=>$data['user_id'],
- 'todo_content'=>$data['content'],
- 'org_id'=>$data['org_id'],
- 'create_user_id'=>$data['user_id'],
- 'create_time'=>date('Y-m-d H:i:s'),
- 'todo_mode'=>2,
- 'work_type_mode'=>$data['work_type_mode'],
- 'create_yyyy'=>date('Y'),
- 'create_yyyymm'=>date('Ym'),
- 'create_yyyymmdd'=>date('Ymd'),
- 'sn'=>get_unique_sn(get_config('sn_prefix')),
- ];
- $todoId = Db::name('todo')->insertGetId($tData);
- if(!$todoId){
- \exception('操作失败');
- }
- $taskData = [
- 'org_id' => $data['org_id'],
- 'user_id' => $data['user_id'],
- 'type' => 1,
- 'start_time' => getTime(),
- 'create_time' => getTime(),
- 'bus_id' => $todoId,
- 'priority' => $data['repair_priority']==3 ?1:0,
- ];
- $task = Db::name('task')->insert($taskData);
- if(!$task){
- \exception('任务保存失败');
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- public function autoSend($id){
- $info = Db::name('orders')
- ->where('id',$id)
- ->find();
- // $org_auto_send = (new \app\common\model\Config())
- // ->getConfig('org_auto_send',$info['org_id']);
- // if(!$org_auto_send){
- // $this->error='自动派单未开启';
- // return false;
- // }
- $repair = Db::name('order_repair')
- ->where('order_id',$id)
- ->find();
- if(empty($repair) || strlen($repair['type_id'])==0){
- $this->error='订单报修事项为空';
- return false;
- }
- $parent_id = Db::name('order_type')
- ->where('id',$repair['type_id'])
- ->value('parent_id');
- if(empty($info)){
- $this->error='订单信息不存在';
- return false;
- }
- if($info['work_type_mode']!=1){
- $this->error='当前订单类型不能自动派单';
- return false;
- }
- if($info['order_mode']!=1){
- $this->error='当前订单已处理';
- return false;
- }
- $depInfo = Db::name('order_auto_send_area')
- ->where('dep_id',$info['dep_id'])
- ->find();
- $workUser = get_sort_user($info['org_id'],1,$parent_id);
- if(empty($workUser)){
- $this->error='人员不存在';
- return false;
- }
- //当前科室未设置负责人
- if(empty($depInfo) || empty($depInfo['user_id'])){
- $lastUser = Db::name('order_auto_send_log')
- ->where('org_id',$info['org_id'])
- ->where('type',0)
- ->where('type_id',$parent_id)
- ->order('id','desc')
- ->value('user_id');
- $newSort = cache("org_user_sort_".$info['org_id']);
- if(empty($newSort)){
- $newSort = get_sort_user($info['org_id'],1,$parent_id);
- }else{
- $newSort = Db::name('user')
- ->where('id','in',$newSort)
- ->where('type_id',$parent_id)
- ->column('id');
- }
- if(cache("auto_send_end_".$info['org_id'])){
- $userId = $newSort[0];
- cache("auto_send_end_".$info['org_id'],false);
- }else{
- $curKey = array_search($lastUser,$newSort);
- if(strlen($curKey) >0){
- if(isset($newSort[$curKey+1])){
- $userId = $newSort[$curKey+1];
- if(!isset($newSort[$curKey+2])){
- cache("org_user_sort_".$info['org_id'],null);
- cache("org_user_sort_list_".$info['org_id'],null);
- cache("auto_send_end_".$info['org_id'],1);
- }
- }else{
- cache("org_user_sort_".$info['org_id'],null);
- cache("org_user_sort_list_".$info['org_id'],null);
- $userId = $newSort[0];
- }
- }else{
- cache("org_user_sort_".$info['org_id'],null);
- cache("org_user_sort_list_".$info['org_id'],null);
- $userId = $newSort[0];
- }
- }
- $type =0;
- }else{//订单科室设置负责人
- $lastUser = Db::name('order_auto_send_log')
- ->where('org_id',$info['org_id'])
- ->where('type',1)
- ->where('dep_id',$info['dep_id'])
- ->where('type_id',$parent_id)
- ->order('id','desc')
- ->value('user_id');
- $idList = explode(',',$depInfo['user_id']);
- $uu = Db::name('user')
- ->where('del',0)
- ->where('enable',1)
- ->where('enable_sorts',1)
- ->where('work',1)
- ->where('type_id',$parent_id)
- ->where('id','in',$idList)
- ->order(['sorts'=>'desc','id'=>'asc'])
- ->column('id');
- if(empty($uu)){
- Db::name('order_auto_send_area')
- ->where('dep_id',$info['dep_id'])
- ->delete();
- $this->autoSend($id);
- }else{
- $old = cache("org_user_sort_".$info['org_id']);
- $old_data = $uu;
- $uu = [];
- $not_exits = [];
- if($old){
- foreach ($old_data as $k=>$v){
- if(in_array($v,$old)){
- $kk = array_search($v,$old);
- $uu[$kk] = $v;
- }else{
- $not_exits[] = $v;
- }
- }
- }else{
- $uu = $old_data;
- }
- ksort($uu);
- $aa = [];
- foreach ($uu as $k=>$v){
- $aa[] = $v;
- }
- foreach ($not_exits as $k=>$v){
- array_push($aa,$v);
- }
- $uu = $aa;
- if(empty($lastUser) || !in_array($lastUser,$uu)){
- $userId = $uu[0];
- }else{
- if($lastUser==$uu[count($uu)-1]){
- $userId = $uu[0];
- }else{
- $cc = array_search($lastUser,$uu);
- if(strlen($cc) >0){
- if(isset($uu[$cc+1])){
- $userId = $uu[$cc+1];
- }else{
- $userId = $uu[0];
- }
- }
- }
- }
- }
- $newSort = $uu;
- $type =1;
- }
- $log = [
- 'org_id'=>$info['org_id'],
- 'order_id'=>$id,
- 'user_id'=>$userId,
- 'type_id'=>$parent_id,
- 'dep_id'=>$type==1?$info['dep_id']:0,
- 'type'=>$type,
- 'create_time'=>getTime(),
- 'user_sort'=>json_encode($newSort)
- ];
- Db::startTrans();
- try{
- $r =Db::name('order_auto_send_log')
- ->insertGetId($log);
- // Db::commit();
- // return true;
- if(!$r){
- \exception('派单失败');
- }
- if($type==1){
- $rr = Db::name('order_auto_send_area')
- ->where('dep_id',$info['dep_id'])
- ->update(['last_user'=>$userId,'update_time'=>getTime()]);
- if(!$rr){
- \exception('更新派单区域管理失败');
- }
- }
- $todoData = [
- 'order_id' => $id,
- 'todo_content' => $info['content'],
- 'create_user_id' => $info['user_id'],
- 'org_id' => $info['org_id'],
- 'create_time' => getTime(),
- 'todo_mode' => 1,
- 'work_type_mode' => $info['work_type_mode'],
- 'create_yyyy' => date('Y'),
- 'create_yyyymm' => date('Ym'),
- 'create_yyyymmdd' => date('Ymd'),
- ];
- $taskData = [
- 'org_id' => $info['org_id'],
- 'type' => 1,
- 'start_time' => getTime(),
- 'create_time' => getTime(),
- ];
- if($info['work_type_mode']==1){
- $orderRepair = Db::name('order_repair')
- ->where('order_id',$info['id'])
- ->find();
- $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0;
- }else if ($info['work_type_mode']==3){
- $order_convey = Db::name('order_convey')
- ->where('order_id',$info['id'])
- ->find();
- $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0;
- }
- $sns = [];
- $to_user_id = [$userId];
- $snpre = (new Config())->getConfig('sn_prefix');
- foreach ($to_user_id as $k => $v) {
- if(empty($v)){
- continue;
- }
- $todoData['to_user_id'] = $v;
- while (true){ //获取唯一工单号
- $sn = get_unique_sn($snpre);
- if(!in_array($sn,$sns)){
- $ret = Db::name('todo')->where('sn',$sn)->find();
- if(!$ret){
- $todoData['sn'] = $sn;
- break;
- }
- }
- }
- $todoData['is_auto'] = 1;
- $res = Db::name('todo')
- ->insertGetId($todoData);
- if (!$res) {
- \exception('执行人:' . $v . '派单失败');
- }
- $pusharr[] = [
- 'user_id' => $v,
- 'todo_id' => $res
- ];
- $taskData['user_id'] = $v;
- $taskData['bus_id'] = $res;
- $res = Db::name('task')
- ->insert($taskData);
- if (!$res) {
- \exception('执行人:' . $v . '任务保存失败');
- }
- }
- $r = Db::name('orders')
- ->where('id',$id)
- ->update(['order_mode'=>4]);
- if(!$r){
- \exception('操作失败');
- }
- if($pusharr) {
- foreach ($pusharr as $k => $v) {
- // 极光推送
- send_jpush([$v['user_id']], AppMsg::PUSH_WORKER_ORDER_SEND, '', ['id' => $v['todo_id']]);
- }
- }
- // 极光推送
- send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]);
- Db::commit();
- return true;
- }catch (Exception $e){
- Db::rollback();
- $this->error=$e->getMessage();
- return false;
- }
- }
- public function get_cur_day_lists($org,$limit=5)
- {
- $ret = Db::name('orders')
- ->alias('o')
- ->where('o.org_id',$org)
- ->where('o.del',0)
- // ->where('o.order_mode','in',[1,4])
- //->where('o.work_type_mode',3)
- ->where('o.create_yyyymmdd',date('Ymd'))
- ->order('o.id','desc')
- ->limit($limit)->select();
- foreach ($ret as $k=>$v){
- $todo =Db::name('todo')
- ->alias('t')
- ->where('t.order_id',$v['id'])
- ->join('user u','u.id = t.to_user_id')
- // ->where('t.todo_mode','in',[1,2,3])
- ->where('t.del',0)
- ->field('u.real_name,t.create_time,t.confirm_time')
- ->order('t.id','asc')
- ->select();
- $users = [];
- $confirmTime = '';
- foreach ($todo as $kk=>$vv){
- $users[] = $vv['real_name'];
- if(!$confirmTime && $vv['confirm_time']){
- $confirmTime = $vv['confirm_time'];
- }else if($confirmTime && $vv['confirm_time'] && $vv['confirm_time'] < $confirmTime){
- $confirmTime = $vv['confirm_time'];
- }
- }
- $ret[$k]['real_names'] = implode(',',$users);
- $ret[$k]['send_time'] = $todo?$todo[0]['create_time']:'';
- $ret[$k]['confirm_time'] = $confirmTime;
- $ret[$k]['order_model_txt'] = Db::name('order_mode')
- ->where('id',$v['order_mode'])
- ->value('out_content');
- }
- return $ret?$ret:[];
- }
- public function get_cur_day_lists_bak($org,$limit=5)
- {
- $ret = Db::name('orders')
- ->alias('o')
- ->join('order_convey oc','oc.order_id = o.id')
- ->field('o.*,oc.type,oc.start,oc.end,oc.xq_time,oc.ywc_time,oc.device_id,oc.name,oc.phone,oc.priority')
- ->where('o.org_id',$org)
- ->where('o.del',0)
- ->where('o.order_mode','in',[1,4])
- ->where('o.work_type_mode',3)
- ->where('o.create_yyyymmdd',date('Ymd'))
- ->order('oc.xq_time','ASC')
- ->limit($limit)->select();
- foreach ($ret as $k=>$v){
- $ret[$k]['start_name'] = $this->getTableField('address',array('id'=>$v['start']),'title');
- $ret[$k]['end_name'] = $this->getTableField('address',array('id'=>$v['end']),'title');
- $ret[$k]['type_name'] = $this->getTableField('convey_cate',array('id'=>$v['type']),'title');
- $ret[$k]['real_name'] = $this->getTableField('user',array('id'=>$v['user_id']),'real_name');
- $todo =Db::name('todo')
- ->alias('t')
- ->where('t.order_id',$v['id'])
- ->join('user u','u.id = t.to_user_id')
- ->where('t.todo_mode','in',[1,2,3])
- ->where('t.del',0)
- ->field('u.real_name,t.create_time,t.confirm_time')
- ->order('t.id','asc')
- ->select();
- $users = [];
- $confirmTime = '';
- foreach ($todo as $kk=>$vv){
- $users[] = $vv['real_name'];
- if(!$confirmTime && $vv['confirm_time']){
- $confirmTime = $vv['confirm_time'];
- }else if($confirmTime && $vv['confirm_time'] && $vv['confirm_time'] < $confirmTime){
- $confirmTime = $vv['confirm_time'];
- }
- }
- $ret[$k]['real_names'] = implode(',',$users);
- $ret[$k]['send_time'] = $todo?$todo[0]['create_time']:'';
- $ret[$k]['confirm_time'] = $confirmTime;
- $ret[$k]['device_name'] = '';
- if($v['device_id'] > 0){
- $ret[$k]['device_name'] = $this->getTableField('convey_device',array('id'=>$v['device_id']),'title');
- }
- $last = strtotime($v['ywc_time']) - time();
- $ret[$k]['last_time'] = $last > 0?round($last/60):0;
- }
- return $ret?$ret:[];
- }
- public function delConveyEnd($orderId,$orgId){
- $order = Db::name('orders')
- ->where('org_id',$orgId)
- ->where('id',$orderId)
- ->where('order_mode',4)
- ->where('del',0)
- ->where('work_type_mode',3)
- ->find();
- if(!$order){
- $this->error = '订单不存在';
- return false;
- }
- // 检查订单是不是病人运送
- $convey = Db::name('order_convey')
- ->alias('a')
- ->join('convey_cate b','a.type = b.id')
- ->where('a.order_id',$orderId)
- // ->where('b.cate',1)
- ->field('a.*')
- ->find();
- if(!$convey){
- $this->error = '订单不存在';
- return false;
- }
- $orderEnd = Db::name('order_convey_end')
- ->where('order_convey_id',$convey['id'])
- ->where('del',0)
- ->order('id','desc')
- ->select();
- if(!$orderEnd){
- $this->error = '没有途径地点不能删除目的地';
- return false;
- }
- Db::startTrans();
- try{
- $ret = Db::name('order_convey_end')
- ->where('id',$orderEnd[0]['id'])->update([
- 'del' => 1,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- // 更换目的地
- $res = Db::name('order_convey')
- ->where('id', $convey['id'])->update([
- 'end' => $orderEnd[0]['addr'],
- ]);
- if(!$res){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- }
|