123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace app\admin\controller\inspection;
- use app\common\controller\Backend;
- use app\common\Service\InspectionService;
- use app\common\model\inspection\InspectionApplication;
- /**
- * 验货员申请管理
- */
- class Application extends Backend
- {
- /**
- * InspectionApplication模型对象
- */
- protected $model = null;
- protected $relationSearch = true;
- protected $searchFields = 'name,phone';
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new InspectionApplication;
- $this->view->assign("auditStatusList", $this->model->getAuditStatusList());
- }
- /**
- * 查看列表
- */
- public function index()
- {
- if ($this->request->isAjax()) {
- // 设置关联查询
- $this->relationSearch = true;
-
- // 构建查询条件
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
-
- $list = $this->model
- ->with(['user', 'supplier'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $item) {
- $item->append(['audit_status_text', 'apply_time_text', 'audit_time_text']);
- $item->user->visible(['id', 'username','avatar','nickname', 'mobile']);
- if ($item->supplier) {
- $item->supplier->visible(['id', 'name', 'contact', 'mobile']);
- }
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 查看详情
- */
- public function detail($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $row->append(['audit_status_text', 'apply_time_text', 'audit_time_text','supplier_name']);
-
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 编辑申请
- */
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- // 只有审核中的申请才可以编辑
- if ($row->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) {
- $this->error('只有审核中的申请才可以编辑');
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- // 验证必填字段
- if (empty($params['name']) || empty($params['phone']) || empty($params['address'])) {
- $this->error('姓名、手机号、详细地址不能为空');
- }
- // 检查手机号是否被其他申请使用
- if (InspectionApplication::isPhoneApplied($params['phone'], $ids)) {
- $this->error('该手机号已被其他用户申请');
- }
- $params = $this->preExcludeFields($params);
-
- // 过滤不允许编辑的字段
- $allowedFields = ['name', 'phone', 'province_adcode', 'city_adcode', 'district_adcode', 'address', 'id_card', 'id_card_front', 'id_card_back'];
- $params = array_intersect_key($params, array_flip($allowedFields));
-
- if ($row->allowField($allowedFields)->save($params) !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $row->append(['audit_status_text', 'apply_time_text']);
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 审核申请
- */
- public function audit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- if ($row->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) {
- $this->error('该申请已被审核,无法重复操作');
- }
- if ($this->request->isPost()) {
- $auditStatus = $this->request->post('audit_status');
- $rejectReason = $this->request->post('reject_reason', '', 'trim');
- $supplierId = $this->request->post('supplier_id', 0, 'intval');
- // 审核通过时必须选择供应商
- if ($auditStatus == InspectionApplication::AUDIT_STATUS_PASSED && empty($supplierId)) {
- $this->error('审核通过时必须选择绑定的供应商');
- }
- $result = InspectionService::auditApplication($ids, $auditStatus, $rejectReason, $supplierId);
-
- if ($result) {
- $this->success('审核成功');
- } else {
- $this->error('审核失败');
- }
-
- }
- $row->append(['audit_status_text', 'apply_time_text']);
-
- // 加载供应商信息
- if ($row->supplier_id) {
- $row->load(['supplier']);
- }
-
- $this->view->assign("row", $row);
- $this->view->assign("auditStatusList", $this->model->getAuditStatusList());
- return $this->view->fetch();
- }
- /**
- * 批量审核通过
- */
- public function batchPass($ids = null)
- {
- if (!$this->request->isPost()) {
- $this->error(__("Invalid parameters"));
- }
- $ids = $ids ? $ids : $this->request->post("ids");
- if (!$ids) {
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- $pk = $this->model->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $this->model->where($this->dataLimitField, 'in', $adminIds);
- }
- $list = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- $successIds = [];
- \think\Db::startTrans();
- try {
- foreach ($list as $item) {
- if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) {
- $item->audit_status = InspectionApplication::AUDIT_STATUS_PASSED;
- $item->audit_time = time();
- $item->reject_reason = '';
- $item->save();
- $count++;
- $successIds[] = $item->id;
- }
- }
- \think\Db::commit();
- } catch (\Exception $e) {
- \think\Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success("成功审核通过{$count}个申请");
- }
- /**
- * 批量审核驳回
- */
- public function batchReject($ids = null)
- {
- if (!$this->request->isPost()) {
- $this->error(__("Invalid parameters"));
- }
- $ids = $ids ? $ids : $this->request->post("ids");
- $rejectReason = $this->request->post("reject_reason", '', 'trim');
-
- if (!$ids) {
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
-
- if (empty($rejectReason)) {
- $this->error('驳回原因不能为空');
- }
- $pk = $this->model->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $this->model->where($this->dataLimitField, 'in', $adminIds);
- }
- $list = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- \think\Db::startTrans();
- try {
- foreach ($list as $item) {
- if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) {
- $item->audit_status = InspectionApplication::AUDIT_STATUS_REJECTED;
- $item->audit_time = time();
- $item->reject_reason = $rejectReason;
- $item->save();
- $count++;
- }
- }
- \think\Db::commit();
- } catch (\Exception $e) {
- \think\Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success("成功驳回{$count}个申请");
- }
- /**
- * 删除
- */
- public function del($ids = "")
- {
- if (!$this->request->isPost()) {
- $this->error(__("Invalid parameters"));
- }
- $ids = $ids ? $ids : $this->request->post("ids");
- if ($ids) {
- $pk = $this->model->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $this->model->where($this->dataLimitField, 'in', $adminIds);
- }
- $list = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- \think\Db::startTrans();
- try {
- foreach ($list as $k => $v) {
- $count += $v->delete();
- }
- \think\Db::commit();
- } catch (\PDOException $e) {
- \think\Db::rollback();
- $this->error($e->getMessage());
- } catch (\Exception $e) {
- \think\Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success();
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- }
|