FarmGoodsController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\admin\controller\life;
  3. use app\admin\service\goods\GoodsService;
  4. use app\admin\validate\goods\GoodsValidate;
  5. use support\Request;
  6. use support\Response;
  7. class FarmGoodsController
  8. {
  9. /**
  10. * @Desc 添加商品
  11. * @Author Gorden
  12. * @Date 2024/3/11 10:21
  13. *
  14. * @param Request $request
  15. * @return Response
  16. */
  17. public function insert(Request $request): Response
  18. {
  19. $validate = new GoodsValidate();
  20. if (!$validate->scene('add')->check($request->post())) {
  21. return json_fail($validate->getError());
  22. }
  23. return GoodsService::insert($request->post());
  24. }
  25. public function update(Request $request): Response
  26. {
  27. $validate = new GoodsValidate();
  28. if (!$validate->scene('update')->check($request->post())) {
  29. return json_fail($validate->getError());
  30. }
  31. return GoodsService::update($request->post());
  32. }
  33. /**
  34. * @Desc 修改商品状态
  35. * @Author Gorden
  36. * @Date 2024/2/28 10:35
  37. *
  38. * @param Request $request
  39. * @return Response
  40. */
  41. public function updateStatus(Request $request)
  42. {
  43. return $this->updateField($request->post('goods_id'), 'goods_status', $request->post('goods_status'));
  44. }
  45. /**
  46. * @Desc 执行插入数据
  47. * @Author Gorden
  48. * @Date 2024/2/28 10:10
  49. *
  50. * @param array $data
  51. * @return mixed|null
  52. */
  53. protected function doInsert(array $data)
  54. {
  55. $primary_key = $this->model->getKeyName();
  56. $model_class = get_class($this->model);
  57. $model = new $model_class;
  58. $data['goods_content'] = htmlspecialchars($data['goods_content']);
  59. foreach ($data as $key => $val) {
  60. $model->{$key} = $val;
  61. }
  62. $model->save();
  63. return $primary_key ? $model->$primary_key : null;
  64. }
  65. }