TPlusServerService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDataJob;
  4. use App\Model\Depart;
  5. use App\Model\Employee;
  6. use App\Model\EmployeeDepartPermission;
  7. use App\Model\Product;
  8. use App\Model\RevenueCost;
  9. use Illuminate\Database\Schema\Blueprint;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Schema;
  12. class TPlusServerService extends Service
  13. {
  14. /**
  15. * @var TPlusDatabaseServerService
  16. */
  17. protected $databaseService;
  18. /**
  19. * @var string|null
  20. */
  21. protected $error;
  22. /**
  23. * TPlusServerService constructor.
  24. */
  25. public function __construct()
  26. {
  27. $service = new TPlusDatabaseServerService();
  28. $this->databaseService = $service->db;
  29. $this->error = $service->error;
  30. }
  31. /**
  32. * 获取错误信息
  33. *
  34. * @return string|null
  35. */
  36. public function getError()
  37. {
  38. return $this->error;
  39. }
  40. private $table = "tmp_revenue_cost_data";
  41. /**
  42. * 同步人员部门
  43. *
  44. * @param array $data
  45. * @param array $user
  46. * @return array
  47. */
  48. public function synPersonDepart($data, $user)
  49. {
  50. try {
  51. $this->databaseService->table('AA_Department')
  52. ->select('id','idparent as parent_id','name as title','code','disabled as is_use')
  53. ->chunkById(100, function ($data) {
  54. DB::transaction(function () use ($data) {
  55. $dataArray = Collect($data)->map(function ($object) {
  56. return (array)$object;
  57. })->toArray();
  58. $d_id = Depart::whereIn('id', array_column($dataArray,'id'))
  59. ->pluck('id')
  60. ->toArray();
  61. $insert = $update = [];
  62. foreach ($dataArray as $value){
  63. $is_use = $value['is_use'] ? 0 : 1;
  64. if(in_array($value['id'], $d_id)){
  65. $update[] = [
  66. 'id' => $value['id'],
  67. 'parent_id' => $value['parent_id'],
  68. 'title' => $value['title'],
  69. 'code' => $value['code'],
  70. 'is_use' => $is_use
  71. ];
  72. }else{
  73. $insert[] = [
  74. 'id' => $value['id'],
  75. 'parent_id' => $value['parent_id'],
  76. 'title' => $value['title'],
  77. 'code' => $value['code'],
  78. 'is_use' => $is_use
  79. ];
  80. }
  81. }
  82. if(! empty($insert)) Depart::insert($insert);
  83. if(! empty($update)) {
  84. foreach ($update as $value){
  85. Depart::where('id', $value['id'])
  86. ->update($value);
  87. }
  88. }
  89. });
  90. });
  91. $this->databaseService->table('AA_Person')
  92. ->select('id','code as number','name as emp_name','mobilePhoneNo as mobile','iddepartment as depart_id','disabled as state')
  93. ->chunkById(100, function ($data) {
  94. DB::transaction(function () use ($data) {
  95. $dataArray = Collect($data)->map(function ($object) {
  96. return (array)$object;
  97. })->toArray();
  98. $employee_id = Employee::whereIn('id', array_column($dataArray,'id'))
  99. ->pluck('id')
  100. ->toArray();
  101. $insert = $update = $depart_update = [];
  102. foreach ($dataArray as $value){
  103. $state = $value['state'] ? Employee::NOT_USE : Employee::USE;
  104. if(in_array($value['id'], $employee_id)){
  105. $update[] = [
  106. 'id' => $value['id'],
  107. 'number' => $value['number'],
  108. 'emp_name' => $value['emp_name'],
  109. 'mobile' => $value['mobile'],
  110. 'state' => $state
  111. ];
  112. }else{
  113. $insert[] = [
  114. 'id' => $value['id'],
  115. 'number' => $value['number'],
  116. 'emp_name' => $value['emp_name'],
  117. 'mobile' => $value['mobile'],
  118. 'state' => $state
  119. ];
  120. }
  121. $depart_update[] = [
  122. 'employee_id' => $value['id'],
  123. 'depart_id' => $value['depart_id']
  124. ];
  125. }
  126. if(! empty($insert)) Employee::insert($insert);
  127. if(! empty($update)) {
  128. foreach ($update as $value){
  129. Employee::where('id', $value['id'])
  130. ->update($value);
  131. }
  132. }
  133. if(! empty($depart_update)){
  134. EmployeeDepartPermission::whereIn('employee_id',array_column($depart_update,'employee_id'))->delete();
  135. EmployeeDepartPermission::insert($depart_update);
  136. }
  137. });
  138. });
  139. } catch (\Throwable $e) {
  140. return [false, $e->getMessage()];
  141. }
  142. return [true, ''];
  143. }
  144. /**
  145. * 收入成本统计同步
  146. *
  147. * @param array $data
  148. * @param array $user
  149. * @return array
  150. */
  151. public function synRevenueCost($data, $user){
  152. if(empty($data['crt_time']) || ! is_array($data['crt_time'])) return [false, '同步时间不能为空'];
  153. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['crt_time'],false);
  154. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "同步时间:时间区间无效"];
  155. list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
  156. if(! $bool) return [false, $bool_msg];
  157. $data['start_timeStamp'] = $start_time;
  158. $data['end_timeStamp'] = $end_time;
  159. $start = date('Y-m-d H:i:s.000', $start_time);
  160. $end = date('Y-m-d H:i:s.000', $end_time);
  161. $data['start_time'] = $start;
  162. $data['end_time'] = $end;
  163. $data['operation_time'] = time();
  164. if(empty($data['type'])) return [false, '同步类型不能为空'];
  165. if(in_array($data['type'],[1,2,3,4])){
  166. list($status,$msg) = $this->limitingSendRequest($this->table);
  167. if(! $status) return [false, '收入成本相关信息同步正在后台运行,请稍后'];
  168. // //同步
  169. // list($status, $msg) = $this->synRevenueCostFromTPlus($data, $user);
  170. // if(! $status) {
  171. // $this->clearTmpTable();
  172. // $this->dellimitingSendRequest($this->table);
  173. // return [false, $msg];
  174. // }
  175. //队列
  176. ProcessDataJob::dispatch($data, $user)->onQueue(RevenueCost::job);
  177. }else{
  178. return [false, '同步类型错误'];
  179. }
  180. return [true, '收入成本相关信息同步已进入后台任务'];
  181. }
  182. public function synRevenueCostFromTPlus($data, $user){
  183. //创建临时表 如果不存在
  184. $this->createTmpTable();
  185. //清理临时表 如果内容不为空
  186. $this->clearTmpTable();
  187. $type = $data['type'];
  188. //写入临时数据
  189. if($type == 1){
  190. list($status, $msg) = $this->xhdTPlus($data, $user);
  191. if(! $status) return [false, $msg];
  192. list($status, $msg) = $this->xsfpTPlus($data, $user);
  193. if(! $status) return [false, $msg];
  194. list($status, $msg) = $this->hkdTPlus($data, $user);
  195. if(! $status) return [false, $msg];
  196. }elseif ($type == 2){
  197. list($status, $msg) = $this->xhdTPlus($data, $user);
  198. if(! $status) return [false, $msg];
  199. }elseif ($type == 3){
  200. list($status, $msg) = $this->xsfpTPlus($data, $user);
  201. if(! $status) return [false, $msg];
  202. }elseif ($type == 4){
  203. list($status, $msg) = $this->hkdTPlus($data, $user);
  204. if(! $status) return [false, $msg];
  205. }
  206. //更新主表数据
  207. list($status,$msg) = $this->updateRevenueCost($data);
  208. if(! $status) return [false, $msg];
  209. //都成功后 清理临时表
  210. $this->clearTmpTable();
  211. //释放redis
  212. $this->delTableKey();
  213. return [true, '同步成功'];
  214. }
  215. private function xhdTPlus($data, $user){
  216. try {
  217. $table = $this->table;
  218. $limit = 500;
  219. $lastId = 0;
  220. do {
  221. $rows = $this->databaseService->table('SA_SaleDelivery_b as sd_b')
  222. ->join('SA_SaleDelivery as sd', 'sd_b.idSaleDeliveryDTO', '=', 'sd.ID')
  223. ->leftJoin('AA_Partner as pn', 'sd.idsettlecustomer', '=', 'pn.ID')
  224. ->leftJoin('AA_Person as ps', 'sd.idclerk', '=', 'ps.ID')
  225. ->leftJoin('AA_Inventory as it', 'sd_b.idinventory', '=', 'it.ID')
  226. ->leftJoin('AA_Unit as ui', 'sd_b.idbaseunit', '=', 'ui.ID')
  227. ->where('sd.voucherdate', '>=', $data['start_time'])
  228. ->where('sd.voucherdate', '<=', $data['end_time'])
  229. ->where('sd_b.ID', '>', $lastId) // 用真实字段
  230. ->orderBy('sd_b.ID')
  231. ->limit($limit)
  232. ->selectRaw("
  233. COALESCE(sd.ID, 0) as order_id,
  234. COALESCE(sd.code, '') as order_number,
  235. sd.voucherdate as order_time,
  236. COALESCE(ps.name, '') as employee_id_1_title,
  237. COALESCE(sd.idclerk, 0) as employee_id_1,
  238. COALESCE(pn.priuserdefnvc15, '') as employee_id_2_title,
  239. COALESCE(pn.code, '') as customer_code,
  240. COALESCE(pn.name, '') as customer_title,
  241. COALESCE(sd.pubuserdefnvc11, '') as channel_finance,
  242. COALESCE(sd.pubuserdefnvc12, '') as channel_details,
  243. COALESCE(it.code, '') as product_code,
  244. COALESCE(it.name, '') as product_title,
  245. COALESCE(it.specification, '') as product_size,
  246. COALESCE(ui.name, '') as unit,
  247. COALESCE(sd_b.quantity, 0) as quantity,
  248. COALESCE(sd_b.taxPrice, 0) as price_3,
  249. COALESCE(sd_b.taxAmount, 0) as price_3_total,
  250. COALESCE(sd_b.ID, 0) as id_detail
  251. ")
  252. ->get();
  253. if ($rows->isEmpty()) break;
  254. $dataArray = Collect($rows)->map(function ($object) {
  255. return (array)$object;
  256. })->toArray();
  257. //存货档案
  258. $product = Product::where('del_time', 0)
  259. ->whereIn('code', array_unique(array_column($dataArray, 'product_code')))
  260. ->select('code', 'write_off_price', 'freight_price', 'business_cost')
  261. ->get()->toArray();
  262. $product_map = array_column($product, null, 'code');
  263. //组织数据
  264. foreach ($dataArray as $key => $value) {
  265. $p_tmp = $product_map[$value['product_code']] ?? [];
  266. $dataArray[$key]['order_type'] = RevenueCost::ORDER_ONE;
  267. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  268. $write_off_price = $p_tmp['write_off_price'] ?? 0;
  269. $dataArray[$key]['price_1'] = $write_off_price;
  270. $dataArray[$key]['price_1_total'] = bcmul($write_off_price, $value['quantity'], 2);
  271. $freight_price = $p_tmp['freight_price'] ?? 0;
  272. $dataArray[$key]['price_2'] = $freight_price;
  273. $dataArray[$key]['price_2_total'] = bcmul($freight_price, $value['quantity'], 2);
  274. $business_cost = $p_tmp['business_cost'] ?? 0;
  275. $dataArray[$key]['price_4'] = $business_cost;
  276. $price_4_total = bcmul($business_cost, $value['quantity'], 2);
  277. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'], 2);
  278. $profit = bcsub($value['price_3_total'], $price_4_total, 2);
  279. $dataArray[$key]['profit'] = $profit;
  280. $dataArray[$key]['profit_rate'] = $value['price_3_total'] > 0 ? bcdiv($profit, $value['price_3_total'], 2) : 0;
  281. }
  282. DB::table($table)->insert($dataArray);
  283. // 更新 lastId 继续下一批
  284. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  285. } while (count($rows) === $limit);
  286. }catch (\Throwable $exception){
  287. return [false, "销货单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  288. }
  289. return [true, ''];
  290. }
  291. private function xsfpTPlus($data, $user){
  292. try {
  293. $table = $this->table;
  294. $limit = 500;
  295. $lastId = 0;
  296. do {
  297. $rows = $this->databaseService->table('SA_SaleInvoice_b as si_b')
  298. ->join('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  299. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  300. ->leftJoin('AA_Person as ps', 'si.idclerk', '=', 'ps.ID')
  301. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  302. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  303. ->where('si.voucherdate','>=',$data['start_time'])
  304. ->where('si.voucherdate','<=',$data['end_time'])
  305. ->where('si_b.ID', '>', $lastId) // 用真实字段
  306. ->orderBy('si_b.ID')
  307. ->limit($limit)
  308. ->selectRaw("
  309. COALESCE(si.ID, 0) as order_id,
  310. COALESCE(si.code, '') as order_number,
  311. si.voucherdate as order_time,
  312. COALESCE(pn.code, '') as customer_code,
  313. COALESCE(pn.name, '') as customer_title,
  314. COALESCE(si.idclerk, 0) as employee_id_1,
  315. COALESCE(ps.name, '') as employee_id_1_title,
  316. COALESCE(pn.priuserdefnvc15, '') as employee_id_2_title,
  317. COALESCE(it.code, '') as product_code,
  318. COALESCE(it.name, '') as product_title,
  319. COALESCE(it.specification, '') as product_size,
  320. COALESCE(ui.name, '') as unit,
  321. COALESCE(si_b.quantity, 0) as quantity,
  322. COALESCE(si_b.taxPrice, 0) as price_1,
  323. COALESCE(si_b.taxAmount, 0) as price_1_total,
  324. COALESCE(si_b.ID, 0) as id_detail,
  325. COALESCE(si_b.sourceVoucherDetailId, 0) as id_detail_upstream,
  326. COALESCE(si_b.sourceVoucherCode, '') as order_number_upstream
  327. ")
  328. ->get();
  329. if ($rows->isEmpty()) break;
  330. $dataArray = Collect($rows)->map(function ($object) {
  331. return (array)$object;
  332. })->toArray();
  333. //存货档案
  334. $product = Product::where('del_time',0)
  335. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  336. ->select('code','business_cost')
  337. ->get()->toArray();
  338. $product_map = array_column($product,null,'code');
  339. //组织数据
  340. foreach ($dataArray as $key => $value){
  341. $p_tmp = $product_map[$value['product_code']] ?? [];
  342. $dataArray[$key]['order_type'] = RevenueCost::ORDER_TWO;
  343. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  344. $business_cost = $p_tmp['business_cost'] ?? 0;
  345. $dataArray[$key]['price_4'] = $business_cost;
  346. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  347. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'],2);
  348. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  349. $dataArray[$key]['profit'] = $profit;
  350. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  351. }
  352. DB::table($table)->insert($dataArray);
  353. // 更新 lastId 继续下一批
  354. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  355. } while (count($rows) === $limit);
  356. }catch (\Throwable $exception){
  357. return [false, "销售发票同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  358. }
  359. return [true, ''];
  360. }
  361. private function hkdTPlus($data, $user){
  362. try{
  363. $table = $this->table;
  364. $limit = 500;
  365. $lastId = 0;
  366. do {
  367. $rows = $this->databaseService->table('ARAP_ReceivePayment_b as rp_b')
  368. ->join('ARAP_ReceivePayment as rp', 'rp_b.idArapReceivePaymentDTO', '=', 'rp.ID')
  369. ->leftJoin('SA_SaleInvoice_b as si_b', 'rp_b.voucherDetailID', '=', 'si_b.ID')
  370. ->leftJoin('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  371. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  372. ->leftJoin('AA_Person as ps', 'rp.idperson', '=', 'ps.ID')
  373. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  374. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  375. ->where('rp.voucherdate','>=',$data['start_time'])
  376. ->where('rp.voucherdate','<=',$data['end_time'])
  377. ->where('rp.isReceiveFlag','=', 1)
  378. ->where('rp_b.ID', '>', $lastId) // 用真实字段
  379. ->orderBy('rp_b.ID')
  380. ->limit($limit)
  381. ->selectRaw("
  382. COALESCE(rp.ID, 0) as order_id,
  383. COALESCE(rp.code, '') as order_number,
  384. rp.voucherdate as order_time,
  385. COALESCE(pn.code, '') as customer_code,
  386. COALESCE(pn.name, '') as customer_title,
  387. COALESCE(it.code, '') as product_code,
  388. COALESCE(it.name, '') as product_title,
  389. COALESCE(rp.idperson, 0) as employee_id_1,
  390. COALESCE(ps.name, '') as employee_id_1_title,
  391. COALESCE(pn.priuserdefnvc15, '') as employee_id_2_title,
  392. COALESCE(it.specification, '') as product_size,
  393. COALESCE(ui.name, '') as unit,
  394. COALESCE(si_b.quantity, 0) as quantity,
  395. COALESCE(si_b.taxPrice, 0) as price_1,
  396. COALESCE(si_b.taxAmount, 0) as price_1_total,
  397. COALESCE(rp_b.amount, 0) as payment_amount,
  398. COALESCE(rp_b.ID, 0) as id_detail,
  399. COALESCE(rp_b.voucherDetailID, 0) as id_detail_upstream,
  400. COALESCE(rp_b.voucherCode, '') as order_number_upstream
  401. ")
  402. ->get();
  403. if ($rows->isEmpty()) break;
  404. $dataArray = Collect($rows)->map(function ($object) {
  405. return (array)$object;
  406. })->toArray();
  407. //存货档案
  408. $product = Product::where('del_time',0)
  409. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  410. ->select('code','business_cost')
  411. ->get()->toArray();
  412. $product_map = array_column($product,null,'code');
  413. //组织数据
  414. foreach ($dataArray as $key => $value){
  415. $p_tmp = $product_map[$value['product_code']] ?? [];
  416. $dataArray[$key]['order_type'] = RevenueCost::ORDER_THREE;
  417. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  418. $business_cost = $p_tmp['business_cost'] ?? 0;
  419. $dataArray[$key]['price_4'] = $business_cost;
  420. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  421. $dataArray[$key]['price_4_total'] = $price_4_total;
  422. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  423. $dataArray[$key]['profit'] = $profit;
  424. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  425. }
  426. DB::table($table)->insert($dataArray);
  427. // 更新 lastId 继续下一批
  428. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  429. } while (count($rows) === $limit);
  430. }catch (\Throwable $exception){
  431. return [false, "回款单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  432. }
  433. return [true, ''];
  434. }
  435. private function updateRevenueCost($data){
  436. try {
  437. $start_timeStamp = $data['start_timeStamp'];
  438. $end_timeStamp = $data['end_timeStamp'];
  439. $tmpTable = $this->table;
  440. $time = time();
  441. DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time) {
  442. // 1. 先软删除旧数据(你已有)
  443. RevenueCost::where('del_time', 0)
  444. ->where('order_time', '>=', $start_timeStamp)
  445. ->where('order_time', '<=', $end_timeStamp)
  446. ->update(['del_time' => $time]);
  447. // 2. 分批从临时表插入新数据
  448. $batchSize = 500;
  449. $lastId = 0;
  450. do {
  451. $chunk = DB::table($tmpTable)
  452. ->where('id', '>', $lastId)
  453. ->orderBy('id')
  454. ->limit($batchSize)
  455. ->get();
  456. if ($chunk->isEmpty()) {
  457. break;
  458. }
  459. $data = $chunk->map(function ($item) use($time){
  460. return [
  461. 'order_id' => $item->order_id,
  462. 'order_number' => $item->order_number,
  463. 'order_time' => $item->order_time,
  464. 'employee_id_1_title' => $item->employee_id_1_title,
  465. 'employee_id_1' => $item->employee_id_1,
  466. 'employee_id_2_title' => $item->employee_id_2_title ?? "",
  467. 'customer_code' => $item->customer_code,
  468. 'customer_title' => $item->customer_title,
  469. 'channel_finance' => $item->channel_finance,
  470. 'channel_details' => $item->channel_details,
  471. 'product_code' => $item->product_code,
  472. 'product_title' => $item->product_title,
  473. 'product_size' => $item->product_size,
  474. 'unit' => $item->unit,
  475. 'quantity' => $item->quantity,
  476. 'price_1' => $item->price_1,
  477. 'price_1_total' => $item->price_1_total,
  478. 'price_2' => $item->price_2,
  479. 'price_2_total' => $item->price_2_total,
  480. 'price_3' => $item->price_3,
  481. 'price_3_total' => $item->price_3_total,
  482. 'price_4' => $item->price_4,
  483. 'price_4_total' => $item->price_4_total,
  484. 'profit' => $item->profit,
  485. 'profit_rate' => $item->profit_rate,
  486. 'id_detail' => $item->id_detail,
  487. 'order_type' => $item->order_type,
  488. 'payment_amount' => $item->payment_amount ?? 0,
  489. 'id_detail_upstream' => $item->id_detail_upstream?? 0,
  490. 'order_number_upstream' => $item->order_number_upstream ?? "",
  491. 'crt_time' => $time,
  492. ];
  493. })->toArray();
  494. // 每批单独插入(可选:加小事务)
  495. RevenueCost::insert($data);
  496. // 更新 lastId
  497. $lastId = $chunk->last()->id;
  498. } while ($chunk->count() == $batchSize);
  499. });
  500. }catch (\Throwable $exception){
  501. return [false, "主表同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  502. }
  503. return [true, ''];
  504. }
  505. private function createTmpTable(){
  506. $table = $this->table;
  507. if (! Schema::hasTable($table)) {
  508. // 可以通过 migration 创建,或程序启动时检查
  509. Schema::create($table, function (Blueprint $table) {
  510. $table->bigIncrements('id'); // BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
  511. $table->integer('order_type')->default(0);
  512. $table->bigInteger('order_id')->default(0);
  513. $table->string('order_number', 50)->default('');
  514. $table->integer('order_time')->nullable();
  515. $table->string('employee_id_1_title', 100)->default('');
  516. $table->bigInteger('employee_id_1')->default(0);
  517. $table->string('employee_id_2_title', 100)->default('');
  518. $table->string('customer_code', 50)->default('');
  519. $table->string('customer_title', 100)->default('');
  520. $table->string('channel_finance', 50)->nullable();
  521. $table->string('channel_details', 50)->nullable();
  522. $table->string('product_code', 50)->default('');
  523. $table->string('product_title', 100)->default('');
  524. $table->string('product_size', 100)->nullable();
  525. $table->string('unit', 20)->nullable();
  526. $table->decimal('quantity', 12, 2)->default(0);
  527. $table->decimal('price_1', 12, 2)->default(0); // 销项成本
  528. $table->decimal('price_1_total', 12, 2)->default(0);
  529. $table->decimal('price_2', 12, 2)->default(0); // 运费
  530. $table->decimal('price_2_total', 12, 2)->default(0);
  531. $table->decimal('price_3', 12, 2)->default(0); // 含税单价
  532. $table->decimal('price_3_total', 12, 2)->default(0); // 含税金额
  533. $table->decimal('price_4', 12, 2)->default(0); // 业务成本
  534. $table->decimal('price_4_total', 12, 2)->default(0);
  535. $table->decimal('profit', 12, 2)->default(0);
  536. $table->decimal('payment_amount', 12, 2)->default(0);
  537. $table->decimal('profit_rate', 10, 3)->default(0);
  538. $table->bigInteger('id_detail')->default(0);
  539. $table->bigInteger('id_detail_upstream')->default(0);
  540. $table->string('order_number_upstream', 100)->nullable();
  541. });
  542. }
  543. }
  544. public function clearTmpTable(){
  545. if (Schema::hasTable($this->table)) DB::table($this->table)->truncate();
  546. }
  547. public function delTableKey(){
  548. $this->dellimitingSendRequest($this->table);
  549. }
  550. }