123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- namespace app\admin\controller\borrow;
- use app\admin\model\Borrow as BorrowModel;
- use app\admin\model\BorrowAccess;
- use app\admin\model\BorrowNumber;
- use app\common\controller\Backend;
- use app\common\library\xmwechat\offiaccount\OaService;
- use ba\Random;
- use modules\notification\Notification;
- use think\facade\Db;
- use app\common\controller\SnowflakeId;
- use Throwable;
- class BorrowApplication extends Backend
- {
- protected array|string $quickSearchField = ['username', 'mobile'];
- public function initialize(): void
- {
- parent::initialize();
- $this->model = new BorrowModel;
- $this->BorrowAccess = new BorrowAccess();
- $this->BorrowNumber = new BorrowNumber();
- $this->BorrowTools = new BorrowTools($this->app);
- }
- public function index(): void
- {
- if ($this->request->param('select')) {
- $this->select();
- }
- $user_id = $this->auth->id;
- $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
- list($where, $alias, $limit, $order) = $this->queryBuilder();
- // halt($where, $alias, $limit, $order);
- if ($user_group_id == 1 || $user_group_id == 2) {
- $res = $this->model
- ->alias($alias)
- ->where($where)
- ->where("status", 0)
- ->order($order)
- ->paginate($limit);
- } else if ($user_group_id == 3) {
- $res = $this->model
- ->alias($alias)
- ->where($where)
- ->where("status", 6)
- ->where('purpose', 0)
- ->order($order)
- ->paginate($limit);
- }
- $this->success('', [
- 'list' => $res->items(),
- 'total' => $res->total(),
- 'remark' => get_route_remark(),
- ]);
- }
- // public function add(): void
- // {
- // if ($this->request->isPost()) {
- // $data = $this->request->post();
- // if (!$data) {
- // $this->error(__('Parameter %s can not be empty', ['']));
- // }
- // if (array_key_exists('borrow_reason', $data) && $data['borrow_reason'] !== null) {
- // $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8');
- // }
- // if (array_key_exists('remarks', $data) && $data['remarks'] !== null) {
- // $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8');
- // }
- //
- // if(!isset($data['asset']) || $data['asset'] == []){
- // $this->error("请先填写借用仪器!!");
- // }else{
- // $isHasAsset = false;
- // foreach ($data['asset'] as $datum) {
- // if($datum['model'] !== '' && $datum['num'] !== 0){
- // $isHasAsset = true;
- // }
- // }
- // if (!$isHasAsset){
- // $this->error("请先填写借用仪器!!");
- // }
- // }
- // //校验数据
- // $uniID = new SnowflakeId;
- // $data['user_id'] = $this->auth->id;
- //
- // //尝试存储
- // try {
- // $data['status'] = 0;
- // $data['encoding'] = $uniID->generateParticle();
- // // halt($data);
- // $result = $this->model->save($data);
- // if (array_key_exists('asset', $data)) {
- // $groupAccess = [];
- // foreach ($data['asset'] as $datum) {
- // $groupAccess[] = [
- // 'borrow_id' => $this->model->id,
- // 'asset_name' => $datum['model'],
- // 'origin' => $datum['origin'],
- // 'num' => $datum['num']
- // ];
- // }
- // $this->BorrowNumber->saveAll($groupAccess);
- // }
- // $this->model->commit();
- // } catch (Throwable $e) {
- // $this->model->rollback();
- // $this->error($e->getMessage());
- // }
- //
- // if ($result !== false) {
- // $this->success(__('Added successfully'));
- // } else {
- // $this->error(__('No rows were added'));
- // }
- // }
- // $this->error(__('Parameter error'));
- // }
- public function del($ids = null): void
- {
- if (!$this->request->isDelete() || !$ids) {
- $this->error(__('Parameter error'));
- }
- $pk = $this->model->getPk();
- $data = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- $this->model->startTrans();
- try {
- foreach ($data as $v) {
- $count += $v->delete();
- $this->BorrowAccess->where('borrow_id', 'in', $ids)->select();
- Db::name('borrow_number')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
- Db::name('borrow_access')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
- }
- $this->model->commit();
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success(__('Deleted successfully'));
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- public function edit($id = null): void
- {
- $row = $this->model->find($id);
- if (!$row) {
- $this->error(__('Record not found'));
- }
- if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6) {
- $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
- } else {
- $rows = $this->BorrowAccess->field('*,asset_name as model')->where('borrow_id', $id)->select();
- }
- $row['asset'] = $rows;
- $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
- if ($this->request->isPost()) {
- $data = $this->request->post();
- $data = $this->BorrowTools->checkText($data);
- $data = $this->excludeFields($data);
- //获取用户类型
- $user_id = $this->auth->id;
- $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
- if($user_id == $data['user_id'] && $user_group_id == 4){
- $data['annotation'] = '';
- $data['college_annotation'] = '';
- }
- $open_id = Db::name('oauth_log')->where('user_id', $data['user_id'])->value('opid');
- if ($open_id !== null) {
- $text = [
- "openid" => $open_id,
- "pagepath" => "pages/login/index",
- "data" => [
- 'character_string1' => $id,
- 'thing5' => $this->auth->nickname,
- ]
- ];
- }
- $this->model->startTrans();
- $result = false;
- try {
- //判断是否是科研类借取并判断是否需要领导审核
- if ($data['purpose'] == 0) {
- $text['data']['const3'] = '科研借单';
- if ($user_group_id !== 3) {
- $data['approval_person'] = $this->auth->nickname;
- $data['approval_person_id'] = $this->auth->id;
- if ($data['status'] == 2) {
- $text['data']['const4'] = ['审批通过'];
- $data['status'] = 6;
- } else if ($data['status'] == 1) {
- $text['data']['const4'] = ['审批未通过'];
- $data['status'] = 1;
- }
- $result = $this->model->update($data);
- } else {
- $data['college_leader_id'] = $user_id;
- $data['college_leader'] = $this->auth->nickname;
- if ($data['status'] == '1') {
- $text['data']['const4'] = ['审批未通过'];
- $data['status'] = 7;
- } else {
- $text['data']['const4'] = ['审批通过'];
- }
- $result = $this->model->update($data);
- if ($data['status'] == 2) {
- foreach ($data['asset'] as $v) {
- for ($i = 0; $i < $v['num']; $i++) {
- $this->BorrowAccess->insert(
- [
- 'asset_name' => $v['model'],
- 'origin' => $v['origin'],
- 'borrow_id' => $data['id'],
- 'status' => -1,
- ]
- );
- }
- }
- }
- }
- } else {
- $data['approval_person'] = $this->auth->nickname;
- $data['approval_person_id'] = $this->auth->id;
- $text['data']['const3'] = ['教学借单'];
- if ($data['status'] == 2) {
- $text['data']['const4'] = ['审批通过'];
- } else if ($data['status'] == 1) {
- $text['data']['const4'] = ['审批未通过'];
- }
- $result = $this->model->update($data);
- if ($data['status'] == 2) {
- foreach ($data['asset'] as $v) {
- for ($i = 0; $i < $v['num']; $i++) {
- $this->BorrowAccess->insert(
- [
- 'asset_name' => $v['model'],
- 'origin' => $v['origin'],
- 'borrow_id' => $data['id'],
- 'status' => -1,
- ]
- );
- }
- }
- }
- }
- // 提交事务
- $this->model->commit();
- } catch (Throwable $e) {
- // 回滚事务
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $open_id = Db::name('oauth_log')->where('user_id', $data['user_id'])->value('opid');
- if ($open_id !== null) {
- // halt($text);
- $res = self::wxResultMessage($text);
- if ($res === true) {
- $this->success(__('Update successful'));
- } else {
- $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
- }
- }else{
- $this->success('更新成功!但对方未配置公众号,消息提示失败!');
- }
- } else {
- $this->error(__('No rows updated'));
- }
- }
- $this->success('', [
- 'row' => $row
- ]);
- }
- /**
- * 仪器预约审核结果通知
- * @param
- * @return void
- * @throws \Exception
- */
- public function wxResultMessage($res): bool
- {
- $openid = $res["openid"];
- // halt($res);
- $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
- $data = [
- 'character_string1' => ['value' => $res["data"]["character_string1"]],
- 'const3' => ['value' => [$res["data"]["const3"]]],
- 'const4' => ['value' => $res["data"]["const4"]],
- 'thing5' => ['value' => $res["data"]["thing5"]],
- // 'time9' => ['value' => date('Y-m-d H:i')],
- ];
- $url = 'http://weixin.qq.com/download';
- $miniprogram = [
- 'appid' => 'wx58633590ab59b7a1',
- 'pagepath' => "pages/login/index"
- ];
- $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
- return $result;
- }
- }
|