TPlusServerService.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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\EmployeeIndex;
  8. use App\Model\Product;
  9. use App\Model\RevenueCost;
  10. use App\Model\RevenueCostTotal;
  11. use App\Model\SalaryEmployee;
  12. use Illuminate\Database\Schema\Blueprint;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Schema;
  15. class TPlusServerService extends Service
  16. {
  17. /**
  18. * @var TPlusDatabaseServerService
  19. */
  20. protected $databaseService;
  21. /**
  22. * @var string|null
  23. */
  24. protected $error;
  25. /**
  26. * TPlusServerService constructor.
  27. */
  28. public function __construct()
  29. {
  30. $service = new TPlusDatabaseServerService();
  31. $this->databaseService = $service->db;
  32. $this->error = $service->error;
  33. }
  34. /**
  35. * 获取错误信息
  36. *
  37. * @return string|null
  38. */
  39. public function getError()
  40. {
  41. return $this->error;
  42. }
  43. private $table = "tmp_revenue_cost_data";
  44. private $table_2 = "tmp_salary_employee";
  45. /**
  46. * 同步人员部门
  47. *
  48. * @param array $data
  49. * @param array $user
  50. * @return array
  51. */
  52. public function synPersonDepart($data, $user)
  53. {
  54. try {
  55. $this->databaseService->table('AA_Department')
  56. ->select('id','idparent as parent_id','name as title','code','disabled as is_use')
  57. ->chunkById(100, function ($data) {
  58. DB::transaction(function () use ($data) {
  59. $dataArray = Collect($data)->map(function ($object) {
  60. return (array)$object;
  61. })->toArray();
  62. $d_id = Depart::whereIn('id', array_column($dataArray,'id'))
  63. ->pluck('id')
  64. ->toArray();
  65. $insert = $update = [];
  66. foreach ($dataArray as $value){
  67. $is_use = $value['is_use'] ? 0 : 1;
  68. if(in_array($value['id'], $d_id)){
  69. $update[] = [
  70. 'id' => $value['id'],
  71. 'parent_id' => $value['parent_id'],
  72. 'title' => $value['title'],
  73. 'code' => $value['code'],
  74. 'is_use' => $is_use
  75. ];
  76. }else{
  77. $insert[] = [
  78. 'id' => $value['id'],
  79. 'parent_id' => $value['parent_id'],
  80. 'title' => $value['title'],
  81. 'code' => $value['code'],
  82. 'is_use' => $is_use
  83. ];
  84. }
  85. }
  86. if(! empty($insert)) Depart::insert($insert);
  87. if(! empty($update)) {
  88. foreach ($update as $value){
  89. Depart::where('id', $value['id'])
  90. ->update($value);
  91. }
  92. }
  93. });
  94. });
  95. $this->databaseService->table('AA_Person')
  96. ->select('id','code as number','name as emp_name','mobilePhoneNo as mobile','iddepartment as depart_id','disabled as state')
  97. ->chunkById(100, function ($data) {
  98. DB::transaction(function () use ($data) {
  99. $dataArray = Collect($data)->map(function ($object) {
  100. return (array)$object;
  101. })->toArray();
  102. $employee_id = Employee::whereIn('id', array_column($dataArray,'id'))
  103. ->pluck('id')
  104. ->toArray();
  105. $insert = $update = $depart_update = [];
  106. foreach ($dataArray as $value){
  107. $state = $value['state'] ? Employee::NOT_USE : Employee::USE;
  108. if(in_array($value['id'], $employee_id)){
  109. $update[] = [
  110. 'id' => $value['id'],
  111. 'number' => $value['number'],
  112. 'emp_name' => $value['emp_name'],
  113. 'mobile' => $value['mobile'],
  114. 'state' => $state
  115. ];
  116. }else{
  117. $insert[] = [
  118. 'id' => $value['id'],
  119. 'number' => $value['number'],
  120. 'emp_name' => $value['emp_name'],
  121. 'mobile' => $value['mobile'],
  122. 'state' => $state
  123. ];
  124. }
  125. $depart_update[] = [
  126. 'employee_id' => $value['id'],
  127. 'depart_id' => $value['depart_id']
  128. ];
  129. }
  130. if(! empty($insert)) Employee::insert($insert);
  131. if(! empty($update)) {
  132. foreach ($update as $value){
  133. Employee::where('id', $value['id'])
  134. ->update($value);
  135. }
  136. }
  137. if(! empty($depart_update)){
  138. EmployeeDepartPermission::whereIn('employee_id',array_column($depart_update,'employee_id'))->delete();
  139. EmployeeDepartPermission::insert($depart_update);
  140. }
  141. });
  142. });
  143. } catch (\Throwable $e) {
  144. return [false, $e->getMessage()];
  145. }
  146. return [true, ''];
  147. }
  148. /**
  149. * 收入成本统计同步
  150. *
  151. * @param array $data
  152. * @param array $user
  153. * @return array
  154. */
  155. public function synRevenueCost($data, $user){
  156. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '同步时间不能为空'];
  157. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['crt_time'],false);
  158. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "同步时间:时间区间无效"];
  159. list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
  160. if(! $bool) return [false, $bool_msg];
  161. $data['start_timeStamp'] = $start_time;
  162. $data['end_timeStamp'] = $end_time;
  163. $start = date('Y-m-d H:i:s.000', $start_time);
  164. $end = date('Y-m-d H:i:s.000', $end_time);
  165. $data['start_time'] = $start;
  166. $data['end_time'] = $end;
  167. $data['operation_time'] = time();
  168. if(empty($data['type'])) return [false, '同步类型不能为空'];
  169. if(in_array($data['type'],[1,2,3,4])){
  170. list($status,$msg) = $this->limitingSendRequest($this->table);
  171. if(! $status) return [false, '收入成本相关信息同步正在后台运行,请稍后'];
  172. // //同步
  173. // list($status, $msg) = $this->synRevenueCostFromTPlus($data, $user);
  174. // if(! $status) {
  175. // $this->clearTmpTable();
  176. // $this->dellimitingSendRequest($this->table);
  177. // return [false, $msg];
  178. // }
  179. //队列
  180. ProcessDataJob::dispatch($data, $user)->onQueue(RevenueCost::job);
  181. }else{
  182. return [false, '同步类型错误'];
  183. }
  184. return [true, '收入成本相关信息同步已进入后台任务'];
  185. }
  186. public function synRevenueCostFromTPlus($data, $user){
  187. //创建临时表 如果不存在
  188. $this->createTmpTable();
  189. //清理临时表 如果内容不为空
  190. $this->clearTmpTable();
  191. $type = $data['type'];
  192. //写入临时数据
  193. if($type == 1){
  194. list($status, $msg) = $this->xhdTPlus($data, $user);
  195. if(! $status) return [false, $msg];
  196. list($status, $msg) = $this->xsfpTPlus($data, $user);
  197. if(! $status) return [false, $msg];
  198. list($status, $msg) = $this->hkdTPlus($data, $user);
  199. if(! $status) return [false, $msg];
  200. }elseif ($type == 2){
  201. list($status, $msg) = $this->xhdTPlus($data, $user);
  202. if(! $status) return [false, $msg];
  203. }elseif ($type == 3){
  204. list($status, $msg) = $this->xsfpTPlus($data, $user);
  205. if(! $status) return [false, $msg];
  206. }elseif ($type == 4){
  207. list($status, $msg) = $this->hkdTPlus($data, $user);
  208. if(! $status) return [false, $msg];
  209. }
  210. //更新数据
  211. list($status,$msg) = $this->updateRevenueCost($data);
  212. if(! $status) return [false, $msg];
  213. // //更新主表数据
  214. // list($status,$msg) = $this->updateRevenueCostTotal($data);
  215. // if(! $status) return [false, $msg];
  216. //都成功后 清理临时表
  217. $this->clearTmpTable();
  218. //释放redis
  219. $this->delTableKey();
  220. return [true, '同步成功'];
  221. }
  222. private function xhdTPlus($data, $user){
  223. try {
  224. $table = $this->table;
  225. $limit = 500;
  226. $lastId = 0;
  227. do {
  228. $rows = $this->databaseService->table('SA_SaleDelivery_b as sd_b')
  229. ->join('SA_SaleDelivery as sd', 'sd_b.idSaleDeliveryDTO', '=', 'sd.ID')
  230. ->leftJoin('AA_Partner as pn', 'sd.idsettlecustomer', '=', 'pn.ID')
  231. ->leftJoin('AA_Person as ps', 'sd.idclerk', '=', 'ps.ID')
  232. ->leftJoin('AA_Person as ps2', 'pn.idsaleman', '=', 'ps2.ID')
  233. ->leftJoin('AA_Inventory as it', 'sd_b.idinventory', '=', 'it.ID')
  234. ->leftJoin('AA_Unit as ui', 'sd_b.idbaseunit', '=', 'ui.ID')
  235. ->where('sd.voucherdate', '>=', $data['start_time'])
  236. ->where('sd.voucherdate', '<=', $data['end_time'])
  237. ->where('sd_b.ID', '>', $lastId) // 用真实字段
  238. ->orderBy('sd_b.ID')
  239. ->limit($limit)
  240. ->selectRaw("
  241. COALESCE(sd.ID, 0) as order_id,
  242. COALESCE(sd.code, '') as order_number,
  243. sd.voucherdate as order_time,
  244. COALESCE(ps.name, '') as employee_id_1_title,
  245. COALESCE(sd.idclerk, 0) as employee_id_1,
  246. COALESCE(ps2.name, '') as employee_id_2_title,
  247. COALESCE(pn.idsaleman, 0) as employee_id_2,
  248. COALESCE(pn.code, '') as customer_code,
  249. COALESCE(pn.name, '') as customer_title,
  250. COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  251. COALESCE(sd.pubuserdefnvc11, '') as channel_finance,
  252. COALESCE(sd.pubuserdefnvc12, '') as channel_details,
  253. COALESCE(it.code, '') as product_code,
  254. COALESCE(it.name, '') as product_title,
  255. COALESCE(it.specification, '') as product_size,
  256. COALESCE(ui.name, '') as unit,
  257. COALESCE(sd_b.quantity, 0) as quantity,
  258. COALESCE(sd_b.taxPrice, 0) as price_3,
  259. COALESCE(sd_b.taxAmount, 0) as price_3_total,
  260. COALESCE(sd_b.ID, 0) as id_detail,
  261. COALESCE(sd_b.pubuserdefdecm9, 0) as is_activity
  262. ")
  263. ->get();
  264. if ($rows->isEmpty()) break;
  265. $dataArray = Collect($rows)->map(function ($object) {
  266. return (array)$object;
  267. })->toArray();
  268. //存货档案
  269. $product = Product::where('del_time', 0)
  270. ->whereIn('code', array_unique(array_column($dataArray, 'product_code')))
  271. ->select('code', 'write_off_price', 'freight_price', 'business_cost')
  272. ->get()->toArray();
  273. $product_map = array_column($product, null, 'code');
  274. //组织数据
  275. foreach ($dataArray as $key => $value) {
  276. $customer_profit_rate = rtrim($value['customer_profit_rate'],'%');
  277. if(is_numeric($customer_profit_rate)){
  278. $customer_profit_rate = bcdiv($customer_profit_rate,100,3);
  279. }else{
  280. $customer_profit_rate = 0;
  281. }
  282. $dataArray[$key]['customer_profit_rate'] = $customer_profit_rate;
  283. $p_tmp = $product_map[$value['product_code']] ?? [];
  284. $dataArray[$key]['order_type'] = RevenueCost::ORDER_ONE;
  285. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  286. $write_off_price = $p_tmp['write_off_price'] ?? 0;
  287. $dataArray[$key]['price_1'] = $write_off_price;
  288. $dataArray[$key]['price_1_total'] = bcmul($write_off_price, $value['quantity'], 2);
  289. $freight_price = $p_tmp['freight_price'] ?? 0;
  290. $dataArray[$key]['price_2'] = $freight_price;
  291. $dataArray[$key]['price_2_total'] = bcmul($freight_price, $value['quantity'], 2);
  292. $business_cost = $p_tmp['business_cost'] ?? 0;
  293. $dataArray[$key]['price_4'] = $business_cost;
  294. $price_4_total = bcmul($business_cost, $value['quantity'], 2);
  295. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'], 2);
  296. $profit = bcsub($value['price_3_total'], $price_4_total, 2);
  297. $dataArray[$key]['profit'] = $profit;
  298. $dataArray[$key]['profit_rate'] = $value['price_3_total'] > 0 ? bcdiv($profit, $value['price_3_total'], 2) : 0;
  299. }
  300. DB::table($table)->insert($dataArray);
  301. // 更新 lastId 继续下一批
  302. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  303. } while (count($rows) === $limit);
  304. }catch (\Throwable $exception){
  305. return [false, "销货单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  306. }
  307. return [true, ''];
  308. }
  309. private function xsfpTPlus($data, $user){
  310. try {
  311. $table = $this->table;
  312. $limit = 500;
  313. $lastId = 0;
  314. do {
  315. $rows = $this->databaseService->table('SA_SaleInvoice_b as si_b')
  316. ->join('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  317. ->leftJoin('SA_SaleDelivery_b as sd_b', 'si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
  318. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  319. ->leftJoin('AA_Person as ps', 'si.idclerk', '=', 'ps.ID')
  320. ->leftJoin('AA_Person as ps2', 'pn.idsaleman', '=', 'ps2.ID')
  321. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  322. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  323. ->where('si.voucherdate','>=',$data['start_time'])
  324. ->where('si.voucherdate','<=',$data['end_time'])
  325. ->where('si_b.ID', '>', $lastId) // 用真实字段
  326. ->orderBy('si_b.ID')
  327. ->limit($limit)
  328. ->selectRaw("
  329. COALESCE(si.ID, 0) as order_id,
  330. COALESCE(si.code, '') as order_number,
  331. si.voucherdate as order_time,
  332. COALESCE(pn.code, '') as customer_code,
  333. COALESCE(pn.name, '') as customer_title,
  334. COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  335. COALESCE(si.idclerk, 0) as employee_id_1,
  336. COALESCE(ps.name, '') as employee_id_1_title,
  337. COALESCE(pn.idsaleman, 0) as employee_id_2,
  338. COALESCE(ps2.name, '') as employee_id_2_title,
  339. COALESCE(it.code, '') as product_code,
  340. COALESCE(it.name, '') as product_title,
  341. COALESCE(it.specification, '') as product_size,
  342. COALESCE(ui.name, '') as unit,
  343. COALESCE(si_b.quantity, 0) as quantity,
  344. COALESCE(si_b.taxPrice, 0) as price_1,
  345. COALESCE(si_b.taxAmount, 0) as price_1_total,
  346. COALESCE(si_b.ID, 0) as id_detail,
  347. COALESCE(si_b.sourceVoucherDetailId, 0) as id_detail_upstream,
  348. COALESCE(si_b.sourceVoucherCode, '') as order_number_upstream,
  349. COALESCE(sd_b.pubuserdefdecm9, 0) as is_activity
  350. ")
  351. ->get();
  352. if ($rows->isEmpty()) break;
  353. $dataArray = Collect($rows)->map(function ($object) {
  354. return (array)$object;
  355. })->toArray();
  356. //存货档案
  357. $product = Product::where('del_time',0)
  358. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  359. ->select('code','business_cost')
  360. ->get()->toArray();
  361. $product_map = array_column($product,null,'code');
  362. //组织数据
  363. foreach ($dataArray as $key => $value){
  364. $customer_profit_rate = rtrim($value['customer_profit_rate'],'%');
  365. if(is_numeric($customer_profit_rate)){
  366. $customer_profit_rate = bcdiv($customer_profit_rate,100,3);
  367. }else{
  368. $customer_profit_rate = 0;
  369. }
  370. $dataArray[$key]['customer_profit_rate'] = $customer_profit_rate;
  371. $p_tmp = $product_map[$value['product_code']] ?? [];
  372. $dataArray[$key]['order_type'] = RevenueCost::ORDER_TWO;
  373. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  374. $business_cost = $p_tmp['business_cost'] ?? 0;
  375. $dataArray[$key]['price_4'] = $business_cost;
  376. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  377. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'],2);
  378. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  379. $dataArray[$key]['profit'] = $profit;
  380. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  381. }
  382. DB::table($table)->insert($dataArray);
  383. // 更新 lastId 继续下一批
  384. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  385. } while (count($rows) === $limit);
  386. }catch (\Throwable $exception){
  387. return [false, "销售发票同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  388. }
  389. return [true, ''];
  390. }
  391. private function hkdTPlus($data, $user){
  392. try{
  393. $table = $this->table;
  394. $limit = 500;
  395. $lastId = 0;
  396. do {
  397. $rows = $this->databaseService->table('ARAP_ReceivePayment_b as rp_b')
  398. ->join('ARAP_ReceivePayment as rp', 'rp_b.idArapReceivePaymentDTO', '=', 'rp.ID')
  399. // ->leftJoin('SA_SaleInvoice_b as si_b', 'rp_b.voucherDetailID', '=', 'si_b.ID')
  400. // ->leftJoin('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  401. // ->leftJoin('SA_SaleDelivery_b as sd_b', 'si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
  402. // 发票子表关联(仅限 idvouchertype = 20)
  403. ->leftJoin('SA_SaleInvoice_b as si_b', function ($join) {
  404. $join->on('rp_b.voucherDetailID', '=', 'si_b.ID')
  405. ->where('rp_b.idvouchertype', '=', 20);
  406. })
  407. ->leftJoin('SA_SaleInvoice as si', function ($join) {
  408. $join->on('si_b.idSaleInvoiceDTO', '=', 'si.ID')
  409. ->where('rp_b.idvouchertype', '=', 20);
  410. })
  411. ->leftJoin('SA_SaleDelivery_b as sd_b', function ($join) {
  412. $join->on('si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
  413. ->where('rp_b.idvouchertype', '=', 20);
  414. })
  415. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  416. ->leftJoin('AA_Person as ps', 'rp.idperson', '=', 'ps.ID')
  417. ->leftJoin('AA_Person as ps2', 'pn.idsaleman', '=', 'ps2.ID')
  418. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  419. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  420. ->where('rp.voucherdate','>=',$data['start_time'])
  421. ->where('rp.voucherdate','<=',$data['end_time'])
  422. // ->where('rp_b.idvouchertype','=', 20) // 销售发票
  423. ->where('rp.isReceiveFlag','=', 1)
  424. ->where('rp_b.ID', '>', $lastId)
  425. ->orderBy('rp_b.ID')
  426. ->limit($limit)
  427. // ->selectRaw("
  428. // COALESCE(rp.ID, 0) as order_id,
  429. // COALESCE(rp.code, '') as order_number,
  430. // rp.voucherdate as order_time,
  431. // COALESCE(pn.code, '') as customer_code,
  432. // COALESCE(pn.name, '') as customer_title,
  433. // COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  434. // COALESCE(it.code, '') as product_code,
  435. // COALESCE(it.name, '') as product_title,
  436. // COALESCE(rp.idperson, 0) as employee_id_1,
  437. // COALESCE(ps.name, '') as employee_id_1_title,
  438. // COALESCE(pn.idsaleman, 0) as employee_id_2,
  439. // COALESCE(ps2.name, '') as employee_id_2_title,
  440. // COALESCE(it.specification, '') as product_size,
  441. // COALESCE(ui.name, '') as unit,
  442. // COALESCE(si_b.quantity, 0) as quantity,
  443. // COALESCE(si_b.taxPrice, 0) as price_1,
  444. // COALESCE(si_b.taxAmount, 0) as price_1_total,
  445. // COALESCE(rp_b.amount, 0) as payment_amount,
  446. // COALESCE(rp_b.ID, 0) as id_detail,
  447. // COALESCE(rp_b.voucherDetailID, 0) as id_detail_upstream,
  448. // COALESCE(rp_b.voucherCode, '') as order_number_upstream,
  449. // COALESCE(sd_b.pubuserdefdecm9, 0) as is_activity
  450. // ")
  451. ->selectRaw("
  452. COALESCE(rp.ID, 0) as order_id,
  453. COALESCE(rp.code, '') as order_number,
  454. rp.voucherdate as order_time,
  455. COALESCE(pn.code, '') as customer_code,
  456. COALESCE(pn.name, '') as customer_title,
  457. COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  458. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.code, '') ELSE '' END as product_code,
  459. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.name, '') ELSE '' END as product_title,
  460. COALESCE(rp.idperson, 0) as employee_id_1,
  461. COALESCE(ps.name, '') as employee_id_1_title,
  462. COALESCE(pn.idsaleman, 0) as employee_id_2,
  463. COALESCE(ps2.name, '') as employee_id_2_title,
  464. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.specification, '') ELSE '' END as product_size,
  465. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(ui.name, '') ELSE '' END as unit,
  466. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.quantity, 0) ELSE 0 END as quantity,
  467. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.taxPrice, 0) ELSE 0 END as price_1,
  468. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.taxAmount, 0) ELSE 0 END as price_1_total,
  469. COALESCE(rp_b.amount, 0) as payment_amount,
  470. COALESCE(rp_b.ID, 0) as id_detail,
  471. COALESCE(rp_b.voucherDetailID, 0) as id_detail_upstream,
  472. COALESCE(rp_b.voucherCode, '') as order_number_upstream,
  473. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(sd_b.pubuserdefdecm9, 0) ELSE 0 END as is_activity,
  474. rp_b.idvouchertype as voucher_type
  475. ")
  476. ->get();
  477. if ($rows->isEmpty()) break;
  478. $dataArray = Collect($rows)->map(function ($object) {
  479. return (array)$object;
  480. })->toArray();
  481. //存货档案
  482. $product = Product::where('del_time',0)
  483. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  484. ->select('code','business_cost')
  485. ->get()->toArray();
  486. $product_map = array_column($product,null,'code');
  487. //组织数据
  488. foreach ($dataArray as $key => $value){
  489. $customer_profit_rate = rtrim($value['customer_profit_rate'],'%');
  490. if(is_numeric($customer_profit_rate)){
  491. $customer_profit_rate = bcdiv($customer_profit_rate,100,3);
  492. }else{
  493. $customer_profit_rate = 0;
  494. }
  495. $dataArray[$key]['customer_profit_rate'] = $customer_profit_rate;
  496. $p_tmp = $product_map[$value['product_code']] ?? [];
  497. $dataArray[$key]['order_type'] = RevenueCost::ORDER_THREE;
  498. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  499. $business_cost = $p_tmp['business_cost'] ?? 0;
  500. $dataArray[$key]['price_4'] = $business_cost;
  501. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  502. $dataArray[$key]['price_4_total'] = $price_4_total;
  503. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  504. $dataArray[$key]['profit'] = $profit;
  505. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  506. }
  507. DB::table($table)->insert($dataArray);
  508. // 更新 lastId 继续下一批
  509. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  510. } while (count($rows) === $limit);
  511. }catch (\Throwable $exception){
  512. return [false, "回款单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  513. }
  514. return [true, ''];
  515. }
  516. private function updateRevenueCost($data){
  517. try {
  518. $start_timeStamp = $data['start_timeStamp'];
  519. $end_timeStamp = $data['end_timeStamp'];
  520. $tmpTable = $this->table;
  521. $time = time();
  522. $ergs = $data;
  523. DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time, $ergs) {
  524. // 1. 先软删除旧数据(你已有)
  525. RevenueCost::where('del_time', 0)
  526. ->where('order_time', '>=', $start_timeStamp)
  527. ->where('order_time', '<=', $end_timeStamp)
  528. ->update(['del_time' => $time]);
  529. // 2. 分批从临时表插入新数据
  530. $batchSize = 500;
  531. $lastId = 0;
  532. do {
  533. $chunk = DB::table($tmpTable)
  534. ->where('id', '>', $lastId)
  535. ->orderBy('id')
  536. ->limit($batchSize)
  537. ->get();
  538. if ($chunk->isEmpty()) {
  539. break;
  540. }
  541. $data = $chunk->map(function ($item) use($time){
  542. return [
  543. 'order_id' => $item->order_id,
  544. 'order_number' => $item->order_number,
  545. 'order_time' => $item->order_time,
  546. 'employee_id_1_title' => $item->employee_id_1_title,
  547. 'employee_id_1' => $item->employee_id_1 ?? 0,
  548. 'employee_id_2' => $item->employee_id_2 ?? 0,
  549. 'employee_id_2_title' => $item->employee_id_2_title ?? "",
  550. 'customer_code' => $item->customer_code,
  551. 'customer_title' => $item->customer_title,
  552. 'channel_finance' => $item->channel_finance,
  553. 'channel_details' => $item->channel_details,
  554. 'product_code' => $item->product_code,
  555. 'product_title' => $item->product_title,
  556. 'product_size' => $item->product_size,
  557. 'unit' => $item->unit,
  558. 'quantity' => $item->quantity,
  559. 'price_1' => $item->price_1,
  560. 'price_1_total' => $item->price_1_total,
  561. 'price_2' => $item->price_2,
  562. 'price_2_total' => $item->price_2_total,
  563. 'price_3' => $item->price_3,
  564. 'price_3_total' => $item->price_3_total,
  565. 'price_4' => $item->price_4,
  566. 'price_4_total' => $item->price_4_total,
  567. 'profit' => $item->profit,
  568. 'profit_rate' => $item->profit_rate,
  569. 'id_detail' => $item->id_detail,
  570. 'order_type' => $item->order_type,
  571. 'payment_amount' => $item->payment_amount ?? 0,
  572. 'id_detail_upstream' => $item->id_detail_upstream?? 0,
  573. 'order_number_upstream' => $item->order_number_upstream ?? "",
  574. 'is_activity' => $item->is_activity ?? 0,
  575. 'customer_profit_rate' => $item->customer_profit_rate ?? '',
  576. 'voucher_type' => $item->voucher_type ?? 0,
  577. 'crt_time' => $time,
  578. ];
  579. })->toArray();
  580. // 每批单独插入(可选:加小事务)
  581. RevenueCost::insert($data);
  582. // 更新 lastId
  583. $lastId = $chunk->last()->id;
  584. } while ($chunk->count() == $batchSize);
  585. // 3. 更新主表
  586. list($status, $msg) = $this->updateRevenueCostTotal($ergs);
  587. if (! $status) {
  588. throw new \Exception($msg);
  589. }
  590. });
  591. }catch (\Throwable $exception){
  592. return [false, "单据明细同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  593. }
  594. return [true, ''];
  595. }
  596. private function updateRevenueCostTotal($data){
  597. try {
  598. $start_timeStamp = $data['start_timeStamp'];
  599. $end_timeStamp = $data['end_timeStamp'];
  600. $time = time();
  601. //组织写入数据
  602. $return = [];
  603. $update_stamp = [];
  604. DB::table('revenue_cost')
  605. ->where('del_time',0)
  606. ->where('order_time', '>=', $start_timeStamp)
  607. ->where('order_time', '<=', $end_timeStamp)
  608. ->select(RevenueCost::$field)
  609. ->chunkById(100, function ($data) use(&$return,&$update_stamp){
  610. $dataArray = Collect($data)->map(function ($object){
  611. return (array)$object;
  612. })->toArray();
  613. foreach ($dataArray as $value){
  614. //变成每个月第一天的时间戳
  615. $time = date("Y-m-01", $value['order_time']);
  616. $stamp = strtotime($time);
  617. if(! in_array($stamp, $update_stamp)) $update_stamp[] = $stamp;
  618. if($value['order_type'] == RevenueCost::ORDER_ONE){
  619. $income = $value['price_3_total'];
  620. }elseif ($value['order_type'] == RevenueCost::ORDER_TWO){
  621. $income = $value['price_1_total'];
  622. }else{
  623. $income = $value['payment_amount'];
  624. }
  625. $adjust = $income < 0 ? $income : 0;
  626. $business = $value['price_4_total'];
  627. if(isset($return[$stamp][$value['order_type']][$value['employee_id_1']])){
  628. $income_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['income'], $income,2);
  629. $adjust_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['adjust'], $adjust,2);
  630. $business_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['business'], $business,2);
  631. $return[$stamp][$value['order_type']][$value['employee_id_1']]['income'] = $income_total;
  632. $return[$stamp][$value['order_type']][$value['employee_id_1']]['adjust'] = $adjust_total;
  633. $return[$stamp][$value['order_type']][$value['employee_id_1']]['business'] = $business_total;
  634. }else{
  635. $return[$stamp][$value['order_type']][$value['employee_id_1']] = [
  636. 'income' => $income,
  637. 'adjust' => $adjust,
  638. 'business' => $business,
  639. 'order_time' => $stamp,
  640. 'employee_id_1_title' => $value['employee_id_1_title'],
  641. ];
  642. }
  643. }
  644. });
  645. $insert = [];
  646. foreach ($return as $value){
  647. foreach ($value as $order_type => $val){
  648. foreach ($val as $employee_id => $v){
  649. $profit = bcsub($v['income'], $v['business'],2);
  650. $profit_rate = $v['income'] > 0 ? bcdiv($profit, $v['income'],2) : 0;
  651. $v['profit'] = $profit;
  652. $v['profit_rate'] = $profit_rate;
  653. $v['order_type'] = $order_type;
  654. $v['employee_id_1'] = $employee_id;
  655. $v['crt_time'] = $time;
  656. $insert[] = $v;
  657. }
  658. }
  659. }
  660. RevenueCostTotal::where('del_time', 0)
  661. ->whereIn('order_time', $update_stamp)
  662. ->update(['del_time' => $time]);
  663. RevenueCostTotal::insert($insert);
  664. }catch (\Throwable $exception){
  665. return [false, "主表同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  666. }
  667. return [true, ''];
  668. }
  669. private function createTmpTable(){
  670. $table = $this->table;
  671. if (! Schema::hasTable($table)) {
  672. // 可以通过 migration 创建,或程序启动时检查
  673. Schema::create($table, function (Blueprint $table) {
  674. $table->bigIncrements('id'); // BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
  675. $table->integer('order_type')->default(0);
  676. $table->bigInteger('order_id')->default(0);
  677. $table->string('order_number', 50)->default('');
  678. $table->integer('order_time')->nullable();
  679. $table->string('employee_id_1_title', 100)->default('');
  680. $table->bigInteger('employee_id_1')->default(0);
  681. $table->bigInteger('employee_id_2')->default(0);
  682. $table->string('employee_id_2_title', 100)->default('');
  683. $table->string('customer_code', 50)->default('');
  684. $table->string('customer_title', 100)->default('');
  685. $table->string('channel_finance', 50)->nullable();
  686. $table->string('channel_details', 50)->nullable();
  687. $table->string('product_code', 50)->default('');
  688. $table->string('product_title', 100)->default('');
  689. $table->string('product_size', 100)->nullable();
  690. $table->string('unit', 20)->nullable();
  691. $table->decimal('quantity', 12, 2)->default(0);
  692. $table->decimal('price_1', 12, 2)->default(0); // 销项成本
  693. $table->decimal('price_1_total', 12, 2)->default(0);
  694. $table->decimal('price_2', 12, 2)->default(0); // 运费
  695. $table->decimal('price_2_total', 12, 2)->default(0);
  696. $table->decimal('price_3', 12, 2)->default(0); // 含税单价
  697. $table->decimal('price_3_total', 12, 2)->default(0); // 含税金额
  698. $table->decimal('price_4', 12, 2)->default(0); // 业务成本
  699. $table->decimal('price_4_total', 12, 2)->default(0);
  700. $table->decimal('profit', 12, 2)->default(0);
  701. $table->decimal('payment_amount', 12, 2)->default(0);
  702. $table->decimal('profit_rate', 10, 3)->default(0);
  703. $table->bigInteger('id_detail')->default(0);
  704. $table->bigInteger('id_detail_upstream')->default(0);
  705. $table->string('order_number_upstream', 100)->nullable();
  706. $table->decimal('is_activity', 2, 0)->default(0);
  707. $table->string('customer_profit_rate', 20)->default('');
  708. $table->bigInteger('voucher_type')->default(0);
  709. });
  710. }
  711. }
  712. public function clearTmpTable(){
  713. if (Schema::hasTable($this->table)) DB::table($this->table)->truncate();
  714. }
  715. public function delTableKey($type = 1){
  716. $key = $this->table;
  717. if($type == 2) $key = $this->table_2;
  718. $this->dellimitingSendRequest($key);
  719. }
  720. public function synSalaryEmployee($data, $user){
  721. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '同步时间不能为空'];
  722. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['crt_time'],false);
  723. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "同步时间:时间区间无效"];
  724. list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
  725. if(! $bool) return [false, $bool_msg];
  726. $data['start_timeStamp'] = $start_time;
  727. $data['end_timeStamp'] = $end_time;
  728. $data['start_time'] = strtotime(date("Y-m-01",$data['start_timeStamp']));
  729. $data['end_time'] = strtotime(date("Y-m-01"),$data['end_timeStamp']);
  730. $data['operation_time'] = time();
  731. list($status,$msg) = $this->limitingSendRequest($this->table_2);
  732. if(! $status) return [false, '业务员工资同步正在后台运行,请稍后'];
  733. //同步
  734. // list($status, $msg) = $this->synSalaryEmployeeFromMine($data, $user);
  735. // if(! $status) {
  736. // $this->dellimitingSendRequest($this->table_2);
  737. // return [false, $msg];
  738. // }
  739. //队列
  740. ProcessDataJob::dispatch($data, $user, 2)->onQueue(RevenueCost::job2);
  741. return [true, '业务员工资相关信息同步已进入后台任务'];
  742. }
  743. public function synSalaryEmployeeFromMine($data, $user){
  744. //创建临时表 如果不存在
  745. $this->createTmpTable2();
  746. //清理临时表 如果内容不为空
  747. $this->clearTmpTable2();
  748. //写入临时表
  749. DB::table('revenue_cost')
  750. ->where('del_time', 0)
  751. ->where('order_type', RevenueCost::ORDER_THREE)
  752. ->where('order_time','>=',$data['start_timeStamp'])
  753. ->where('order_time','<=',$data['end_timeStamp'])
  754. ->select([
  755. 'employee_id_1',
  756. 'employee_id_1_title',
  757. 'order_time as time',
  758. DB::raw("DATE_FORMAT(FROM_UNIXTIME(order_time), '%Y-%m-01') as order_time"),
  759. DB::raw("SUM(payment_amount) as payment_amount"),
  760. DB::raw("SUM(CASE WHEN is_activity = 0 THEN payment_amount ELSE 0 END) as payment_amount_not_include_activity"),
  761. DB::raw("SUM(CASE WHEN is_activity = 1 THEN payment_amount ELSE 0 END) as payment_amount_activity"),
  762. DB::raw("SUM(CASE WHEN profit_rate < customer_profit_rate and is_activity = 0 THEN payment_amount ELSE 0 END) as payment_amount_lower_than_rate"),
  763. DB::raw("SUM(CASE WHEN profit_rate >= customer_profit_rate and is_activity = 0 THEN payment_amount ELSE 0 END) as payment_amount_greater_than_rate"),
  764. DB::raw("SUM(CASE WHEN profit_rate >= customer_profit_rate and is_activity = 0 THEN price_4_total ELSE 0 END) as business"),
  765. DB::raw("ROW_NUMBER() OVER (ORDER BY MIN(id)) as fake_id")
  766. ])
  767. ->groupBy('employee_id_1', DB::raw("DATE_FORMAT(FROM_UNIXTIME(order_time), '%Y-%m-01')"))
  768. ->orderBy('order_time', 'desc')
  769. ->chunkById(500, function ($data){
  770. $dataArray = Collect($data)->map(function ($object){
  771. return (array)$object;
  772. })->toArray();
  773. $indexes = $this->getEmployeeIndex($dataArray);
  774. foreach ($dataArray as $key => $value){
  775. $value['index_' . EmployeeIndex::TYPE_ONE] = 0;
  776. $value['index_' . EmployeeIndex::TYPE_SIX] = 0;
  777. $value['index_' . EmployeeIndex::TYPE_EIGHT] = 0;
  778. $this->findIndex($indexes, $value);
  779. $dataArray[$key]['order_type'] = RevenueCost::ORDER_THREE;
  780. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  781. $dataArray[$key]['sale_bonus'] = $value['index_' . EmployeeIndex::TYPE_EIGHT];
  782. $dataArray[$key]['index_' . EmployeeIndex::TYPE_ONE] = $value['index_' . EmployeeIndex::TYPE_ONE];
  783. $rate = bcdiv($value['index_' . EmployeeIndex::TYPE_ONE],100,2);
  784. $pay_in_advance = bcmul($rate, $value['payment_amount_greater_than_rate'],2);
  785. $dataArray[$key]['pay_in_advance'] = $pay_in_advance;
  786. $dataArray[$key]['basic_salary'] = $value['index_' . EmployeeIndex::TYPE_SIX];
  787. $dataArray[$key]['should_pay'] = bcadd(bcadd($value['index_' . EmployeeIndex::TYPE_EIGHT], $pay_in_advance,2),$value['index_' . EmployeeIndex::TYPE_SIX],2);
  788. unset($dataArray[$key]['fake_id']);
  789. unset($dataArray[$key]['time']);
  790. }
  791. DB::table($this->table_2)->insert($dataArray);
  792. }, 'fake_id');
  793. //更新数据
  794. list($status,$msg) = $this->updateSalaryEmployee($data);
  795. if(! $status) return [false, $msg];
  796. //清理临时表 如果内容不为空
  797. $this->clearTmpTable2();
  798. //释放redis
  799. $this->delTableKey(2);
  800. return [true, '同步成功'];
  801. }
  802. private function getEmployeeIndex($existingData)
  803. {
  804. if (empty($existingData)) {
  805. return collect();
  806. }
  807. // 取出所有涉及的 employee_id 和时间区间
  808. $employeeIds = array_column($existingData, 'employee_id_1');
  809. $time = array_column($existingData, 'time');
  810. $minStart = ! empty($time) ? min($time) : 0;
  811. $maxEnd = ! empty($time) ? max($time) : 0;
  812. // 一次性查出这些员工在最大区间范围内的所有指标
  813. $results = EmployeeIndex::where('del_time', 0)
  814. ->whereIn('type', [EmployeeIndex::TYPE_ONE, EmployeeIndex::TYPE_EIGHT, EmployeeIndex::TYPE_SIX])
  815. ->whereIn('employee_id', $employeeIds)
  816. ->where('start_time', '<=', $maxEnd)
  817. ->where('end_time', '>=', $minStart)
  818. ->select('start_time','end_time','employee_id','index','type')
  819. ->get();
  820. return $results;
  821. }
  822. private function findIndex($indexes, &$value){
  823. // 找到所有符合条件的 index(可能多个 type)
  824. $matchedIndexes = $indexes->filter(function ($item) use ($value) {
  825. return $item['employee_id'] == $value['employee_id_1']
  826. && $item['start_time'] <= $value['time']
  827. && $item['end_time'] >= $value['time'];
  828. });
  829. // 按 type 去重,只保留第一次出现的
  830. $uniqueByType = [];
  831. foreach ($matchedIndexes as $item) {
  832. $index = "index_" . $item['type'];
  833. if (! isset($uniqueByType[$index])) {
  834. $uniqueByType[$index] = $item['index'];
  835. }
  836. }
  837. $value = array_merge($value, $uniqueByType);
  838. }
  839. private function createTmpTable2(){
  840. $table = $this->table_2;
  841. if (! Schema::hasTable($table)) {
  842. // 可以通过 migration 创建,或程序启动时检查
  843. Schema::create($table, function (Blueprint $table) {
  844. $table->bigIncrements('id'); // BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
  845. $table->integer('order_type')->default(0);
  846. $table->integer('order_time')->default(0);
  847. $table->string('employee_id_1_title', 100)->default('');
  848. $table->bigInteger('employee_id_1')->default(0);
  849. $table->decimal('payment_amount', 12, 2)->default(0);
  850. $table->decimal('payment_amount_not_include_activity', 12, 2)->default(0);
  851. $table->decimal('payment_amount_activity', 12, 2)->default(0);
  852. $table->decimal('payment_amount_lower_than_rate', 12, 2)->default(0);
  853. $table->decimal('payment_amount_greater_than_rate', 12, 2)->default(0);
  854. $table->decimal('business', 12, 2)->default(0);
  855. $table->decimal('sale_bonus', 12, 2)->default(0);
  856. $table->decimal('index_1', 10, 2)->default(0);
  857. $table->decimal('pay_in_advance', 12, 2)->default(0);
  858. $table->decimal('basic_salary', 12, 2)->default(0);
  859. $table->decimal('should_pay', 12, 2)->default(0);
  860. });
  861. }
  862. }
  863. public function clearTmpTable2(){
  864. if (Schema::hasTable($this->table_2)) DB::table($this->table_2)->truncate();
  865. }
  866. private function updateSalaryEmployee($data){
  867. try {
  868. $start_timeStamp = $data['start_time'];
  869. $end_timeStamp = $data['end_time'];
  870. $tmpTable = $this->table_2;
  871. $time = time();
  872. $ergs = $data;
  873. DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time, $ergs) {
  874. // 1. 先软删除旧数据(你已有)
  875. SalaryEmployee::where('del_time', 0)
  876. ->where('order_time', '>=', $start_timeStamp)
  877. ->where('order_time', '<=', $end_timeStamp)
  878. ->update(['del_time' => $time]);
  879. // 2. 分批从临时表插入新数据
  880. $batchSize = 500;
  881. $lastId = 0;
  882. do {
  883. $chunk = DB::table($tmpTable)
  884. ->where('id', '>', $lastId)
  885. ->orderBy('id')
  886. ->limit($batchSize)
  887. ->get();
  888. if ($chunk->isEmpty()) {
  889. break;
  890. }
  891. $data = $chunk->map(function ($item) use($time){
  892. return [
  893. 'order_type' => $item->order_type,
  894. 'order_time' => $item->order_time,
  895. 'employee_id_1_title' => $item->employee_id_1_title,
  896. 'employee_id_1' => $item->employee_id_1 ?? 0,
  897. 'payment_amount' => $item->payment_amount,
  898. 'payment_amount_not_include_activity' => $item->payment_amount_not_include_activity,
  899. 'payment_amount_activity' => $item->payment_amount_activity,
  900. 'payment_amount_lower_than_rate' => $item->payment_amount_lower_than_rate,
  901. 'payment_amount_greater_than_rate' => $item->payment_amount_greater_than_rate,
  902. 'business' => $item->business,
  903. 'sale_bonus' => $item->sale_bonus,
  904. 'index_1' => $item->index_1,
  905. 'pay_in_advance' => $item->pay_in_advance,
  906. 'basic_salary' => $item->basic_salary,
  907. 'should_pay' => $item->should_pay,
  908. 'crt_time' => $time,
  909. ];
  910. })->toArray();
  911. // 每批单独插入(可选:加小事务)
  912. SalaryEmployee::insert($data);
  913. // 更新 lastId
  914. $lastId = $chunk->last()->id;
  915. } while ($chunk->count() == $batchSize);
  916. });
  917. }catch (\Throwable $exception){
  918. return [false, "单据明细同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  919. }
  920. return [true, ''];
  921. }
  922. }