PayMealsService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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', strtotime($data['create_time']));
  57. $data['update_time_text'] = date('Y-m-d H:i:s', strtotime($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. 'phone_type' => (int)($params['phone_type'] ?? 1),
  109. 'phone_service' => (int)($params['phone_service'] ?? 1),
  110. 'electric_type' => (int)($params['electric_type'] ?? 1),
  111. 'gas_type' => (int)($params['gas_type'] ?? 1),
  112. 'sort' => (int)($params['sort'] ?? 0),
  113. 'status' => (int)($params['status'] ?? 1),
  114. 'create_time' => time(),
  115. 'update_time' => time(),
  116. 'mark' => 1
  117. ];
  118. $result = PayMealsModel::insert($data);
  119. if ($result) {
  120. return ['code' => 0, 'msg' => '添加成功'];
  121. }
  122. return ['code' => 1, 'msg' => '添加失败'];
  123. }
  124. /**
  125. * 编辑
  126. */
  127. public function edit()
  128. {
  129. $params = request()->all();
  130. $id = $params['id'] ?? 0;
  131. $meal = PayMealsModel::where('id', $id)
  132. ->where('mark', 1)
  133. ->first();
  134. if (!$meal) {
  135. return ['code' => 1, 'msg' => '记录不存在'];
  136. }
  137. $meal->product_id = $params['product_id'] ?? $meal->product_id;
  138. $meal->money = $params['money'] ?? $meal->money;
  139. $meal->discount = $params['discount'] ?? $meal->discount;
  140. $meal->remark = isset($params['remark']) ? $params['remark'] : '';
  141. $meal->phone_type = $params['phone_type'] ?? $meal->phone_type;
  142. $meal->phone_service = $params['phone_service'] ?? $meal->phone_service;
  143. $meal->electric_type = $params['electric_type'] ?? $meal->electric_type;
  144. $meal->gas_type = $params['gas_type'] ?? $meal->gas_type;
  145. $meal->sort = $params['sort'] ?? $meal->sort;
  146. $meal->status = $params['status'] ?? $meal->status;
  147. $meal->update_time = time();
  148. $meal->save();
  149. return ['code' => 0, 'msg' => '修改成功'];
  150. }
  151. /**
  152. * 删除
  153. */
  154. public function delete()
  155. {
  156. $id = request()->input('id');
  157. $meal = PayMealsModel::where('id', $id)
  158. ->where('mark', 1)
  159. ->first();
  160. if (!$meal) {
  161. return ['code' => 1, 'msg' => '记录不存在'];
  162. }
  163. $meal->mark = 0;
  164. $meal->save();
  165. return ['code' => 0, 'msg' => '删除成功'];
  166. }
  167. /**
  168. * 批量删除
  169. */
  170. public function deleteAll($ids = null)
  171. {
  172. if ($ids === null) {
  173. $ids = request()->input('ids', []);
  174. }
  175. $count = PayMealsModel::whereIn('id', $ids)
  176. ->where('mark', 1)
  177. ->update(['mark' => 0]);
  178. return [
  179. 'code' => 0,
  180. 'msg' => "成功删除{$count}条记录"
  181. ];
  182. }
  183. /**
  184. * 设置状态
  185. */
  186. public function status()
  187. {
  188. $params = request()->all();
  189. $id = $params['id'] ?? 0;
  190. $status = $params['status'] ?? 1;
  191. $meal = PayMealsModel::where('id', $id)
  192. ->where('mark', 1)
  193. ->first();
  194. if (!$meal) {
  195. return ['code' => 1, 'msg' => '记录不存在'];
  196. }
  197. $meal->status = $status;
  198. $meal->update_time = time();
  199. $meal->save();
  200. return ['code' => 0, 'msg' => '设置成功'];
  201. }
  202. }