package com.tuoheng.admin.service.impl;

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.InspectionFile;
import com.tuoheng.admin.entity.QuestionType;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.UserTypeEnum;
import com.tuoheng.admin.enums.code.inspectionFile.ListByDeptUserTypeEnum;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.QuestionTypeMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.query.InspectionFileQuery;
import com.tuoheng.admin.service.IInspectionFileService;
import com.tuoheng.admin.utils.ShiroUtils;
import com.tuoheng.admin.vo.InspectionFileVo;
import com.tuoheng.admin.vo.ListByDeptUserTypeVo;
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.SecurityUserUtils;
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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务问题管理 前端控制器
 *
 * @Author ChengWang
 * @Date 2022/11/29
 */
@Service
@Slf4j
public class InspectionFileServiceImpl implements IInspectionFileService {

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private QuestionTypeMapper questionTypeMapper;

    @Autowired
    private UserMapper userMapper;



    /**
     * 问题类型和任务名称
     *
     * @return
     */
    @Override
    public JsonResult getQuestionList() {
        //String tenantId = ShiroUtils.getTenantId();
        String tenantId = "0";
        //查询区域范范围问题
        List<InspectionFile> inspectionFileLists = new ArrayList<>();
        List<InspectionFile> inspectionFileList = inspectionFileMapper.selectList(Wrappers.<InspectionFile>lambdaQuery()
                .eq(InspectionFile::getMark, 1)
                .eq(InspectionFile::getTenantId, tenantId));
        if (null == inspectionFileList) {
            return JsonResult.error("该覆盖区域范围内没有问题");
        }
        for (InspectionFile inspectionFile : inspectionFileList) {
            //任务状态判断
            if(20 == inspectionFile.getStatus()){
                //任务状态判断，已生成工单，问题未解决
                Inspection inspection = inspectionMapper.selectById(inspectionFile.getInspectionId());
                if (ObjectUtil.isNull(inspection)) {
                    JsonResult.error("问题所属的任务不存在");
                }
                inspectionFileLists.add(inspectionFile);
            }
        }
        List<InspectionFileVo> list = inspectionFileLists.stream().map(x -> {
            InspectionFileVo vo = new InspectionFileVo();
            BeanUtils.copyProperties(x, vo);
            //任务名称
            if (StringUtils.isNotEmpty(x.getInspectionId())) {
                Inspection inspection = inspectionMapper.selectById(x.getInspectionId());
                if (ObjectUtil.isNull(inspection)) {
                    JsonResult.error();
                }
                vo.setName(inspection.getName());
                //问题类型
                if (StringUtils.isNotEmpty(x.getQuestionId())) {
                    QuestionType questionType = questionTypeMapper.selectById(x.getQuestionId());
                    if (null != questionType) {
                        vo.setType(questionType.getName());
                    }
                }
            }

            return vo;
        }).collect(Collectors.toList());
        return JsonResult.success(list);
    }

    @Override
    public JsonResult getListByDeptUserType(InspectionFileQuery query) {
        if(query.getUserId()==null){
            JsonResult.error(ListByDeptUserTypeEnum.QUERY_IS_FAILED.getCode(),ListByDeptUserTypeEnum.QUERY_IS_FAILED.getMsg());
        }
        //获取当前登录人信息
        String username = SecurityUserUtils.username();
        if(StringUtils.isEmpty(username)){
            JsonResult.error(ListByDeptUserTypeEnum.USER_NAME_IS_NULL.getCode(),ListByDeptUserTypeEnum.USER_NAME_IS_NULL.getMsg());
        }
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getMark, 1).eq(User::getUsername, username));
        if(ObjectUtil.isNull(user)){
            JsonResult.error(ListByDeptUserTypeEnum.USER_IS_NULL.getCode(),ListByDeptUserTypeEnum.USER_IS_NULL.getMsg());
        }
        String tenantId = ShiroUtils.getTenantId();
        //用户角色判断 1超级管理员 2部门管理员 3普通用户
        if(null == user.getType()){
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        List<ListByDeptUserTypeVo> list = new ArrayList<>();
        ListByDeptUserTypeVo vo = new ListByDeptUserTypeVo();
        //问题数量
        Long count = 0l;
        //若角色为超级管理员，查看状态为已生成工单和和问题已处理
        if(UserTypeEnum.SUPER_ADMIN.getCode()==user.getType()){
            //直接查问题列表
            List<InspectionFile> inspectionFileList = inspectionFileMapper.selectList(Wrappers.<InspectionFile>lambdaQuery()
                    .eq(InspectionFile::getMark, 1)
                    .eq(InspectionFile::getTenantId, tenantId)
                    .in(InspectionFile::getStatus,20,25));
            //根据状态类型分类
            if(null == inspectionFileList){
                JsonResult.error(ListByDeptUserTypeEnum.INSPECTION_TYPE_LIST_IS_NULL.getCode(),ListByDeptUserTypeEnum.INSPECTION_TYPE_LIST_IS_NULL.getMsg());
            }
            for (InspectionFile inspectionFile : inspectionFileList) {
                if(StringUtils.isEmpty(inspectionFile.getQuestionId())){
                    JsonResult.error(ListByDeptUserTypeEnum.QUESTION_ID_IS_NULL.getCode(),ListByDeptUserTypeEnum.QUESTION_ID_IS_NULL.getMsg());
                }
                //查找问题类型
                QuestionType questionType = questionTypeMapper.selectOne(Wrappers.<QuestionType>lambdaQuery()
                        .eq(QuestionType::getMark, 1)
                        .eq(QuestionType::getId, inspectionFile.getQuestionId()));
                if(ObjectUtil.isNull(questionType)){
                    throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
                }
                Integer name = questionType.getName();
                //1坑槽
                if(name==1) {
                    count+=1;
                    vo.setType(1);
                    vo.setNum(count);
                    list.add(vo);
                }
                //
                if(name==2){

                }
                //
                if(name==3){

                }

            }



        }


        return null;
    }


}
