TPlusServerService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. function isOverThreeMonths($timestamp1, $timestamp2, $default_month = 3) {
  183. $date1 = new \DateTime();
  184. $date1->setTimestamp($timestamp1);
  185. $date2 = new \DateTime();
  186. $date2->setTimestamp($timestamp2);
  187. // 计算两个日期的间隔
  188. $interval = $date1->diff($date2);
  189. // 总月数 = 年差 * 12 + 月差
  190. $months = $interval->y * 12 + $interval->m;
  191. if ($months > $default_month) {
  192. return [false, "时间区间要在" . $default_month . "个月内"];
  193. }elseif ($months == 3 && ($interval->d > 0 || $interval->h > 0 || $interval->i > 0 || $interval->s > 0)) {
  194. return [false, "时间区间要在" . $default_month . "个月内"];
  195. }
  196. return [true, ''];
  197. }
  198. public function synRevenueCostFromTPlus($data, $user){
  199. //创建临时表 如果不存在
  200. $this->createTmpTable();
  201. //清理临时表 如果内容不为空
  202. $this->clearTmpTable();
  203. $type = $data['type'];
  204. //写入临时数据
  205. if($type == 1){
  206. list($status, $msg) = $this->xhdTPlus($data, $user);
  207. if(! $status) return [false, $msg];
  208. list($status, $msg) = $this->xsfpTPlus($data, $user);
  209. if(! $status) return [false, $msg];
  210. list($status, $msg) = $this->hkdTPlus($data, $user);
  211. if(! $status) return [false, $msg];
  212. }elseif ($type == 2){
  213. list($status, $msg) = $this->xhdTPlus($data, $user);
  214. if(! $status) return [false, $msg];
  215. }elseif ($type == 3){
  216. list($status, $msg) = $this->xsfpTPlus($data, $user);
  217. if(! $status) return [false, $msg];
  218. }elseif ($type == 4){
  219. list($status, $msg) = $this->hkdTPlus($data, $user);
  220. if(! $status) return [false, $msg];
  221. }
  222. //更新主表数据
  223. list($status,$msg) = $this->updateRevenueCost($data);
  224. if(! $status) return [false, $msg];
  225. //都成功后 清理临时表
  226. $this->clearTmpTable();
  227. //释放redis
  228. $this->delTableKey();
  229. return [true, '同步成功'];
  230. }
  231. private function xhdTPlus($data, $user){
  232. try {
  233. $table = $this->table;
  234. $limit = 500;
  235. $lastId = 0;
  236. do {
  237. $rows = $this->databaseService->table('SA_SaleDelivery_b as sd_b')
  238. ->join('SA_SaleDelivery as sd', 'sd_b.idSaleDeliveryDTO', '=', 'sd.ID')
  239. ->leftJoin('AA_Partner as pn', 'sd.idsettlecustomer', '=', 'pn.ID')
  240. ->leftJoin('AA_Person as ps', 'sd.idclerk', '=', 'ps.ID')
  241. ->leftJoin('AA_Inventory as it', 'sd_b.idinventory', '=', 'it.ID')
  242. ->leftJoin('AA_Unit as ui', 'sd_b.idbaseunit', '=', 'ui.ID')
  243. ->where('sd.voucherdate', '>=', $data['start_time'])
  244. ->where('sd.voucherdate', '<=', $data['end_time'])
  245. ->where('sd_b.ID', '>', $lastId) // 用真实字段
  246. ->orderBy('sd_b.ID')
  247. ->limit($limit)
  248. ->selectRaw("
  249. COALESCE(sd.ID, 0) as order_id,
  250. COALESCE(sd.code, '') as order_number,
  251. sd.voucherdate as order_time,
  252. COALESCE(ps.name, '') as employee_id_1_title,
  253. COALESCE(sd.idclerk, 0) as employee_id_1,
  254. COALESCE(pn.priuserdefnvc15, '') as employee_id_2_title,
  255. COALESCE(pn.code, '') as customer_code,
  256. COALESCE(pn.name, '') as customer_title,
  257. COALESCE(sd.pubuserdefnvc11, '') as channel_finance,
  258. COALESCE(sd.pubuserdefnvc12, '') as channel_details,
  259. COALESCE(it.code, '') as product_code,
  260. COALESCE(it.name, '') as product_title,
  261. COALESCE(it.specification, '') as product_size,
  262. COALESCE(ui.name, '') as unit,
  263. COALESCE(sd_b.quantity, 0) as quantity,
  264. COALESCE(sd_b.taxPrice, 0) as price_3,
  265. COALESCE(sd_b.taxAmount, 0) as price_3_total,
  266. COALESCE(sd_b.ID, 0) as id_detail
  267. ")
  268. ->get();
  269. if ($rows->isEmpty()) break;
  270. $dataArray = Collect($rows)->map(function ($object) {
  271. return (array)$object;
  272. })->toArray();
  273. //存货档案
  274. $product = Product::where('del_time', 0)
  275. ->whereIn('code', array_unique(array_column($dataArray, 'product_code')))
  276. ->select('code', 'write_off_price', 'freight_price', 'business_cost')
  277. ->get()->toArray();
  278. $product_map = array_column($product, null, 'code');
  279. //组织数据
  280. foreach ($dataArray as $key => $value) {
  281. $p_tmp = $product_map[$value['product_code']] ?? [];
  282. $dataArray[$key]['order_type'] = RevenueCost::ORDER_ONE;
  283. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  284. $write_off_price = $p_tmp['write_off_price'] ?? 0;
  285. $dataArray[$key]['price_1'] = $write_off_price;
  286. $dataArray[$key]['price_1_total'] = bcmul($write_off_price, $value['quantity'], 2);
  287. $freight_price = $p_tmp['freight_price'] ?? 0;
  288. $dataArray[$key]['price_2'] = $freight_price;
  289. $dataArray[$key]['price_2_total'] = bcmul($freight_price, $value['quantity'], 2);
  290. $business_cost = $p_tmp['business_cost'] ?? 0;
  291. $dataArray[$key]['price_4'] = $business_cost;
  292. $price_4_total = bcmul($business_cost, $value['quantity'], 2);
  293. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'], 2);
  294. $profit = bcsub($value['price_3_total'], $price_4_total, 2);
  295. $dataArray[$key]['profit'] = $profit;
  296. $dataArray[$key]['profit_rate'] = $value['price_3_total'] > 0 ? bcdiv($profit, $value['price_3_total'], 2) : 0;
  297. }
  298. DB::table($table)->insert($dataArray);
  299. // 更新 lastId 继续下一批
  300. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  301. } while (count($rows) === $limit);
  302. }catch (\Throwable $exception){
  303. return [false, "销货单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  304. }
  305. return [true, ''];
  306. }
  307. private function xsfpTPlus($data, $user){
  308. try {
  309. $table = $this->table;
  310. $limit = 500;
  311. $lastId = 0;
  312. do {
  313. $rows = $this->databaseService->table('SA_SaleInvoice_b as si_b')
  314. ->join('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  315. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  316. ->leftJoin('AA_Person as ps', 'si.idclerk', '=', 'ps.ID')
  317. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  318. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  319. ->where('si.voucherdate','>=',$data['start_time'])
  320. ->where('si.voucherdate','<=',$data['end_time'])
  321. ->where('si_b.ID', '>', $lastId) // 用真实字段
  322. ->orderBy('si_b.ID')
  323. ->limit($limit)
  324. ->selectRaw("
  325. COALESCE(si.ID, 0) as order_id,
  326. COALESCE(si.code, '') as order_number,
  327. si.voucherdate as order_time,
  328. COALESCE(pn.code, '') as customer_code,
  329. COALESCE(pn.name, '') as customer_title,
  330. COALESCE(si.idclerk, 0) as employee_id_1,
  331. COALESCE(ps.name, '') as employee_id_1_title,
  332. COALESCE(pn.priuserdefnvc15, '') as employee_id_2_title,
  333. COALESCE(it.code, '') as product_code,
  334. COALESCE(it.name, '') as product_title,
  335. COALESCE(it.specification, '') as product_size,
  336. COALESCE(ui.name, '') as unit,
  337. COALESCE(si_b.quantity, 0) as quantity,
  338. COALESCE(si_b.taxPrice, 0) as price_1,
  339. COALESCE(si_b.taxAmount, 0) as price_1_total,
  340. COALESCE(si_b.ID, 0) as id_detail,
  341. COALESCE(si_b.sourceVoucherDetailId, 0) as id_detail_upstream,
  342. COALESCE(si_b.sourceVoucherCode, '') as order_number_upstream
  343. ")
  344. ->get();
  345. if ($rows->isEmpty()) break;
  346. $dataArray = Collect($rows)->map(function ($object) {
  347. return (array)$object;
  348. })->toArray();
  349. //存货档案
  350. $product = Product::where('del_time',0)
  351. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  352. ->select('code','business_cost')
  353. ->get()->toArray();
  354. $product_map = array_column($product,null,'code');
  355. //组织数据
  356. foreach ($dataArray as $key => $value){
  357. $p_tmp = $product_map[$value['product_code']] ?? [];
  358. $dataArray[$key]['order_type'] = RevenueCost::ORDER_TWO;
  359. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  360. $business_cost = $p_tmp['business_cost'] ?? 0;
  361. $dataArray[$key]['price_4'] = $business_cost;
  362. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  363. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'],2);
  364. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  365. $dataArray[$key]['profit'] = $profit;
  366. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  367. }
  368. DB::table($table)->insert($dataArray);
  369. // 更新 lastId 继续下一批
  370. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  371. } while (count($rows) === $limit);
  372. }catch (\Throwable $exception){
  373. return [false, "销售发票同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  374. }
  375. return [true, ''];
  376. }
  377. private function hkdTPlus($data, $user){
  378. try{
  379. $table = $this->table;
  380. $limit = 500;
  381. $lastId = 0;
  382. do {
  383. $rows = $this->databaseService->table('ARAP_ReceivePayment_b as rp_b')
  384. ->join('ARAP_ReceivePayment as rp', 'rp_b.idArapReceivePaymentDTO', '=', 'rp.ID')
  385. ->leftJoin('SA_SaleInvoice_b as si_b', 'rp_b.voucherDetailID', '=', 'si_b.ID')
  386. ->leftJoin('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  387. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  388. ->leftJoin('AA_Person as ps', 'rp.idperson', '=', 'ps.ID')
  389. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  390. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  391. ->where('rp.voucherdate','>=',$data['start_time'])
  392. ->where('rp.voucherdate','<=',$data['end_time'])
  393. ->where('rp.isReceiveFlag','=', 1)
  394. ->where('rp_b.ID', '>', $lastId) // 用真实字段
  395. ->orderBy('rp_b.ID')
  396. ->limit($limit)
  397. ->selectRaw("
  398. COALESCE(rp.ID, 0) as order_id,
  399. COALESCE(rp.code, '') as order_number,
  400. rp.voucherdate as order_time,
  401. COALESCE(pn.code, '') as customer_code,
  402. COALESCE(pn.name, '') as customer_title,
  403. COALESCE(it.code, '') as product_code,
  404. COALESCE(it.name, '') as product_title,
  405. COALESCE(rp.idperson, 0) as employee_id_1,
  406. COALESCE(ps.name, '') as employee_id_1_title,
  407. COALESCE(pn.priuserdefnvc15, '') as employee_id_2_title,
  408. COALESCE(it.specification, '') as product_size,
  409. COALESCE(ui.name, '') as unit,
  410. COALESCE(si_b.quantity, 0) as quantity,
  411. COALESCE(si_b.taxPrice, 0) as price_1,
  412. COALESCE(si_b.taxAmount, 0) as price_1_total,
  413. COALESCE(rp_b.amount, 0) as payment_amount,
  414. COALESCE(rp_b.ID, 0) as id_detail,
  415. COALESCE(rp_b.voucherDetailID, 0) as id_detail_upstream,
  416. COALESCE(rp_b.voucherCode, '') as order_number_upstream
  417. ")
  418. ->get();
  419. if ($rows->isEmpty()) break;
  420. $dataArray = Collect($rows)->map(function ($object) {
  421. return (array)$object;
  422. })->toArray();
  423. //存货档案
  424. $product = Product::where('del_time',0)
  425. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  426. ->select('code','business_cost')
  427. ->get()->toArray();
  428. $product_map = array_column($product,null,'code');
  429. //组织数据
  430. foreach ($dataArray as $key => $value){
  431. $p_tmp = $product_map[$value['product_code']] ?? [];
  432. $dataArray[$key]['order_type'] = RevenueCost::ORDER_THREE;
  433. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  434. $business_cost = $p_tmp['business_cost'] ?? 0;
  435. $dataArray[$key]['price_4'] = $business_cost;
  436. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  437. $dataArray[$key]['price_4_total'] = $price_4_total;
  438. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  439. $dataArray[$key]['profit'] = $profit;
  440. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  441. }
  442. DB::table($table)->insert($dataArray);
  443. // 更新 lastId 继续下一批
  444. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  445. } while (count($rows) === $limit);
  446. }catch (\Throwable $exception){
  447. return [false, "回款单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  448. }
  449. return [true, ''];
  450. }
  451. private function updateRevenueCost($data){
  452. try {
  453. $start_timeStamp = $data['start_timeStamp'];
  454. $end_timeStamp = $data['end_timeStamp'];
  455. $tmpTable = $this->table;
  456. $time = time();
  457. DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time) {
  458. // 1. 先软删除旧数据(你已有)
  459. RevenueCost::where('del_time', 0)
  460. ->where('order_time', '>=', $start_timeStamp)
  461. ->where('order_time', '<=', $end_timeStamp)
  462. ->update(['del_time' => $time]);
  463. // 2. 分批从临时表插入新数据
  464. $batchSize = 500;
  465. $lastId = 0;
  466. do {
  467. $chunk = DB::table($tmpTable)
  468. ->where('id', '>', $lastId)
  469. ->orderBy('id')
  470. ->limit($batchSize)
  471. ->get();
  472. if ($chunk->isEmpty()) {
  473. break;
  474. }
  475. $data = $chunk->map(function ($item) use($time){
  476. return [
  477. 'order_id' => $item->order_id,
  478. 'order_number' => $item->order_number,
  479. 'order_time' => $item->order_time,
  480. 'employee_id_1_title' => $item->employee_id_1_title,
  481. 'employee_id_1' => $item->employee_id_1,
  482. 'employee_id_2_title' => $item->employee_id_2_title ?? "",
  483. 'customer_code' => $item->customer_code,
  484. 'customer_title' => $item->customer_title,
  485. 'channel_finance' => $item->channel_finance,
  486. 'channel_details' => $item->channel_details,
  487. 'product_code' => $item->product_code,
  488. 'product_title' => $item->product_title,
  489. 'product_size' => $item->product_size,
  490. 'unit' => $item->unit,
  491. 'quantity' => $item->quantity,
  492. 'price_1' => $item->price_1,
  493. 'price_1_total' => $item->price_1_total,
  494. 'price_2' => $item->price_2,
  495. 'price_2_total' => $item->price_2_total,
  496. 'price_3' => $item->price_3,
  497. 'price_3_total' => $item->price_3_total,
  498. 'price_4' => $item->price_4,
  499. 'price_4_total' => $item->price_4_total,
  500. 'profit' => $item->profit,
  501. 'profit_rate' => $item->profit_rate,
  502. 'id_detail' => $item->id_detail,
  503. 'order_type' => $item->order_type,
  504. 'payment_amount' => $item->payment_amount ?? 0,
  505. 'id_detail_upstream' => $item->id_detail_upstream?? 0,
  506. 'order_number_upstream' => $item->order_number_upstream ?? "",
  507. 'crt_time' => $time,
  508. ];
  509. })->toArray();
  510. // 每批单独插入(可选:加小事务)
  511. RevenueCost::insert($data);
  512. // 更新 lastId
  513. $lastId = $chunk->last()->id;
  514. } while ($chunk->count() == $batchSize);
  515. });
  516. }catch (\Throwable $exception){
  517. return [false, "主表同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  518. }
  519. return [true, ''];
  520. }
  521. private function createTmpTable(){
  522. $table = $this->table;
  523. if (! Schema::hasTable($table)) {
  524. // 可以通过 migration 创建,或程序启动时检查
  525. Schema::create($table, function (Blueprint $table) {
  526. $table->bigIncrements('id'); // BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
  527. $table->integer('order_type')->default(0);
  528. $table->bigInteger('order_id')->default(0);
  529. $table->string('order_number', 50)->default('');
  530. $table->integer('order_time')->nullable();
  531. $table->string('employee_id_1_title', 100)->default('');
  532. $table->bigInteger('employee_id_1')->default(0);
  533. $table->string('employee_id_2_title', 100)->default('');
  534. $table->string('customer_code', 50)->default('');
  535. $table->string('customer_title', 100)->default('');
  536. $table->string('channel_finance', 50)->nullable();
  537. $table->string('channel_details', 50)->nullable();
  538. $table->string('product_code', 50)->default('');
  539. $table->string('product_title', 100)->default('');
  540. $table->string('product_size', 100)->nullable();
  541. $table->string('unit', 20)->nullable();
  542. $table->decimal('quantity', 12, 2)->default(0);
  543. $table->decimal('price_1', 12, 2)->default(0); // 销项成本
  544. $table->decimal('price_1_total', 12, 2)->default(0);
  545. $table->decimal('price_2', 12, 2)->default(0); // 运费
  546. $table->decimal('price_2_total', 12, 2)->default(0);
  547. $table->decimal('price_3', 12, 2)->default(0); // 含税单价
  548. $table->decimal('price_3_total', 12, 2)->default(0); // 含税金额
  549. $table->decimal('price_4', 12, 2)->default(0); // 业务成本
  550. $table->decimal('price_4_total', 12, 2)->default(0);
  551. $table->decimal('profit', 12, 2)->default(0);
  552. $table->decimal('payment_amount', 12, 2)->default(0);
  553. $table->decimal('profit_rate', 10, 3)->default(0);
  554. $table->bigInteger('id_detail')->default(0);
  555. $table->bigInteger('id_detail_upstream')->default(0);
  556. $table->string('order_number_upstream', 100)->nullable();
  557. });
  558. }
  559. }
  560. public function clearTmpTable(){
  561. if (Schema::hasTable($this->table)) DB::table($this->table)->truncate();
  562. }
  563. public function delTableKey(){
  564. $this->dellimitingSendRequest($this->table);
  565. }
  566. }