PlatCat.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\listener;
  4. use app\admin\model\Cat;
  5. use app\admin\model\CatPlat;use app\admin\model\Platform;
  6. class PlatCat
  7. {
  8. /**
  9. * 事件监听处理
  10. *
  11. * @return mixed
  12. */
  13. public function handle($event)
  14. {
  15. // if($event['info']=='cat'){
  16. // $this->cat($event);
  17. // }
  18. // if($event['info']=='plat'){
  19. // $this->plat($event);
  20. // }
  21. }
  22. public function cat($event){
  23. $info = Cat::where(['id'=>$event['data']['id']??[]])->field('id cat_id,fund_code,status,is_del,creater apply_name,createrid apply_id,addtime,updatetime')->select();
  24. if($info->isEmpty())return;
  25. $plat = Platform::where(['platform_type' => 1, 'is_del' => 0])->field('id')->select();
  26. if(!$plat->isEmpty()){
  27. $data = [];
  28. foreach ($info->toArray() as $item){
  29. $temp= array_map(function($v)use($item){
  30. $item['id'] = CatPlat::where(['cat_id' => $item['cat_id'], 'platform_id' => $v['id']])->value('id',null);
  31. $item['platform_id'] = $v['id'];
  32. return $item;
  33. },$plat->toArray());
  34. $data = array_merge($data,$temp);
  35. }
  36. if(!empty($data)) $this->save($data);
  37. }
  38. }
  39. /**
  40. * 批量保存
  41. * @param $data
  42. */
  43. protected function save($data){
  44. if(!empty($data)){
  45. $model = new CatPlat();
  46. try{
  47. $model->saveAll($data);
  48. }catch(\Exception $e){
  49. throw new \Exception($e->getMessage());
  50. }
  51. }
  52. }
  53. public function plat($event){
  54. $info = Platform::where(['id'=>$event['data']['id']??[],'platform_type' => 1, 'is_del' => 0])->field('id platform_id,platform_type')->select();
  55. if($info->isEmpty())return;
  56. $cat = Cat::where(['is_del' => 0])->field('id cat_id,fund_code,status,is_del,creater apply_name,createrid apply_id,addtime,updatetime')->select();
  57. if(!$cat->isEmpty()){
  58. $data = [];
  59. foreach ($info->toArray() as$item){
  60. $temp= array_map(function($v)use($item){
  61. $item['id'] = CatPlat::where(['cat_id' => $v['cat_id'], 'platform_id' => $item['platform_id']])->value('id',null);
  62. $item['cat_id'] = $v['id'];
  63. return $item;
  64. },$cat->toArray());
  65. $data = array_merge($data,$temp);
  66. }
  67. if(!empty($data)) $this->save($data);
  68. }
  69. }
  70. }