ServiceLogic.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\ServiceModel;
  5. use think\Exception;
  6. use think\exception\ValidateException;
  7. use think\facade\Db;
  8. use think\response\Json;
  9. class ServiceLogic extends BaseLogic
  10. {
  11. //获取服务列表
  12. public static function list(array $data = []): Json
  13. {
  14. $db = ServiceModel::alias('a')
  15. ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal)
  16. ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal)
  17. ->where('a.is_del', CommonModel::$del_normal);
  18. if ($data['status'] != '') $db->where('a.status', $data['status']);
  19. if ($data['activity_status'] != '') $db->where('a.activity_status', $data['activity_status']);
  20. if ($data['company_title'] != '') $db->whereLike('b.title', '%' . $data['company_title'] . '%');
  21. if ($data['card_title'] != '') $db->whereLike('c.title', '%' . $data['card_title'] . '%');
  22. $count = $db->count('a.id');
  23. $list = $db->field('a.id,b.title company_title,c.title card_title,a.title,a.original_price,a.activity_price,a.starttime,a.endtime,a.status,a.addtime')
  24. ->page($data['page'], $data['size'])
  25. ->order(['a.id' => 'desc'])
  26. ->select()
  27. ->toArray();
  28. return json_show(CommonModel::$success, '获取服务列表成功', ['count' => $count, 'list' => $list]);
  29. }
  30. //添加服务
  31. public static function add(array $data = []): Json
  32. {
  33. $rs = ServiceModel::field('id')
  34. ->where([
  35. 'is_del' => CommonModel::$del_normal,
  36. 'title' => $data['title'],
  37. ])
  38. ->findOrEmpty()
  39. ->isEmpty();
  40. if (!$rs) return json_show(CommonModel::$error_param, '该服务已存在');
  41. $date = date('Y-m-d H:i:s');
  42. $data = array_merge($data, [
  43. 'activity_status' => ServiceModel::$activity_status_not_started,
  44. 'status' => CommonModel::$status_normal,
  45. 'is_del' => CommonModel::$del_normal,
  46. 'createrid' => self::$uid,
  47. 'creater' => self::$uname,
  48. 'addtime' => $date,
  49. 'updaterid' => self::$uid,
  50. 'updater' => self::$uname,
  51. 'updatetime' => $date,
  52. 'endtime' => date('Y-m-d 23:59:59', strtotime($data['endtime'])),
  53. 'expiretime' => date('Y-m-d 23:59:59', strtotime($data['expiretime'])),
  54. ]);
  55. $res = ServiceModel::create($data)->save();
  56. return $res ? json_show(CommonModel::$success, '添加服务成功') : json_show(CommonModel::$error_param, '添加服务失败');
  57. }
  58. //获取服务详情
  59. public static function read(int $id = 0): Json
  60. {
  61. $res = ServiceModel::alias('a')
  62. ->field('a.*,b.title company_title,c.title card_title')
  63. ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal)
  64. ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal)
  65. ->where(['a.id' => $id, 'a.is_del' => CommonModel::$del_normal])
  66. ->findOrEmpty()
  67. ->toArray();
  68. return empty($res) ? json_show(CommonModel::$error_param, '该服务为空') : json_show(CommonModel::$success, '获取服务详情成功', $res);
  69. }
  70. //编辑服务
  71. public static function edit(array $data = []): Json
  72. {
  73. $rs = ServiceModel::field('id,title')
  74. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  75. ->findOrEmpty();
  76. if ($rs->isEmpty()) return json_show(CommonModel::$error_param, '该服务不存在');
  77. if (($rs->title != $data['title'])) {
  78. $temp = ServiceModel::field('id')
  79. ->where(['is_del' => CommonModel::$del_normal, 'title' => $data['title']])
  80. ->findOrEmpty()
  81. ->isEmpty();
  82. if (!$temp) return json_show(CommonModel::$error_param, '该分组名称已存在');
  83. }
  84. $date = date('Y-m-d H:i:s');
  85. $res = ServiceModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  86. ->save(array_merge($data, ['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uname]));
  87. return $res ? json_show(CommonModel::$success, '编辑服务成功') : json_show(CommonModel::$success, '编辑服务失败');
  88. }
  89. //服务启禁用
  90. public static function status(array $data = []): Json
  91. {
  92. $res = ServiceModel::where('id', $data['id'])
  93. ->where('status', '<>', $data['status'])
  94. ->save([
  95. 'status' => $data['status'],
  96. 'updatetime' => date('Y-m-d H:i:s'),
  97. 'updaterid' => self::$uid,
  98. 'updater' => self::$uname
  99. ]);
  100. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '该服务不存在或重复操作');
  101. }
  102. //删除服务
  103. public static function delete(array $ids = []): Json
  104. {
  105. Db::startTrans();
  106. try {
  107. $res = ServiceModel::whereIn('id', $ids)
  108. ->where('is_del', CommonModel::$del_normal)
  109. ->save([
  110. 'is_del' => CommonModel::$del_deleted,
  111. 'updatetime' => date('Y-m-d H:i:s'),
  112. 'updaterid' => self::$uid,
  113. 'updater' => self::$uname
  114. ]);
  115. if (!$res) throw new Exception('该服务不存在');
  116. Db::name('good_group_item')
  117. ->whereIn('good_group_id', $ids)
  118. ->where('is_del', CommonModel::$del_normal)
  119. ->update([
  120. 'is_del' => CommonModel::$del_deleted,
  121. 'updatetime' => date('Y-m-d H:i:s')
  122. ]);
  123. Db::commit();
  124. return json_show(CommonModel::$success, '删除成功');
  125. } catch (Exception $exception) {
  126. Db::rollback();
  127. return json_show(CommonModel::$error_param, '删除失败,' . $exception->getMessage());
  128. }
  129. }
  130. }