SalesmanController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\admin\controller\medical;
  3. use support\exception\BusinessException;
  4. use support\Request;
  5. use app\admin\validate\medical\SalesmanValidate;
  6. use app\controller\Curd;
  7. use app\model\MedicalSalesman;
  8. use support\Response;
  9. class SalesmanController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new MedicalSalesman();
  14. $this->validate = true;
  15. $this->validateClass = new SalesmanValidate();
  16. }
  17. public function select(Request $request): Response
  18. {
  19. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  20. $order = $request->get('order', 'desc');
  21. $field = $field ?? 'salesman_addTime';
  22. $where['salesman_is_del'] = 0;
  23. $query = $this->doSelect($where, $field, $order);
  24. return $this->doFormat($query, $format, $limit);
  25. }
  26. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  27. {
  28. $model = $this->model->with([
  29. 'shop' => function ($query) {
  30. $query->select('shop_id', 'shop_name');
  31. }
  32. ]);
  33. foreach ($where as $column => $value) {
  34. if (is_array($value)) {
  35. if ($value[0] === 'like' || $value[0] === 'not like') {
  36. $model = $model->where($column, $value[0], "%$value[1]%");
  37. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  38. $model = $model->where($column, $value[0], $value[1]);
  39. } elseif ($value[0] == 'in' && !empty($value[1])) {
  40. $valArr = $value[1];
  41. if (is_string($value[1])) {
  42. $valArr = explode(",", trim($value[1]));
  43. }
  44. $model = $model->whereIn($column, $valArr);
  45. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  46. $valArr = $value[1];
  47. if (is_string($value[1])) {
  48. $valArr = explode(",", trim($value[1]));
  49. }
  50. $model = $model->whereNotIn($column, $valArr);
  51. } elseif ($value[0] == 'null') {
  52. $model = $model->whereNull($column);
  53. } elseif ($value[0] == 'not null') {
  54. $model = $model->whereNotNull($column);
  55. } elseif ($value[0] !== '' || $value[1] !== '') {
  56. $model = $model->whereBetween($column, $value);
  57. }
  58. } else {
  59. $model = $model->where($column, $value);
  60. }
  61. }
  62. if ($field) {
  63. $model = $model->orderBy($field, $order);
  64. }
  65. return $model;
  66. }
  67. protected function afterQuery($items)
  68. {
  69. foreach ($items as &$item) {
  70. $item->salesman_status = strval($item->salesman_status);
  71. unset($item->salesman_password);
  72. }
  73. return $items;
  74. }
  75. /**
  76. * @Desc insert 数据处理
  77. * @Author Gorden
  78. * @Date 2024/3/4 9:57
  79. *
  80. * @param Request $request
  81. * @return array
  82. * @throws \support\exception\BusinessException
  83. */
  84. protected function insertInput(Request $request): array
  85. {
  86. $data = $this->inputFilter($request->post());
  87. // 验证手机号是否已存在
  88. if ($this->model->phoneExist($data['salesman_phone'])) {
  89. throw new BusinessException('手机号已存在');
  90. }
  91. // 用户已存在
  92. if ($this->model->usernameExist($data['salesman_username'])) {
  93. throw new BusinessException('用户名已存在');
  94. }
  95. return $data;
  96. }
  97. /**
  98. * @Desc 执行写入数据
  99. * @Author Gorden
  100. * @Date 2024/3/4 9:39
  101. *
  102. * @param $data
  103. * @return mixed|null
  104. */
  105. protected function doInsert($data)
  106. {
  107. $data['salesman_salt'] = random_string(6);
  108. $data['salesman_password'] = md5(sha1($data['salesman_password'] . $data['salesman_salt']));
  109. $primary_key = $this->model->getKeyName();
  110. $model_class = get_class($this->model);
  111. $model = new $model_class;
  112. foreach ($data as $key => $val) {
  113. $model->{$key} = $val;
  114. }
  115. $model->save();
  116. return $primary_key ? $model->$primary_key : null;
  117. }
  118. /**
  119. * @Desc 数据处理
  120. * @Author Gorden
  121. * @Date 2024/3/4 10:17
  122. *
  123. * @param Request $request
  124. * @return array
  125. * @throws BusinessException
  126. */
  127. protected function updateInput(Request $request): array
  128. {
  129. $primary_key = $this->model->getKeyName();
  130. $id = $request->post($primary_key);
  131. $data = $this->inputFilter($request->post());
  132. $model = $this->model->find($id);
  133. if (!$model) {
  134. throw new BusinessException('记录不存在', 2);
  135. }
  136. // 其他账号下是否存在当前手机号
  137. if ($model->phoneIsInOtherAccountExist($data['salesman_phone'], $data['salesman_id'])) {
  138. throw new BusinessException('手机号已存在');
  139. }
  140. // 其他账号下是否存在当前用户名
  141. if ($model->usernameIsInOtherAccountExist($data['salesman_username'], $data['salesman_id'])) {
  142. throw new BusinessException('用户名已存在');
  143. }
  144. $passwordFiled = 'salesman_password';
  145. if (isset($data[$passwordFiled])) {
  146. // 密码为空,则不更新密码
  147. if ($data[$passwordFiled] === '') {
  148. unset($data[$passwordFiled]);
  149. } else {
  150. $data[$passwordFiled] = md5(sha1($data[$passwordFiled] . $model->salesman_slat));
  151. }
  152. }
  153. unset($data[$primary_key]);
  154. return [$id, $data];
  155. }
  156. /**
  157. * @Desc 修改状态
  158. * @Author Gorden
  159. * @Date 2024/3/4 10:20
  160. *
  161. * @param Request $request
  162. * @return \support\Response
  163. */
  164. public function updateStatus(Request $request)
  165. {
  166. return $this->updateField($request->post('salesman_id'), 'salesman_status', $request->post('salesman_status'));
  167. }
  168. /**
  169. * @Desc 软删除
  170. * @Author Gorden
  171. * @Date 2024/3/4 10:22
  172. *
  173. * @param Request $request
  174. * @return Response
  175. * @throws BusinessException
  176. */
  177. public function delete(Request $request): Response
  178. {
  179. $ids = $this->deleteInput($request);
  180. $this->doSoftDelete($ids, ['salesman_is_del' => 1]);
  181. return json_success('success');
  182. }
  183. }