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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.matter.model.MatterEntity;
import com.mortals.xhx.module.matter.model.MatterQuery;
import com.mortals.xhx.module.matter.service.MatterService;
import com.mortals.xhx.module.one.model.OneClassifyQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.one.dao.OneClassifyDao;
import com.mortals.xhx.module.one.model.OneClassifyEntity;
import com.mortals.xhx.module.one.service.OneClassifyService;
import org.springframework.util.ObjectUtils;

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

/**
 * <p>Title: 分类引导表</p>
 * <p>Description: OneClassifyService service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 *
 * @version 1.0.0
 */
@Service("oneClassifyService")
public class OneClassifyServiceImpl extends AbstractCRUDCacheServiceImpl<OneClassifyDao, OneClassifyEntity, Long> implements OneClassifyService {

//    @Autowired
//    private OneMatterService oneMatterService;
    @Autowired
    private MatterService matterService;

    @Override
    protected void logicDeleteBefore(OneClassifyEntity entity, Context context) throws AppException {
        //是否级联删除子问题
        List<OneClassifyEntity> list = this.find(new OneClassifyQuery().parentId(entity.getId()).deleted(YesNoEnum.NO.getValue()));
        if(!ObjectUtils.isEmpty(list)){
            //级联删除子问题
            list.stream().peek(item->this.logicDelete(item,context)).count();
        }
    }

    @Override
    protected void saveBefore(OneClassifyEntity entity, Context context) throws AppException {
       //判断当前添加节点是问题还是答案
       if(!ObjectUtils.isEmpty(entity.getParentId())){
           OneClassifyEntity oneClassifyEntity = this.get(entity.getParentId());
           if(oneClassifyEntity.getClassifyLevel()>1){
               if(oneClassifyEntity.getIsAns()==YesNoEnum.NO.getValue()){
                   entity.setIsAns(YesNoEnum.YES.getValue());

               }else if(oneClassifyEntity.getIsAns()==YesNoEnum.YES.getValue()){
                   //判断当前节点是否是问题 且父节点是否已经存在问题
                   List<OneClassifyEntity> list = this.find(new OneClassifyQuery().parentId(entity.getParentId()).deleted(YesNoEnum.NO.getValue()));
                   if(list.size()>0){
                       throw new AppException("当前选项已经存在关联问题！");
                   }
               }
           }
       }
        super.saveBefore(entity, context);
    }

    @Override
    protected void saveAfter(OneClassifyEntity entity, Context context) throws AppException {
        //判断父节点是否为叶子节点如果是，更改为非叶子节点
        if (!ObjectUtils.isEmpty(entity.getParentId())) {
            OneClassifyEntity oneClassifyEntity = this.get(entity.getParentId());
            if (YesNoEnum.YES.getValue() == oneClassifyEntity.getIsLeaf()) {
                oneClassifyEntity.setIsLeaf(YesNoEnum.NO.getValue());
                this.update(oneClassifyEntity);
            }
        }


        super.saveAfter(entity, context);

    }

    @Override
    protected void updateAfter(OneClassifyEntity entity, Context context) throws AppException {
        //更新事项数据
        List<Long> matterIds = Arrays.asList(entity.getMatterIds().split(",")).stream().filter(e->!ObjectUtils.isEmpty(e)).map(Long::valueOf).collect(Collectors.toList());
        if(!ObjectUtils.isEmpty(matterIds)){
            List<MatterEntity> oneMatterEntities = matterService.find(new MatterQuery().idList(matterIds));
            String matterNames = oneMatterEntities.stream().map(matterEntity -> matterEntity.getMatterName()).collect(Collectors.joining(","));
            entity.setMatterNames(matterNames);
            List<String> collect = oneMatterEntities.stream().map(MatterEntity::getMatterName).collect(Collectors.toList());
            entity.setMatterNameList(collect);
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void logicDeleteAfter(OneClassifyEntity entity, Context context) throws AppException {
        //逻辑删除，查询当前父节点是否还存在子节点 如果没有，则更新
        if (!ObjectUtils.isEmpty(entity.getParentId())) {
            List<OneClassifyEntity> oneClassifyEntities = this.find(new OneClassifyQuery().parentId(entity.getParentId()).deleted(YesNoEnum.NO.getValue()));
            if (ObjectUtils.isEmpty(oneClassifyEntities)) {
                OneClassifyEntity oneClassifyEntity = this.get(entity.getParentId());
                if (YesNoEnum.NO.getValue() == oneClassifyEntity.getIsLeaf()) {
                    oneClassifyEntity.setIsLeaf(YesNoEnum.YES.getValue());
                    this.update(oneClassifyEntity);
                }
            }
        }
    }


    public static void main(String[] args) {

        Long.parseLong("3951293992071127040");


    }
}