Application.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace app\admin\controller\inspection;
  3. use app\common\controller\Backend;
  4. use app\common\Service\InspectionService;
  5. use app\common\model\inspection\InspectionApplication;
  6. /**
  7. * 验货员申请管理
  8. */
  9. class Application extends Backend
  10. {
  11. /**
  12. * InspectionApplication模型对象
  13. */
  14. protected $model = null;
  15. protected $relationSearch = true;
  16. protected $searchFields = 'name,phone';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new InspectionApplication;
  21. $this->view->assign("auditStatusList", $this->model->getAuditStatusList());
  22. }
  23. /**
  24. * 查看列表
  25. */
  26. public function index()
  27. {
  28. if ($this->request->isAjax()) {
  29. // 设置关联查询
  30. $this->relationSearch = true;
  31. // 构建查询条件
  32. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  33. $list = $this->model
  34. ->with(['user', 'supplier'])
  35. ->where($where)
  36. ->order($sort, $order)
  37. ->paginate($limit);
  38. foreach ($list as $item) {
  39. $item->append(['audit_status_text', 'apply_time_text', 'audit_time_text']);
  40. $item->user->visible(['id', 'username','avatar','nickname', 'mobile']);
  41. if ($item->supplier) {
  42. $item->supplier->visible(['id', 'name', 'contact', 'mobile']);
  43. }
  44. }
  45. $result = array("total" => $list->total(), "rows" => $list->items());
  46. return json($result);
  47. }
  48. return $this->view->fetch();
  49. }
  50. /**
  51. * 查看详情
  52. */
  53. public function detail($ids = null)
  54. {
  55. $row = $this->model->get($ids);
  56. if (!$row) {
  57. $this->error(__('No Results were found'));
  58. }
  59. $row->append(['audit_status_text', 'apply_time_text', 'audit_time_text','supplier_name']);
  60. $this->view->assign("row", $row);
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 编辑申请
  65. */
  66. public function edit($ids = null)
  67. {
  68. $row = $this->model->get($ids);
  69. if (!$row) {
  70. $this->error(__('No Results were found'));
  71. }
  72. // 只有审核中的申请才可以编辑
  73. if ($row->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) {
  74. $this->error('只有审核中的申请才可以编辑');
  75. }
  76. if ($this->request->isPost()) {
  77. $params = $this->request->post("row/a");
  78. if ($params) {
  79. // 验证必填字段
  80. if (empty($params['name']) || empty($params['phone']) || empty($params['address'])) {
  81. $this->error('姓名、手机号、详细地址不能为空');
  82. }
  83. // 检查手机号是否被其他申请使用
  84. if (InspectionApplication::isPhoneApplied($params['phone'], $ids)) {
  85. $this->error('该手机号已被其他用户申请');
  86. }
  87. $params = $this->preExcludeFields($params);
  88. // 过滤不允许编辑的字段
  89. $allowedFields = ['name', 'phone', 'province_adcode', 'city_adcode', 'district_adcode', 'address', 'id_card', 'id_card_front', 'id_card_back'];
  90. $params = array_intersect_key($params, array_flip($allowedFields));
  91. if ($row->allowField($allowedFields)->save($params) !== false) {
  92. $this->success();
  93. } else {
  94. $this->error(__('No rows were updated'));
  95. }
  96. }
  97. $this->error(__('Parameter %s can not be empty', ''));
  98. }
  99. $row->append(['audit_status_text', 'apply_time_text']);
  100. $this->view->assign("row", $row);
  101. return $this->view->fetch();
  102. }
  103. /**
  104. * 审核申请
  105. */
  106. public function audit($ids = null)
  107. {
  108. $row = $this->model->get($ids);
  109. if (!$row) {
  110. $this->error(__('No Results were found'));
  111. }
  112. if ($row->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) {
  113. $this->error('该申请已被审核,无法重复操作');
  114. }
  115. if ($this->request->isPost()) {
  116. $auditStatus = $this->request->post('audit_status');
  117. $rejectReason = $this->request->post('reject_reason', '', 'trim');
  118. $supplierId = $this->request->post('supplier_id', 0, 'intval');
  119. // 审核通过时必须选择供应商
  120. if ($auditStatus == InspectionApplication::AUDIT_STATUS_PASSED && empty($supplierId)) {
  121. $this->error('审核通过时必须选择绑定的供应商');
  122. }
  123. $result = InspectionService::auditApplication($ids, $auditStatus, $rejectReason, $supplierId);
  124. if ($result) {
  125. $this->success('审核成功');
  126. } else {
  127. $this->error('审核失败');
  128. }
  129. }
  130. $row->append(['audit_status_text', 'apply_time_text']);
  131. // 加载供应商信息
  132. if ($row->supplier_id) {
  133. $row->load(['supplier']);
  134. }
  135. $this->view->assign("row", $row);
  136. $this->view->assign("auditStatusList", $this->model->getAuditStatusList());
  137. return $this->view->fetch();
  138. }
  139. /**
  140. * 批量审核通过
  141. */
  142. public function batchPass($ids = null)
  143. {
  144. if (!$this->request->isPost()) {
  145. $this->error(__("Invalid parameters"));
  146. }
  147. $ids = $ids ? $ids : $this->request->post("ids");
  148. if (!$ids) {
  149. $this->error(__('Parameter %s can not be empty', 'ids'));
  150. }
  151. $pk = $this->model->getPk();
  152. $adminIds = $this->getDataLimitAdminIds();
  153. if (is_array($adminIds)) {
  154. $this->model->where($this->dataLimitField, 'in', $adminIds);
  155. }
  156. $list = $this->model->where($pk, 'in', $ids)->select();
  157. $count = 0;
  158. $successIds = [];
  159. \think\Db::startTrans();
  160. try {
  161. foreach ($list as $item) {
  162. if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) {
  163. $item->audit_status = InspectionApplication::AUDIT_STATUS_PASSED;
  164. $item->audit_time = time();
  165. $item->reject_reason = '';
  166. $item->save();
  167. $count++;
  168. $successIds[] = $item->id;
  169. }
  170. }
  171. \think\Db::commit();
  172. } catch (\Exception $e) {
  173. \think\Db::rollback();
  174. $this->error($e->getMessage());
  175. }
  176. $this->success("成功审核通过{$count}个申请");
  177. }
  178. /**
  179. * 批量审核驳回
  180. */
  181. public function batchReject($ids = null)
  182. {
  183. if (!$this->request->isPost()) {
  184. $this->error(__("Invalid parameters"));
  185. }
  186. $ids = $ids ? $ids : $this->request->post("ids");
  187. $rejectReason = $this->request->post("reject_reason", '', 'trim');
  188. if (!$ids) {
  189. $this->error(__('Parameter %s can not be empty', 'ids'));
  190. }
  191. if (empty($rejectReason)) {
  192. $this->error('驳回原因不能为空');
  193. }
  194. $pk = $this->model->getPk();
  195. $adminIds = $this->getDataLimitAdminIds();
  196. if (is_array($adminIds)) {
  197. $this->model->where($this->dataLimitField, 'in', $adminIds);
  198. }
  199. $list = $this->model->where($pk, 'in', $ids)->select();
  200. $count = 0;
  201. \think\Db::startTrans();
  202. try {
  203. foreach ($list as $item) {
  204. if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) {
  205. $item->audit_status = InspectionApplication::AUDIT_STATUS_REJECTED;
  206. $item->audit_time = time();
  207. $item->reject_reason = $rejectReason;
  208. $item->save();
  209. $count++;
  210. }
  211. }
  212. \think\Db::commit();
  213. } catch (\Exception $e) {
  214. \think\Db::rollback();
  215. $this->error($e->getMessage());
  216. }
  217. $this->success("成功驳回{$count}个申请");
  218. }
  219. /**
  220. * 删除
  221. */
  222. public function del($ids = "")
  223. {
  224. if (!$this->request->isPost()) {
  225. $this->error(__("Invalid parameters"));
  226. }
  227. $ids = $ids ? $ids : $this->request->post("ids");
  228. if ($ids) {
  229. $pk = $this->model->getPk();
  230. $adminIds = $this->getDataLimitAdminIds();
  231. if (is_array($adminIds)) {
  232. $this->model->where($this->dataLimitField, 'in', $adminIds);
  233. }
  234. $list = $this->model->where($pk, 'in', $ids)->select();
  235. $count = 0;
  236. \think\Db::startTrans();
  237. try {
  238. foreach ($list as $k => $v) {
  239. $count += $v->delete();
  240. }
  241. \think\Db::commit();
  242. } catch (\PDOException $e) {
  243. \think\Db::rollback();
  244. $this->error($e->getMessage());
  245. } catch (\Exception $e) {
  246. \think\Db::rollback();
  247. $this->error($e->getMessage());
  248. }
  249. if ($count) {
  250. $this->success();
  251. } else {
  252. $this->error(__('No rows were deleted'));
  253. }
  254. }
  255. $this->error(__('Parameter %s can not be empty', 'ids'));
  256. }
  257. }