| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace app\job;
- //use think\Db;
- //use think\Cache;
- //use think\cache\driver\Redis;
- use think\Db;
- use think\Cache;
- use think\Log;
- use think\cache\driver\Redis;
- class InsertDataJob
- {
- // protected $data;
- //
- // public function __construct($data)
- // {
- // $this->data = $data;
- // }
- protected $data;
- protected $taskData;
- public function __construct($data)
- {
- $this->data = $data;
- }
- public function handle()
- {
- // $options = [
- // 'host' => '127.0.0.1',
- // 'port' => 6379,
- // 'password' => '',
- // 'select' => 15,
- // 'timeout' => 0,
- // 'expire' => 0,
- // 'persistent' => false,
- // 'prefix' => '',
- // ];
- // $redis = new Redis($options);
- // $taskIdentifier = md5(json_encode('date'));
- // $handData = [];
- $options = [
- 'host' => '127.0.0.1',
- 'port' => 6379,
- 'password' => '123456',
- 'select' => 15,
- 'timeout' => 0,
- 'expire' => 0,
- 'persistent' => false,
- 'prefix' => '',
- ];
- $redis = new Redis($options);
- // 从任务数据中获取月份
- $date = $this->taskData['date'] ?? '';
- $taskId = $this->taskData['task_id'] ?? 0;
- $taskIdentifier = md5('salary_calculation_' . $date);
- try {
- // 更新任务状态为执行中
- if ($taskId) {
- Db::name('queue_tasks')
- ->where('id', $taskId)
- ->update([
- 'status' => 'processing',
- 'start_time' => date('Y-m-d H:i:s'),
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- }
- // 原有的数据处理逻辑
- $handData = [];
- foreach ($this->data as $key=>$value){
- $handData[$key]['sczl_gdbh'] = $value['sczl_gdbh'];
- $handData[$key]['sczl_yjno'] = $value['sczl_yjno'];
- $handData[$key]['sczl_gxh'] = $value['sczl_gxh'];
- $handData[$key]['sczl_type'] = $value['sczl_type'];
- $handData[$key]['sczl_rq'] = $value['sczl_rq'];
- $handData[$key]['sczl_jtbh'] = $value['sczl_jtbh'];
- $handData[$key]['班组车头产量'] = $value['班组车头产量'];
- $handData[$key]['工价系数'] = $value['工价系数'];
- $handData[$key]['工序难度系数'] = $value['工序难度系数'];
- $handData[$key]['装版工时'] = $value['装版工时'];
- $handData[$key]['保养工时'] = $value['保养工时'];
- $handData[$key]['打样工时'] = $value['打样工时'];
- $handData[$key]['异常停机工时'] = $value['异常停机工时'];
- $handData[$key]['车头产量占用机时'] = $value['车头产量占用机时'];
- $handData[$key]['日定额'] = $value['日定额'];
- $handData[$key]['千件工价'] = $value['千件工价'];
- $handData[$key]['补产标准'] = $value['补产标准'];
- $handData[$key]['班组换算产量'] = $value['班组换算产量'];
- $handData[$key]['计时补差额工资'] = $value['计时补差额工资'];
- $handData[$key]['bh'] = $value['bh'];
- $handData[$key]['xm'] = $value['xm'];
- $handData[$key]['Rate'] = $value['Rate'];
- $handData[$key]['sczl_ms'] = $value['sczl_ms'];
- $handData[$key]['工时占比'] = $value['工时占比'];
- $handData[$key]['达标定额'] = $value['达标定额'];
- $handData[$key]['个人计件工资'] = $value['个人计件工资'];
- $handData[$key]['个人加班工资'] = $value['个人加班工资'];
- $handData[$key]['UniqID'] = $value['UniqID'];
- $handData[$key]['sys_ny'] = $value['sys_ny'];
- $handData[$key]['sys_rq'] = $value['sys_rq'];
- $handData[$key]['sys_id'] = $value['sys_id'];
- $handData[$key]['法定天数'] = $value['法定天数'];
- }
- $sql =Db::name('绩效工资汇总')->fetchSql(true)->insertAll($handData);
- $res = Db::query($sql);
- // if ($res !== false){
- // // 获取队列的键名
- // $queueKey = 'default';
- // // 删除队列
- // Cache::store('redis')->handler()->del($queueKey);
- // $redis->rm($taskIdentifier);
- // }
- if ($res !== false){
- // 更新任务状态为成功
- if ($taskId) {
- Db::name('queue_tasks')
- ->where('id', $taskId)
- ->update([
- 'status' => 'success',
- 'end_time' => date('Y-m-d H:i:s'),
- 'result' => json_encode(['success' => true, 'message' => '工资计算完成']),
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- }
- // 清理缓存
- $queueKey = 'salary_calculation';
- Cache::store('redis')->handler()->del($queueKey);
- $redis->rm($taskIdentifier);
- Log::info('工资计算任务执行成功', ['date' => $date]);
- } else {
- throw new \Exception('数据插入失败');
- }
- } catch (\Exception $e) {
- Log::error('工资计算任务执行失败: ' . $e->getMessage());
- // 更新任务状态为失败
- if ($taskId) {
- Db::name('queue_tasks')
- ->where('id', $taskId)
- ->update([
- 'status' => 'failed',
- 'end_time' => date('Y-m-d H:i:s'),
- 'error' => $e->getMessage(),
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- }
- $redis->rm($taskIdentifier);
- }
- }
- }
|