package com.mortals.xhx.module.classify.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.xhx.busiz.question.*;
import com.mortals.xhx.busiz.question.mapper.QuestionMapper;
import com.mortals.xhx.module.accept.model.AcceptQuery;
import com.mortals.xhx.module.accept.service.AcceptService;
import com.mortals.xhx.module.basics.model.BasicsInfoEntity;
import com.mortals.xhx.module.basics.service.BasicsInfoService;
import com.mortals.xhx.module.datum.model.DatumQuery;
import com.mortals.xhx.module.datum.service.DatumService;
import com.mortals.xhx.module.flowlimit.model.FlowlimitEntity;
import com.mortals.xhx.module.flowlimit.model.FlowlimitQuery;
import com.mortals.xhx.module.flowlimit.service.FlowlimitService;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.classify.dao.ClassifyOptionDao;
import com.mortals.xhx.module.classify.model.ClassifyOptionEntity;
import com.mortals.xhx.module.classify.service.ClassifyOptionService;

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

/**
 * <p>Title: 一件事问题选项表</p>
 * <p>Description: ClassifyOptionService service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 *
 * @version 1.0.0
 */
@Service("classifyOptionService")
public class ClassifyOptionServiceImpl extends AbstractCRUDServiceImpl<ClassifyOptionDao, ClassifyOptionEntity, Long> implements ClassifyOptionService {

    @Autowired
    private BasicsInfoService basicsInfoService;
    @Autowired
    private FlowlimitService flowlimitService;
    @Autowired
    private AcceptService acceptService;
    @Autowired
    private DatumService datumService;


    @Override
    public QuestionInfo conditionQuestion(Long optionId, String key) {
        QuestionInfo questionInfo = new QuestionInfo();
        try {
            ClassifyOptionEntity optionEntity = this.get(optionId);
            BasicsInfoEntity basicsInfoEntity = basicsInfoService.get(optionEntity.getBasicsinfoId());

            BasicsInfo basicsInfo = QuestionMapper.INSTANCE.basicsEntity2Info(basicsInfoEntity);
            basicsInfo.setCreate_time(basicsInfoEntity.getCreateTime().getTime());
            questionInfo.setBasicsInfo(basicsInfo);

            List<Long> idAcciptList = Stream.of(optionEntity.getAcceptId().split(",")).map(Long::valueOf).collect(Collectors.toList());
            List<AcceptInfo> acceptInfoList = acceptService.find(new AcceptQuery().idList(idAcciptList)).stream().map(item -> {
                AcceptInfo acceptInfo = QuestionMapper.INSTANCE.acceptEntity2Info(item);
                acceptInfo.setCreate_time(item.getCreateTime().getTime());
                return acceptInfo;
            }).collect(Collectors.toList());
            questionInfo.setAccept(acceptInfoList);


            List<Long> idFlowList = Stream.of(optionEntity.getFlowlimitId().split(",")).map(Long::valueOf).collect(Collectors.toList());
            List<FlowLimitinfo> flowLimitinfos = flowlimitService.find(new FlowlimitQuery().idList(idFlowList)).stream().map(item -> {
                FlowLimitinfo flowLimitinfo = QuestionMapper.INSTANCE.flowLimitEntity2Info(item);
                flowLimitinfo.setCreate_time(item.getCreateTime().getTime());
                return flowLimitinfo;
            }).collect(Collectors.toList());
            questionInfo.setFlowlimit(flowLimitinfos);

            List<Long> idDatumList = Stream.of(optionEntity.getDatumId().split(",")).map(Long::valueOf).collect(Collectors.toList());
            List<DatumInfo> datumInfoList = datumService.find(new DatumQuery().idList(idDatumList)).stream().map(item -> {
                DatumInfo datumInfo = QuestionMapper.INSTANCE.datumEntity2Info(item);
                datumInfo.setCreate_time(item.getCreateTime().getTime());
                return datumInfo;
            }).collect(Collectors.toList());
            questionInfo.setDatum(datumInfoList);


        } catch (AppException e) {
            throw new AppException("异常:" + e.getMessage());
        }
        return questionInfo;
    }
}