ConstructionService.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Construction;
  5. use App\Model\ConstructionFile;
  6. use App\Model\ConstructionInfo;
  7. use App\Model\ConstructionProductInfo;
  8. use App\Model\Customer;
  9. use App\Model\CustomerInfo;
  10. use App\Model\DeliveryNote;
  11. use App\Model\Depart;
  12. use App\Model\Employee;
  13. use App\Model\EmployeeDepartPermission;
  14. use App\Model\Oa;
  15. use App\Model\OaSub;
  16. use App\Model\OaSubEmployee;
  17. use App\Model\OaSubReportEmployee;
  18. use App\Model\OaSubRule;
  19. use App\Model\Product;
  20. use App\Model\SalesOrder;
  21. use App\Model\SalesOrderInfo;
  22. use App\Model\SalesOrderProductInfo;
  23. use App\Model\ScheduleInfo;
  24. use App\Model\SeeRange;
  25. use App\Model\Storehouse;
  26. use Illuminate\Support\Facades\DB;
  27. //use Barryvdh\DomPDF\Facade as PDF;
  28. use Barryvdh\DomPDF\PDF;
  29. /**
  30. * 施工订单
  31. */
  32. class ConstructionService extends Service
  33. {
  34. /**
  35. * 施工订单编辑
  36. * @param $data
  37. * @param $user
  38. * @return array
  39. */
  40. public function constructionEdit($data,$user){
  41. list($status,$msg) = $this->constructionRule($data, $user, false);
  42. if(!$status) return [$status,$msg];
  43. $params = $this->getDataFile($data);
  44. (new OperationLogService())->setOperationList($params,$user,2);
  45. try {
  46. DB::beginTransaction();
  47. $model = Construction::where('id', $data['id'])->first();
  48. $model->model_type = $data['model_type'];
  49. $model->order_number = $data['order_number'];
  50. $model->title = $data['title'] ?? '';
  51. $model->customer_id = $data['customer_id'] ?? 0;
  52. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  53. $model->install_method = $data['install_method'] ?? 0;
  54. $model->install_position = $data['install_position'] ?? 0;
  55. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  56. $model->construction_fee = $data['construction_fee'] ?? 0;
  57. $model->service_price = $data['service_price'] ?? 0;
  58. $model->construction_time = $data['construction_time'] ?? 0;
  59. $model->handover_time = $data['handover_time'] ?? 0;
  60. $model->urgency = $data['urgency'] ?? 0;
  61. $model->mark = $data['mark'] ?? '';
  62. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  63. $model->address2 = $data['address2'] ?? '';
  64. $model->introduction = $data['introduction'] ?? '';
  65. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  66. $model->start_time = $data['start_time'] ?? 0;
  67. $model->end_time = $data['end_time'] ?? 0;
  68. $model->schedule_id = $data['schedule_id'] ?? 0;
  69. $model->day_stamp = $data['day_stamp'] ?? 0;
  70. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  71. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  72. $model->save();
  73. $time = time();
  74. ConstructionInfo::where('del_time',0)
  75. ->where('construction_id',$data['id'])
  76. ->update(['del_time' => $time]);
  77. ConstructionProductInfo::where('del_time',0)
  78. ->where('construction_id',$data['id'])
  79. ->update(['del_time' => $time]);
  80. $old = ConstructionFile::where('del_time',0)
  81. ->where('construction_id',$data['id'])
  82. ->select('file')
  83. ->get()->toArray();
  84. $old = array_column($old,'file');
  85. ConstructionFile::where('del_time',0)
  86. ->where('construction_id',$data['id'])
  87. ->update(['del_time' => $time]);
  88. if(! empty($data['construction_contact'])){
  89. $insert = [];
  90. foreach ($data['construction_contact'] as $value){
  91. $insert[] = [
  92. 'construction_id' => $model->id,
  93. 'contact_type' => $value['id'],
  94. 'contact_info' => $value['info'],
  95. 'type' => ConstructionInfo::type_one,
  96. 'crt_time' => $time,
  97. ];
  98. }
  99. ConstructionInfo::insert($insert);
  100. }
  101. if(! empty($data['employee_one'])){
  102. $insert = [];
  103. foreach ($data['employee_one'] as $value){
  104. $insert[] = [
  105. 'construction_id' => $model->id,
  106. 'employee_id' => $value,
  107. 'type' => ConstructionInfo::type_two,
  108. 'crt_time' => $time,
  109. ];
  110. }
  111. ConstructionInfo::insert($insert);
  112. }
  113. if(! empty($data['product'])){
  114. $insert = [];
  115. foreach ($data['product'] as $value){
  116. $insert[] = [
  117. 'construction_id' => $model->id,
  118. 'product_id' => $value['product_id'],
  119. 'number' => $value['number'],
  120. 'cost' => $value['cost'] ?? 0,
  121. 'retail_price' => $value['retail_price'] ?? 0,
  122. 'mark' => $value['mark'] ?? '',
  123. 'crt_time' => $time,
  124. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  125. 'basic_type_id' => $value['basic_type_id'],
  126. 'price' => $value['price'],
  127. 'final_amount' => $value['final_amount'] ?? 0,
  128. ];
  129. }
  130. ConstructionProductInfo::insert($insert);
  131. //锁定库存
  132. ProductInventoryService::changeLockNumber($user,$msg[0],$msg[1]);
  133. }
  134. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  135. $new = [];
  136. if(! empty($data['file'])){
  137. $insert = [];
  138. foreach ($data['file'] as $value){
  139. $insert[] = [
  140. 'order_number' => $data['order_number'],
  141. 'construction_id' => $data['id'],
  142. 'file' => $value['url'],
  143. 'name' => $value['name'],
  144. 'mark' => $value['mark'] ?? "",
  145. 'crt_time' => $time,
  146. ];
  147. $new[]= $value['url'];
  148. }
  149. ConstructionFile::insert($insert);
  150. }
  151. DB::commit();
  152. }catch (\Exception $exception){
  153. DB::rollBack();
  154. return [false,$exception->getMessage()];
  155. }
  156. $this->delStorageFile($old, $new);
  157. if(! empty($data['check'])) {
  158. list($status,$msg) = (new CheckService())->checkAll([
  159. "id" => $model->id,
  160. "order_number" => $data['order_number'],
  161. "opt_case" => CheckService::five,
  162. "menu_id" => $data['menu_id']
  163. ],$user);
  164. if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  165. }
  166. return [true,''];
  167. }
  168. /**
  169. * 施工订单新增
  170. * @param $data
  171. * @param $user
  172. * @return array
  173. */
  174. public function constructionAdd($data,$user){
  175. list($status,$msg) = $this->constructionRule($data,$user);
  176. if(!$status) return [$status,$msg];
  177. try {
  178. DB::beginTransaction();
  179. $model = new Construction();
  180. $model->model_type = $data['model_type'];
  181. $model->order_number = $data['order_number'];
  182. $model->title = $data['title'] ?? '';
  183. $model->customer_id = $data['customer_id'] ?? 0;
  184. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  185. $model->install_method = $data['install_method'] ?? 0;
  186. $model->install_position = $data['install_position'] ?? 0;
  187. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  188. $model->construction_fee = $data['construction_fee'] ?? 0;
  189. $model->service_price = $data['service_price'] ?? 0;
  190. $model->construction_time = $data['construction_time'] ?? 0;
  191. $model->handover_time = $data['handover_time'] ?? 0;
  192. $model->urgency = $data['urgency'] ?? 0;
  193. $model->mark = $data['mark'] ?? '';
  194. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  195. $model->address2 = $data['address2'] ?? '';
  196. $model->introduction = $data['introduction'] ?? '';
  197. $model->crt_id = $user['id'];
  198. $model->depart_id = $data['depart_id'] ?? 0;
  199. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  200. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  201. $model->start_time = $data['start_time'] ?? 0;
  202. $model->end_time = $data['end_time'] ?? 0;
  203. $model->schedule_id = $data['schedule_id'] ?? 0;
  204. $model->day_stamp = $data['day_stamp'] ?? 0;
  205. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  206. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  207. $model->save();
  208. $time = time();
  209. if(! empty($data['construction_contact'])){
  210. $insert = [];
  211. foreach ($data['construction_contact'] as $value){
  212. $insert[] = [
  213. 'construction_id' => $model->id,
  214. 'contact_type' => $value['id'],
  215. 'contact_info' => $value['info'],
  216. 'type' => ConstructionInfo::type_one,
  217. 'crt_time' => $time,
  218. ];
  219. }
  220. ConstructionInfo::insert($insert);
  221. }
  222. if(! empty($data['employee_one'])){
  223. $insert = [];
  224. foreach ($data['employee_one'] as $value){
  225. $insert[] = [
  226. 'construction_id' => $model->id,
  227. 'employee_id' => $value,
  228. 'type' => ConstructionInfo::type_two,
  229. 'crt_time' => $time,
  230. ];
  231. }
  232. ConstructionInfo::insert($insert);
  233. }
  234. if(! empty($data['product'])){
  235. $insert = [];
  236. foreach ($data['product'] as $value){
  237. $insert[] = [
  238. 'construction_id' => $model->id,
  239. 'product_id' => $value['product_id'],
  240. 'number' => $value['number'],
  241. 'cost' => $value['cost'] ?? 0,
  242. 'retail_price' => $value['retail_price'] ?? 0,
  243. 'mark' => $value['mark'] ?? '',
  244. 'crt_time' => $time,
  245. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  246. 'basic_type_id' => $value['basic_type_id'],
  247. 'price' => $value['price'],
  248. 'final_amount' => $value['final_amount'] ?? 0,
  249. ];
  250. }
  251. ConstructionProductInfo::insert($insert);
  252. //锁定库存
  253. ProductInventoryService::changeLockNumber($user,$msg[0],[]);
  254. }
  255. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  256. if(! empty($data['file'])){
  257. $insert = [];
  258. foreach ($data['file'] as $value){
  259. $insert[] = [
  260. 'order_number' => $data['order_number'],
  261. 'construction_id' => $model->id,
  262. 'file' => $value['url'],
  263. 'name' => $value['name'],
  264. 'mark' => $value['mark'] ?? "",
  265. 'crt_time' => $time,
  266. ];
  267. }
  268. ConstructionFile::insert($insert);
  269. }
  270. DB::commit();
  271. }catch (\Exception $exception){
  272. DB::rollBack();
  273. return [false,$exception->getMessage()];
  274. }
  275. (new OperationLogService())->setOperationList($data,$user);
  276. if(! empty($data['check'])) {
  277. list($status,$msg) = (new CheckService())->checkAll([
  278. "id" => $model->id,
  279. "order_number" => $data['order_number'],
  280. "opt_case" => CheckService::five,
  281. "menu_id" => $data['menu_id']
  282. ],$user);
  283. if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  284. }
  285. return [true,''];
  286. }
  287. /**
  288. * 施工订单删除
  289. * @param $data
  290. * @return array
  291. */
  292. public function constructionDel($data,$user){
  293. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  294. $construction = Construction::where('del_time',0)->where('id',$data['id'])->first();
  295. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  296. $construction = $construction->toArray();
  297. if($construction['state'] > Construction::STATE_ZERO) return [false,'请确认施工单状态,操作失败'];
  298. $product_save = $this->getSaveDetail($data['id']);
  299. try {
  300. DB::beginTransaction();
  301. Construction::where('id',$data['id'])->update([
  302. 'del_time'=> time()
  303. ]);
  304. ConstructionInfo::where('del_time',0)
  305. ->where('construction_id',$data['id'])
  306. ->update(['del_time' => time()]);
  307. $old = ConstructionFile::where('del_time',0)
  308. ->where('construction_id',$data['id'])
  309. ->select('file')
  310. ->get()->toArray();
  311. $old = array_column($old,'file');
  312. ConstructionFile::where('del_time',0)
  313. ->where('construction_id',$data['id'])
  314. ->update(['del_time' => time()]);
  315. ConstructionProductInfo::where('del_time',0)
  316. ->where('construction_id',$data['id'])
  317. ->update(['del_time' => time()]);
  318. (new RangeService())->RangeDelete($data['id'],SeeRange::type_two);
  319. //锁定库存释放
  320. ProductInventoryService::changeLockNumber($user,[],$product_save);
  321. //排班修改
  322. // $schedule = ScheduleInfo::where('del_time',0)
  323. // ->where('schedule_id',$construction['schedule_id'])
  324. // ->where('start_time',$construction['day_start_stamp'])
  325. // ->where('end_time',$construction['day_end_stamp'])
  326. // ->where('is_use','>', ScheduleInfo::not_use)
  327. // ->first();
  328. // if(! empty($schedule)) ScheduleInfo::where('id',$schedule->id)->update(['is_use' => ScheduleInfo::not_use]);
  329. DB::commit();
  330. }catch (\Exception $exception){
  331. DB::rollBack();
  332. return [false,$exception->getMessage()];
  333. }
  334. $this->delStorageFile($old);
  335. return [true,''];
  336. }
  337. /**
  338. * 施工订单详情
  339. * @param $data
  340. * @return array
  341. */
  342. public function detail($data){
  343. if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
  344. if(! empty($data['id'])){
  345. $construction = Construction::where('del_time',0)
  346. ->where('id',$data['id'])
  347. ->first();
  348. }else{
  349. $construction = Construction::where('del_time',0)
  350. ->where('order_number',$data['order_number'])
  351. ->first();
  352. $data['id'] = empty($construction->id) ? 0 : $construction->id;
  353. }
  354. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  355. $construction = $construction->toArray();
  356. $construction['state_title'] = Construction::$name[$construction['state']] ?? '';
  357. $construction['pq_state_title'] = Construction::$pq_name[$construction['pq_state']] ?? '';
  358. $address_map = config('address');
  359. $address_str = [];
  360. if(! empty($construction['address1'])) {
  361. $tmp = json_decode($construction['address1'],true);
  362. $construction['address1'] = $tmp;
  363. $this->findLabelsByValue($address_map,$tmp,$address_str);
  364. $tmp = implode(' ',$address_str);
  365. $tmp .= ' ' . $construction['address2'];
  366. $address = $tmp;
  367. }else{
  368. $address = $construction['address2'];
  369. }
  370. $construction['address'] = $address;
  371. $start_time = $construction['start_time'] ? date("Y-m-d H:i",$construction['start_time']) : '';
  372. $end_time = $construction['end_time'] ? date("Y-m-d H:i",$construction['end_time']) : '';
  373. $construction['construction_period'] = $start_time . '——' . $end_time;
  374. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  375. $construction['sales_order_number'] = $sales;
  376. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  377. $construction['customer_title'] = $customer_title ?? "";
  378. $info = CustomerInfo::from('customer_info as a')
  379. ->leftJoin('basic_type as b','b.id','a.contact_type')
  380. ->where('a.del_time',0)
  381. ->where('a.customer_id',$construction['customer_id'])
  382. ->where('a.contact_type','>',0)
  383. ->select('b.title','a.contact_info')
  384. ->get()->toArray();
  385. $construction['customer_array'] = $info;
  386. $info = CustomerInfo::from('customer_info as a')
  387. ->leftJoin('basic_type as b','b.id','a.contact_type')
  388. ->where('a.del_time',0)
  389. ->where('a.customer_id',$construction['customer_id'])
  390. ->where('a.contact_type','>',0)
  391. ->select('b.title','a.contact_info')
  392. ->get()->toArray();
  393. $construction['customer_array'] = $info;
  394. $construction['storehouse_title'] = Storehouse::where('id',$construction['storehouse_id'])->value('title');
  395. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  396. $construction['customer_contact_title'] = $emp_title;
  397. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  398. $array = [
  399. $construction['install_method'],
  400. $construction['install_position'],
  401. $construction['urgency'],
  402. ];
  403. $basic_map = BasicType::whereIn('id',$array)
  404. ->pluck('title','id')
  405. ->toArray();
  406. $construction = [$construction];
  407. foreach ($construction as $key => $value){
  408. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  409. $construction[$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  410. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  411. }
  412. $construction = $construction[0];
  413. $construction['file'] = [];
  414. $file = ConstructionFile::where('del_time',0)
  415. ->where('construction_id',$construction['id'])
  416. ->select('id','construction_id','file','name','mark')
  417. ->get()->toArray();
  418. foreach ($file as $value){
  419. $construction['file'][] = [
  420. 'url' => $value['file'],
  421. 'name' => $value['name'],
  422. 'mark' => $value['mark'],
  423. ];
  424. }
  425. $construction_info = ConstructionInfo::where('del_time',0)
  426. ->where('construction_id',$construction['id'])
  427. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  428. ->get()->toArray();
  429. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  430. ->pluck('emp_name','id')
  431. ->toArray();
  432. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  433. ->pluck('title','id')
  434. ->toArray();
  435. foreach ($construction_info as $value){
  436. if($value['type'] == ConstructionInfo::type_one){
  437. $tmp = [
  438. 'id' => $value['contact_type'],
  439. 'title' => $basic_map2[$value['contact_type']] ?? '',
  440. 'info' => $value['contact_info']
  441. ];
  442. $construction['construction_contact'][] = $tmp;
  443. }elseif ($value['type'] == ConstructionInfo::type_two){
  444. $tmp = [
  445. 'id' => $value['employee_id'],
  446. 'name' => $emp_map[$value['employee_id']] ?? '',
  447. ];
  448. $construction['employee_one'][] = $tmp;
  449. }
  450. }
  451. $p_info = ConstructionProductInfo::where('del_time',0)
  452. ->where('construction_id',$construction['id'])
  453. ->get()->toArray();
  454. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  455. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  456. foreach ($p_info as $value){
  457. $tmp = $map[$value['product_id']] ?? [];
  458. $value['title'] = $tmp['title'] ?? "";
  459. $value['code'] = $tmp['code'] ?? "";
  460. $value['size'] = $tmp['size'] ?? "";
  461. $value['unit'] = $tmp['unit'] ?? "";
  462. $value['bar_code'] = $tmp['bar_code'] ?? "";
  463. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  464. $construction['product'][] = $value;
  465. }
  466. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  467. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  468. //可见范围
  469. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_two);
  470. $construction['depart'] = $return[0] ?? [];
  471. $construction['employee'] = $return[1] ?? [];
  472. return [true, $construction];
  473. }
  474. /**
  475. * 施工订单列表
  476. * @param $data
  477. * @param $user
  478. * @return array
  479. */
  480. public function constructionList($data,$user){
  481. $model = Construction::Clear($user,$data);
  482. $model = $model->where('del_time',0)
  483. ->select('title','id','model_type','order_number','customer_id','customer_contact_id','install_method','install_position','sales_order_id','construction_fee','construction_time','handover_time','urgency','crt_id','crt_time','mark','state','address1','address2','introduction','service_price','storehouse_id','start_time','end_time','pq_state','day_start_stamp','day_end_stamp')
  484. ->orderby('id', 'desc');
  485. if(isset($data['state'])) $model->where('state', $data['state']);
  486. if(isset($data['pq_state'])) $model->where('pq_state', $data['pq_state']);
  487. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  488. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  489. if(! empty($data['time_type'])) {
  490. if($data['time_type'] == 1) {
  491. $start = strtotime('today');
  492. $end = strtotime('tomorrow') - 1;
  493. }elseif ($data['time_type'] == 2){
  494. $start = strtotime('this week',strtotime('today'));
  495. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  496. }
  497. if(! empty($start) && ! empty($end)) {
  498. $model->where('crt_time','>=',$start);
  499. $model->where('crt_time','<=',$end);
  500. }
  501. }
  502. if(! empty($data['construction_period'][0]) && ! empty($data['construction_period'][1])) {
  503. $return = $this->changeDateToTimeStampAboutRange($data['construction_period']);
  504. $model->where('start_time','>=',$return[0]);
  505. $model->where('end_time','<=',$return[1]);
  506. }
  507. if(! empty($data['pq_period'][0]) && ! empty($data['pq_period'][1])) {
  508. $model->where('day_start_stamp','>=',$this->changeDateToDateMin($data['pq_period'][0]));
  509. $model->where('day_end_stamp','<=',$this->changeDateToDateMin($data['pq_period'][1]));
  510. }
  511. if(! empty($data['sale_order'])){
  512. $model2 = SalesOrder::Clear($user,$data);
  513. $sale = $model2->where('del_time',0)
  514. ->where('order_number', 'LIKE', '%'.$data['sale_order'].'%')
  515. ->select('id')
  516. ->get()->toArray();
  517. $model->whereIn('sales_order_id',array_unique(array_column($sale,'id')));
  518. }
  519. if(! empty($data['install_method'])) $model->where('install_method',$data['install_method']);
  520. if(! empty($data['install_position'])) $model->where('install_position',$data['install_position']);
  521. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  522. $list = $this->limit($model,'',$data);
  523. $list = $this->fillData($list);
  524. return [true, $list];
  525. }
  526. /**
  527. * 参数规则
  528. * @param $data
  529. * @param $user
  530. * @param $is_add
  531. * @return array
  532. */
  533. public function constructionRule(&$data, $user, $is_add = true){
  534. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  535. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  536. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  537. if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
  538. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  539. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  540. if(empty($sale)) return [false,'合同不存在或已被删除'];
  541. $sale = $sale->toArray();
  542. if($sale['state'] < SalesOrder::State_two) return [false,'合同未派单,不允许新建施工单'];
  543. list($status,$msg) = $this->limitingSendRequestBackgExpire("construction" . $sale['order_number']);
  544. if(! $status) return [false, $msg];
  545. if(empty($data['product'])) return [false,'请选择产品'];
  546. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  547. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  548. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  549. if(! empty($data['construction_fee'])){
  550. $res = $this->checkNumber($data['construction_fee']);
  551. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  552. }
  553. if(! empty($data['service_price'])){
  554. $res = $this->checkNumber($data['service_price']);
  555. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  556. }
  557. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  558. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  559. if($data['model_type'] == Construction::Model_type_one){
  560. // if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  561. // if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  562. }else{
  563. // if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  564. // if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  565. }
  566. //所属部门 以及 顶级部门
  567. if(empty($data['depart_id'])) {
  568. $data['depart_id'] = $this->getDepart($user);
  569. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  570. }
  571. $product_submit = $product_id = [];
  572. foreach ($data['product'] as $value){
  573. if(empty($value['number'])) return [false,'产品数量不能为空'];
  574. $res = $this->checkNumber($value['number']);
  575. if(! $res) return [false,'请输入正确的产品数量'];
  576. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  577. if(isset($product_submit[$key])){
  578. $product_submit[$key] += $value['number'];
  579. }else{
  580. $product_submit[$key] = $value['number'];
  581. }
  582. $product_id[] = $value['product_id'];
  583. }
  584. //剩余能施工
  585. $id = $data['id'] ?? 0;
  586. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  587. //比较
  588. foreach ($product_submit as $pro => $number){
  589. $tmp = explode(',',$pro);
  590. $p = $tmp[0];
  591. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  592. $s_number = $s_product[$p];
  593. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据'];
  594. }
  595. $id = $data['id'] ?? 0;
  596. $product_save = $this->getSaveDetail($id);
  597. //是否校验库存
  598. ProductInventoryService::is_check($user,$data);
  599. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  600. if(! $status) return [false, $msg];
  601. $start_time = date("Y-m-d H:i",$data['start_time']);
  602. $end_time = date("Y-m-d H:i",$data['end_time']);
  603. if($is_add){
  604. $bool = Construction::where('del_time',0)
  605. ->where('order_number',$data['order_number'])
  606. ->exists();
  607. if($bool) return [false,'工单编号已存在,请重新获取'];
  608. $bool = Construction::where('del_time',0)
  609. ->where('sales_order_id',$data['sales_order_id'])
  610. ->where('start_time', '<=', $data['end_time'])
  611. ->where('end_time', '>=', $data['start_time'])
  612. ->exists();
  613. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  614. }else{
  615. if(empty($data['id'])) return [false,'ID不能为空'];
  616. $bool = Construction::where('del_time',0)
  617. ->where('id','<>',$data['id'])
  618. ->where('sales_order_id',$data['sales_order_id'])
  619. ->where('start_time', '<=', $data['end_time'])
  620. ->where('end_time', '>=', $data['start_time'])
  621. ->exists();
  622. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  623. }
  624. return [true, [$product_submit, $product_save]];
  625. }
  626. /**
  627. * 数据拼接
  628. * @param $data
  629. * @return array
  630. */
  631. public function fillData($data){
  632. if(empty($data['data'])) return $data;
  633. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  634. $basic_map = BasicType::whereIn('id',$array)
  635. ->pluck('title','id')
  636. ->toArray();
  637. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  638. ->pluck('emp_name','id')
  639. ->toArray();
  640. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  641. ->pluck('title','id')
  642. ->toArray();
  643. $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->select('order_number','id','handover_time')->get()->toArray();
  644. $sales_map = [];
  645. foreach ($sales as $value){
  646. $sales_map[$value['id']] = $value;
  647. }
  648. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  649. ->pluck('title','id')
  650. ->toArray();
  651. //分派的总社或分社
  652. $dispatch = $this->getDispatchData($data['data']);
  653. //施工产品
  654. $product_map = $this->getProduct($data['data']);
  655. $address_map = config('address');
  656. foreach ($data['data'] as $key => $value){
  657. $address_str = [];
  658. $product_tmp = $product_map[$value['id']] ?? [];
  659. $data['data'][$key]['product_detail'] = implode(',',$product_tmp);
  660. if(! empty($value['address1'])) {
  661. $tmp = json_decode($value['address1'],true);
  662. $this->findLabelsByValue($address_map,$tmp,$address_str);
  663. $tmp = implode(' ',$address_str);
  664. $tmp .= ' ' . $value['address2'];
  665. $address = $tmp;
  666. }else{
  667. $address = $value['address2'];
  668. }
  669. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  670. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  671. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  672. $data['data'][$key]['address'] = $address;
  673. $data['data'][$key]['model_type_title'] = Construction::$model_type_title[$value['model_type']] ?? '';
  674. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  675. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  676. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  677. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  678. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  679. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  680. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  681. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  682. $tmp_sales = $sales_map[$value['sales_order_id']] ?? [];
  683. // $tmp_sales_time = $tmp_sales['handover_time'] ? date("Y-m-d") : "";
  684. $data['data'][$key]['sales_order_number'] = $tmp_sales['order_number'];
  685. $data['data'][$key]['handover_time'] = $value['handover_time'] ? date("Y-m-d") : "";
  686. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  687. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  688. $data['data'][$key]['pq_state_title'] = Construction::$pq_name[$value['pq_state']] ?? '';
  689. $str = "";
  690. $start_time = $value['day_start_stamp'] ? date("Y-m-d H:i",$value['day_start_stamp']) : '';
  691. $end_time = $value['day_end_stamp'] ? date("Y-m-d H:i",$value['day_end_stamp']) : '';
  692. if(! empty($start_time) && ! empty($end_time)) $str = $start_time . '——' . $end_time;
  693. $data['data'][$key]['pq_period'] = $str;
  694. }
  695. return $data;
  696. }
  697. public function getProduct($data){
  698. $search_id = array_column($data,'id');
  699. if(empty($search_id)) return [];
  700. $product = ConstructionProductInfo::where('del_time',0)
  701. ->whereIn('construction_id',$search_id)
  702. ->select('product_id','construction_id')
  703. ->get()->toArray();
  704. $product_map = Product::whereIn('id',array_unique(array_column($product,'product_id')))->pluck('title','id')->toArray();
  705. $return = [];
  706. foreach ($product as $value){
  707. $product_tmp = $product_map[$value['product_id']] ?? "";
  708. if($product_tmp) $return[$value['construction_id']][] = $product_tmp;
  709. }
  710. return $return;
  711. }
  712. public function getDispatchData($data){
  713. $search_id = [];
  714. foreach ($data as $value){
  715. $search_id[] = $value['sales_order_id'];
  716. }
  717. if(empty($search_id)) return [];
  718. $see = SeeRange::where('del_time',0)
  719. ->whereIn('data_id',$search_id)
  720. ->where('data_type',SeeRange::type_seven)
  721. ->where('type',SeeRange::data_three)
  722. ->select('data_id','param_id')
  723. ->get()->toArray();
  724. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  725. ->pluck('title','id')
  726. ->toArray();
  727. $see_array = [];
  728. foreach ($see as $value){
  729. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  730. }
  731. return $see_array;
  732. }
  733. /**
  734. * 获取施工单号
  735. * @param $data
  736. * @return array
  737. */
  738. public function constructionGet($data){
  739. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  740. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  741. $prefix = Construction::$prefix[$data['model_type']];
  742. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  743. if(! $order_number) return [false,'工单编号生成失败!'];
  744. return [true,['order_number' => $order_number]];
  745. }
  746. /**
  747. * 获取保存详情
  748. * @param $id
  749. * @return array
  750. */
  751. public function getSaveDetail($id){
  752. $product_save = [];
  753. if(empty($id)) return $product_save;
  754. $sub = ConstructionProductInfo::where('construction_id',$id)
  755. ->where('del_time',0)
  756. ->get()->toArray();
  757. foreach ($sub as $value){
  758. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  759. if(isset($product_save[$key])){
  760. $product_save[$key] += $value['number'];
  761. }else{
  762. $product_save[$key] = $value['number'];
  763. }
  764. }
  765. return $product_save;
  766. }
  767. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  768. $construction = Construction::where('del_time',0)
  769. ->where('sales_order_id',$sales_order_id)
  770. ->select('id')->get()->toArray();
  771. $construction_id = array_column($construction,'id');
  772. $product_save = [];
  773. $sub = ConstructionProductInfo::where('del_time',0)
  774. ->whereIn('construction_id',$construction_id)
  775. ->when(! empty($id), function ($query) use ($id) {
  776. return $query->where('construction_id', '<>',$id);
  777. })
  778. ->get()->toArray();
  779. foreach ($sub as $value){
  780. if(isset($product_save[$value['product_id']])){
  781. $product_save[$value['product_id']] += $value['number'];
  782. }else{
  783. $product_save[$value['product_id']] = $value['number'];
  784. }
  785. }
  786. $sales_order_product = [];
  787. $sales_product = SalesOrderProductInfo::where('del_time',0)
  788. ->where('sales_order_id',$sales_order_id)
  789. ->get()->toArray();
  790. foreach ($sales_product as $value){
  791. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  792. if(isset($sales_order_product[$value['product_id']])){
  793. $sales_order_product[$value['product_id']] += $value['number'];
  794. }else{
  795. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp;
  796. }
  797. }
  798. return $sales_order_product;
  799. }
  800. public function deliveryNoteEdit($data,$user){
  801. $id = $user['id'];
  802. if(isset($data['id'])) {
  803. $model = DeliveryNote::where('id',$data['id'])->first();
  804. if($model->img3) return [false,'客户已签字,无法编辑!'];
  805. if(DeliveryNote::where('del_time',0)->where('construction_order_number',$data['construction_order_number'])->where('id','<>',$data['id'])->first()) return [false,'施工单客户确认单已存在!'];
  806. }
  807. else {
  808. $model = new DeliveryNote();
  809. $model->crt_id = $id;
  810. }
  811. try {
  812. $model->upd_id = $id;
  813. $model->construction_order_number = $data['construction_order_number'] ?? '';
  814. $model->start_time = $data['start_time'] ?? '';
  815. $model->end_time = $data['end_time'] ?? '';
  816. $model->vin_no = $data['vin_no'] ?? '';
  817. $model->system = $data['system'] ?? '';
  818. $model->mile = $data['mile'] ?? '';
  819. $model->is_wait = $data['is_wait'] ?? '';
  820. $model->customer_name = $data['customer_name'] ?? '';
  821. $model->customer_mobile = $data['customer_mobile'] ?? '';
  822. $model->sale_man = $data['sale_man'] ?? '';
  823. $model->install_man = $data['install_man'] ?? '';
  824. $model->is_brash = $data['is_brash'] ?? '';
  825. $model->is_chong = $data['is_chong'] ?? '';
  826. $model->service_mark = $data['service_mark'] ?? '';
  827. $model->mark = $data['mark'] ?? '';
  828. $model->break = $data['break'] ? json_encode($data['break']):json_encode([]);
  829. $model->break_mark = $data['break_mark'] ? json_encode($data['break_mark']):json_encode([]);
  830. $model->project_id = $data['project_id'] ? json_encode($data['project_id']):json_encode([]);
  831. $model->other_project_mark = $data['other_project_mark'] ?? '';
  832. $model->img = $data['img'] ?? '';
  833. $model->img1 = $data['img1'] ?? '';
  834. $model->img2 = $data['img2'] ?? '';
  835. $model->img3 = $data['img3'] ?? '';
  836. $model->save();
  837. return [true,'保存成功!'];
  838. }catch (\Throwable $e){
  839. return [true,'保存成功!'];
  840. }
  841. }
  842. public function deliveryNoteList($data)
  843. {
  844. $list = DeliveryNote::where('del_time',0);
  845. $list = $this->limit($list,'*',$data);
  846. return [true,$list];
  847. }
  848. public function deliveryNoteDetail($data){
  849. if(isset($data['id'])) $model = DeliveryNote::where('id',$data['id'])->where('del_time',0)->first();
  850. if(isset($data['construction_order_number'])) $model = DeliveryNote::where('id',$data['construction_order_number'])->where('del_time',0)->first();
  851. if(empty($model)) return [false,'数据不存在!'];
  852. $employee_key_list = Employee::pluck('emp_name','id')->toArray();
  853. $detail = $model->toArray();
  854. $detail['install_man_title'] = $employee_key_list[$detail['install_man']] ?? "";
  855. $detail['sale_man_title'] = $employee_key_list[$detail['sale_man']] ?? "";
  856. $img_list = ConstructionFile::where('order_number',$detail['construction_order_number'])->where('del_time',0)->select('file as url','name','mark')->get()->toArray();
  857. $detail['file'] = $img_list;
  858. return [true,$detail];
  859. }
  860. public function deliveryNoteDel($data,$user){
  861. DeliveryNote::where('id',$data['id'])->update(
  862. [
  863. 'del_time' => time(),
  864. 'upd_id' => $user['id'],
  865. ]
  866. );
  867. return [true,'删除成功!'];
  868. }
  869. public function constructionPdf($data, $user){
  870. if(empty($data['id'])) return [false, '请选择下载的施工单'];
  871. $construction = Construction::where('del_time',0)
  872. ->where('id',$data['id'])
  873. ->first();
  874. if(empty($construction)) return [false, '施工单不存在或已被删除'];
  875. $construction = $construction->toArray();
  876. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  877. $construction['customer_title'] = $customer_title ?? "";
  878. $construction['sales_order_number'] = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number') ?? '';
  879. $construction['handover_time'] = $construction['handover_time'] ? date("Y-m-d H:i:s",$construction['handover_time']): '';
  880. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  881. $construction_info = ConstructionInfo::where('del_time',0)
  882. ->where('construction_id',$construction['id'])
  883. ->where('type',CustomerInfo::type_two)
  884. ->select('id','employee_id')
  885. ->get()->toArray();
  886. $emp_list = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  887. ->select('emp_name','id','mobile')
  888. ->get()->toArray();
  889. $emp_map = [];
  890. foreach ($emp_list as $value){
  891. $emp_map[$value['id']] = [
  892. 'name' => $value['emp_name'],
  893. 'mobile' => $value['mobile'],
  894. ];
  895. }
  896. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  897. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  898. ->select('a.employee_id', 'b.title')
  899. ->whereIn('employee_id',array_column($emp_list,'id'))
  900. ->get()->toArray();
  901. $emp_message_map = [];
  902. foreach ($emp_message as $value){
  903. if(isset($emp_message_map[$value['employee_id']])){
  904. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  905. }else{
  906. $emp_message_map[$value['employee_id']] = $value['title'];
  907. }
  908. }
  909. $crt_name = ($emp_map[$construction['crt_id']]['name'] ?? '') . ' ' . ($emp_message_map[$construction['crt_id']] ?? '') . ' ' . ($emp_map[$construction['crt_id']]['mobile'] ?? '');
  910. $construction['crt_name'] = $crt_name;
  911. $xt_name = "";
  912. foreach ($construction_info as $value){
  913. $xt_name .= ($emp_map[$value['employee_id']]['name'] ?? '') . ' ' . ($emp_message_map[$value['employee_id']] ?? '') . ' ' . ($emp_map[$value['employee_id']]['mobile'] ?? '') . " | ";
  914. }
  915. $xt_name = rtrim($xt_name, " | ");
  916. $construction['xt_name'] = $xt_name;
  917. $p_info = ConstructionProductInfo::where('del_time',0)
  918. ->where('construction_id',$construction['id'])
  919. ->get()->toArray();
  920. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  921. $unit_map = BasicType::whereIn('id',array_column($map,'unit'))
  922. ->pluck('title','id')->toArray();
  923. $product = [];
  924. $count = 0;
  925. foreach ($p_info as $value){
  926. $tmp = $map[$value['product_id']] ?? [];
  927. $value['count'] = $count+1;
  928. $value['title'] = $tmp['title'] ?? "";
  929. $value['code'] = $tmp['code'] ?? "";
  930. $value['size'] = $tmp['size'] ?? "";
  931. $value['unit'] = $unit_map[$tmp['unit']] ?? "";
  932. $product[] = $value;
  933. }
  934. //工单进展
  935. $detail = $this->getOaJz($data,$user);
  936. $order_message = ['order' => $construction, 'product' => $product,'detail'=> $detail];
  937. $pdf = app('dompdf.wrapper')->loadView('pdf.construction', $order_message);
  938. $file_name = time().rand(1000,9999);
  939. $filename = '施工单_' . $file_name.'.' . 'pdf';
  940. $pdf->save(storage_path('app/public/export/' . $filename));
  941. return [true, ['file' => $filename]];
  942. }
  943. public function getOaJz($data,$user)
  944. {
  945. $oa = Oa::where('menu_id', 34)
  946. ->where('del_time',0)
  947. ->where('channel',$user['depart_top'][0]['depart_id'])
  948. ->first();
  949. if(empty($oa)) return [];
  950. $oa = $oa->toArray();
  951. $list = OaSub::where('oa_id', $oa['id'])->where('del_time', 0)->get()->toArray();
  952. $oa_sub_ids = [];
  953. foreach ($list as $v) {
  954. $oa_sub_ids[] = $v['id'];
  955. }
  956. $oaEmployee = OaSubEmployee::wherein('oa_sub_id', $oa_sub_ids)->get()->toArray();
  957. $emp_list = Employee::whereIn('id',array_column($oaEmployee,'employee_id'))
  958. ->select('emp_name','id','mobile')
  959. ->get()->toArray();
  960. $emp_map = [];
  961. foreach ($emp_list as $value){
  962. $emp_map[$value['id']] = [
  963. 'name' => $value['emp_name'],
  964. 'mobile' => $value['mobile'],
  965. ];
  966. }
  967. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  968. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  969. ->whereIn('a.employee_id',array_column($emp_list,'id'))
  970. ->select('a.employee_id', 'b.title')
  971. ->get()->toArray();
  972. $emp_message_map = [];
  973. foreach ($emp_message as $value){
  974. if(isset($emp_message_map[$value['employee_id']])){
  975. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  976. }else{
  977. $emp_message_map[$value['employee_id']] = $value['title'];
  978. }
  979. }
  980. $oaEmployeeKey = [];
  981. foreach ($oaEmployee as $v) {
  982. $str = "";
  983. $tmp = $emp_map[$v['employee_id']] ?? [];
  984. $tmp2 = $emp_message_map[$v['employee_id']] ?? "";
  985. $str = $tmp['name'] . " " . $tmp2 . " " . $tmp['mobile'];
  986. $oaEmployeeKey[$v['oa_sub_id']][] = [
  987. 'id' => $v['employee_id'],
  988. 'emp_name' => $str,
  989. ];
  990. }
  991. $return = [];
  992. foreach ($list as $v) {
  993. $return[$v['sort']][] = [
  994. 'emp_id' => $oaEmployeeKey[$v['id']],
  995. 'index' => $v['h5_key'],
  996. ];
  997. }
  998. $detail = [];
  999. foreach ($return as $v) {
  1000. $children = "";
  1001. foreach ($v as $vv) {
  1002. $children .= implode('|',array_column($vv['emp_id'],'emp_name')) . ',';
  1003. }
  1004. $detail[] = rtrim($children,',');
  1005. }
  1006. return $detail;
  1007. }
  1008. }