package com.jeeplus.modules.sg.managementcenter.reserveManagement.service;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ReserveManagement;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.mapper.ReserveManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 项目储备信息 Service
 * @author 徐滕
 * @version 2020-09-14
 */
@Service
@Transactional(readOnly = true)
public class ReserveManagementService extends CrudService<ReserveManagementMapper, ReserveManagement> {

    @Autowired
    private ReserveManagementMapper dao;

    public ReserveManagement get(String id) {
        return super.get(id);
    }

    public Page<ReserveManagement> findPage(Page<ReserveManagement> page, ReserveManagement reserveManagement) {
        Page<ReserveManagement> pageList = super.findPage(page, reserveManagement);
        return pageList;
    }

    public List<ReserveManagement> exportFindList( ReserveManagement reserveManagement) {
        List<ReserveManagement> pageList = dao.exportFindList(reserveManagement);
        return pageList;
    }

    /**
     * 新增方法
     * @param reserveManagement
     */
    @Transactional(readOnly = false)
    public void save(ReserveManagement reserveManagement) {
        //查询数据表中是否含有同项目储备编号的数据 若有则进行修改 没有则新增
        //根据项目储备编号 查询数据
        ReserveManagement reserveManagementByProjectReserveId = dao.queryByProjectReserveId(reserveManagement);
        //如果存在则修改信息
        if(null !=reserveManagementByProjectReserveId){
            reserveManagement.preUpdate();
            reserveManagement.setId(reserveManagementByProjectReserveId.getId());
            mapper.update(reserveManagement);
        }else{
            super.save(reserveManagement);
        }
    }

    /**
     * 专职修改方法
     * @param reserveManagement
     */
    @Transactional(readOnly = false)
    public void update(ReserveManagement reserveManagement) {
        super.save(reserveManagement);
    }
    /**
     * 经理修改方法
     * @param reserveManagement
     */
    @Transactional(readOnly = false)
    public void updatejl(ReserveManagement reserveManagement) {
        reserveManagement.preUpdate();
        dao.updatejl(reserveManagement);
    }

    /**
     * 删除方法
     * @param reserveManagement
     */
    @Transactional(readOnly = false)
    public void delete(ReserveManagement reserveManagement) {
        super.delete(reserveManagement);
    }


    /**
     * 查询已有的参数数据 并分类返回已存在数据和未存在数据
     * @param reserveManagement
     * @return
     */
    public Map<String,List<ReserveManagement>> getExistProjectList(List<ReserveManagement> reserveManagement){
        Map<String,List<ReserveManagement>> map = new HashMap();
        List<ReserveManagement> existReserveManagementList = new ArrayList<>();
        Iterator<ReserveManagement> iterator = reserveManagement.iterator();
        while (iterator.hasNext()) {
            ReserveManagement rm = iterator.next();
            //对数据进行处理
            rm.preInsert();
            ReserveManagement reserveManag = dao.queryByProjectReserveId(rm);
            //数据库中已含有的数据在list中删除并创建一个list加入 区分为已存在数据
            if (null != reserveManag) {
                rm.setId(reserveManag.getId());
                existReserveManagementList.add(rm);
                iterator.remove();//使用迭代器删除已存在的数据
            }
        }
        map.put("without",reserveManagement);
        map.put("exist",existReserveManagementList);
        return map;
    }

    /**
     * 批量添加数据
     * @param reserveManagementList
     */
    @Transactional(readOnly = false)
    public void insertList(List<ReserveManagement> reserveManagementList){
        dao.insertList(reserveManagementList);
    }


}
