Customer.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\AdminModel;
  5. use app\common\model\CustomerModel;
  6. use app\common\model\StoreModel;
  7. use think\App;
  8. use think\facade\View;
  9. use think\Request;
  10. class Customer extends BaseController
  11. {
  12. private $customerModel;
  13. private $storeModel;
  14. private $adminModel;
  15. public function __construct(App $app)
  16. {
  17. parent::__construct($app);
  18. $this->customerModel = new CustomerModel();
  19. $this->storeModel = new StoreModel();
  20. $this->adminModel = new AdminModel();
  21. }
  22. /**
  23. * @param Request $request
  24. * @return \think\response\View
  25. * @throws \think\db\exception\DbException
  26. */
  27. public function index(Request $request) {
  28. $params = $request->param();
  29. $format_params = [
  30. 'name_zh' => format_string($params['name_zh'] ?? null),
  31. 'name_en' => format_string($params['name_en'] ?? null),
  32. 'mobile' => format_string($params['mobile'] ?? null),
  33. ];
  34. $list = $this->customerModel->findByPaginate($format_params);
  35. foreach ($list as &$item) $item['sex_str'] = ['',lang("Man"), lang("Woman"),lang("Unknown")][$item->sex];
  36. View::assign([
  37. 'list' => $list,
  38. 'params' => $format_params
  39. ]);
  40. return view();
  41. }
  42. /**
  43. * @param Request $request
  44. * @return \think\response\Json|\think\response\View
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function add(Request $request) {
  50. if($request->isAjax()) {
  51. $params = $request->param();
  52. $isExist = $this->customerModel->doesItExist($params['mobile']);
  53. if ($isExist)
  54. return $this->fail(lang("Fail to add. Data duplication"));
  55. $admin = $this->adminModel->findById($params['follow_user_id']);
  56. if (!$admin)
  57. return $this->fail(lang("The employees dont exist"));
  58. $store_id = explode(",", $admin->store_ids ?? '-1')[0];
  59. if (!($store_id > 0))
  60. return $this->fail(lang("The employee is not bound to a store"));
  61. $store = $this->storeModel->findById($store_id);
  62. if(!$store)
  63. return $this->fail(lang("Stores do not exist"));
  64. $params['store_id'] = $store->id;
  65. $params['store_abbr'] = $store->abbr;
  66. $params['follow_username'] = $admin->nickname;
  67. $params['create_user_id'] = $admin->id;
  68. $params['create_username'] = $admin->nickname;
  69. $this->customerModel->save($params);
  70. return $this->ok(true);
  71. } else {
  72. View::assign('stores', $this->storeModel->findAllStore());
  73. return view();
  74. }
  75. }
  76. /**
  77. * @param Request $request
  78. * @return \think\response\Json|\think\response\View
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function edit(Request $request) {
  84. $params = $request->param();
  85. if(!isset($params['id']))
  86. return $this->fail(lang('ID not exist'));
  87. $customer = $this->customerModel->findById($params['id']);
  88. if(!$customer)
  89. return $this->fail(lang('"The customer doesn t exist"'));
  90. if ($request->isAjax()) {
  91. $admin = $this->adminModel->findById($params['follow_user_id']);
  92. if (!$admin)
  93. return $this->fail(lang("The employees dont exist"));
  94. $store_id = explode(",", $admin->store_ids ?? '-1')[0];
  95. if (!($store_id > 0))
  96. return $this->fail(lang("The employee is not bound to a store"));
  97. $store = $this->storeModel->findById($store_id);
  98. if(!$store)
  99. return $this->fail(lang("Stores do not exist"));
  100. $params['store_id'] = $store->id;
  101. $params['store_abbr'] = $store->abbr;
  102. $params['follow_username'] = $admin->nickname;
  103. $params['create_user_id'] = $admin->id;
  104. $params['create_username'] = $admin->nickname;
  105. $params['update_time'] = time();
  106. $this->customerModel->where('id',$params['id'])->update($params);
  107. return $this->ok(true);
  108. }
  109. View::assign([
  110. 'stores' => $this->storeModel->findAllStore(),
  111. 'customer' => $customer
  112. ]);
  113. return view();
  114. }
  115. public function delete(Request $request) {
  116. $params = $request->param();
  117. if(!isset($params['ids']))
  118. return $this->fail(lang("Please select the data you want to delete"));
  119. $this->customerModel->deleteByIds(explode(',',$params['ids']));
  120. return $this->ok(true);
  121. }
  122. }