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

import com.mortals.framework.model.Context;;
import com.mortals.xhx.busiz.oneProblemlist.OneProblemInfo;
import com.mortals.xhx.busiz.oneProblemlist.OptionDetailInfo;
import com.mortals.xhx.busiz.oneProblemlist.mapper.ProblemMapper;
import com.mortals.xhx.module.classify.model.*;
import com.mortals.xhx.module.classify.service.ClassifyOptionService;
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.ClassifyDao;
import com.mortals.xhx.module.classify.service.ClassifyService;
import org.springframework.util.ObjectUtils;

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

/**
 * <p>Title: 一件事问题分类</p>
 * <p>Description: ClassifyService service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 *
 * @version 1.0.0
 */
@Service("classifyService")
public class ClassifyServiceImpl extends AbstractCRUDServiceImpl<ClassifyDao, ClassifyEntity, Long> implements ClassifyService {

    @Autowired
    private ClassifyOptionService classifyOptionService;

    @Override
    public ClassifyEntity saveOrUpdateClassifyTree(ClassifyVo classifyvo, Context context) {
        //判断是更新 还是新增
        if (ObjectUtils.isEmpty(classifyvo.getEntity().getId())) {
            //新增
            classifyvo.getEntity().setCreateUser(context.getUser().getLoginName());
            classifyvo.getEntity().setCreateTime(new Date());
            ClassifyEntity save = this.save(classifyvo.getEntity());

            //新增问题库
            List<ClassifyOptionEntity> classifyOptionEntityList = classifyvo.getList().stream().map(item -> {
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateTime(new Date());
                item.setClassifyId(save.getId());
                return item;
            }).collect(Collectors.toList());
            int count = classifyOptionService.save(classifyOptionEntityList);
            return save;
        } else {
            //更新
            classifyvo.getEntity().setUpdateUser(context.getUser().getLoginName());
            classifyvo.getEntity().setUpdateTime(new Date());
            ClassifyEntity update = this.update(classifyvo.getEntity());
            //求交集并集更新或删除问题库
            List<ClassifyOptionEntity> oldList = classifyOptionService.find(new ClassifyOptionQuery().classifyId(update.getId()));

            List<ClassifyOptionEntity> newList = classifyvo.getList();

            List<ClassifyOptionEntity> updateList = classifyOptionService.sameList(oldList, newList).stream().map(item -> {
                item.setUpdateUser(context.getUser().getLoginName());
                item.setUpdateTime(new Date());
                return item;
            }).collect(Collectors.toList());
            classifyOptionService.update(updateList);

            //求old 差集  做删除
            Long[] delIds = classifyOptionService.diffList(oldList, newList).stream().map(item -> item.getId()).toArray(Long[]::new);
            classifyOptionService.remove(delIds, context);
            //求new 差集  做新增
            List<ClassifyOptionEntity> insertList = classifyOptionService.diffList(newList, oldList).stream().map(item -> {
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateTime(new Date());
                return item;
            }).collect(Collectors.toList());
            classifyOptionService.save(insertList);

            return update;
        }
    }

    @Override
    public int removeQueston(Long basicsId, Context context) {

        List<Long> idList = this.find(new ClassifyQuery().basicsId(basicsId)).stream().map(item -> item.getId()).collect(Collectors.toList());
        Long[] ids = idList.stream().toArray(Long[]::new);

        Long[] optionIds = classifyOptionService.find(new ClassifyOptionQuery().classifyIdList(idList)).stream().map(ClassifyOptionEntity::getId).toArray(Long[]::new);
        //级联删除
        int remove = this.remove(ids, context);
        int remove1 = classifyOptionService.remove(optionIds, context);
        return remove + remove1;
    }

    @Override
    public List<OneProblemInfo> oneProblemList(Long basicsId) {
        return this.find(new ClassifyQuery().basicsId(basicsId)).stream().map(item -> {
            OneProblemInfo oneProblemInfo = ProblemMapper.INSTANCE.classifyEntity2Info(item);
            oneProblemInfo.setCreate_time(item.getCreateTime().getTime());
            List<OptionDetailInfo> list = classifyOptionService.find(new ClassifyOptionQuery().classifyId(item.getId())).stream().map(option -> {
                OptionDetailInfo optionDetailInfo = ProblemMapper.INSTANCE.classifyOptionEntity2DetailInfo(option);
                optionDetailInfo.setCreate_time(option.getCreateTime().getTime());
                return optionDetailInfo;
            }).collect(Collectors.toList());

            oneProblemInfo.setOption(list);


            ClassifyOptionEntity temp = classifyOptionService.selectOne(new ClassifyOptionQuery().classifyNextId(item.getId()), null);

            if(!ObjectUtils.isEmpty(temp)){
                //option_ids 为父问题
                oneProblemInfo.setOption_ids(temp.getId().toString());
            }
           return oneProblemInfo;
        }).collect(Collectors.toList());
    }

}