package com.tuoheng.admin.service.inspection.query;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.DataPermissionEnum;
import com.tuoheng.admin.enums.code.inspection.QueryInspectionListServiceEnum;
import com.tuoheng.admin.enums.code.inspectionfile.ListByDeptUserTypeEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.inspection.InspectionListVo;
import com.tuoheng.common.core.enums.ServiceExceptionEnum;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Stream;

/**
 * @Author ChengWang
 * @Date 2022/12/2
 */
@Service
@Slf4j
public class QueryNewInspectionListService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private UserMapper userMapper;


    public JsonResult getList() {
        //获取登录用户信息
        User user = CurrentUserUtil.getUserInfo();
        String tenantId = user.getTenantId();
        //判断用户角色 1超级管理员 2部门管理员 3普通用户
        if (null == user.getDataPermission()) {
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        //若角色为超级管理员，查询任务表最新五条数据
        if (DataPermissionEnum.ALL.getCode() == user.getDataPermission()) {
            List<Inspection> inspectionList = inspectionMapper.selectList(Wrappers.<Inspection>lambdaQuery()
                    .eq(Inspection::getTenantId, tenantId)
                    .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                    .orderByDesc(Inspection::getCreateTime)
                    .last("limit 5"));
            if (CollectionUtil.isEmpty(inspectionList)) {
                return JsonResult.success();
            }
            if (DataPermissionEnum.ALL.getCode() == user.getDataPermission() || DataPermissionEnum.DEPT.getCode() == user.getDataPermission()) {
                Stream<InspectionListVo> inspections = this.getInspectionListVoStream(inspectionList);
                return JsonResult.success(inspections);
            }
        }

        //若角色为部门管理员或普通用户，查询最新五条数据
        if (DataPermissionEnum.DEPT_AND_SUB_DEPT.getCode() == user.getDataPermission() || DataPermissionEnum.DEPT.getCode() == user.getDataPermission()) {
            String deptId = user.getDeptId();
            if (StringUtils.isEmpty(deptId)) {
                JsonResult.error(QueryInspectionListServiceEnum.DEPT_ID_IS_NULL.getCode(), QueryInspectionListServiceEnum.DEPT_ID_IS_NULL.getMsg());
            }
            List<Inspection> inspectionList = inspectionMapper.selectList(new LambdaQueryWrapper<Inspection>()
                    .eq(Inspection::getTenantId, tenantId)
                    .eq(Inspection::getDeptId, deptId)
                    .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                    .orderByDesc(Inspection::getCreateTime)
                    .last("limit 5"));
            if (CollectionUtil.isEmpty(inspectionList)) {
                return JsonResult.success();
            }
            Stream<InspectionListVo> inspectionListVoStream = this.getInspectionListVoStream(inspectionList);
            return JsonResult.success(inspectionListVoStream);
        }
        return JsonResult.error();
    }

    private Stream<InspectionListVo> getInspectionListVoStream(List<Inspection> inspectionList) {
        return inspectionList.stream().map(x -> {
            InspectionListVo vo = new InspectionListVo();
            BeanUtils.copyProperties(x, vo);
            //修改执行人属性为realname
            User user = userMapper.selectById(x.getCreateUser());
            if (ObjectUtil.isNotNull(user)) {
                vo.setCreateUser(user.getRealname());
            }
            return vo;
        });
    }
}
