Application.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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'])
  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', 'nickname', 'mobile']);
  41. }
  42. $result = array("total" => $list->total(), "rows" => $list->items());
  43. return json($result);
  44. }
  45. return $this->view->fetch();
  46. }
  47. /**
  48. * 查看详情
  49. */
  50. public function detail($ids = null)
  51. {
  52. $row = $this->model->get($ids);
  53. if (!$row) {
  54. $this->error(__('No Results were found'));
  55. }
  56. $row->append(['audit_status_text', 'apply_time_text', 'audit_time_text']);
  57. $this->view->assign("row", $row);
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 审核申请
  62. */
  63. public function audit($ids = null)
  64. {
  65. $row = $this->model->get($ids);
  66. if (!$row) {
  67. $this->error(__('No Results were found'));
  68. }
  69. if ($row->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) {
  70. $this->error('该申请已被审核,无法重复操作');
  71. }
  72. if ($this->request->isPost()) {
  73. $auditStatus = $this->request->post('audit_status');
  74. $rejectReason = $this->request->post('reject_reason', '', 'trim');
  75. try {
  76. InspectionService::auditApplication($ids, $auditStatus, $rejectReason);
  77. $this->success('审核成功');
  78. } catch (\Exception $e) {
  79. $this->error($e->getMessage());
  80. }
  81. }
  82. $row->append(['audit_status_text', 'apply_time_text']);
  83. $this->view->assign("row", $row);
  84. $this->view->assign("auditStatusList", $this->model->getAuditStatusList());
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 批量审核通过
  89. */
  90. public function batchPass($ids = null)
  91. {
  92. if (!$this->request->isPost()) {
  93. $this->error(__("Invalid parameters"));
  94. }
  95. $ids = $ids ? $ids : $this->request->post("ids");
  96. if (!$ids) {
  97. $this->error(__('Parameter %s can not be empty', 'ids'));
  98. }
  99. $pk = $this->model->getPk();
  100. $adminIds = $this->getDataLimitAdminIds();
  101. if (is_array($adminIds)) {
  102. $this->model->where($this->dataLimitField, 'in', $adminIds);
  103. }
  104. $list = $this->model->where($pk, 'in', $ids)->select();
  105. $count = 0;
  106. $successIds = [];
  107. \think\Db::startTrans();
  108. try {
  109. foreach ($list as $item) {
  110. if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) {
  111. $item->audit_status = InspectionApplication::AUDIT_STATUS_PASSED;
  112. $item->audit_time = time();
  113. $item->reject_reason = '';
  114. $item->save();
  115. $count++;
  116. $successIds[] = $item->id;
  117. }
  118. }
  119. \think\Db::commit();
  120. } catch (\Exception $e) {
  121. \think\Db::rollback();
  122. $this->error($e->getMessage());
  123. }
  124. $this->success("成功审核通过{$count}个申请");
  125. }
  126. /**
  127. * 批量审核驳回
  128. */
  129. public function batchReject($ids = null)
  130. {
  131. if (!$this->request->isPost()) {
  132. $this->error(__("Invalid parameters"));
  133. }
  134. $ids = $ids ? $ids : $this->request->post("ids");
  135. $rejectReason = $this->request->post("reject_reason", '', 'trim');
  136. if (!$ids) {
  137. $this->error(__('Parameter %s can not be empty', 'ids'));
  138. }
  139. if (empty($rejectReason)) {
  140. $this->error('驳回原因不能为空');
  141. }
  142. $pk = $this->model->getPk();
  143. $adminIds = $this->getDataLimitAdminIds();
  144. if (is_array($adminIds)) {
  145. $this->model->where($this->dataLimitField, 'in', $adminIds);
  146. }
  147. $list = $this->model->where($pk, 'in', $ids)->select();
  148. $count = 0;
  149. \think\Db::startTrans();
  150. try {
  151. foreach ($list as $item) {
  152. if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) {
  153. $item->audit_status = InspectionApplication::AUDIT_STATUS_REJECTED;
  154. $item->audit_time = time();
  155. $item->reject_reason = $rejectReason;
  156. $item->save();
  157. $count++;
  158. }
  159. }
  160. \think\Db::commit();
  161. } catch (\Exception $e) {
  162. \think\Db::rollback();
  163. $this->error($e->getMessage());
  164. }
  165. $this->success("成功驳回{$count}个申请");
  166. }
  167. /**
  168. * 删除
  169. */
  170. public function del($ids = "")
  171. {
  172. if (!$this->request->isPost()) {
  173. $this->error(__("Invalid parameters"));
  174. }
  175. $ids = $ids ? $ids : $this->request->post("ids");
  176. if ($ids) {
  177. $pk = $this->model->getPk();
  178. $adminIds = $this->getDataLimitAdminIds();
  179. if (is_array($adminIds)) {
  180. $this->model->where($this->dataLimitField, 'in', $adminIds);
  181. }
  182. $list = $this->model->where($pk, 'in', $ids)->select();
  183. $count = 0;
  184. \think\Db::startTrans();
  185. try {
  186. foreach ($list as $k => $v) {
  187. $count += $v->delete();
  188. }
  189. \think\Db::commit();
  190. } catch (\PDOException $e) {
  191. \think\Db::rollback();
  192. $this->error($e->getMessage());
  193. } catch (\Exception $e) {
  194. \think\Db::rollback();
  195. $this->error($e->getMessage());
  196. }
  197. if ($count) {
  198. $this->success();
  199. } else {
  200. $this->error(__('No rows were deleted'));
  201. }
  202. }
  203. $this->error(__('Parameter %s can not be empty', 'ids'));
  204. }
  205. }