tp6框架的销售统计系统

Seller.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\controller;
  3. use think\App;
  4. use think\facade\View;
  5. use app\model\Seller as SellerModel;
  6. use app\model\Department as DepartmentModel;
  7. class Seller extends Base
  8. {
  9. protected $model;
  10. public function __construct(App $app)
  11. {
  12. parent::__construct($app);
  13. $this->model = new SellerModel();
  14. }
  15. public function index()
  16. {
  17. $list = $this->model->select();
  18. return View::fetch('',['list'=>$list]);
  19. }
  20. public function save()
  21. {
  22. $departmentModel = new DepartmentModel();
  23. $departments = $departmentModel->column('name', 'id');
  24. if ($this->request->isPost()) {
  25. $param = $this->request->param();
  26. try {
  27. if ($param['id']) {
  28. $seller = $this->model->find(intval($param['id']));
  29. } else {
  30. $seller = new SellerModel();
  31. $seller->create_time = time();
  32. }
  33. $seller->name = $param['name'];
  34. $seller->department_id = $param['department_id'];
  35. $seller->department_name = $departments[$param['department_id']];
  36. $seller->save();
  37. } catch (\Exception $e) {
  38. $this->error($e->getMessage());
  39. }
  40. $this->success('操作成功', 'seller/index');
  41. } else {
  42. $id = $this->request->has('id','route') ? intval($this->request->param('id')) : 0;
  43. if ($id) {
  44. $data = $this->model->find($id);
  45. } else {
  46. $data = [
  47. 'id' => 0,
  48. 'name' => '',
  49. 'department_id' => 1,
  50. ];
  51. }
  52. return View::fetch('', [
  53. 'data' => $data,
  54. 'departments' => $departments
  55. ]);
  56. }
  57. }
  58. public function delete($id)
  59. {
  60. if ($this->model->destroy($id)) {
  61. return json(['code'=>2]);
  62. } else {
  63. return json(['code'=>0]);
  64. }
  65. }
  66. }