LedgerController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\admin\controller\device;
  3. use app\admin\validate\device\LedgerValidate;
  4. use app\controller\Curd;
  5. use app\model\DeviceLedger;
  6. use support\exception\BusinessException;
  7. use support\Request;
  8. use support\Response;
  9. class LedgerController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new DeviceLedger();
  14. $this->validate = true;
  15. $this->validateClass = new LedgerValidate();
  16. }
  17. /**
  18. * @Desc 列表
  19. * @Author Gorden
  20. * @Date 2024/3/26 11:17
  21. *
  22. * @param Request $request
  23. * @return Response
  24. * @throws \support\exception\BusinessException
  25. */
  26. public function select(Request $request): Response
  27. {
  28. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  29. $order = $request->get('order', 'desc');
  30. $field = $field ?? 'device_ledger_addtimes';
  31. $query = $this->doSelect($where, $field, $order);
  32. return $this->doFormat($query, $format, $limit);
  33. }
  34. public function selectList()
  35. {
  36. $class = get_class($this->model);
  37. $data = $class::where('device_ledger_status', 'ACTIVED')
  38. ->select('device_ledger_id', 'device_ledger_name')
  39. ->get()
  40. ->toArray();
  41. return json_success('', $data);
  42. }
  43. protected function afterQuery($items)
  44. {
  45. foreach ($items as &$item){
  46. if (!empty($item->device_ledger_metadata_json)){
  47. $item->meta_data = json_decode($item->device_ledger_metadata_json);
  48. }
  49. }
  50. return $items;
  51. }
  52. protected function insertInput(Request $request): array
  53. {
  54. $data = $this->inputFilter($request->post());
  55. $data['device_ledger_metadata_json'] = $request->post('meta_data','[]');
  56. return $data;
  57. }
  58. protected function updateInput(Request $request): array
  59. {
  60. //meta_data
  61. $primary_key = $this->model->getKeyName();
  62. $id = $request->post($primary_key);
  63. $data = $this->inputFilter($request->post());
  64. $data['device_ledger_metadata_json'] = $request->post('meta_data','[]');
  65. $model = $this->model->find($id);
  66. if (!$model) {
  67. throw new BusinessException('记录不存在', 2);
  68. }
  69. unset($data[$primary_key]);
  70. return [$id, $data];
  71. }
  72. }