package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

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

/**
 * 模版属性值Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-31
 */
@Service
public class BusinessTemplatePropValueServiceImpl extends ServiceImpl<BusinessTemplatePropValueMapper, BusinessTemplatePropValue> implements IBusinessTemplatePropValueService {
    @Autowired
    private BusinessTemplatePropValueMapper businessTemplatePropValueMapper;

    @Autowired
    private IBusinessTemplateService businessTemplateService;

    @Autowired
    private IBusinessTemplatePropService businessTemplatePropService;

    @Autowired
    private IBusinessSurveyService businessSurveyService;


    /**
     * 获取统计数据
     *
     * @param surveyId
     * @return
     */
    @Override
    public Object countGroup(String surveyId) {
        BusinessSurvey businessSurvey = businessSurveyService.lambdaQuery().eq(BusinessSurvey::getId, surveyId).one();

        BusinessTemplate businessTemplate = businessTemplateService.lambdaQuery().eq(BusinessTemplate::getId, businessSurvey.getTempId()).one();

        List<BusinessTemplateProp> businessTemplateProps = businessTemplatePropService.lambdaQuery()
                .eq(BusinessTemplateProp::getTempId, businessTemplate.getId()).in(BusinessTemplateProp::getCompType, new String[]{"0" , "1" , "6"}).list();

        JSONObject map = new JSONObject();
        List<Map<String, Object>> list = new ArrayList<>();
        businessTemplateProps.forEach(e -> {
            List<Map<String, Object>> countList = businessTemplatePropValueMapper.countGroup(surveyId, e.getId());
            map.put(e.getTitle(), countList);
        });

        return map;
    }

    /**
     * 批量写入值
     *
     * @param data
     * @return
     */
    @Override
    public boolean addList(Map<String, Object> data) {

        // 试卷id
        String surveyId = data.get("surveyId").toString();
        data.remove("surveyId");
        List<BusinessTemplatePropValue> datas = new ArrayList<>();
        data.forEach((k, v) -> {
            BusinessTemplatePropValue businessTemplatePropValue = new BusinessTemplatePropValue();
            BusinessTemplateProp businessTemplateProp = businessTemplatePropService.getById(k);
            List<BusinessTemplatePropValue> businessTemplatePropValues = businessTemplatePropValueMapper.selectList(new LambdaQueryWrapper<BusinessTemplatePropValue>()
                    .eq(BusinessTemplatePropValue::getPropId, k).eq(BusinessTemplatePropValue::getCreateBy, SecurityUtils.getUsername()));

            businessTemplatePropValues.forEach(e -> {
                if (e.getPropId().equals(k)) {
                    businessTemplatePropValue.setId(e.getId());
                }
            });
            businessTemplatePropValue.setPropName(businessTemplateProp.getFieldName());
            businessTemplatePropValue.setPropType(businessTemplateProp.getCompType());
            businessTemplatePropValue.setPropId(k);
            businessTemplatePropValue.setValueKey(v.toString());
            if (businessTemplateProp.getCompType().equals("0")) {
                String optionalValue = businessTemplateProp.getOptionalValue();
                String[] split = optionalValue.split("\\|");
                for (String v1 : split) {
                    String[] v2 = v1.split(",");
                    if (v2[0].equals(v)) {
                        businessTemplatePropValue.setValueText(v2[1]);
                    }
                }
                businessTemplatePropValue.setValueSources(v.toString());
            } else if (businessTemplateProp.getCompType().equals("1")) {
                String optionalValue = businessTemplateProp.getOptionalValue();
                String[] split = optionalValue.split("\\|");
                StringBuilder sb = new StringBuilder();

                for (String v1 : split) {
                    String[] v2 = v1.split(",");
                    if (((ArrayList) v).contains(v2[0])) {
                        sb.append(v2[1]).append(",");
                    }
                }
                businessTemplatePropValue.setValueText(sb.substring(0, sb.length() - 1));
                businessTemplatePropValue.setValueSources(v.toString());
            } else if (businessTemplateProp.getCompType().equals("6")) {
                String optionalValue = businessTemplateProp.getOptionalValue();
                String[] split = optionalValue.split("\\|");
                StringBuilder sb = new StringBuilder();

                for (String v1 : split) {
                    String[] v2 = v1.split(",");
                    if (v.equals(v2[0])) {
                        sb.append(v2[1]).append(",");
                    }
                }
                businessTemplatePropValue.setValueText(sb.substring(0, sb.length() - 1));
                businessTemplatePropValue.setValueSources(v.toString());
            } else {
                businessTemplatePropValue.setValueText(v.toString());
                businessTemplatePropValue.setValueSources(v.toString());
            }
            businessTemplatePropValue.setSurveyId(surveyId);
            datas.add(businessTemplatePropValue);
        });

        return saveOrUpdateBatch(datas);
    }
}
