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

import com.mortals.framework.model.Context;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.basics.dao.BasicsDatumDao;
import com.mortals.xhx.module.basics.model.BasicsDatumEntity;
import com.mortals.xhx.module.basics.service.BasicsDatumService;

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

/**
 * <p>Title: 一件事材料关联集合</p>
 * <p>Description: BasicsDatumService service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 *
 * @version 1.0.0
 */
@Service("basicsDatumService")
public class BasicsDatumServiceImpl extends AbstractCRUDServiceImpl<BasicsDatumDao, BasicsDatumEntity, Long> implements BasicsDatumService {

    @Override
    public int updateBatch(List<BasicsDatumEntity> oldList, List<BasicsDatumEntity> newList, Context context) {
        //求交集 只更新
        List<BasicsDatumEntity> updateList = sameList(oldList, newList).stream().map(item -> {
            item.setUpdateUser(context == null ? "" : context.getUser().getLoginName());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());
        this.update(updateList);

        //求old 差集  做删除
        Long[] delIds = diffList(oldList, newList).stream().map(item -> item.getId()).toArray(Long[]::new);
        this.remove(delIds, context);
        //求new 差集  做新增
        List<BasicsDatumEntity> insertList = diffList(newList, oldList);
        this.save(insertList);
        return 0;
    }


//    private List<BasicsDatumEntity> sameList(List<BasicsDatumEntity> oldArrayList, List<BasicsDatumEntity> newArrayList) {
//        List<BasicsDatumEntity> resultList = newArrayList.stream()
//                .filter(item -> oldArrayList.stream().map(e -> e.getId())
//                        .collect(Collectors.toList()).contains(item.getId()))
//                .collect(Collectors.toList());
//        return resultList;
//    }
//
//    private List<BasicsDatumEntity> diffList(List<BasicsDatumEntity> firstArrayList, List<BasicsDatumEntity> secondArrayList) {
//        List<BasicsDatumEntity> resultList = firstArrayList.stream()
//                .filter(item -> !secondArrayList.stream().map(e -> e.getId()).collect(Collectors.toList()).contains(item.getId()))
//                .collect(Collectors.toList());
//        return resultList;
//    }
}