TestController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Authority;
  4. use App\Batch;
  5. use App\City;
  6. use App\Commodity;
  7. use App\CommodityBarcode;
  8. use App\Events\CancelOrder;
  9. use App\InventoryAccountMission;
  10. use App\InventoryCompare;
  11. use App\InventoryDailyLog;
  12. use App\Log;
  13. use App\Logistic;
  14. use App\LogisticTiming;
  15. use App\OracleActAllocationDetails;
  16. use App\OracleBasCode;
  17. use App\OracleBasSKU;
  18. use App\OracleDOCASNDetail;
  19. use App\OracleDOCOrderHeader;
  20. use App\OracleDocOrderPackingSummary;
  21. use App\OracleDOCWaveDetails;
  22. use App\Order;
  23. use App\OrderIssue;
  24. use App\OrderPackage;
  25. use App\OrderPackageCommodities;
  26. use App\OrderTracking;
  27. use App\OrderTrackingOwner;
  28. use App\Owner;
  29. use App\Package;
  30. use App\Process;
  31. use App\ProcessDaily;
  32. use App\ProcessesContent;
  33. use App\ProcessStatistic;
  34. use App\Province;
  35. use App\RejectedBill;
  36. use App\RejectedBillItem;
  37. use App\Services\CityService;
  38. use App\Services\CommodityService;
  39. use App\Services\common\BatchUpdateService;
  40. use App\Services\common\DataHandlerService;
  41. use App\Services\InventoryCompareService;
  42. use App\Services\LogService;
  43. use App\Services\OracleActAllocationDetailService;
  44. use App\Services\OracleDOCOrderHeaderService;
  45. use App\Services\OrderIssuePerformanceService;
  46. use App\Services\OrderIssueService;
  47. use App\Services\OrderPackageCommoditiesService;
  48. use App\Services\OrderPackageService;
  49. use App\Services\OrderService;
  50. use App\Services\OrderTrackingOwnerService;
  51. use App\Services\OrderTrackingService;
  52. use App\StoreCheckingReceive;
  53. use App\StoreCheckingReceiveItem;
  54. use App\Unit;
  55. use App\User;
  56. use App\Waybill;
  57. use Carbon\Carbon;
  58. use Exception;
  59. use Illuminate\Database\Eloquent\Builder;
  60. use Illuminate\Support\Facades\Auth;
  61. use Illuminate\Support\Facades\Cache;
  62. use Illuminate\Http\Request;
  63. use Illuminate\Support\Facades\DB;
  64. use Illuminate\Support\Facades\Http;
  65. use Zttp\Zttp;
  66. class TestController extends Controller
  67. {
  68. private $data = [];
  69. public function __construct()
  70. {
  71. $this->data["active_test"] = "active";
  72. }
  73. public function method(Request $request, $method)
  74. {
  75. return call_user_func([$this, $method], $request);
  76. }
  77. public function test4(){
  78. dd(round("1"/"30",2));
  79. }
  80. public function test2(){
  81. $db = DB::connection('oracle');
  82. $db->beginTransaction();
  83. try{
  84. $bas = $db->select("SELECT * FROM BAS_CODES where codeid = 'IVC_STS' and code = '00'");
  85. foreach ($bas as $a) {
  86. $row = $db->update(DB::raw("UPDATE BAS_CODES set codename_c = '创建' WHERE codeid = ? and code = ?"), [$a->codeid, $a->code]);
  87. if ($row > 0) $db->commit();
  88. }
  89. }catch (\Exception $e){
  90. $db->rollBack();
  91. }
  92. }
  93. function packageFromLog(Request $request)
  94. { //x $packagesBatch=Package::where('batch_number',$batch_number)->first();
  95. ini_set('max_execution_time', 2500);
  96. ini_set('memory_limit', '1526M');
  97. $uploaded = 0;
  98. $count = DB::table('logs')->where('operation', 'like', "%PackageController::new_%")
  99. ->where('created_at', '>', "2020-06-08 15:46:00")
  100. ->where('created_at', '<', "2020-06-08 15:47:00")
  101. ->where('type', "request_new_")
  102. ->count();
  103. $requests = DB::table('logs')->where('operation', 'like', "%PackageController::new_%")
  104. ->where('created_at', '>', "2020-06-08 15:46:00")
  105. ->where('created_at', '<', "2020-06-08 15:47:00")
  106. ->where('type', "request_new_")
  107. ->get();
  108. $requests->each(function ($request) use (&$uploaded) {
  109. $requestJson = json_decode($request->description, true);
  110. $response = Zttp::withHeaders(['content-type' => 'application/json',
  111. ])->post('https://was.baoshi56.com/api/thirdPart/flux/package/new',
  112. $requestJson
  113. );
  114. if ($response->json() && $response->json()['response'] && $response->json()['response']['flag'] == 'Y')
  115. $uploaded += 1;
  116. });
  117. dd($uploaded . '/' . $count);
  118. }
  119. function issues()
  120. {
  121. /** @var OrderPackageService $orderPackageService */
  122. $orderPackageService = app('orderPackageService');
  123. $ids = [741, 739, 738, 736, 735, 733, 732, 731, 730, 729, 728, 725, 724, 723, 721, 720, 719, 718, 717, 715, 714, 713, 712, 711, 710, 705, 703, 702, 701, 700, 699, 698, 696, 694, 693, 692, 691, 690];
  124. $orderIssues = OrderIssue::query()->whereIn('id', $ids)->get();
  125. $order_ids = data_get($orderIssues, '*.order_id');
  126. $orders = Order::query()->whereIn('id', $order_ids)->get();
  127. foreach ($orders as $item) {
  128. $orderPackageService->createdByOrder($item);
  129. }
  130. }
  131. function t2(Request $request)
  132. { //x $packagesBatch=Package::where('batch_number',$batch_number)->first();
  133. $inventoryCompareService = new InventoryCompareService();
  134. echo ($inventoryCompareService)->getCreatingMissionCode('安桥主品');
  135. echo ($inventoryCompareService)->getCreatingMissionCode('安桥主品');
  136. echo ($inventoryCompareService)->getCreatingMissionCode('安桥主品');
  137. echo ($inventoryCompareService)->getCreatingMissionCode('安桥主品');
  138. }
  139. function setCache(Request $request)
  140. {
  141. $today = now();
  142. Cache::put('storedTest', $today);
  143. return "cacheing:'$today'<script>localStorage.setItem('storedTest','{$today}')</script>";
  144. }
  145. function getCache(Request $request)
  146. {
  147. $cache = Cache::get('storedTest');
  148. return "cacheing get:'$cache'<script>document.write('<br>localStorage:'+localStorage.getItem('storedTest'))</script>";
  149. }
  150. function t1(Request $request)
  151. {
  152. {
  153. $username = config('database.connections.oracle.username');
  154. $password = config('database.connections.oracle.password');
  155. $host = config('database.connections.oracle.host');
  156. $service_name = config('database.connections.oracle.service_name');
  157. $conn = oci_connect($username, $password, $host . '/' . $service_name); //连接oracle数据库
  158. $IN_WarehouseID = '';
  159. $IN_TraceID = '';
  160. $IN_WavenNo = '';
  161. $IN_UserID = '';
  162. $IN_Language = '';
  163. $OUT_Return_Code = '';
  164. $sql_sp = "begin SPCUS_EDI_WCS001(:IN_WarehouseID,:IN_Process_Action_C,:IN_Process_By_C,
  165. :IN_WaveNO_C,:IN_OrderNO_C,:IN_OrderLineNO_C,:IN_AllocationDetailsID,:IN_Language,:IN_UserID,:OUT_Return_Code); end;";
  166. $stmt = oci_parse($conn, $sql_sp);
  167. oci_bind_by_name($stmt, ':IN_Warehouse', $IN_WarehouseID);
  168. oci_bind_by_name($stmt, ':IN_TraceID', $IN_TraceID);
  169. oci_bind_by_name($stmt, ':IN_WavenNo', $IN_WavenNo);
  170. oci_bind_by_name($stmt, ':IN_Language', $IN_Language);
  171. oci_bind_by_name($stmt, ':IN_UserID', $IN_UserID);
  172. oci_bind_by_name($stmt, ':OUT_Return_Code', $OUT_Return_Code);
  173. return oci_execute($stmt);
  174. }
  175. }
  176. function packageT(Request $request)
  177. {
  178. $package = Package::where('created_at', '<', '2020-07-08')->whereNotNull('logistic_number')->first();
  179. $package->fetchLogistic();
  180. }
  181. function injectJS(Request $request)
  182. {
  183. $items = RejectedBillItem::whereHas('rejectedBill', function ($query) {
  184. return $query->where('id_owner', 2);
  185. })->where('created_at', '>', '2019-12-23 18:11:00')->where('created_at', '<', '2019-12-24 11:25:00')->get();
  186. (new RejectedBillItemController())->collectionsToPackConfirm($items);
  187. }
  188. public function tj2(Request $request)
  189. {
  190. $numbersesOfBatch = OracleDOCWaveDetails::select('picktotraceid as logistic_number', 'act_allocation_details.orderno')->where('doc_wave_details.waveno', 'W190630000042')
  191. ->leftJoin('act_allocation_details', 'doc_wave_details.orderno', 'act_allocation_details.orderno')->get();
  192. $oneIdxAsrsReceive = OracleDocOrderPackingSummary::where('traceid', $numbersesOfBatch[0]['logistic_number'])->first();
  193. if (!$oneIdxAsrsReceive) {
  194. $inserts = '';
  195. $values = [];
  196. foreach ($numbersesOfBatch as $numbers) {
  197. $inserts .= " into doc_order_packing_summary (traceid,orderno,grossweight,addwho,addtime,editwho,edittime)values(?,?,?,?,?,?,?) ";
  198. $values[] = $numbers['logistic_number'] ?? '';
  199. $values[] = $numbers['orderno'] ?? '';
  200. $values[] = 12;
  201. $values[] = 'WAS';
  202. $values[] = Carbon::now();
  203. $values[] = 'WAS';
  204. $values[] = Carbon::now();
  205. }
  206. DB::connection('oracle')->insert("insert all {$inserts} select * from dual", $values);
  207. } else {
  208. $logisticNumbers = $numbersesOfBatch->map(function ($numbers) {
  209. return $numbers['logistic_number'];
  210. });
  211. OracleDocOrderPackingSummary::whereIn('traceid', $logisticNumbers)->update(['grossweight' => 32]);
  212. }
  213. }
  214. public function delme()
  215. {
  216. $fields = [
  217. 'doc_order_header.userdefine1',
  218. 'doc_order_header.userdefine2',
  219. 'doc_order_header.SOReference5',
  220. 'doc_order_header.waveno',
  221. 'doc_order_header.orderno',
  222. 'doc_order_header.customerid',
  223. 'doc_order_header.Consigneename'
  224. ];
  225. if ('') {
  226. $resultOracleObj = OracleDOCOrderHeader::select($fields)->where('orderno', 'SO200603004708');
  227. } else {
  228. $resultOracleObj = OracleActAllocationDetails::select($fields);
  229. $resultOracleObj->where('picktotraceid', '546152742096');
  230. $resultOracleObj->leftJoin('DOC_Order_Header', 'act_allocation_details.orderno', 'doc_order_header.orderno');
  231. }
  232. $_temOracleInfo = $resultOracleObj->first();
  233. dd($_temOracleInfo);
  234. }
  235. public function mergerPackageData()
  236. {
  237. ini_set('max_execution_time', 36000);
  238. ini_set('memory_limit', '1526M');
  239. $number = 3000;
  240. $start_at = 1;
  241. $sum = Package::query()->where('id', '>', $start_at)->count();
  242. $len = ceil($sum / $number);
  243. $id = $start_at;
  244. for ($i = 1; $i <= $len; $i++) {
  245. $packages = Package::query()->orderBy('id')->where('id', '>', $id)->limit($number)->get();
  246. $error = [];
  247. foreach ($packages as $package) {
  248. /** @var $service OrderPackageService */
  249. $service = app('orderPackageService');
  250. $orderPackage = $service->firstOrCreate($package->logistic_number,
  251. ['batch_number' => $package->batch_number,
  252. 'batch_rule' => $package->batch_rule,
  253. 'bulk' => $package->bulk,
  254. 'weight' => $package->weight,
  255. 'length' => $package->length,
  256. 'width' => $package->width,
  257. 'height' => $package->height,
  258. 'paper_box_id' => $package->paper_box_id,
  259. 'measuring_machine_id' => $package->measuring_machine_id,
  260. 'weighed_at' => $package->weighed_at,
  261. 'status' => $package->status]);
  262. if (!$orderPackage) array_push($error, $package->id);
  263. }
  264. LogService::log(__METHOD__, "同步时WMS中未找到这些订单", json_encode($error));
  265. $id = $packages[count($packages) - 1]->id;
  266. echo $number . ' a bunch done at:' . now()->toFormattedDateString() . '<br>';
  267. }
  268. }
  269. public function excelIt()
  270. {
  271. $excel = new ExcelController();
  272. return $excel->makeExcel();
  273. }
  274. public function featureIt()
  275. {
  276. LogisticNumberFeatureController::loadRecentRejectedsToFeatures(5, 2500);
  277. }
  278. public function newSku()
  279. {
  280. $url = 'http://bswcs/api/sorting/flux/newSku';
  281. $response = Zttp::post($url, [
  282. 'request' => [
  283. [
  284. "SKU" => "1234567890",
  285. "NAME" => "瑞士莲**巧克力",
  286. "Alternate_SKU1" => "1122334455",
  287. "GrossWeight" => "1.2",
  288. "Cube" => "0.75",
  289. "SKULength" => "0.25",
  290. "SKUWidth" => "0.15",
  291. "SKUHigh" => "0.05"
  292. ],
  293. [
  294. "SKU" => "1234567892",
  295. "NAME" => "跳跳饼",
  296. "Alternate_SKU1" => "",
  297. "GrossWeight" => "1.3",
  298. "Cube" => "0.75",
  299. "SKULength" => "0.25",
  300. "SKUWidth" => "0.15",
  301. "SKUHigh" => "0.25"
  302. ],
  303. ]
  304. ]);
  305. return $response->json();
  306. }
  307. public function changePackage()
  308. {
  309. $packages = Package::where('delivery_number', '<>', null)->where('logistic_id', null)->get();
  310. $packages = Package::whereRaw('delivery_number IS NOT NULL AND logistic_id IS NULL')->get();
  311. $logistics = Logistic::get();
  312. $packages->each(function ($package) use ($logistics) {
  313. $logistics->each(function ($logistic) use ($package) {
  314. if ($package->WMSReflectPackage->CarrierID == $logistic->code) {
  315. $package->logistic_id = $logistic->id;
  316. return;
  317. }
  318. });
  319. });
  320. return "OK";
  321. }
  322. public function tNull()
  323. {
  324. User::query()->get()->toArray();
  325. User::query()->get()->toJson();
  326. }
  327. public function deletePackageAuthority()
  328. {
  329. $authorities = Authority::where('name', 'like', '%包裹信息%')->get();
  330. $authorities->each(function ($authority) {
  331. $authority->delete();
  332. });
  333. }
  334. function test()
  335. {
  336. $ids = [];
  337. $logs = Log::query()->select('description')->where('type', '同步时WMS中未找到这些订单')->get();
  338. foreach ($logs as $log) {
  339. $arr = [];
  340. eval("\$arr = " . $log->description . '; ');
  341. if (count($arr) < 1) continue;
  342. $ids = array_merge($ids, $arr);
  343. }
  344. $ids = array_unique($ids);
  345. $error = [];
  346. foreach ($ids as $id) {
  347. $package = Package::query()->find($id);
  348. /** @var OrderPackageService */
  349. $orderPackage = app('orderPackageService')->firstOrCreate($package->logistic_number,
  350. ['batch_number' => $package->batch_number,
  351. 'batch_rule' => $package->batch_rule,
  352. 'bulk' => $package->bulk,
  353. 'weight' => $package->weight,
  354. 'length' => $package->length,
  355. 'width' => $package->width,
  356. 'height' => $package->height,
  357. 'paper_box_id' => $package->paper_box_id,
  358. 'measuring_machine_id' => $package->measuring_machine_id,
  359. 'weighed_at' => $package->weighed_at,
  360. 'status' => $package->status]);
  361. if (!$orderPackage) array_push($error, $package->id);
  362. }
  363. if (count($error) > 0) LogService::log(__METHOD__, "同步时WMS中未找到这些订单_two", json_encode($error));
  364. }
  365. function test1()
  366. {
  367. $statistics = ProcessStatistic::query()->whereNull('revenue')->get();
  368. $id = array_column($statistics->toArray(), 'process_id');
  369. $processes = Process::query()->where(function (Builder $query) {
  370. $query->where('status', '待交接')->orWhere('status', '交接完成');
  371. })->whereIn('id', $id)->get();
  372. $sign_end = true;
  373. foreach ($processes as $process) {
  374. if (count($process->processDailies) > 0) {
  375. $completed_amount = 0;
  376. foreach ($process->processDailies as $processDaily) {
  377. $completed_amount = $completed_amount + ($processDaily->output);
  378. }
  379. $process->completed_amount = $completed_amount;
  380. }
  381. //统计:
  382. $revenue = ($process->unit_price) * ($process->completed_amount); //收入合计
  383. $processDailies = ProcessDaily::with('processDailyParticipants')->where('process_id', $process->id)->where('output', '>', 0)->get();
  384. $duration_days = count($processDailies); //完成天数
  385. $duration_man_hours = 0; //总工时
  386. $total_cost = 0; //合计成本
  387. foreach ($processDailies as $processDailyOne) {
  388. foreach ($processDailyOne->processDailyParticipants as $processDailyParticipant) {
  389. if (!$processDailyParticipant->unit_price && !$processDailyParticipant->hour_price) continue;
  390. $duration_man_hours += $processDailyParticipant->hour_count;
  391. if ($processDailyParticipant->unit_count) {
  392. $total_cost += ($processDailyParticipant->unit_count) * ($processDailyParticipant->unit_price);
  393. continue;
  394. }
  395. $total_cost += ($processDailyParticipant->hour_count) * ($processDailyParticipant->hour_price);
  396. }
  397. };
  398. $processStatistic = ProcessStatistic::query()->find($process->id);
  399. $processStatistic->revenue = $revenue;
  400. $processStatistic->duration_days = $duration_days;
  401. $processStatistic->duration_man_hours = $duration_man_hours;
  402. if ($sign_end) $processStatistic->ended_at = date('Y-m-d H:i:s');
  403. if (!$revenue || !$total_cost) {
  404. $processStatistic->update();
  405. continue;
  406. }
  407. $gross_profit = $revenue - $total_cost; //毛利润
  408. if ($gross_profit != 0) $gross_profit_rate = $gross_profit / $revenue; //毛利率;
  409. else $gross_profit_rate = 0;
  410. $processStatistic->total_cost = $total_cost;
  411. $processStatistic->gross_profit = $gross_profit;
  412. $processStatistic->gross_profit_rate = $gross_profit_rate;
  413. $processStatistic->update();
  414. $this->log(__METHOD__, "修改二次加工单统计单_" . __FUNCTION__, json_encode($processStatistic), Auth::user()['id']);
  415. }
  416. }
  417. public function test5(){
  418. ini_set('max_execution_time',2500);
  419. ini_set('memory_limit','1526M');
  420. //清理冗余条码
  421. //$this->cleanBarcode();
  422. while(true){
  423. $toDay = Carbon::now();
  424. $skus = DB::select(DB::raw('select sku from commodities group by sku,owner_id having count(*)>1 limit 500 '));
  425. $skus = array_column($skus,'sku');
  426. $commodities = Commodity::query()->with('barcodes')->whereNotNull('owner_id')->whereIn('sku',$skus)->get();
  427. if (count($commodities) < 1)return "SUCCESS";
  428. $commodityMap = [];
  429. $commodityDel = [];
  430. $commodityTag = [];
  431. $commodityBar = [];
  432. $createBarcodes = [];
  433. $logs = [];
  434. foreach ($commodities as $commodity){
  435. if ($commodityMap[$commodity->sku.'_'.$commodity->owner_id] ?? false){
  436. $codes = $commodity->barcodes ? array_column($commodity->barcodes->toArray(),'code') : [];
  437. $logs[] = [
  438. 'id' => $commodity->id,
  439. 'sku' => $commodity->sku,
  440. 'owner_id'=>$commodity->owner_id,
  441. 'code'=>$codes,
  442. ];
  443. $commodityDel[] = $commodity->id;
  444. $commodityTag[$commodity->id] = $commodityMap[$commodity->sku.'_'.$commodity->owner_id];
  445. $arr = array_diff($codes,$commodityBar[$commodity->sku.'_'.$commodity->owner_id]);
  446. foreach ($arr as $code){
  447. if (!$code)continue;
  448. $createBarcodes[] = [
  449. 'code' => $code,
  450. 'commodity_id' => $commodityMap[$commodity->sku.'_'.$commodity->owner_id],
  451. 'created_at' => $toDay,
  452. ];
  453. }
  454. }else{
  455. $commodityMap[$commodity->sku.'_'.$commodity->owner_id] = $commodity->id;
  456. $commodityBar[$commodity->sku.'_'.$commodity->owner_id] = $commodity->barcodes ? array_column($commodity->barcodes->toArray(),'code') : [];
  457. }
  458. }
  459. dd($commodityMap,$commodityDel,$commodityTag);
  460. LogService::log(__METHOD__,'清理商品',json_encode($logs,JSON_UNESCAPED_UNICODE));
  461. LogService::log(__METHOD__,'重新分配商品',json_encode($commodityTag,JSON_UNESCAPED_UNICODE));
  462. app('inventoryAccountMissionService')->batchUpdateItself('commodity_id', $commodityTag);//批量更新库存盘点任务
  463. app('inventoryCompareService')->batchUpdateItself('commodity_id', $commodityTag);//批量更新库存对比
  464. app('inventoryDailyLogService')->batchUpdateItself('commodity_id', $commodityTag);//批量更新库存每日记录
  465. app('processesContentService')->batchUpdateItself('commodity_id', $commodityTag);//批量更新二次加工内容单
  466. app('storeCheckingReceiveItemService')->batchUpdateItself('commodity_id', $commodityTag);//批量更新入库盘收一体
  467. app('orderPackageCommoditiesService')->batchUpdateItself('commodity_id', $commodityTag);//批量更新订单商品
  468. LogService::log(__METHOD__,'删除商品与对应条码',json_encode($commodityDel,JSON_UNESCAPED_UNICODE));
  469. CommodityBarcode::query()->whereIn('commodity_id',$commodityDel)->delete();
  470. Commodity::destroy($commodityDel);
  471. }
  472. }
  473. private function cleanBarcode(){
  474. $logCommodityBarcodes = CommodityBarcode::query()->where('code', "")->get();
  475. if (count($logCommodityBarcodes) > 0) LogService::log(__METHOD__, "纠正商品-删除空条码", json_encode($logCommodityBarcodes, JSON_UNESCAPED_UNICODE));
  476. CommodityBarcode::query()->where('code', "")->delete();
  477. $barcodes = DB::select(DB::raw('select * from commodity_barcodes c
  478. where (c.code,c.commodity_id) in (select code,commodity_id from commodity_barcodes group by code,commodity_id having count(*) > 1) order by commodity_id'));
  479. $barcodeMap = [];
  480. $barcodeDelete = [];
  481. foreach ($barcodes as $barcode) {
  482. if (isset($barcodeMap[$barcode->code . '_' . $barcode->commodity_id])) $barcodeDelete[] = $barcode->id;
  483. else $barcodeMap[$barcode->code . '_' . $barcode->commodity_id] = $barcode->id;
  484. }
  485. $logCommodityBarcodes = CommodityBarcode::query()->whereIn('id', $barcodeDelete)->get();
  486. if (count($logCommodityBarcodes) > 0) LogService::log(__METHOD__, "纠正商品-删除重复条码", json_encode($logCommodityBarcodes, JSON_UNESCAPED_UNICODE));
  487. CommodityBarcode::destroy($barcodeDelete);
  488. }
  489. public function correctCommodity()
  490. {
  491. ini_set('max_execution_time', 2500);
  492. ini_set('memory_limit', '1526M');
  493. //清理冗余条码
  494. //$this->cleanBarcode();
  495. //获取重复条码
  496. $commodities = DB::select(DB::raw('select commodities.name,commodities.sku,commodities.owner_id,commodity_barcodes.id as barcode_id,commodity_barcodes.code as barcode_code,commodity_barcodes.commodity_id from commodities LEFT JOIN commodity_barcodes on commodities.id = commodity_barcodes.commodity_id
  497. where (commodities.owner_id,commodity_barcodes.code) in (select commodities.owner_id,commodity_barcodes.code from commodities LEFT JOIN commodity_barcodes on commodities.id = commodity_barcodes.commodity_id group by commodities.owner_id,commodity_barcodes.code having count(*) > 1) order by commodities.owner_id,commodity_barcodes.code'));
  498. //对比map池
  499. $commodityMap = [];
  500. //需要删除项
  501. $commodityDelete = [];
  502. foreach ($commodities as $index => $commodity) {
  503. $commodity->barcode_code = strtolower(trim($commodity->barcode_code));
  504. //货主+条码 为唯一key值 设想正常数据下同货主不应该有同条码
  505. $key = $commodity->owner_id . '_' . $commodity->barcode_code;
  506. //使用map池对比为重复数据
  507. if (isset($commodityMap[$key])) {
  508. //获取下标指针指向的源数据
  509. $c = $commodities[$commodityMap[$key]];
  510. //源数据不符合规范,当前数据替换掉它
  511. if (($c->sku == null || $c->sku == "") && ($commodity->sku != null && $commodity->sku != "")) {
  512. $commodityDelete[] = $commodityMap[$key];
  513. $commodityMap[$key] = $index;
  514. } else {
  515. //当前数据比源数据更符合预期,替换掉
  516. if (($c->sku == $commodity->sku) && (strlen($c->name) < strlen($commodity->name))) {
  517. $commodityDelete[] = $commodityMap[$key];
  518. $commodityMap[$key] = $index;
  519. } else {
  520. //扔进处理池
  521. $commodityDelete[] = $index;
  522. }
  523. }
  524. //第一次出现的新数据,val为下标,当作指针指向源数据
  525. } else $commodityMap[$key] = $index;
  526. }
  527. $createBarcodes = []; //批量生成条码
  528. $deleteCommodities = []; //批量删除商品及商品条码
  529. $updateCommodities = [];
  530. foreach ($commodityDelete as $index) {
  531. //获取到被处理数据 与 目标数据
  532. $del = $commodities[$index];
  533. $target = $commodities[$commodityMap[$del->owner_id . '_' . $del->barcode_code]];
  534. //记录ID删除商品与条码
  535. $deleteCommodities[] = $del->commodity_id;
  536. //有效条码合并
  537. $barcodes = DB::select(DB::raw("select * from
  538. (select * from commodity_barcodes where commodity_id = " . $del->commodity_id . ")a
  539. where (select count(1) as num from commodity_barcodes b
  540. where commodity_id = " . $target->commodity_id . " and b.code = a.code) = 0"));
  541. if (count($barcodes) > 0) {
  542. foreach ($barcodes as $barcode) {
  543. $createBarcodes[] = ['code' => $barcode->code, 'commodity_id' => $target->commodity_id];
  544. }
  545. }
  546. $updateCommodities[$del->commodity_id] = $target->commodity_id;
  547. }
  548. if (count($updateCommodities) > 0){
  549. app('inventoryAccountMissionService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新库存盘点任务
  550. app('inventoryCompareService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新库存对比
  551. app('inventoryDailyLogService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新库存每日记录
  552. app('processesContentService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新二次加工内容单
  553. app('storeCheckingReceiveItemService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新入库盘收一体
  554. app('orderPackageCommoditiesService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新订单商品
  555. }
  556. if (count($createBarcodes) > 0) LogService::log(__METHOD__, "纠正商品-录入合并条码", json_encode(data_get($createBarcodes, '*.id'), JSON_UNESCAPED_UNICODE));
  557. CommodityBarcode::query()->insert($createBarcodes);
  558. $logCommodityBarcodes = CommodityBarcode::query()->whereIn('commodity_id', $deleteCommodities)->get();
  559. if (count($logCommodityBarcodes) > 0) LogService::log(__METHOD__, "纠正商品-删除无用商品条码", json_encode(data_get($logCommodityBarcodes, '*.id'), JSON_UNESCAPED_UNICODE));
  560. CommodityBarcode::query()->whereIn('commodity_id', $deleteCommodities)->delete();
  561. // $logCommodities = Commodity::query()->whereIn('id',$deleteCommodities)->get();
  562. if (count($deleteCommodities) > 0) LogService::log(__METHOD__, "纠正商品-删除无用商品", json_encode($deleteCommodities, JSON_UNESCAPED_UNICODE));
  563. Commodity::destroy($deleteCommodities);
  564. }
  565. /*1*/
  566. function socket(Request $request)
  567. {/**/
  568. $a = microtime(true);
  569. error_reporting(E_ALL);
  570. set_time_limit(0);
  571. echo "<h2>TCP/IP Connection</h2>\n";
  572. $port = 8091;
  573. $ip = "127.0.0.1";
  574. /*
  575. +-------------------------------
  576. * @socket连接整个过程
  577. +-------------------------------
  578. * @socket_create
  579. * @socket_connect
  580. * @socket_write
  581. * @socket_read
  582. * @socket_close
  583. +--------------------------------
  584. */
  585. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  586. if ($socket < 0) {
  587. echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
  588. } else {
  589. echo "OK.\n";
  590. }
  591. echo "试图连接 '$ip' 端口 '$port'...\n";
  592. $result = socket_connect($socket, $ip, $port);
  593. if ($result < 0) {
  594. echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
  595. } else {
  596. echo "连接OK\n";
  597. }
  598. $b = microtime(true);
  599. echo "连接耗时:" . ($b - $a) . "\n";
  600. $content = Commodity::query()->limit(200)->get();
  601. $c = microtime(true);
  602. echo "查询数据耗时:" . ($c - $b) . "\n";
  603. $content->toArray();
  604. /*$chunks=$content->chunk(100);
  605. foreach ($chunks as $chunk){
  606. $chunk->toJson();
  607. var_dump(mb_strlen($chunk->toJson()));
  608. //socket_write($socket, $chunk->toJson(), mb_strlen($chunk->toJson()));
  609. }*/
  610. $d = microtime(true);
  611. echo "发送数据耗时:" . ($d - $c) . "\n";
  612. socket_close($socket);
  613. $e = microtime(true);
  614. echo "共耗时:" . ($e - $a) . "\n";
  615. }
  616. public function test3()
  617. {
  618. /** @var OrderTrackingService $orderTrackingService */
  619. $orderTrackingService = app('orderTrackingService');
  620. /** @var OrderService $orderService */
  621. $orderService = app(OrderService::class);
  622. $owners = app(OrderTrackingOwnerService::class)->getTrackingOrderOwner();
  623. $startDate = \Illuminate\Support\Carbon::parse('2020-10-10 00:00:00')->toDateTimeString();
  624. $orderHeaders = OracleDOCOrderHeader::query()->with(['oracleDOCOrderDetails', 'actAllocationDetails', 'oracleBASCode'])
  625. ->where('addTime', '>=', $startDate)
  626. ->whereIn('customerID', data_get($owners, '*.code'))
  627. ->get();
  628. $orderService->updateByWmsOrders($orderHeaders);
  629. $orderTrackingService->updateByWmsOrderHeaders($orderHeaders);
  630. }
  631. public function usage()
  632. {
  633. dd(Request::all());
  634. }
  635. }