123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672 |
- <?php
- namespace app\common\service;
- use app\common\constant\ApplyConstant;
- use app\common\constant\CommonConstant;
- use app\common\model\Approve;
- use app\common\model\ApproveApplyGoods;
- use app\common\model\ApproveCopy;
- use app\common\model\ApproveEvectionPeerUser;
- use app\common\model\ApproveInfo;
- use app\common\model\ApproveInfoLog;
- use app\common\model\ApproveMaintain;
- use app\common\model\ApproveStockGoods;
- use app\common\model\ApproveUseGoods;
- use app\common\model\GoodsStock;
- use app\common\model\GoodsStockLog;
- use app\common\model\User;
- use think\cache\driver\Redis;
- use think\Db;
- use think\Exception;
- /**
- * 审批服务类
- */
- class ApproveService
- {
- /**
- * 审批待处理统计
- *
- * @param mixed $user 用户信息
- **/
- public static function get_count(User $user)
- {
- $get_module_list = CommonConstant::get_module_list();
- $modules = array_keys($get_module_list);
- $userid = $user['userid'];
- $ids = Approve::where('status', CommonConstant::STATUS_2)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->where('approve_user', $userid)
- ->column('info_id');
- $key = [];
- if ($ids) {
- $list = ApproveInfo::field('module,count(id) as number')
- ->where('id', 'in', $ids)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->where('module', 'in', $modules)
- ->where('status', CommonConstant::STATUS_2)
- ->group('module')
- ->select()
- ->toArray();
- $key = $list ? array_column($list, null, 'module') : [];
- }
- $data = [];
- foreach ($get_module_list as $module => $value) {
- $module_text = $value;
- $number = 0;
- if (array_key_exists($module, $key)) {
- $number = $key[$module]['number'];
- }
- $params = compact('module', "module_text", 'number');
- $data[] = $params;
- }
- return $data;
- }
- /**
- * 审批列表
- *
- * @param integer $status 处理状态
- * @param integer $module 模块类型
- * @param string $start_time 申请开始时间
- * @param string $end_time 申请结束时间
- * @param string $search 搜索
- * @param integer $offset 起始位置
- * @param integer $length 查询数量
- * @param mixed $user 用户信息
- **/
- public static function get_list($status, $module, $start_time, $end_time, $search, $offset, $length, $user)
- {
- if (!in_array($status, [1, 2, 3])) {
- return [];
- }
- if ($module && !array_key_exists($module, CommonConstant::get_module_list())) {
- return [];
- }
- $userids = $search && !is_numeric($search) ? User::where('name', 'like', '%' . $search . '%')->column('userid') : [];
- $userid = $user['userid'];
- $group = $status == 3 ? 'approve_copy' : 'approve';
- if ($group == 'approve') {
- $model = Approve::class;
- $field = 'id as approve_id,info_id,status,create_at';
- } else {
- $model = ApproveCopy::class;
- $field = 'id as approve_id,info_id,create_at';
- }
- $aliasName = 'approve.';
- $jobName = 'approveInfo.';
- $joinField = 'module,user_id,apply_user_id,order_no,apply_date,reason,type,desc,start_time,end_time';
- $field = CommonService::getAliasField($field, $aliasName);
- $joinField = CommonService::getAliasField($joinField, $jobName);
- $field = implode(',', array_merge($field, $joinField));
- $list = $model::alias('approve')
- ->field($field)
- ->when($status, function ($query) use ($aliasName, $status) {
- if ($status == 1) {
- $query->where($aliasName . 'status', CommonConstant::STATUS_2);
- // $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_1, CommonConstant::STATUS_2]);
- }
- if ($status == 2) {
- $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4]);
- }
- })
- ->where($aliasName . 'is_deleted', CommonConstant::IS_DELETED_0)
- ->when($group, function ($query) use ($aliasName, $group) {
- if ($group == 'approve') {
- $query->where($aliasName . 'group', CommonConstant::IS_WHO_0);
- }
- })
- ->when($group, function ($query) use ($aliasName, $group) {
- if ($group == 'approve') {
- $query->where($aliasName . 'state', CommonConstant::IS_WHO_0);
- }
- })
- ->where($aliasName . 'approve_user', $userid)
- ->where($jobName . 'is_deleted', CommonConstant::IS_DELETED_0)
- ->when($module, function ($query) use ($jobName, $module) {
- $query->where($jobName . 'module', 'in', $module);
- })
- ->when(!empty($start_time) && !empty($end_time), function ($query) use ($aliasName, $start_time, $end_time) {
- $query->where($aliasName . 'create_at', 'BETWEEN', [$start_time, $end_time]);
- })
- ->when($userids, function ($query) use ($jobName, $userids) {
- $query->where($jobName . 'user_id', 'in', $userids);
- })
- ->when($search, function ($query) use ($search, $userids) {
- if (!$userids) {
- if (!is_numeric($search)) {
- $query->where('order_no|reason', 'like', '%' . $search . '%');
- } else {
- $query->where('order_no', 'like', '%' . $search . '%');
- }
- }
- })
- ->join('__APPROVE_INFO__ approveInfo', 'approve.info_id = approveInfo.id', 'INNER');
- if ($status == 1) {
- // 待处理(待审批 审批中) 才关联 审批人信息
- $list->with([
- 'approveInfoUser' => function ($query) {
- $query->field('userid,name');
- },
- 'approveOne' => function ($query) {
- $query->field('id,info_id,status,approve_user')
- ->where('status', CommonConstant::STATUS_2)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- }
- ]);
- } else {
- $list->with([
- 'approveInfoUser' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- }
- $list = $list->order('approve_id desc')
- ->limit($offset, $length)
- ->select();
- return self::get_array($list);
- }
- public static function get_array($list)
- {
- $apply_goods_info_ids = [];
- $stock_goods_info_ids = [];
- $use_goods_info_ids = [];
- $peer_user_info_ids = [];
- $apply_goods_list = [];
- $stock_goods_list = [];
- $use_goods_list = [];
- $peer_user_list = [];
- foreach ($list as $value) {
- $module = $value['module'];
- switch ($module) {
- case CommonConstant::MODULE_1:
- if ($value['type'] == ApplyConstant::TYPE_1) {
- $apply_goods_info_ids[] = $value['info_id'];
- }
- break;
- case CommonConstant::MODULE_3:
- $stock_goods_info_ids[] = $value['info_id'];
- break;
- case CommonConstant::MODULE_4:
- $use_goods_info_ids[] = $value['info_id'];
- break;
- case CommonConstant::MODULE_5:
- $peer_user_info_ids[] = $value['info_id'];
- break;
- }
- }
- if ($apply_goods_info_ids) {
- $apply_goods_list = ApproveApplyGoods::field('id,info_id,goods_name')
- ->where('info_id', 'in', $apply_goods_info_ids)
- ->select()
- ->toArray();
- }
- if ($stock_goods_info_ids) {
- $stock_goods_list = ApproveStockGoods::field('id,info_id,goods_name')
- ->where('info_id', 'in', $stock_goods_info_ids)
- ->select()
- ->toArray();
- }
- if ($use_goods_info_ids) {
- $use_goods_list = ApproveUseGoods::field('id,info_id,goods_name')
- ->where('info_id', 'in', $use_goods_info_ids)
- ->select()
- ->toArray();
- }
- if ($peer_user_info_ids) {
- $peer_user_list = ApproveEvectionPeerUser::field('id,info_id,name')
- ->where('info_id', 'in', $peer_user_info_ids)
- ->select()
- ->toArray();
- }
- foreach ($list as $value) {
- $module = $value['module'];
- $apply_goods = [];
- $stock_goods = [];
- $use_goods = [];
- $peer_user = [];
- switch ($module) {
- case CommonConstant::MODULE_1:
- if ($value['type'] == ApplyConstant::TYPE_1) {
- foreach ($apply_goods_list as $val) {
- if ($value['info_id'] == $val['info_id']) {
- $apply_goods[] = $val;
- }
- }
- }
- break;
- case CommonConstant::MODULE_3:
- foreach ($stock_goods_list as $val) {
- if ($value['info_id'] == $val['info_id']) {
- $stock_goods[] = $val;
- }
- }
- break;
- case CommonConstant::MODULE_4:
- foreach ($use_goods_list as $val) {
- if ($value['info_id'] == $val['info_id']) {
- $use_goods[] = $val;
- }
- }
- break;
- case CommonConstant::MODULE_5:
- foreach ($peer_user_list as $val) {
- if ($value['info_id'] == $val['info_id']) {
- $peer_user[] = $val;
- }
- }
- break;
- }
- $value['apply_goods'] = $apply_goods;
- $value['stock_goods'] = $stock_goods;
- $value['use_goods'] = $use_goods;
- $value['peer_user'] = $peer_user;
- }
- return $list;
- // $info_ids = [];
- // foreach ($list as $value){
- // if($value['status'] == CommonConstant::STATUS_1) {
- // $info_ids[] = $value['info_id'];
- // }
- // }
- // if($info_ids){
- // $approve_one_list = Approve::field('id,info_id,status,approve_user')
- // ->where('info_id','in',$info_ids)
- // ->where('status',CommonConstant::STATUS_2)
- // ->where('is_deleted', CommonConstant::IS_DELETED_0)
- // ->with(['user' => function ($query) {
- // $query->field('userid,name');
- // }])
- // ->select();
- // $approve_one_data = array_column($approve_one_list, null, 'info_id');
- // foreach ($list as $val){
- // if(array_key_exists($val['info_id'],$approve_one_data)){
- // $val['approve_one'] = $approve_one_data[$val['info_id']];
- // }
- // }
- // }
- }
- /**
- * 详情
- *
- * @param integer $id 审批ID
- * @param mixed $user 用户信息
- * @param string $group 类别:approve=审批,approve_copy=审批抄送
- * @param string $type 类型:detail=详情,info=信息
- **/
- public static function get_detail($id, $user, $group, $type)
- {
- $userid = $user['userid'];
- if ($group == 'approve') {
- $model = Approve::class;
- $field = 'id,info_id,status';
- } else {
- $model = ApproveCopy::class;
- $field = 'id,info_id';
- }
- $info = $model::field($field)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->when($group, function ($query) use ($group) {
- if ($group == 'approve') {
- $query->where('group', CommonConstant::IS_WHO_0);
- }
- })
- ->when($group, function ($query) use ($group) {
- if ($group == 'approve') {
- $query->where('state', CommonConstant::IS_WHO_0);
- }
- })
- ->where('approve_user', $userid)
- ->find($id);
- $data = [];
- $is_maintain = false;
- $is_feedback = false;
- if ($info) {
- $data = ApproveInfoService::get_detail($info['info_id'], $user, CommonConstant::IS_WHO_0, $type);
- if ($data) {
- $data['approve_id'] = $id;
- $data['approve_status'] = isset($info['status']) ? $info['status'] : CommonConstant::STATUS_1;
- if ($group == 'approve') {
- // 条件:维修模块 审批的审批状态是审批中 用户是物业主管或信息负责人 是否显示维修人员:false=不显示,true=显示 目的:审批
- // 条件:维修模块 审批申请的审批状态是审批同意 用户是物业主管或信息负责人 反馈状态 是否显示上传反馈结果:false=不显示,true=显示 目的:上传反馈结果
- if ($data['module'] == CommonConstant::MODULE_8) {
- if ($data['approve_status'] == CommonConstant::STATUS_2) {
- $is_maintain = CommonService::isMaintain($user);
- }
- if ($data['status'] == CommonConstant::STATUS_3) {
- // if (CommonService::isMaintain($user) && $data['maintain_user_id'] > 0 && $data['module_info']['feedback_status'] == CommonConstant::IS_WHO_0) {
- if (CommonService::isMaintain($user) && $data['module_info']['feedback_status'] == CommonConstant::IS_WHO_0) {
- $is_feedback = true;
- }
- }
- }
- }
- $data['is_maintain'] = $is_maintain;
- $data['is_feedback'] = $is_feedback;
- }
- }
- return $data;
- }
- /**
- * 操作方法
- *
- * @param integer $id 审批ID
- * @param array $params 数组
- * @param mixed $user 用户信息
- * @param string $type 类型:audit=审批,edit=修改,feedback=上传反馈结果
- **/
- public static function make($id, $params, $user, $type)
- {
- $userid = $user['userid'];
- $approve = Approve::field('id,info_id,status,approve_user,approve_flow,approve_time')
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->where('approve_user', $userid)
- ->with([
- 'approveInfo' => function ($query) {
- $query->field('is_deleted', true);
- }
- ])
- ->find($id);
- switch ($type) {
- case 'audit':
- if (!$approve) {
- except('审批记录不存在或已删除');
- }
- if ($approve->status != CommonConstant::STATUS_2) {
- except('非待处理状态无法操作');
- }
- $info = $approve->approve_info;
- if (!$info) {
- except('申请记录不存在或已删除');
- }
- if ($info->status != CommonConstant::STATUS_2) {
- except('非待处理状态无法操作!');
- }
- self::audit($approve, $info, $params, $user);
- break;
- case 'edit':
- if (!$approve) {
- except('审批记录不存在或已删除');
- }
- if ($approve->status != CommonConstant::STATUS_2) {
- except('非待处理状态无法操作');
- }
- $info = $approve->approve_info;
- if (!$info) {
- except('申请记录不存在或已删除');
- }
- if ($info->status != CommonConstant::STATUS_2) {
- except('非待处理状态无法操作!');
- }
- $info->module_info;
- if (!$info->module_info) {
- except(CommonConstant::get_module_list()[$params['module']] . '记录不存在或已删除');
- }
- $info = ApproveInfoService::get_item($info, $type);
- self::edit($approve, $info, $params);
- break;
- case 'feedback':
- if (!$approve) {
- except('审批记录不存在或已删除');
- }
- if ($approve->status != CommonConstant::STATUS_3) {
- except('非审批同意状态无法操作');
- }
- $info = $approve->approve_info;
- if (!$info) {
- except('申请记录不存在或已删除');
- }
- if ($info->status != CommonConstant::STATUS_3) {
- except('非审批同意状态无法操作!');
- }
- if ($info->module != CommonConstant::MODULE_8) {
- except(CommonConstant::get_module_list()[CommonConstant::MODULE_8] . '记录不存在或已删除');
- }
- $is_maintain = CommonService::isMaintain($user);
- if (!$is_maintain) {
- except('您没有权限操作');
- }
- // TODO 没有判断反馈状态
- try {
- // 更新维修信息
- $params['maintain_user_id'] = $info->maintain_user_id;
- $params['feedback_status'] = CommonConstant::IS_WHO_1;
- ApproveMaintain::where('info_id', $info->id)->update($params);
- } catch (Exception $e) {
- except('出现错误:' . $e->getMessage());
- }
- break;
- }
- return true;
- }
- /**
- * 审批
- *
- * @param mixed $approve 审批信息
- * @param mixed $info 申请信息
- * @param array $params 数组
- * @param mixed $user 用户信息
- **/
- public static function audit($approve, $info, $params, $user)
- {
- $status = $params['status'];
- $remark = $params['remark'];
- $maintain_user_id = $params['maintain_user_id'];
- $approve_time = date('Y-m-d H:i:s');
- $duration = time() - strtotime($approve['approve_time']);
- $module = $info->module;
- $goodsStockSave = [];
- $goodsStockLog = [];
- $redisStock = [];
- if ($module == CommonConstant::MODULE_4) {
- // 4=领用申请
- $goods = $info->use_goods;
- if (!$goods->toArray()) {
- except('领用商品不存在');
- }
- if ($status == CommonConstant::STATUS_3) {
- if ($approve['approve_flow'] < $info['approve_num']) {
- } else {
- list($goodsStockSave, $goodsStockLog, $redisStock) = ApproveInfoService::composition_data($goods->toArray(), CommonConstant::MODULE_4, CommonConstant::STATUS_3);
- }
- }
- if ($status == CommonConstant::STATUS_4) {
- list($goodsStockSave, $goodsStockLog, $redisStock) = ApproveInfoService::composition_data($goods->toArray(), CommonConstant::MODULE_4, CommonConstant::STATUS_4);
- }
- }
- if ($module == CommonConstant::MODULE_3 && $status == CommonConstant::STATUS_3) {
- // 3=入库申请
- $goods = $info->stock_goods;
- if (!$goods->toArray()) {
- except('入库商品不存在');
- }
- if ($approve['approve_flow'] < $info['approve_num']) {
- } else {
- list($goodsStockSave, $goodsStockLog, $redisStock) = ApproveInfoService::composition_data($goods->toArray(), CommonConstant::MODULE_3, CommonConstant::STATUS_3);
- }
- }
- if ($module == CommonConstant::MODULE_8 && $status == CommonConstant::STATUS_3) {
- // 8=维修申请
- $is_maintain = CommonService::isMaintain($user);
- if ($is_maintain && !$maintain_user_id) {
- except('请选择维修人员');
- }
- // 维修人员ID
- if ($maintain_user_id > 0) {
- $info->maintain_user_id = $maintain_user_id;
- }
- }
- Db::startTrans();
- try {
- if ($goodsStockSave) {
- // 批量更新商品库存
- (new GoodsStock)->saveAll($goodsStockSave);
- }
- if ($goodsStockLog) {
- // 批量添加商品入库出库记录
- GoodsStockLog::insertAll($goodsStockLog);
- }
- // 更新审批申请
- if ($status == CommonConstant::STATUS_3) {
- // 审批流程批次小于审批次数 更新下一级审批状态
- if ($approve['approve_flow'] < $info['approve_num']) {
- Approve::where(['info_id' => $approve['info_id'], 'approve_flow' => $approve['approve_flow'] + 1])->update(['status' => CommonConstant::STATUS_2, 'approve_time' => $approve_time]);
- } else {
- // 审批通过
- $info->status = $status;
- }
- }
- if ($status == CommonConstant::STATUS_4) {
- // 审批驳回
- $info->status = $status;
- }
- $info->cur_num = $info['cur_num'] + 1;
- $info->save();
- // 更新审批
- $approve->status = $status;
- $approve->approve_time = $approve_time;
- $approve->remark = $remark;
- $approve->time = $duration;
- $approve->time_text = get_stay_time($duration);
- $approve->save();
- Db::commit();
- } catch (Exception $e) {
- except('出现错误:' . $e->getMessage(), 2, $e);
- }
- // 4=领用申请 扣除冻结库存
- if ($module == CommonConstant::MODULE_4) {
- if ($redisStock) {
- // 库存缓存记录
- $redis = new Redis(config('redis.'));
- foreach ($redisStock as $key => $value) {
- $redis->dec($value['id'], $value['stock']);
- }
- }
- }
- return true;
- }
- /**
- * 修改
- *
- * @param mixed $approve 审批信息
- * @param mixed $info 申请信息
- * @param array $params 数组
- **/
- public static function edit($approve, $info, $params)
- {
- $module = $info->module;
- $before = $info->toArray();
- $copy_user = $params['copy_user'] ? explode(',', $params['copy_user']) : [];
- $data = [
- 'reason' => $params['reason'],
- 'type' => $params['type'],
- 'desc' => $params['desc'],
- 'start_time' => $params['module'] == CommonConstant::MODULE_6 ? $params['start_time'] . ' ' . $params['start_am'] : $params['start_time'],
- 'end_time' => $params['module'] == CommonConstant::MODULE_6 ? $params['end_time'] . ' ' . $params['end_am'] : $params['end_time'],
- 'apply_id' => $params['apply_id'],
- ];
- Db::startTrans();
- try {
- // 编辑对应模块
- $result = ApproveInfoService::create_module($module, $params, $info->id, $info, $module == CommonConstant::MODULE_4 ? 'edit' : 'update');
- // 编辑抄送人
- ApproveInfoService::create_approve_copy($copy_user, $info->id, 'update');
- // 编辑审批申请
- if ($module == CommonConstant::MODULE_9) {
- // 9=合同呈批 不编辑合同编号
- unset($data['reason'],$data['type']);
- }
- $info->save($data);
- // 保存修改记录
- self::create_approve_info_log($approve->approve_user,$info->id, $module, $before, $info, $result);
- Db::commit();
- } catch (Exception $e) {
- except('出现错误:' . $e->getMessage(), 2, $e);
- }
- // 4=领用申请 领用商品库存扣除重新计算
- if ($module == CommonConstant::MODULE_4) {
- list($goodsStockSave, $redisStock) = [$result['other']['goodsStockSave'],$result['other']['redisStock']];
- if ($goodsStockSave) {
- // 批量更新商品库存
- (new GoodsStock)->saveAll($goodsStockSave);
- }
- if ($redisStock) {
- // 库存缓存记录
- $redis = new Redis(config('redis.'));
- foreach ($redisStock as $key => $value) {
- if($value['exp'] == 'inc'){
- $redis->inc($value['id'], $value['stock']);
- }
- if($value['exp'] == 'dec'){
- $redis->dec($value['id'], $value['stock']);
- }
- }
- }
- }
- return true;
- }
- /**
- * 保存修改记录
- *
- * @param integer $user_id 审批人userid
- * @param integer $info_id 审批申请ID
- * @param array $module 模块类型
- * @param array $before 数组
- * @param array $info 数组
- * @param array $params 数组
- **/
- public static function create_approve_info_log($user_id, $info_id, $module, $before, $info, $params)
- {
- unset($info['module_info'], $info['data'], $info['apply_goods'], $info['stock_goods'], $info['use_goods'], $info['peer_user'], $info['contract'], $info['apply']);
- $info['module_info'] = $params['module_info'];
- $info['data'] = isset($params['other']['data']) && !empty($params['other']['data']) ? $params['other']['data'] : [];
- $data = [
- 'user_id' => $user_id,
- 'info_id' => $info_id,
- 'module' => $module,
- 'before' => json_encode($before, JSON_UNESCAPED_UNICODE),
- 'after' => json_encode($info, JSON_UNESCAPED_UNICODE),
- ];
- ApproveInfoLog::create($data);
- return true;
- }
- }
|