Store.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\DepartmentModel;
  5. use app\common\model\StoreModel;
  6. use think\App;
  7. use think\facade\View;
  8. use think\Request;
  9. class Store extends BaseController
  10. {
  11. private $storeModel;
  12. private $departmentModel;
  13. public function __construct(App $app)
  14. {
  15. $this->storeModel = new StoreModel();
  16. $this->departmentModel = new DepartmentModel();
  17. parent::__construct($app);
  18. }
  19. public function index(Request $request) {
  20. $params = $request->param();
  21. View::assign([
  22. 'list' => $this->storeModel->findByPaginate($params),
  23. 'params' => $params,
  24. ]);
  25. return view();
  26. }
  27. public function add(Request $request) {
  28. if($request->isAjax()) {
  29. $params = $request->param();
  30. list($name,$abbr,$address) = [$params['name'],$params['abbr'],$params['address']];
  31. $store = $this->storeModel->doesItExist($abbr);
  32. if ($store)
  33. return $this->fail(lang('Fail to add. Data duplication'));
  34. $last_id = $this->storeModel->insert([
  35. 'name' => $name,
  36. 'abbr' => $abbr,
  37. 'address' => $address,
  38. 'update_time' => time(),
  39. 'create_time' => time()
  40. ],true);
  41. $this->departmentModel->save([
  42. 'name' => $name,
  43. 'store_id' => $last_id,
  44. 'pid' => 5,
  45. 'describe' => $name
  46. ]);
  47. return $this->ok($last_id);
  48. }
  49. return view();
  50. }
  51. public function edit(Request $request) {
  52. $params = $request->param();
  53. if(!isset($params['id']))
  54. return $this->fail(lang('ID not exist'));
  55. $store = $this->storeModel->findById($params['id']);
  56. if (!$store)
  57. return $this->fail(lang('Data not exist'));
  58. if ($request->isAjax()) {
  59. $exist_store = $this->storeModel->doesItExist($params['abbr']);
  60. if ($exist_store && $exist_store->id != $params['id'])
  61. return $this->fail(lang('Data duplication'));
  62. $res = $this->storeModel->where('id',$params['id'])->update([
  63. 'name' => $params['name'],
  64. 'abbr' => $params['abbr'],
  65. 'address' => $params['address'],
  66. 'update_time' => time()
  67. ]);
  68. $this->departmentModel->where('store_id', $store->id)->update([
  69. 'name' => $params['name'],
  70. 'describe' => $params['name']
  71. ]);
  72. return $this->ok($res);
  73. }
  74. View::assign('store', $store);
  75. return view();
  76. }
  77. public function delete(Request $request) {
  78. $params = $request->param();
  79. if(!isset($params['ids']))
  80. return $this->fail(lang("Please select the data you want to delete"));
  81. $this->storeModel->deleteByIds(explode(',', $params['ids']));
  82. $this->departmentModel->where('store_id', 'in', $params['ids'])->delete();
  83. return $this->ok(true);
  84. }
  85. public function total_data(Request $request) {
  86. $params = $request->param();
  87. if(!isset($params['id']))
  88. return $this->fail(lang('ID not exist'));
  89. $store = $this->storeModel->findById($params['id']);
  90. if (!$store)
  91. return $this->fail(lang('Data not exist'));
  92. View::assign('store', $store);
  93. return view();
  94. }
  95. }