TPlusServerService.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. sd.voucherState as order_state,
  245. COALESCE(ps.name, '') as employee_id_1_title,
  246. COALESCE(sd.idclerk, 0) as employee_id_1,
  247. COALESCE(ps2.name, '') as employee_id_2_title,
  248. COALESCE(pn.idsaleman, 0) as employee_id_2,
  249. COALESCE(pn.code, '') as customer_code,
  250. COALESCE(pn.name, '') as customer_title,
  251. COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  252. COALESCE(sd.pubuserdefnvc11, '') as channel_finance,
  253. COALESCE(sd.pubuserdefnvc12, '') as channel_details,
  254. COALESCE(it.code, '') as product_code,
  255. COALESCE(it.name, '') as product_title,
  256. COALESCE(it.specification, '') as product_size,
  257. COALESCE(ui.name, '') as unit,
  258. COALESCE(sd_b.quantity, 0) as quantity,
  259. COALESCE(sd_b.taxPrice, 0) as price_3,
  260. COALESCE(sd_b.taxAmount, 0) as price_3_total,
  261. COALESCE(sd_b.ID, 0) as id_detail,
  262. COALESCE(sd_b.pubuserdefdecm9, 0) as is_activity
  263. ")
  264. ->get();
  265. if ($rows->isEmpty()) break;
  266. $dataArray = Collect($rows)->map(function ($object) {
  267. return (array)$object;
  268. })->toArray();
  269. //存货档案
  270. $product = Product::where('del_time', 0)
  271. ->whereIn('code', array_unique(array_column($dataArray, 'product_code')))
  272. ->select('code', 'write_off_price', 'freight_price', 'business_cost')
  273. ->get()->toArray();
  274. $product_map = array_column($product, null, 'code');
  275. //组织数据
  276. foreach ($dataArray as $key => $value) {
  277. $customer_profit_rate = rtrim($value['customer_profit_rate'],'%');
  278. if(is_numeric($customer_profit_rate)){
  279. $customer_profit_rate = bcdiv($customer_profit_rate,100,3);
  280. }else{
  281. $customer_profit_rate = 0;
  282. }
  283. $dataArray[$key]['customer_profit_rate'] = $customer_profit_rate;
  284. $p_tmp = $product_map[$value['product_code']] ?? [];
  285. $dataArray[$key]['order_type'] = RevenueCost::ORDER_ONE;
  286. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  287. $write_off_price = $p_tmp['write_off_price'] ?? 0;
  288. $dataArray[$key]['price_1'] = $write_off_price;
  289. $dataArray[$key]['price_1_total'] = bcmul($write_off_price, $value['quantity'], 2);
  290. $freight_price = $p_tmp['freight_price'] ?? 0;
  291. $dataArray[$key]['price_2'] = $freight_price;
  292. $dataArray[$key]['price_2_total'] = bcmul($freight_price, $value['quantity'], 2);
  293. $business_cost = $p_tmp['business_cost'] ?? 0;
  294. $dataArray[$key]['price_4'] = $business_cost;
  295. $price_4_total = bcmul($business_cost, $value['quantity'], 2);
  296. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'], 2);
  297. $profit = bcsub($value['price_3_total'], $price_4_total, 2);
  298. $dataArray[$key]['profit'] = $profit;
  299. $dataArray[$key]['profit_rate'] = $value['price_3_total'] > 0 ? bcdiv($profit, $value['price_3_total'], 2) : 0;
  300. }
  301. DB::table($table)->insert($dataArray);
  302. // 更新 lastId 继续下一批
  303. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  304. } while (count($rows) === $limit);
  305. }catch (\Throwable $exception){
  306. return [false, "销货单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  307. }
  308. return [true, ''];
  309. }
  310. private function xsfpTPlus($data, $user){
  311. try {
  312. $table = $this->table;
  313. $limit = 500;
  314. $lastId = 0;
  315. do {
  316. $rows = $this->databaseService->table('SA_SaleInvoice_b as si_b')
  317. ->join('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  318. ->leftJoin('SA_SaleDelivery_b as sd_b', 'si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
  319. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  320. ->leftJoin('AA_Person as ps', 'si.idclerk', '=', 'ps.ID')
  321. ->leftJoin('AA_Person as ps2', 'pn.idsaleman', '=', 'ps2.ID')
  322. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  323. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  324. ->where('si.voucherdate','>=',$data['start_time'])
  325. ->where('si.voucherdate','<=',$data['end_time'])
  326. ->where('si_b.ID', '>', $lastId) // 用真实字段
  327. ->orderBy('si_b.ID')
  328. ->limit($limit)
  329. ->selectRaw("
  330. COALESCE(si.ID, 0) as order_id,
  331. COALESCE(si.code, '') as order_number,
  332. si.voucherdate as order_time,
  333. si.voucherState as order_state,
  334. COALESCE(pn.code, '') as customer_code,
  335. COALESCE(pn.name, '') as customer_title,
  336. COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  337. COALESCE(si.idclerk, 0) as employee_id_1,
  338. COALESCE(ps.name, '') as employee_id_1_title,
  339. COALESCE(pn.idsaleman, 0) as employee_id_2,
  340. COALESCE(ps2.name, '') as employee_id_2_title,
  341. COALESCE(it.code, '') as product_code,
  342. COALESCE(it.name, '') as product_title,
  343. COALESCE(it.specification, '') as product_size,
  344. COALESCE(ui.name, '') as unit,
  345. COALESCE(si_b.quantity, 0) as quantity,
  346. COALESCE(si_b.taxPrice, 0) as price_1,
  347. COALESCE(si_b.taxAmount, 0) as price_1_total,
  348. COALESCE(si_b.ID, 0) as id_detail,
  349. COALESCE(si_b.sourceVoucherDetailId, 0) as id_detail_upstream,
  350. COALESCE(si_b.sourceVoucherCode, '') as order_number_upstream,
  351. COALESCE(sd_b.pubuserdefdecm9, 0) as is_activity
  352. ")
  353. ->get();
  354. if ($rows->isEmpty()) break;
  355. $dataArray = Collect($rows)->map(function ($object) {
  356. return (array)$object;
  357. })->toArray();
  358. //存货档案
  359. $product = Product::where('del_time',0)
  360. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  361. ->select('code','business_cost')
  362. ->get()->toArray();
  363. $product_map = array_column($product,null,'code');
  364. //组织数据
  365. foreach ($dataArray as $key => $value){
  366. $customer_profit_rate = rtrim($value['customer_profit_rate'],'%');
  367. if(is_numeric($customer_profit_rate)){
  368. $customer_profit_rate = bcdiv($customer_profit_rate,100,3);
  369. }else{
  370. $customer_profit_rate = 0;
  371. }
  372. $dataArray[$key]['customer_profit_rate'] = $customer_profit_rate;
  373. $p_tmp = $product_map[$value['product_code']] ?? [];
  374. $dataArray[$key]['order_type'] = RevenueCost::ORDER_TWO;
  375. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  376. $business_cost = $p_tmp['business_cost'] ?? 0;
  377. $dataArray[$key]['price_4'] = $business_cost;
  378. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  379. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'],2);
  380. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  381. $dataArray[$key]['profit'] = $profit;
  382. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  383. }
  384. DB::table($table)->insert($dataArray);
  385. // 更新 lastId 继续下一批
  386. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  387. } while (count($rows) === $limit);
  388. }catch (\Throwable $exception){
  389. return [false, "销售发票同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  390. }
  391. return [true, ''];
  392. }
  393. private function hkdTPlus($data, $user){
  394. try{
  395. $table = $this->table;
  396. $limit = 500;
  397. $lastId = 0;
  398. do {
  399. $rows = $this->databaseService->table('ARAP_ReceivePayment_b as rp_b')
  400. ->join('ARAP_ReceivePayment as rp', 'rp_b.idArapReceivePaymentDTO', '=', 'rp.ID')
  401. // ->leftJoin('SA_SaleInvoice_b as si_b', 'rp_b.voucherDetailID', '=', 'si_b.ID')
  402. // ->leftJoin('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  403. // ->leftJoin('SA_SaleDelivery_b as sd_b', 'si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
  404. // 发票子表关联(仅限 idvouchertype = 20)
  405. ->leftJoin('SA_SaleInvoice_b as si_b', function ($join) {
  406. $join->on('rp_b.voucherDetailID', '=', 'si_b.ID')
  407. ->where('rp_b.idvouchertype', '=', 20);
  408. })
  409. ->leftJoin('SA_SaleInvoice as si', function ($join) {
  410. $join->on('si_b.idSaleInvoiceDTO', '=', 'si.ID')
  411. ->where('rp_b.idvouchertype', '=', 20);
  412. })
  413. ->leftJoin('SA_SaleDelivery_b as sd_b', function ($join) {
  414. $join->on('si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
  415. ->where('rp_b.idvouchertype', '=', 20);
  416. })
  417. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  418. ->leftJoin('AA_Person as ps', 'rp.idperson', '=', 'ps.ID')
  419. ->leftJoin('AA_Person as ps2', 'pn.idsaleman', '=', 'ps2.ID')
  420. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  421. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  422. ->where('rp.voucherdate','>=',$data['start_time'])
  423. ->where('rp.voucherdate','<=',$data['end_time'])
  424. // ->where('rp_b.idvouchertype','=', 20) // 销售发票
  425. ->where('rp.isReceiveFlag','=', 1)
  426. ->where('rp_b.ID', '>', $lastId)
  427. ->orderBy('rp_b.ID')
  428. ->limit($limit)
  429. // ->selectRaw("
  430. // COALESCE(rp.ID, 0) as order_id,
  431. // COALESCE(rp.code, '') as order_number,
  432. // rp.voucherdate as order_time,
  433. // COALESCE(pn.code, '') as customer_code,
  434. // COALESCE(pn.name, '') as customer_title,
  435. // COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  436. // COALESCE(it.code, '') as product_code,
  437. // COALESCE(it.name, '') as product_title,
  438. // COALESCE(rp.idperson, 0) as employee_id_1,
  439. // COALESCE(ps.name, '') as employee_id_1_title,
  440. // COALESCE(pn.idsaleman, 0) as employee_id_2,
  441. // COALESCE(ps2.name, '') as employee_id_2_title,
  442. // COALESCE(it.specification, '') as product_size,
  443. // COALESCE(ui.name, '') as unit,
  444. // COALESCE(si_b.quantity, 0) as quantity,
  445. // COALESCE(si_b.taxPrice, 0) as price_1,
  446. // COALESCE(si_b.taxAmount, 0) as price_1_total,
  447. // COALESCE(rp_b.amount, 0) as payment_amount,
  448. // COALESCE(rp_b.ID, 0) as id_detail,
  449. // COALESCE(rp_b.voucherDetailID, 0) as id_detail_upstream,
  450. // COALESCE(rp_b.voucherCode, '') as order_number_upstream,
  451. // COALESCE(sd_b.pubuserdefdecm9, 0) as is_activity
  452. // ")
  453. ->selectRaw("
  454. COALESCE(rp.ID, 0) as order_id,
  455. COALESCE(rp.code, '') as order_number,
  456. rp.voucherdate as order_time,
  457. rp.voucherstate as order_state,
  458. COALESCE(pn.code, '') as customer_code,
  459. COALESCE(pn.name, '') as customer_title,
  460. COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  461. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.code, '') ELSE '' END as product_code,
  462. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.name, '') ELSE '' END as product_title,
  463. COALESCE(rp.idperson, 0) as employee_id_1,
  464. COALESCE(ps.name, '') as employee_id_1_title,
  465. COALESCE(pn.idsaleman, 0) as employee_id_2,
  466. COALESCE(ps2.name, '') as employee_id_2_title,
  467. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.specification, '') ELSE '' END as product_size,
  468. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(ui.name, '') ELSE '' END as unit,
  469. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.quantity, 0) ELSE 0 END as quantity,
  470. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.taxPrice, 0) ELSE 0 END as price_1,
  471. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.taxAmount, 0) ELSE 0 END as price_1_total,
  472. COALESCE(rp_b.amount, 0) as payment_amount,
  473. COALESCE(rp_b.ID, 0) as id_detail,
  474. COALESCE(rp_b.voucherDetailID, 0) as id_detail_upstream,
  475. COALESCE(rp_b.voucherCode, '') as order_number_upstream,
  476. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(sd_b.pubuserdefdecm9, 0) ELSE 0 END as is_activity,
  477. rp_b.idvouchertype as voucher_type
  478. ")
  479. ->get();
  480. if ($rows->isEmpty()) break;
  481. $dataArray = Collect($rows)->map(function ($object) {
  482. return (array)$object;
  483. })->toArray();
  484. //存货档案
  485. $product = Product::where('del_time',0)
  486. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  487. ->select('code','business_cost')
  488. ->get()->toArray();
  489. $product_map = array_column($product,null,'code');
  490. //组织数据
  491. foreach ($dataArray as $key => $value){
  492. $customer_profit_rate = rtrim($value['customer_profit_rate'],'%');
  493. if(is_numeric($customer_profit_rate)){
  494. $customer_profit_rate = bcdiv($customer_profit_rate,100,3);
  495. }else{
  496. $customer_profit_rate = 0;
  497. }
  498. $dataArray[$key]['customer_profit_rate'] = $customer_profit_rate;
  499. $p_tmp = $product_map[$value['product_code']] ?? [];
  500. $dataArray[$key]['order_type'] = RevenueCost::ORDER_THREE;
  501. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  502. $business_cost = $p_tmp['business_cost'] ?? 0;
  503. $dataArray[$key]['price_4'] = $business_cost;
  504. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  505. $dataArray[$key]['price_4_total'] = $price_4_total;
  506. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  507. $dataArray[$key]['profit'] = $profit;
  508. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  509. }
  510. DB::table($table)->insert($dataArray);
  511. // 更新 lastId 继续下一批
  512. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  513. } while (count($rows) === $limit);
  514. }catch (\Throwable $exception){
  515. return [false, "回款单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  516. }
  517. return [true, ''];
  518. }
  519. private function updateRevenueCost($data){
  520. try {
  521. $start_timeStamp = $data['start_timeStamp'];
  522. $end_timeStamp = $data['end_timeStamp'];
  523. $tmpTable = $this->table;
  524. $time = time();
  525. $ergs = $data;
  526. DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time, $ergs) {
  527. // 1. 先软删除旧数据(你已有)
  528. RevenueCost::where('del_time', 0)
  529. ->where('order_time', '>=', $start_timeStamp)
  530. ->where('order_time', '<=', $end_timeStamp)
  531. ->update(['del_time' => $time]);
  532. // 2. 分批从临时表插入新数据
  533. $batchSize = 500;
  534. $lastId = 0;
  535. do {
  536. $chunk = DB::table($tmpTable)
  537. ->where('id', '>', $lastId)
  538. ->orderBy('id')
  539. ->limit($batchSize)
  540. ->get();
  541. if ($chunk->isEmpty()) {
  542. break;
  543. }
  544. $data = $chunk->map(function ($item) use($time){
  545. return [
  546. 'order_id' => $item->order_id,
  547. 'order_number' => $item->order_number,
  548. 'order_time' => $item->order_time,
  549. 'order_state' => $item->order_state,
  550. 'employee_id_1_title' => $item->employee_id_1_title,
  551. 'employee_id_1' => $item->employee_id_1 ?? 0,
  552. 'employee_id_2' => $item->employee_id_2 ?? 0,
  553. 'employee_id_2_title' => $item->employee_id_2_title ?? "",
  554. 'customer_code' => $item->customer_code,
  555. 'customer_title' => $item->customer_title,
  556. 'channel_finance' => $item->channel_finance,
  557. 'channel_details' => $item->channel_details,
  558. 'product_code' => $item->product_code,
  559. 'product_title' => $item->product_title,
  560. 'product_size' => $item->product_size,
  561. 'unit' => $item->unit,
  562. 'quantity' => $item->quantity,
  563. 'price_1' => $item->price_1,
  564. 'price_1_total' => $item->price_1_total,
  565. 'price_2' => $item->price_2,
  566. 'price_2_total' => $item->price_2_total,
  567. 'price_3' => $item->price_3,
  568. 'price_3_total' => $item->price_3_total,
  569. 'price_4' => $item->price_4,
  570. 'price_4_total' => $item->price_4_total,
  571. 'profit' => $item->profit,
  572. 'profit_rate' => $item->profit_rate,
  573. 'id_detail' => $item->id_detail,
  574. 'order_type' => $item->order_type,
  575. 'payment_amount' => $item->payment_amount ?? 0,
  576. 'id_detail_upstream' => $item->id_detail_upstream?? 0,
  577. 'order_number_upstream' => $item->order_number_upstream ?? "",
  578. 'is_activity' => $item->is_activity ?? 0,
  579. 'customer_profit_rate' => $item->customer_profit_rate ?? '',
  580. 'voucher_type' => $item->voucher_type ?? 0,
  581. 'crt_time' => $time,
  582. ];
  583. })->toArray();
  584. // 每批单独插入(可选:加小事务)
  585. RevenueCost::insert($data);
  586. // 更新 lastId
  587. $lastId = $chunk->last()->id;
  588. } while ($chunk->count() == $batchSize);
  589. // 3. 更新主表
  590. list($status, $msg) = $this->updateRevenueCostTotal($ergs);
  591. if (! $status) {
  592. throw new \Exception($msg);
  593. }
  594. });
  595. }catch (\Throwable $exception){
  596. return [false, "单据明细同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  597. }
  598. return [true, ''];
  599. }
  600. private function updateRevenueCostTotal($data){
  601. try {
  602. $start_timeStamp = $data['start_timeStamp'];
  603. $end_timeStamp = $data['end_timeStamp'];
  604. $time = time();
  605. //组织写入数据
  606. $return = [];
  607. $update_stamp = [];
  608. DB::table('revenue_cost')
  609. ->where('del_time',0)
  610. ->where('order_time', '>=', $start_timeStamp)
  611. ->where('order_time', '<=', $end_timeStamp)
  612. ->select(RevenueCost::$field)
  613. ->chunkById(100, function ($data) use(&$return,&$update_stamp){
  614. $dataArray = Collect($data)->map(function ($object){
  615. return (array)$object;
  616. })->toArray();
  617. foreach ($dataArray as $value){
  618. //变成每个月第一天的时间戳
  619. $time = date("Y-m-01", $value['order_time']);
  620. $stamp = strtotime($time);
  621. if(! in_array($stamp, $update_stamp)) $update_stamp[] = $stamp;
  622. if($value['order_type'] == RevenueCost::ORDER_ONE){
  623. $income = $value['price_3_total'];
  624. }elseif ($value['order_type'] == RevenueCost::ORDER_TWO){
  625. $income = $value['price_1_total'];
  626. }else{
  627. $income = $value['payment_amount'];
  628. }
  629. $adjust = $income < 0 ? $income : 0;
  630. $business = $value['price_4_total'];
  631. if(isset($return[$stamp][$value['order_type']][$value['employee_id_1']])){
  632. $income_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['income'], $income,2);
  633. $adjust_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['adjust'], $adjust,2);
  634. $business_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['business'], $business,2);
  635. $return[$stamp][$value['order_type']][$value['employee_id_1']]['income'] = $income_total;
  636. $return[$stamp][$value['order_type']][$value['employee_id_1']]['adjust'] = $adjust_total;
  637. $return[$stamp][$value['order_type']][$value['employee_id_1']]['business'] = $business_total;
  638. }else{
  639. $return[$stamp][$value['order_type']][$value['employee_id_1']] = [
  640. 'income' => $income,
  641. 'adjust' => $adjust,
  642. 'business' => $business,
  643. 'order_time' => $stamp,
  644. 'employee_id_1_title' => $value['employee_id_1_title'],
  645. ];
  646. }
  647. }
  648. });
  649. $insert = [];
  650. foreach ($return as $value){
  651. foreach ($value as $order_type => $val){
  652. foreach ($val as $employee_id => $v){
  653. $profit = bcsub($v['income'], $v['business'],2);
  654. $profit_rate = $v['income'] > 0 ? bcdiv($profit, $v['income'],2) : 0;
  655. $v['profit'] = $profit;
  656. $v['profit_rate'] = $profit_rate;
  657. $v['order_type'] = $order_type;
  658. $v['employee_id_1'] = $employee_id;
  659. $v['crt_time'] = $time;
  660. $insert[] = $v;
  661. }
  662. }
  663. }
  664. RevenueCostTotal::where('del_time', 0)
  665. ->whereIn('order_time', $update_stamp)
  666. ->update(['del_time' => $time]);
  667. RevenueCostTotal::insert($insert);
  668. }catch (\Throwable $exception){
  669. return [false, "主表同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  670. }
  671. return [true, ''];
  672. }
  673. private function createTmpTable(){
  674. $table = $this->table;
  675. if (! Schema::hasTable($table)) {
  676. // 可以通过 migration 创建,或程序启动时检查
  677. Schema::create($table, function (Blueprint $table) {
  678. $table->bigIncrements('id'); // BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
  679. $table->integer('order_type')->default(0);
  680. $table->bigInteger('order_id')->default(0);
  681. $table->string('order_number', 50)->default('');
  682. $table->integer('order_time')->nullable();
  683. $table->integer('order_state')->default(0);
  684. $table->string('employee_id_1_title', 100)->default('');
  685. $table->bigInteger('employee_id_1')->default(0);
  686. $table->bigInteger('employee_id_2')->default(0);
  687. $table->string('employee_id_2_title', 100)->default('');
  688. $table->string('customer_code', 50)->default('');
  689. $table->string('customer_title', 100)->default('');
  690. $table->string('channel_finance', 50)->nullable();
  691. $table->string('channel_details', 50)->nullable();
  692. $table->string('product_code', 50)->default('');
  693. $table->string('product_title', 100)->default('');
  694. $table->string('product_size', 100)->nullable();
  695. $table->string('unit', 20)->nullable();
  696. $table->decimal('quantity', 12, 2)->default(0);
  697. $table->decimal('price_1', 12, 2)->default(0); // 销项成本
  698. $table->decimal('price_1_total', 12, 2)->default(0);
  699. $table->decimal('price_2', 12, 2)->default(0); // 运费
  700. $table->decimal('price_2_total', 12, 2)->default(0);
  701. $table->decimal('price_3', 12, 2)->default(0); // 含税单价
  702. $table->decimal('price_3_total', 12, 2)->default(0); // 含税金额
  703. $table->decimal('price_4', 12, 2)->default(0); // 业务成本
  704. $table->decimal('price_4_total', 12, 2)->default(0);
  705. $table->decimal('profit', 12, 2)->default(0);
  706. $table->decimal('payment_amount', 12, 2)->default(0);
  707. $table->decimal('profit_rate', 10, 3)->default(0);
  708. $table->bigInteger('id_detail')->default(0);
  709. $table->bigInteger('id_detail_upstream')->default(0);
  710. $table->string('order_number_upstream', 100)->nullable();
  711. $table->decimal('is_activity', 2, 0)->default(0);
  712. $table->string('customer_profit_rate', 20)->default('');
  713. $table->bigInteger('voucher_type')->default(0);
  714. });
  715. }
  716. }
  717. public function clearTmpTable(){
  718. if (Schema::hasTable($this->table)) DB::table($this->table)->truncate();
  719. }
  720. public function delTableKey($type = 1){
  721. $key = $this->table;
  722. if($type == 2) $key = $this->table_2;
  723. $this->dellimitingSendRequest($key);
  724. }
  725. public function synSalaryEmployee($data, $user){
  726. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '同步时间不能为空'];
  727. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['crt_time'],false);
  728. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "同步时间:时间区间无效"];
  729. list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
  730. if(! $bool) return [false, $bool_msg];
  731. $data['start_timeStamp'] = $start_time;
  732. $data['end_timeStamp'] = $end_time;
  733. $data['start_time'] = strtotime(date("Y-m-01",$data['start_timeStamp']));
  734. $data['end_time'] = strtotime(date("Y-m-01"),$data['end_timeStamp']);
  735. $data['operation_time'] = time();
  736. list($status,$msg) = $this->limitingSendRequest($this->table_2);
  737. if(! $status) return [false, '业务员工资同步正在后台运行,请稍后'];
  738. //同步
  739. // list($status, $msg) = $this->synSalaryEmployeeFromMine($data, $user);
  740. // if(! $status) {
  741. // $this->dellimitingSendRequest($this->table_2);
  742. // return [false, $msg];
  743. // }
  744. //队列
  745. ProcessDataJob::dispatch($data, $user, 2)->onQueue(RevenueCost::job2);
  746. return [true, '业务员工资相关信息同步已进入后台任务'];
  747. }
  748. public function synSalaryEmployeeFromMine($data, $user){
  749. //创建临时表 如果不存在
  750. $this->createTmpTable2();
  751. //清理临时表 如果内容不为空
  752. $this->clearTmpTable2();
  753. //写入临时表
  754. DB::table('revenue_cost')
  755. ->where('del_time', 0)
  756. ->where('order_type', RevenueCost::ORDER_THREE)
  757. ->where('order_time','>=',$data['start_timeStamp'])
  758. ->where('order_time','<=',$data['end_timeStamp'])
  759. ->select([
  760. 'employee_id_1',
  761. 'employee_id_1_title',
  762. 'order_time as time',
  763. DB::raw("DATE_FORMAT(FROM_UNIXTIME(order_time), '%Y-%m-01') as order_time"),
  764. DB::raw("SUM(payment_amount) as payment_amount"),
  765. DB::raw("SUM(CASE WHEN is_activity = 0 THEN payment_amount ELSE 0 END) as payment_amount_not_include_activity"),
  766. DB::raw("SUM(CASE WHEN is_activity = 1 THEN payment_amount ELSE 0 END) as payment_amount_activity"),
  767. 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"),
  768. 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"),
  769. DB::raw("SUM(CASE WHEN profit_rate >= customer_profit_rate and is_activity = 0 THEN price_4_total ELSE 0 END) as business"),
  770. DB::raw("ROW_NUMBER() OVER (ORDER BY MIN(id)) as fake_id")
  771. ])
  772. ->groupBy('employee_id_1', DB::raw("DATE_FORMAT(FROM_UNIXTIME(order_time), '%Y-%m-01')"))
  773. ->orderBy('order_time', 'desc')
  774. ->chunkById(500, function ($data){
  775. $dataArray = Collect($data)->map(function ($object){
  776. return (array)$object;
  777. })->toArray();
  778. $indexes = $this->getEmployeeIndex($dataArray);
  779. foreach ($dataArray as $key => $value){
  780. $value['index_' . EmployeeIndex::TYPE_ONE] = 0;
  781. $value['index_' . EmployeeIndex::TYPE_SIX] = 0;
  782. $value['index_' . EmployeeIndex::TYPE_EIGHT] = 0;
  783. $this->findIndex($indexes, $value);
  784. $dataArray[$key]['order_type'] = RevenueCost::ORDER_THREE;
  785. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  786. $dataArray[$key]['sale_bonus'] = $value['index_' . EmployeeIndex::TYPE_EIGHT];
  787. $dataArray[$key]['index_' . EmployeeIndex::TYPE_ONE] = $value['index_' . EmployeeIndex::TYPE_ONE];
  788. $rate = bcdiv($value['index_' . EmployeeIndex::TYPE_ONE],100,2);
  789. $pay_in_advance = bcmul($rate, $value['payment_amount_greater_than_rate'],2);
  790. $dataArray[$key]['pay_in_advance'] = $pay_in_advance;
  791. $dataArray[$key]['basic_salary'] = $value['index_' . EmployeeIndex::TYPE_SIX];
  792. $dataArray[$key]['should_pay'] = bcadd(bcadd($value['index_' . EmployeeIndex::TYPE_EIGHT], $pay_in_advance,2),$value['index_' . EmployeeIndex::TYPE_SIX],2);
  793. unset($dataArray[$key]['fake_id']);
  794. unset($dataArray[$key]['time']);
  795. }
  796. DB::table($this->table_2)->insert($dataArray);
  797. }, 'fake_id');
  798. //更新数据
  799. list($status,$msg) = $this->updateSalaryEmployee($data);
  800. if(! $status) return [false, $msg];
  801. //清理临时表 如果内容不为空
  802. $this->clearTmpTable2();
  803. //释放redis
  804. $this->delTableKey(2);
  805. return [true, '同步成功'];
  806. }
  807. private function getEmployeeIndex($existingData)
  808. {
  809. if (empty($existingData)) {
  810. return collect();
  811. }
  812. // 取出所有涉及的 employee_id 和时间区间
  813. $employeeIds = array_column($existingData, 'employee_id_1');
  814. $time = array_column($existingData, 'time');
  815. $minStart = ! empty($time) ? min($time) : 0;
  816. $maxEnd = ! empty($time) ? max($time) : 0;
  817. // 一次性查出这些员工在最大区间范围内的所有指标
  818. $results = EmployeeIndex::where('del_time', 0)
  819. ->whereIn('type', [EmployeeIndex::TYPE_ONE, EmployeeIndex::TYPE_EIGHT, EmployeeIndex::TYPE_SIX])
  820. ->whereIn('employee_id', $employeeIds)
  821. ->where('start_time', '<=', $maxEnd)
  822. ->where('end_time', '>=', $minStart)
  823. ->select('start_time','end_time','employee_id','index','type')
  824. ->get();
  825. return $results;
  826. }
  827. private function findIndex($indexes, &$value){
  828. // 找到所有符合条件的 index(可能多个 type)
  829. $matchedIndexes = $indexes->filter(function ($item) use ($value) {
  830. return $item['employee_id'] == $value['employee_id_1']
  831. && $item['start_time'] <= $value['time']
  832. && $item['end_time'] >= $value['time'];
  833. });
  834. // 按 type 去重,只保留第一次出现的
  835. $uniqueByType = [];
  836. foreach ($matchedIndexes as $item) {
  837. $index = "index_" . $item['type'];
  838. if (! isset($uniqueByType[$index])) {
  839. $uniqueByType[$index] = $item['index'];
  840. }
  841. }
  842. $value = array_merge($value, $uniqueByType);
  843. }
  844. private function createTmpTable2(){
  845. $table = $this->table_2;
  846. if (! Schema::hasTable($table)) {
  847. // 可以通过 migration 创建,或程序启动时检查
  848. Schema::create($table, function (Blueprint $table) {
  849. $table->bigIncrements('id'); // BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
  850. $table->integer('order_type')->default(0);
  851. $table->integer('order_time')->default(0);
  852. $table->string('employee_id_1_title', 100)->default('');
  853. $table->bigInteger('employee_id_1')->default(0);
  854. $table->decimal('payment_amount', 12, 2)->default(0);
  855. $table->decimal('payment_amount_not_include_activity', 12, 2)->default(0);
  856. $table->decimal('payment_amount_activity', 12, 2)->default(0);
  857. $table->decimal('payment_amount_lower_than_rate', 12, 2)->default(0);
  858. $table->decimal('payment_amount_greater_than_rate', 12, 2)->default(0);
  859. $table->decimal('business', 12, 2)->default(0);
  860. $table->decimal('sale_bonus', 12, 2)->default(0);
  861. $table->decimal('index_1', 10, 2)->default(0);
  862. $table->decimal('pay_in_advance', 12, 2)->default(0);
  863. $table->decimal('basic_salary', 12, 2)->default(0);
  864. $table->decimal('should_pay', 12, 2)->default(0);
  865. });
  866. }
  867. }
  868. public function clearTmpTable2(){
  869. if (Schema::hasTable($this->table_2)) DB::table($this->table_2)->truncate();
  870. }
  871. private function updateSalaryEmployee($data){
  872. try {
  873. $start_timeStamp = $data['start_time'];
  874. $end_timeStamp = $data['end_time'];
  875. $tmpTable = $this->table_2;
  876. $time = time();
  877. $ergs = $data;
  878. DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time, $ergs) {
  879. // 1. 先软删除旧数据(你已有)
  880. SalaryEmployee::where('del_time', 0)
  881. ->where('order_time', '>=', $start_timeStamp)
  882. ->where('order_time', '<=', $end_timeStamp)
  883. ->update(['del_time' => $time]);
  884. // 2. 分批从临时表插入新数据
  885. $batchSize = 500;
  886. $lastId = 0;
  887. do {
  888. $chunk = DB::table($tmpTable)
  889. ->where('id', '>', $lastId)
  890. ->orderBy('id')
  891. ->limit($batchSize)
  892. ->get();
  893. if ($chunk->isEmpty()) {
  894. break;
  895. }
  896. $data = $chunk->map(function ($item) use($time){
  897. return [
  898. 'order_type' => $item->order_type,
  899. 'order_time' => $item->order_time,
  900. 'employee_id_1_title' => $item->employee_id_1_title,
  901. 'employee_id_1' => $item->employee_id_1 ?? 0,
  902. 'payment_amount' => $item->payment_amount,
  903. 'payment_amount_not_include_activity' => $item->payment_amount_not_include_activity,
  904. 'payment_amount_activity' => $item->payment_amount_activity,
  905. 'payment_amount_lower_than_rate' => $item->payment_amount_lower_than_rate,
  906. 'payment_amount_greater_than_rate' => $item->payment_amount_greater_than_rate,
  907. 'business' => $item->business,
  908. 'sale_bonus' => $item->sale_bonus,
  909. 'index_1' => $item->index_1,
  910. 'pay_in_advance' => $item->pay_in_advance,
  911. 'basic_salary' => $item->basic_salary,
  912. 'should_pay' => $item->should_pay,
  913. 'crt_time' => $time,
  914. ];
  915. })->toArray();
  916. // 每批单独插入(可选:加小事务)
  917. SalaryEmployee::insert($data);
  918. // 更新 lastId
  919. $lastId = $chunk->last()->id;
  920. } while ($chunk->count() == $batchSize);
  921. });
  922. }catch (\Throwable $exception){
  923. return [false, "单据明细同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  924. }
  925. return [true, ''];
  926. }
  927. }