InsertDataJob.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\job;
  3. //use think\Db;
  4. //use think\Cache;
  5. //use think\cache\driver\Redis;
  6. use think\Db;
  7. use think\Cache;
  8. use think\Log;
  9. use think\cache\driver\Redis;
  10. class InsertDataJob
  11. {
  12. // protected $data;
  13. //
  14. // public function __construct($data)
  15. // {
  16. // $this->data = $data;
  17. // }
  18. protected $data;
  19. protected $taskData;
  20. public function __construct($data)
  21. {
  22. $this->data = $data;
  23. }
  24. public function handle()
  25. {
  26. // $options = [
  27. // 'host' => '127.0.0.1',
  28. // 'port' => 6379,
  29. // 'password' => '',
  30. // 'select' => 15,
  31. // 'timeout' => 0,
  32. // 'expire' => 0,
  33. // 'persistent' => false,
  34. // 'prefix' => '',
  35. // ];
  36. // $redis = new Redis($options);
  37. // $taskIdentifier = md5(json_encode('date'));
  38. // $handData = [];
  39. $options = [
  40. 'host' => '127.0.0.1',
  41. 'port' => 6379,
  42. 'password' => '123456',
  43. 'select' => 15,
  44. 'timeout' => 0,
  45. 'expire' => 0,
  46. 'persistent' => false,
  47. 'prefix' => '',
  48. ];
  49. $redis = new Redis($options);
  50. // 从任务数据中获取月份
  51. $date = $this->taskData['date'] ?? '';
  52. $taskId = $this->taskData['task_id'] ?? 0;
  53. $taskIdentifier = md5('salary_calculation_' . $date);
  54. try {
  55. // 更新任务状态为执行中
  56. if ($taskId) {
  57. Db::name('queue_tasks')
  58. ->where('id', $taskId)
  59. ->update([
  60. 'status' => 'processing',
  61. 'start_time' => date('Y-m-d H:i:s'),
  62. 'update_time' => date('Y-m-d H:i:s')
  63. ]);
  64. }
  65. // 原有的数据处理逻辑
  66. $handData = [];
  67. foreach ($this->data as $key=>$value){
  68. $handData[$key]['sczl_gdbh'] = $value['sczl_gdbh'];
  69. $handData[$key]['sczl_yjno'] = $value['sczl_yjno'];
  70. $handData[$key]['sczl_gxh'] = $value['sczl_gxh'];
  71. $handData[$key]['sczl_type'] = $value['sczl_type'];
  72. $handData[$key]['sczl_rq'] = $value['sczl_rq'];
  73. $handData[$key]['sczl_jtbh'] = $value['sczl_jtbh'];
  74. $handData[$key]['班组车头产量'] = $value['班组车头产量'];
  75. $handData[$key]['工价系数'] = $value['工价系数'];
  76. $handData[$key]['工序难度系数'] = $value['工序难度系数'];
  77. $handData[$key]['装版工时'] = $value['装版工时'];
  78. $handData[$key]['保养工时'] = $value['保养工时'];
  79. $handData[$key]['打样工时'] = $value['打样工时'];
  80. $handData[$key]['异常停机工时'] = $value['异常停机工时'];
  81. $handData[$key]['车头产量占用机时'] = $value['车头产量占用机时'];
  82. $handData[$key]['日定额'] = $value['日定额'];
  83. $handData[$key]['千件工价'] = $value['千件工价'];
  84. $handData[$key]['补产标准'] = $value['补产标准'];
  85. $handData[$key]['班组换算产量'] = $value['班组换算产量'];
  86. $handData[$key]['计时补差额工资'] = $value['计时补差额工资'];
  87. $handData[$key]['bh'] = $value['bh'];
  88. $handData[$key]['xm'] = $value['xm'];
  89. $handData[$key]['Rate'] = $value['Rate'];
  90. $handData[$key]['sczl_ms'] = $value['sczl_ms'];
  91. $handData[$key]['工时占比'] = $value['工时占比'];
  92. $handData[$key]['达标定额'] = $value['达标定额'];
  93. $handData[$key]['个人计件工资'] = $value['个人计件工资'];
  94. $handData[$key]['个人加班工资'] = $value['个人加班工资'];
  95. $handData[$key]['UniqID'] = $value['UniqID'];
  96. $handData[$key]['sys_ny'] = $value['sys_ny'];
  97. $handData[$key]['sys_rq'] = $value['sys_rq'];
  98. $handData[$key]['sys_id'] = $value['sys_id'];
  99. $handData[$key]['法定天数'] = $value['法定天数'];
  100. }
  101. $sql =Db::name('绩效工资汇总')->fetchSql(true)->insertAll($handData);
  102. $res = Db::query($sql);
  103. // if ($res !== false){
  104. // // 获取队列的键名
  105. // $queueKey = 'default';
  106. // // 删除队列
  107. // Cache::store('redis')->handler()->del($queueKey);
  108. // $redis->rm($taskIdentifier);
  109. // }
  110. if ($res !== false){
  111. // 更新任务状态为成功
  112. if ($taskId) {
  113. Db::name('queue_tasks')
  114. ->where('id', $taskId)
  115. ->update([
  116. 'status' => 'success',
  117. 'end_time' => date('Y-m-d H:i:s'),
  118. 'result' => json_encode(['success' => true, 'message' => '工资计算完成']),
  119. 'update_time' => date('Y-m-d H:i:s')
  120. ]);
  121. }
  122. // 清理缓存
  123. $queueKey = 'salary_calculation';
  124. Cache::store('redis')->handler()->del($queueKey);
  125. $redis->rm($taskIdentifier);
  126. Log::info('工资计算任务执行成功', ['date' => $date]);
  127. } else {
  128. throw new \Exception('数据插入失败');
  129. }
  130. } catch (\Exception $e) {
  131. Log::error('工资计算任务执行失败: ' . $e->getMessage());
  132. // 更新任务状态为失败
  133. if ($taskId) {
  134. Db::name('queue_tasks')
  135. ->where('id', $taskId)
  136. ->update([
  137. 'status' => 'failed',
  138. 'end_time' => date('Y-m-d H:i:s'),
  139. 'error' => $e->getMessage(),
  140. 'update_time' => date('Y-m-d H:i:s')
  141. ]);
  142. }
  143. $redis->rm($taskIdentifier);
  144. }
  145. }
  146. }