package com.tuoheng.admin.service.inspectionfile.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.conver.QuestionTypeConverMapper;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.InspectionFile;
import com.tuoheng.admin.entity.QuestionType;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.DataPermissionEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.questiontype.QuestionTypeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.QuestionTypeMapper;
import com.tuoheng.admin.query.InspectionFileQuery;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.ListByDeptUserTypeVo;
import com.tuoheng.admin.vo.QuestionTypeVo;
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.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 根据任务ID查询巡检任务问题分页列表业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-05
 */
@Slf4j
@Service
public class QueryInspectionFileListByDeptUserTypeService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private QuestionTypeMapper questionTypeMapper;

    public JsonResult getListByDeptUserType(InspectionFileQuery query) {
        User user = CurrentUserUtil.getUserInfo();
        //用户角色判断 1超级管理员 2部门管理员 3普通用户
        if (null == user.getDataPermission()) {
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        List<QuestionType> questionTypeList = questionTypeMapper.selectList(new LambdaQueryWrapper<QuestionType>()
                .eq(QuestionType::getMark, MarkEnum.VALID.getCode()));
        if (CollectionUtil.isEmpty(questionTypeList)) {
            log.info("问题类型为空");
            return JsonResult.success();
        }

        List<InspectionFile> inspectionFileList = null;
        //若角色为超级管理员，查看状态为已生成工单和和问题已处理
        if (DataPermissionEnum.ALL.getCode() == user.getDataPermission()) {
            //直接查问题列表
            inspectionFileList = inspectionFileMapper.selectList(Wrappers.<InspectionFile>lambdaQuery()
                    .eq(InspectionFile::getMark, MarkEnum.VALID.getCode())
                    .eq(InspectionFile::getTenantId, user.getTenantId())
                    .in(InspectionFile::getStatus, 15, 20, 25));
        } else if (DataPermissionEnum.DEPT_AND_SUB_DEPT.getCode() == user.getDataPermission() || DataPermissionEnum.DEPT.getCode() == user.getDataPermission()) {
            //若角色为部门管理员或普通用户
            //获取用户对应的部门
            String deptId = user.getDeptId();
            if (StringUtils.isEmpty(deptId)) {
                log.info("部门id为空");
                return JsonResult.success();
            }
            //根据部门id获取部门id列表
            List<String> deptIdList = deptMapper.selectAllChildListById(deptId);
            if (CollectionUtil.isEmpty(deptIdList)) {
                log.info("部门列表为空");
                return JsonResult.success();
            }
            //根据部门id列表查多条任务
            List<Inspection> inspectionList = inspectionMapper.selectListByDeptIdList(deptIdList);
            if (CollectionUtil.isEmpty(inspectionList)) {
                log.info("任务列表为空");
                return JsonResult.success();
            }
            List<String> inspectionIdList = inspectionList.stream().map(o -> o.getId()).collect(Collectors.toList());
            //根据任务id列表查找多条问题
            inspectionFileList = inspectionFileMapper.selectListByInspectIdList(inspectionIdList);
        }

        if (CollectionUtil.isEmpty(inspectionFileList)) {
            log.info("问题列表为空");
            return JsonResult.success();
        }

        List<QuestionTypeVo> questionTypeVoList = QuestionTypeConverMapper.INSTANCE.fromUListToVoList(questionTypeList);
        for (QuestionTypeVo questionTypeVo : questionTypeVoList) {
            if (ObjectUtil.isEmpty(questionTypeVo.getNum())) {
                questionTypeVo.setNum(0);
            }
            for (InspectionFile inspectionFile : inspectionFileList) {
                if (StringUtils.isEmpty(inspectionFile.getQuestionCode())) {
                    log.info("问题类型为空");
                    continue;
                }
                if (questionTypeVo.getCode().equals(inspectionFile.getQuestionCode())) {
                    questionTypeVo.setNum(questionTypeVo.getNum() + 1);
                }
            }
        }

        return JsonResult.success(questionTypeVoList);
    }

}
