package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.business.domain.BusinessTemplateProp;
import com.ruoyi.business.domain.BusinessTemplatePropValue;
import com.ruoyi.business.mapper.BusinessTemplatePropMapper;
import com.ruoyi.business.mapper.BusinessTemplatePropValueMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusinessSurveyMapper;
import com.ruoyi.business.domain.BusinessSurvey;
import com.ruoyi.business.service.IBusinessSurveyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 调查问卷管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-01
 */
@Service
public class BusinessSurveyServiceImpl  extends ServiceImpl<BusinessSurveyMapper, BusinessSurvey> implements IBusinessSurveyService
{
   @Autowired
   private BusinessTemplatePropMapper businessTemplatePropMapper;
@Autowired
   private BusinessTemplatePropValueMapper businessTemplatePropValueMapper;

    /**
     * 根据试卷获取题目
     * @param surveyId
     * @return
     */
    @Override
    public AjaxResult listBySurveyId(String surveyId) {
        BusinessSurvey businessSurvey = baseMapper.selectOne(new LambdaQueryWrapper<BusinessSurvey>().eq(BusinessSurvey::getId, surveyId));

        List<BusinessTemplateProp> list = businessTemplatePropMapper.selectList(new LambdaQueryWrapper<BusinessTemplateProp>()
                .eq(BusinessTemplateProp::getTempId, businessSurvey.getTempId())
                .orderByDesc(BusinessTemplateProp::getSort));

        JSONObject jsonObject = new JSONObject();
        list.forEach(e -> {
            BusinessTemplatePropValue one = businessTemplatePropValueMapper.selectOne(new LambdaQueryWrapper<BusinessTemplatePropValue>()
                    .eq(BusinessTemplatePropValue::getPropId, e.getId())
                    .eq(BusinessTemplatePropValue::getSurveyId, surveyId)
                    .eq(BusinessTemplatePropValue::getCreateBy, SecurityUtils.getUsername()));
            if (e.getCompType().equals("0")) {
                jsonObject.put(e.getId(), Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources());
            }
            if (e.getCompType().equals("1")) {
                String valueSources = Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources();
                if(null != valueSources){
                    List<String> collect = Stream.of(valueSources.substring(1, valueSources.length() - 1).split(", ")).collect(Collectors.toList());
                    jsonObject.put(e.getId(),collect);
                }else{
                    jsonObject.put(e.getId(),new ArrayList<>());
                }
            }
            if (e.getCompType().equals("2")) {
                jsonObject.put(e.getId(), Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources());
            }
            if (e.getCompType().equals("3")) {
                jsonObject.put(e.getId(), Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources());
            }
            if (e.getCompType().equals("4")) {
                jsonObject.put(e.getId(), Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources());
            }
            if (e.getCompType().equals("5")) {
                jsonObject.put(e.getId(), Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources());
            }
            if (e.getCompType().equals("6")) {
                jsonObject.put(e.getId(), Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources());
            }
            if (e.getCompType().equals("7")) {
                jsonObject.put(e.getId(), Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources());
            }
            if (e.getCompType().equals("8")) {
                jsonObject.put(e.getId(), Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources());
            }
            if (e.getCompType().equals("9")) {
                jsonObject.put(e.getId(), Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources() == null ? new JSONArray() : Optional.ofNullable(one).orElse(new BusinessTemplatePropValue()).getValueSources());
            }
        });
        AjaxResult success = AjaxResult.success(list);
        success.put("form" , jsonObject);
        return success;

    }
}
