PayMealsService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Models\PayMealsModel;
  4. use App\Services\BaseService;
  5. /**
  6. * 缴费充值套餐服务
  7. */
  8. class PayMealsService extends BaseService
  9. {
  10. /**
  11. * 获取列表
  12. */
  13. public function getList()
  14. {
  15. $params = request()->all();
  16. $page = $params['page'] ?? 1;
  17. $limit = $params['limit'] ?? 20;
  18. $type = $params['type'] ?? '';
  19. $status = $params['status'] ?? '';
  20. $keyword = $params['keyword'] ?? '';
  21. $query = PayMealsModel::where('mark', 1);
  22. // 类型筛选
  23. if ($type !== '') {
  24. $query->where('type', $type);
  25. }
  26. // 状态筛选
  27. if ($status !== '') {
  28. $query->where('status', $status);
  29. }
  30. // 关键词搜索
  31. if (!empty($keyword)) {
  32. $query->where(function($q) use ($keyword) {
  33. $q->where('money', 'like', "%{$keyword}%")
  34. ->orWhere('remark', 'like', "%{$keyword}%")
  35. ->orWhere('product_id', 'like', "%{$keyword}%");
  36. });
  37. }
  38. $total = $query->count();
  39. $list = $query->orderBy('sort', 'desc')
  40. ->orderBy('id', 'desc')
  41. ->offset(($page - 1) * $limit)
  42. ->limit($limit)
  43. ->get();
  44. // 格式化数据
  45. $result = [];
  46. foreach ($list as $item) {
  47. $data = $item->toArray();
  48. $data['money'] = number_format($data['money'], 2, '.', '');
  49. $data['discount'] = number_format($data['discount'], 2, '.', '');
  50. // 计算实付金额
  51. if ($data['discount'] > 0) {
  52. $data['pay_money'] = number_format($data['money'] * $data['discount'] / 100, 2, '.', '');
  53. } else {
  54. $data['pay_money'] = $data['money'];
  55. }
  56. $data['create_time_text'] = date('Y-m-d H:i:s', (int)$data['create_time']);
  57. $data['update_time_text'] = date('Y-m-d H:i:s', (int)$data['update_time']);
  58. $result[] = $data;
  59. }
  60. return [
  61. 'code' => 0,
  62. 'msg' => '获取成功',
  63. 'data' => $result,
  64. 'count' => $total
  65. ];
  66. }
  67. /**
  68. * 获取详情
  69. */
  70. public function getInfo($id = null)
  71. {
  72. if ($id === null) {
  73. $id = request()->input('id');
  74. }
  75. $info = PayMealsModel::where('id', $id)
  76. ->where('mark', 1)
  77. ->first();
  78. if (!$info) {
  79. return ['code' => 1, 'msg' => '记录不存在'];
  80. }
  81. $info = $info->toArray();
  82. $info['money'] = number_format($info['money'], 2, '.', '');
  83. $info['discount'] = number_format($info['discount'], 2, '.', '');
  84. // 计算实付金额
  85. if ($info['discount'] > 0) {
  86. $info['pay_money'] = number_format($info['money'] * $info['discount'] / 100, 2, '.', '');
  87. } else {
  88. $info['pay_money'] = $info['money'];
  89. }
  90. return [
  91. 'code' => 0,
  92. 'msg' => '获取成功',
  93. 'data' => $info
  94. ];
  95. }
  96. /**
  97. * 添加
  98. */
  99. public function add()
  100. {
  101. $params = request()->all();
  102. $data = [
  103. 'product_id' => (int)($params['product_id'] ?? 0),
  104. 'money' => (float)($params['money'] ?? 0),
  105. 'discount' => (float)($params['discount'] ?? 0),
  106. 'remark' => (string)($params['remark'] ?? ''),
  107. 'type' => (int)($params['type'] ?? 1),
  108. 'sort' => (int)($params['sort'] ?? 0),
  109. 'status' => (int)($params['status'] ?? 1),
  110. 'create_time' => time(),
  111. 'update_time' => time(),
  112. 'mark' => 1
  113. ];
  114. $result = PayMealsModel::insert($data);
  115. if ($result) {
  116. return ['code' => 0, 'msg' => '添加成功'];
  117. }
  118. return ['code' => 1, 'msg' => '添加失败'];
  119. }
  120. /**
  121. * 编辑
  122. */
  123. public function edit()
  124. {
  125. $params = request()->all();
  126. $id = $params['id'] ?? 0;
  127. $meal = PayMealsModel::where('id', $id)
  128. ->where('mark', 1)
  129. ->first();
  130. if (!$meal) {
  131. return ['code' => 1, 'msg' => '记录不存在'];
  132. }
  133. $meal->product_id = $params['product_id'] ?? $meal->product_id;
  134. $meal->money = $params['money'] ?? $meal->money;
  135. $meal->discount = $params['discount'] ?? $meal->discount;
  136. $meal->remark = $params['remark'] ?? $meal->remark;
  137. $meal->sort = $params['sort'] ?? $meal->sort;
  138. $meal->status = $params['status'] ?? $meal->status;
  139. $meal->update_time = time();
  140. $meal->save();
  141. return ['code' => 0, 'msg' => '修改成功'];
  142. }
  143. /**
  144. * 删除
  145. */
  146. public function delete()
  147. {
  148. $id = request()->input('id');
  149. $meal = PayMealsModel::where('id', $id)
  150. ->where('mark', 1)
  151. ->first();
  152. if (!$meal) {
  153. return ['code' => 1, 'msg' => '记录不存在'];
  154. }
  155. $meal->mark = 0;
  156. $meal->save();
  157. return ['code' => 0, 'msg' => '删除成功'];
  158. }
  159. /**
  160. * 批量删除
  161. */
  162. public function deleteAll($ids = null)
  163. {
  164. if ($ids === null) {
  165. $ids = request()->input('ids', []);
  166. }
  167. $count = PayMealsModel::whereIn('id', $ids)
  168. ->where('mark', 1)
  169. ->update(['mark' => 0]);
  170. return [
  171. 'code' => 0,
  172. 'msg' => "成功删除{$count}条记录"
  173. ];
  174. }
  175. /**
  176. * 设置状态
  177. */
  178. public function status()
  179. {
  180. $params = request()->all();
  181. $id = $params['id'] ?? 0;
  182. $status = $params['status'] ?? 1;
  183. $meal = PayMealsModel::where('id', $id)
  184. ->where('mark', 1)
  185. ->first();
  186. if (!$meal) {
  187. return ['code' => 1, 'msg' => '记录不存在'];
  188. }
  189. $meal->status = $status;
  190. $meal->update_time = time();
  191. $meal->save();
  192. return ['code' => 0, 'msg' => '设置成功'];
  193. }
  194. }