package cn.com.taiji.beidou.ship.warning.record.service.impl;

import cn.com.taiji.beidou.common.constant.Constants;
import cn.com.taiji.beidou.common.utils.SecurityUtils;
import cn.com.taiji.beidou.common.utils.uuid.SnowFlakeIdWorker;
import cn.com.taiji.beidou.ship.archives.domain.ShipDisposeData;
import cn.com.taiji.beidou.ship.archives.mapper.ShipArchivesMapper;
import cn.com.taiji.beidou.ship.status.mapper.ShipStatusMapper;
import cn.com.taiji.beidou.ship.warning.record.domain.*;
import cn.com.taiji.beidou.ship.warning.record.mapper.WarningRecordMapper;
import cn.com.taiji.beidou.ship.warning.record.service.WarningRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author CHEN
 * @Date 2022/11/22 18:00
 */
@Service
public class WarningRecordServiceImpl implements WarningRecordService {

    @Resource
    private WarningRecordMapper warningRecordMapper;

    @Resource
    private ShipStatusMapper shipStatusMapper;


    @Override
    public List<StatisticsByRuleResult> statisticsByRule(String deptId,String startTime,String endTime) {
        return warningRecordMapper.statisticsByRule(deptId,startTime,endTime);
    }

    @Override
    public List<WarningRecordListResult> warningRecordList(WarningRecordReq req) {
        return warningRecordMapper.warningRecordList(req.getRuleId(),req.getDeptId(),req.getShipId(),req.getStartTime(),req.getEndTime());
    }

    @Override
    public List<SimpleWarningRecordResult> getWarningRecordByDeviceId(String devideNo,String ruleId,String startTime,String endTime) {
        return warningRecordMapper.getWarningRecordByDeviceId(devideNo,ruleId,startTime,endTime);
    }

    @Override
    public List<WarningRecordExcel> warningRecordExcel(String ruleId, String deptId,String startTime,String endTime) {
        return warningRecordMapper.warningRecordExcel(ruleId,deptId,startTime,endTime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDispose(ShipDisposeData shipDisposeData) {
        shipDisposeData.setDisposeUserId(String.valueOf(SecurityUtils.getUserId()));
        shipDisposeData.setId(new SnowFlakeIdWorker(1,0).nextId());

        WarningRecordData byId = warningRecordMapper.getRecordById(shipDisposeData.getWarningId());
        //更新处置状态  如果处置类型是正常，则不添加标记
        if(!Constants.ZERO.equals(shipDisposeData.getDisposeType())){
            shipStatusMapper.updateMark("MARK",byId.getDeviceId());
        }else {
            shipStatusMapper.updateMark("ONLINE",byId.getDeviceId());
        }
        shipDisposeData.setDevideNo(byId.getDeviceId());
        return warningRecordMapper.insertDispose(shipDisposeData);
    }

    @Override
    public List<Map<Integer, String>> getDisposeStatus() {
        return warningRecordMapper.getDisposeStatus();
    }

    @Override
    public List<WarningRecordResult> getRecordByDeviceId(String deviceId) {
        return warningRecordMapper.getRecordByDeviceId(deviceId);
    }
}
