package org.jeecg.modules.ai.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.jeecg.modules.ai.entity.PreExaminationMattersField;
import org.jeecg.modules.ai.entity.PreReviewMaterial;
import org.jeecg.modules.ai.entity.PreReviewRecords;
import org.jeecg.modules.ai.entity.PreReviewRecordsDetail;
import org.jeecg.modules.ai.entity.PreReviewRecordsDetailTemplate;
import org.jeecg.modules.ai.mapper.PreExaminationMattersFieldMapper;
import org.jeecg.modules.ai.mapper.PreReviewMaterialMapper;
import org.jeecg.modules.ai.mapper.PreReviewRecordsMapper;
import org.jeecg.modules.ai.qo.PreExaminationMattersFieldQuery;
import org.jeecg.modules.ai.qo.PreReviewRecordsDetailQuery;
import org.jeecg.modules.ai.service.IPreExaminationMattersFieldService;
import org.jeecg.modules.ai.service.IPreReviewRecordsDetailService;
import org.jeecg.modules.ai.service.IPreReviewRecordsDetailTemplateService;
import org.jeecg.modules.ai.service.IPreReviewRecordsService;
import org.jeecg.modules.ai.vo.PreExaminationMattersFieldVo;
import org.jeecg.modules.ai.vo.PreReviewMaterialVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;

/**
 * @Description: 事项表单回显字段与材料对应关系表
 * @Author: jeecg-boot
 * @Date:   2023-11-29
 * @Version: V1.0
 */
@Service
public class PreExaminationMattersFieldServiceImpl extends ServiceImpl<PreExaminationMattersFieldMapper, PreExaminationMattersField> implements IPreExaminationMattersFieldService {

	@Resource
	private PreExaminationMattersFieldMapper preExaminationMattersFieldMapper;
	@Resource
	private PreReviewMaterialMapper preReviewMaterialMapper;
	@Autowired
	private IPreReviewRecordsService iPreReviewRecordsService;
	@Autowired
	private IPreReviewRecordsDetailService iPreReviewRecordsDetailService;
	@Autowired
	private IPreReviewRecordsDetailTemplateService iPreReviewRecordsDetailTemplateService;
	
	/**
	 * 获取已经配置的表单字段
	 * @param preExaminationMattersField
	 * @return
	 */
	public List<PreExaminationMattersFieldVo> getPreExaminationMattersFieldListByEvent(PreExaminationMattersFieldQuery query){
		
		List<PreExaminationMattersField> list = preExaminationMattersFieldMapper.selectByEventCode(query.getEventCode());
		List<PreExaminationMattersFieldVo> listPreExaminationMattersFieldVo = new ArrayList<PreExaminationMattersFieldVo>();
		for(PreExaminationMattersField field:list) {
			PreExaminationMattersFieldVo vo = new PreExaminationMattersFieldVo();
			BeanUtils.copyProperties(field, vo);
			PreReviewMaterialVo material = preReviewMaterialMapper.getPreReviewMaterialByCode(field.getMaterialCode());
			vo.setMaterialName(material.getMaterialName());
			listPreExaminationMattersFieldVo.add(vo);
		}
		return listPreExaminationMattersFieldVo;
	}
	
	/**
	 * 保存表单数据
	 */
	@Override
	public boolean saveInfo(String reviewInfo) {
		JSONObject json = JSONObject.parseObject(reviewInfo);
		String eventCode = json.getString("eventCode");
		JSONArray listMaterial = json.getJSONArray("listMaterial");
		if(listMaterial.size()>0) {
			//删除旧数据
			LambdaQueryWrapper<PreExaminationMattersField> queryWrapper = new LambdaQueryWrapper<PreExaminationMattersField>();
			queryWrapper.eq(PreExaminationMattersField::getEventCode, eventCode);
			preExaminationMattersFieldMapper.delete(queryWrapper);
		}
		for(int i=0;i<listMaterial.size();i++) {
			JSONObject jsonMaterial = listMaterial.getJSONObject(i);
			PreExaminationMattersField preExaminationMattersField = new PreExaminationMattersField();
			preExaminationMattersField.setEventCode(eventCode);
			preExaminationMattersField.setFieldCode(jsonMaterial.getString("fieldCode"));
			preExaminationMattersField.setFieldName(jsonMaterial.getString("fieldName"));
			preExaminationMattersField.setMaterialCode(jsonMaterial.getString("materialCode"));
			preExaminationMattersFieldMapper.insert(preExaminationMattersField);
		}
		return true;
	}
	
	/**
	 * 表单回显
	 * @param reviewInfo
	 * @return
	 */
	@Override
	public String echoForm(String reviewRecordId) {
		
		JSONObject result = new JSONObject();
		PreReviewRecords preReviewRecords = iPreReviewRecordsService.getById(reviewRecordId);
		//获取配置的字段信息
		List<PreExaminationMattersField> listFile = preExaminationMattersFieldMapper.selectByEventCode(preReviewRecords.getEventCode());
		//获取识别结果
		for(PreExaminationMattersField field:listFile) {
			LambdaQueryWrapper<PreReviewRecordsDetail> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(PreReviewRecordsDetail::getEventCode, field.getEventCode());
			queryWrapper.eq(PreReviewRecordsDetail::getMaterialCode, field.getMaterialCode());
			queryWrapper.eq(PreReviewRecordsDetail::getReviewRecordsId, reviewRecordId);
			PreReviewRecordsDetail detail = iPreReviewRecordsDetailService.getOne(queryWrapper);
			
			result.put(field.getFieldName(), "");
			
			LambdaQueryWrapper<PreReviewRecordsDetailTemplate> queryWrapperTemplate = new LambdaQueryWrapper<>();
			queryWrapperTemplate.eq(PreReviewRecordsDetailTemplate::getRecordDetailId, detail.getId());
			queryWrapperTemplate.eq(PreReviewRecordsDetailTemplate::getMaterialCode, detail.getMaterialCode());
			List<PreReviewRecordsDetailTemplate> listPreReviewRecordsDetailTemplate = iPreReviewRecordsDetailTemplateService.list(queryWrapperTemplate);
			for(PreReviewRecordsDetailTemplate template:listPreReviewRecordsDetailTemplate) {
				JSONObject jsonObject = JSONObject.parseObject(template.getOcrResult());
				Iterator<String> keys = jsonObject.keySet().iterator();
				while (keys.hasNext()) {
				    String key = keys.next();
				    if(field.getFieldCode().equals(key)) {
						result.put(field.getFieldName(), jsonObject.getString(key));
						 break;
					}
				}
			}
		}
		return result.toString();
	}
}
