Data.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\platform;
  10. use app\admin\service\Service;
  11. use app\common\models\Data as DataModel;
  12. use app\common\models\DataItem as DataItemModel;
  13. class Data extends Service {
  14. /**
  15. * 获取列表数据
  16. * @param $data
  17. * @return array
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public static function getList($data){
  23. $count = DataModel::count();
  24. $result = DataModel::page($data["page"]??1,$data["limit"]??10)->order('id','desc')->select()->toArray();
  25. return ["count"=>$count, "data"=>$result];
  26. }
  27. /**
  28. * 获取详情数据
  29. * @param $id
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public static function detail($id){
  36. return [
  37. "data"=>DataModel::where(["id"=>$id])->find() ?? [],
  38. "marketing"=>DataItemModel::where('pid',$id)->order('sort','ASC')->select()->toArray() ?? []
  39. ];
  40. }
  41. /**
  42. * 保存数据
  43. * @param array $data
  44. * @return bool
  45. */
  46. public static function save($data=[]){
  47. if(DataModel::where("id",$data["id"])->count()){
  48. DataModel::where("id",$data["id"])->save($data);
  49. }else{
  50. unset($data["id"]);
  51. $data["id"] = DataModel::create($data)->id;
  52. }
  53. $marketing = $data['marketing']; $i=0;
  54. foreach($marketing['id'] as $key=>$value){
  55. $arr = [
  56. "pid"=>$data['id'],
  57. "name"=>!empty($marketing["name"][$key]) ? $marketing["name"][$key] : "",
  58. "url"=>!empty($marketing["url"][$key]) ? $marketing["url"][$key] : "",
  59. "photo"=>!empty($marketing["photo"][$key]) ? $marketing["photo"][$key] : "",
  60. "sort"=>$i,
  61. "target"=>!empty($marketing["target"][$key]) ? $marketing["target"][$key] : 0
  62. ];
  63. if(DataItemModel::where("id",$value)->count()){
  64. $in[] = $value;
  65. DataItemModel::where("id",$value)->save($arr);
  66. }else{
  67. $in[] = DataItemModel::create($arr)->id;
  68. }
  69. $i++;
  70. }
  71. if(!empty($in)){
  72. DataItemModel::where("pid",$data['id'])->where("id","not in",$in)->delete();
  73. }
  74. return true;
  75. }
  76. /**
  77. * 删除
  78. * @param $id
  79. * @return bool
  80. * @throws \Exception
  81. */
  82. public static function delete($id){
  83. if(!DataModel::where("id",$id)->delete()){
  84. throw new \Exception("删除失败,请稍后重试!",0);
  85. }
  86. DataItemModel::where(["pid"=>$id])->delete();
  87. return true;
  88. }
  89. }