package com.elephant.framework.galaxy.aroad.module.emergencyevent.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elephant.framework.galaxy.aroad.module.emergencyevent.entity.EmergencyEvent;
import com.elephant.framework.galaxy.aroad.module.emergencyevent.entity.Evendeal;
import com.elephant.framework.galaxy.aroad.module.emergencyevent.mapper.EmergencyEventMapper;
import com.elephant.framework.galaxy.aroad.module.emergencyevent.service.EmergencyEventService;
import com.elephant.framework.galaxy.aroad.module.emergencyevent.service.EvendealService;
import com.elephant.framework.galaxy.aroad.module.system.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 应急事件表
 *
 * @author songwenjie
 * @date 2019-05-08 14:43:42
 */
@Service
public class EmergencyEventServiceImpl extends ServiceImpl<EmergencyEventMapper, EmergencyEvent> implements EmergencyEventService {
    @Autowired
    private DeptService deptService;

    @Autowired
    private EvendealService evendealService;

   /** 
   * @Description: 分页获取应急事件
   * @param: [page, event]
   * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.elephant.framework.galaxy.aroad.module.emergencyevent.entity.EmergencyEvent> 
   * @Author: songwenjie
   * @Date: 2019/5/15 
   */ 
    public Page<EmergencyEvent> getEventPage (Page page, EmergencyEvent event){
        int reportingDeptId = event.getReportingDeptId();
        boolean isProvince = deptService.isProvince(reportingDeptId);
        if(isProvince){
            return baseMapper.getEventPage(page,null,event.getStatus());
        }
        String deptName = deptService.getDeptName(reportingDeptId);
        return baseMapper.getEventPage(page,deptName,event.getStatus());
    }

    /** 
    * @Description: 新增（上报）应急事件
    * @Param: [event] 
    * @return: boolean 
    * @Author: songwenjie 
    * @Date: 2019/5/8 
    */
    @Transactional
    public boolean saveEvent(EmergencyEvent event){
        int reportingDeptId = event.getReportingDeptId();
        String deptName = deptService.getDeptName(reportingDeptId);
        event.setReportingDept(deptName);
        baseMapper.saveEvent(event);

        Evendeal eventdeal = Evendeal.builder()
                .eventId(event.getEventId())
                .dealType(2)//"事件上报"
                .dealContent(event.getEventDetail())
                .dealTime(event.getReportingTime())
                .userId(event.getReporterId())
                .usr(event.getReporter())
                .deptId(event.getReportingDeptId())
                .dept(event.getReportingDept())
                .build();
        return evendealService.save(eventdeal);
    }

    /** 
    * @Description: 路况事件加入应急事件
    * @param: [event]
    * @return: java.lang.Integer 
    * @Author: songwenjie
    * @Date: 2019/5/22 
    */ 
    @Transactional
    public Integer joinEvent(EmergencyEvent event){
        int reportingDeptId = event.getReportingDeptId();
        String deptName = deptService.getDeptName(reportingDeptId);
        event.setReportingDept(deptName);
        baseMapper.saveEvent(event);

        Evendeal eventdeal = Evendeal.builder()
                .eventId(event.getEventId())
                .dealType(2)//"事件上报"
                .dealContent(event.getEventDetail())
                .dealTime(event.getReportingTime())
                .userId(event.getReporterId())
                .usr(event.getReporter())
                .deptId(event.getReportingDeptId())
                .dept(event.getReportingDept())
                .build();
        evendealService.save(eventdeal);

        return event.getEventId();
    }

    /** 
    * @Description: 更新应急事件的状态
    * @param: 
    * @return:  
    * @Author: songwenjie
    * @Date: 2019/5/15 
    */  
    public boolean updateStatus(Integer eventId,String newStatus,String oldStatus){
        return baseMapper.updateStatus(eventId,newStatus,oldStatus);
    }

    /**
     * @Description: 更新应急事件的状态
     * @param:
     * @return:
     * @Author: songwenjie
     * @Date: 2019/5/15
     */
    public boolean updateStatusById(Integer eventId,String newStatus){
        return baseMapper.updateStatusById(eventId,newStatus);
    }

    /**
     * @Description: 更新评估总结文档信息
     * @param: [event]
     * @return: boolean
     * @Author: songwenjie
     * @Date: 2019/5/16
     */
    public boolean updateFile(EmergencyEvent event){
        return baseMapper.updateFile(event);
    }
}
