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.ExportScheme;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ProgressSchedule;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ReserveManagement;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.mapper.ProgressScheduleMapper;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.mapper.ReserveManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

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

    @Autowired
    private ReserveManagementMapper dao;

    @Autowired
    private ProgressScheduleMapper scheduleMapper;

    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 Page<ReserveManagement> findModulePage (Page<ReserveManagement> page, ReserveManagement entity) {
        dataRuleFilter(entity);
        entity.setPage(page);
        page.setList(mapper.findModuleList(entity));
        return page;
    }

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

    /**
     * 新增方法
     * @param entity
     */
    @Transactional(readOnly = false)
    public void save(ReserveManagement entity) {
        //查询数据表中是否含有同项目储备编号的数据 若有则进行修改 没有则新增
        //根据项目储备编号 查询数据
        if (entity.getIsNewRecord()){
            entity.preInsert();
            mapper.insert(entity);
        }else{
            entity.preUpdate();
            mapper.update(entity);
        }
        //通过项目储备编号 查询流程时间数据是否存在，存在更新，不存在插入
        ProgressSchedule progressSchedule = scheduleMapper.findProjectReserveId(entity.getProjectReserveId());
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dft.format(new Date());
        ProgressSchedule schedule = new ProgressSchedule();
        if (null == progressSchedule) {
            schedule.preInsert();
            schedule.setProjectBasic(format);
            schedule.setProjectReserveId(entity.getProjectReserveId());
            scheduleMapper.insert(schedule);
        } else {
            schedule.preUpdate();
            schedule.setProjectBasic(format);
            schedule.setProjectReserveId(entity.getProjectReserveId());
            scheduleMapper.updateTimeNode(schedule);
        }
    }

    @Transactional(readOnly = false)
    public void jlSave(ExportScheme scheme) {
        //查询数据表中是否含有同项目储备编号的数据 若有则进行修改
        //根据项目储备编号 查询数据
        Integer byProjectId = mapper.findByProjectId(scheme.getProjectReserveId());
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dft.format(new Date());
        ProgressSchedule schedule = new ProgressSchedule();
        //如果存在则修改信息
        if (null != byProjectId && 1 == byProjectId) {
            scheme.preUpdate();
            scheme.setWhetherFinishDisclosure(returnStatus(scheme.getWhetherFinishDisclosure()));
            scheme.setCapitalEquipment(returnStatus(scheme.getCapitalEquipment()));
            scheme.setSubstationNewLine(returnStatus(scheme.getSubstationNewLine()));
            scheme.setWhetherImplement(returnStatus(scheme.getWhetherImplement()));
            scheme.setNecessaryOfImplementation(returnStatus(scheme.getNecessaryOfImplementation()));
            scheme.setPrecondition(returnStatus(scheme.getPrecondition()));
            scheme.setConstructionSchemeProblem(returnStatus(scheme.getConstructionSchemeProblem()));
            scheme.setExistingSecurityThreat(returnStatus(scheme.getExistingSecurityThreat()));
            scheme.setCustomizedMaterials(returnStatus(scheme.getCustomizedMaterials()));
            scheme.setDismantlingTheOldMaterial(returnStatus(scheme.getDismantlingTheOldMaterial()));
            scheme.setCivilWork(returnStatus(scheme.getCivilWork()));
            scheme.setVerticalRodWorkload(returnStatus(scheme.getVerticalRodWorkload()));
            scheme.setProjectApproval(returnStatus(scheme.getProjectApproval()));
            scheme.setCourseEvaluationProcedures(returnStatus(scheme.getCourseEvaluationProcedures()));
            mapper.updateScheme(scheme);
            schedule.preUpdate();
            schedule.setConclusionDisclosure(format);
            schedule.setProjectReserveId(scheme.getProjectReserveId());
            scheduleMapper.updateTimeNode(schedule);
        }
    }

    private String returnStatus(String scheme) {
        if ("是".equals(scheme)) {
            return "1";
        } else {
            return "0";
        }
    }

    /**
     * 专职修改方法
     * @param reserveManagement
     */
    @Transactional(readOnly = false)
    public void update(ReserveManagement reserveManagement) {
        ProgressSchedule progressSchedule = scheduleMapper.findProjectReserveId(reserveManagement.getProjectReserveId());
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dft.format(new Date());
        ProgressSchedule schedule = new ProgressSchedule();
        if (null == progressSchedule) {
            schedule.preInsert();
            schedule.setProjectBasic(format);
            schedule.setProjectReserveId(reserveManagement.getProjectReserveId());
            scheduleMapper.insert(schedule);
        } else {
            schedule.preUpdate();
            schedule.setProjectBasic(format);
            schedule.setProjectReserveId(reserveManagement.getProjectReserveId());
            scheduleMapper.updateTimeNode(schedule);
        }
        super.save(reserveManagement);
    }

    /**
     * 经理修改方法
     * @param reserveManagement
     */
    @Transactional(readOnly = false)
    public void updatejl(ReserveManagement reserveManagement) {
        reserveManagement.preUpdate();
        dao.updatejl(reserveManagement);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dft.format(new Date());
        ProgressSchedule schedule = new ProgressSchedule();
        schedule.preUpdate();
        schedule.setConclusionDisclosure(format);
        schedule.setProjectReserveId(reserveManagement.getProjectReserveId());
        scheduleMapper.updateTimeNode(schedule);
    }

    /**
     * 删除方法
     * @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){
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dft.format(new Date());
        ProgressSchedule schedule = new ProgressSchedule();
        for (ReserveManagement a :
                reserveManagementList) {
            a.preInsert();
            schedule.preInsert();
            schedule.setProjectBasic(format);
            schedule.setProjectReserveId(a.getProjectReserveId());
            scheduleMapper.insert(schedule);
        }
        dao.insertList(reserveManagementList);
    }


    /**
     * 导出方案交底结论数据
     * */
    public List<ExportScheme> exportModuleList(ExportScheme entity) {
        return mapper.exportModuleList(entity);
    }

}
