TSpaceService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Service;
  3. use App\Model\TSpaceSet;
  4. use Illuminate\Support\Facades\DB;
  5. class TSpaceService extends Service
  6. {
  7. public function add($data, $user){
  8. return [true, ''];
  9. }
  10. public function edit($data, $user){
  11. list($status, $msg) = $this->constructionEditOtherRule($data, $user);
  12. if(! $status) return [false, $msg];
  13. DB::beginTransaction();
  14. try{
  15. $set = TSpaceSet::where('del_time',0)
  16. ->where('type', $data['type'])
  17. ->select('file', 'id')
  18. ->get()->toArray();
  19. $set_map = array_column($set,'file','id');
  20. $time = time();
  21. $insert = $update = $new = $old = $id = [];
  22. foreach ($data['data'] as $value){
  23. $text = "";
  24. if(! empty($value['text'])) $text = json_encode($data['text']);
  25. if(! empty($value['id'])){
  26. $file = $set_map[$value['id']] ?? "";
  27. if($value['file'] != $file){
  28. $old[] = $file;
  29. }else{
  30. $new[] = $value['file'];
  31. }
  32. $update[] = [
  33. 'id' => $value['id'],
  34. 'type' => $data['type'],
  35. 'text' => $text,
  36. 'file' => $value['file'],
  37. 'crt_time' => $time,
  38. ];
  39. $id[] = $value['id'];
  40. }else{
  41. $new[] = $value['file'];
  42. $insert[] = [
  43. 'type' => $data['type'],
  44. 'text' => $text,
  45. 'file' => $value['file'],
  46. 'crt_time' => $time,
  47. ];
  48. }
  49. }
  50. foreach ($set as $value){
  51. if(! in_array($value['id'], $id)){
  52. $update[] = [
  53. 'id' => $value['id'],
  54. 'del_time' => $time,
  55. ];
  56. $old[] = $value['file'];
  57. }
  58. }
  59. if(! empty($update)){
  60. foreach ($update as $value){
  61. TSpaceSet::where('id',$value['id'])
  62. ->update($value);
  63. }
  64. }
  65. if(! empty($insert)) TSpaceSet::insert($insert);
  66. DB::commit();
  67. }catch (\Exception $exception){
  68. DB::rollBack();
  69. return [false, $exception->getMessage()];
  70. }
  71. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  72. }
  73. public function constructionEditOtherRule($data,$user){
  74. if(empty($data['type'])) return [false,'TSpace首页设置类型不能为空'];
  75. if(! isset(TSpaceSet::$model_type_title[$data['type']])) return [false,'TSpace首页设置类型错误'];
  76. if(empty($data['data'])) return [false, '设置内容不能为空'];
  77. foreach ($data['data'] as $value){
  78. if(empty($value['file'])) return [false, '文件不能为空'];
  79. }
  80. return [true, ''];
  81. }
  82. public function detail($data, $user){
  83. if(empty($data['table_head'])) return [false,'自定义表头不能为空'];
  84. if(empty($data['menu_id'])) return [false, 'menu_id不能为空'];
  85. return [true,''];
  86. }
  87. }