package cn.com.taiji.beidou.ship.archives.service.impl;

import cn.com.taiji.beidou.common.constant.Constants;
import cn.com.taiji.beidou.common.utils.MathUtils;
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.*;
import cn.com.taiji.beidou.ship.archives.mapper.ShipArchivesMapper;
import cn.com.taiji.beidou.ship.archives.service.ShipArchivesService;
import cn.com.taiji.beidou.ship.warning.record.mapper.WarningRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author CHEN
 * @Date 2022/11/9 9:45
 */
@Service
public class ShipArchivesServiceImpl implements ShipArchivesService {

    @Resource
    private ShipArchivesMapper shipArchivesMapper;


    @Override
    public List<ShipArchivesData> findShipList(ShipArchivesREQ req) {
        String online = req.getOnline();
        if(Constants.ONE.equals(online)){
            req.setOnline("ONLINE");
        }else if(Constants.TWO.equals(online)){
            req.setOnline("OFFLINE");
        }else if(Constants.THREE.equals(online)){
            req.setOnline("MARK");
        }
        String inPort = req.getInPort();
        if(Constants.ZERO.equals(inPort)){
            req.setInPort("FALSE");
        }else if(Constants.ONE.equals(inPort)){
            req.setInPort("TRUE");
        }
        List<ShipArchivesData> list = shipArchivesMapper.selectShipArchivesList(req);
        return list;
    }

    @Override
    public ShipArchivesData findShipById(String devideNo) {
        String userId = String.valueOf(SecurityUtils.getUserId());
        ShipArchivesData data = shipArchivesMapper.selectById(devideNo,userId);
        if(null==data.getLastStatus()){
            data.setLastStatus(1);
        }
        return data;
    }

    @Override
    public ShipDisposeResult selectDisposeById(String devideNo, String warningId) {

        return shipArchivesMapper.selectDisposeById(devideNo,warningId);
    }

    @Override
    public ShipArchivesTotal getShipTotal(String deptId,String speed){

        ShipArchivesTotal total = shipArchivesMapper.getShipTotal(deptId,speed);
        int shipTotal = Integer.parseInt(total.getShipTotal());
        Integer online = Integer.parseInt(total.getOnlineTotal());

        if(shipTotal > 0){
            double onlineRate = MathUtils.getHalfUp((double) online / shipTotal * 100, 2);
            double offlineRate = MathUtils.getHalfUp(Double.parseDouble(total.getOfflineTotal()) / shipTotal * 100, 2);
            total.setOnlineTotalRate(onlineRate);
            total.setOfflineTotalRate(offlineRate);
            total.setInPortRate(MathUtils.getHalfUp(Double.parseDouble(total.getInPort())/shipTotal*100,2));
            total.setOffPortRate(MathUtils.getHalfUp(Double.parseDouble(total.getOffPort())/shipTotal*100,2));
            total.setMarkRate(MathUtils.getHalfUp(100-onlineRate-offlineRate,2));
        }
        return total;
    }

}
