123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\common\service;
- use app\common\constant\CommonConstant;
- use app\common\model\Approve;
- use app\common\model\User;
- use think\Db;
- use think\Exception;
- /**
- * 审批服务类
- */
- class ApproveService
- {
- /**
- * 审批列表
- *
- * @param $status
- * @param $module
- * @param $start_time
- * @param $end_time
- * @param $search
- * @param $offset
- * @param $length
- * @param $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'];
- $aliasName = 'approve.';
- $jobName = 'approveInfo.';
- $field = 'id,info_id,status,create_at';
- $joinField = 'user_id,order_no,apply_date,reason,type,desc,start_time,end_time';
- $field = self::getAliasField($field, $aliasName);
- $joinField = self::getAliasField($joinField, $jobName);
- $field = implode(',', array_merge($field, $joinField));
- $list = Approve::alias('approve')
- ->field($field)
- ->when($status, function ($query) use ($aliasName, $status) {
- if ($status == 1) {
- $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)
- ->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)
- ->with(['user' => function ($query) {
- $query->field('userid,name');
- }]);
- }]);
- } else {
- $list->with(['approveInfoUser' => function ($query) {
- $query->field('userid,name');
- }]);
- }
- $list = $list->limit($offset, $length)
- ->order('id desc')
- ->select();
- // $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']];
- // }
- // }
- // }
- return $list;
- }
- /**
- * 详情
- *
- * @param integer $id
- * @param mixed $user
- * @param string $type
- **/
- public static function get_detail($id, $user,$type)
- {
- $userid = $user['userid'];
- $info = Approve::field('info_id,status')
- ->where('approve_user', $userid)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($id);
- $data = [];
- if ($info) {
- $data = ApproveInfoService::get_detail($info['info_id'], '', $type);
- $data['approve_id'] = $id;
- $data['approve_status'] = $info['status'];
- }
- return $data;
- }
- /**
- * 拼接查询字段
- */
- public static function getAliasField($field, $aliasName)
- {
- $field = explode(',', $field);
- foreach ($field as &$value) {
- $value = $aliasName . $value;
- }
- return $field;
- }
- }
|