PayMealsService.php 6.7 KB

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