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

import cn.com.taiji.beidou.common.core.domain.R;
import cn.com.taiji.beidou.common.utils.MathUtils;
import cn.com.taiji.beidou.ship.status.domain.OfflineShipStatistics;
import cn.com.taiji.beidou.ship.status.domain.ShipStatusReq;
import cn.com.taiji.beidou.ship.status.domain.ShipStatusResult;
import cn.com.taiji.beidou.ship.status.domain.StasisShipStatistics;
import cn.com.taiji.beidou.ship.status.mapper.ShipStatusMapper;
import cn.com.taiji.beidou.ship.status.service.ShipStatusService;
import org.springframework.stereotype.Service;

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

/**
 * @Author CHEN
 * @Date 2022/11/12 15:27
 */
@Service
public class ShipStatusServiceImpl implements ShipStatusService {

    @Resource
    private ShipStatusMapper shipStatusMapper;

    @Override
    public List<ShipStatusResult> selectShipStatusList(ShipStatusReq req) {
        return shipStatusMapper.selectShipStatusList(req);
    }

    @Override
    public ShipStatusResult selectById(String devideNo,String isOnline,String isStasis) {
        return shipStatusMapper.selectById(devideNo,isOnline,isStasis);
    }

    @Override
    public R<StasisShipStatistics> stasisShipTotal(String shipId,String deptId)
    {
        try {
            StasisShipStatistics statistics = shipStatusMapper.stasisShipTotal(shipId,deptId);

            int shipTotal = Integer.parseInt(statistics.getShipTotal());
            if (shipTotal>0){
                statistics.setSmallDayRate(MathUtils.getHalfUp(Double.parseDouble(statistics.getSmallDay())/shipTotal*100,2));
                statistics.setMiddleDayRate(MathUtils.getHalfUp(Double.parseDouble(statistics.getMiddleDay())/shipTotal*100,2));
                statistics.setLargeDayRate(MathUtils.getHalfUp(Double.parseDouble(statistics.getLargeDay())/shipTotal*100,2));
            }

            return R.ok(statistics);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }

    }

    @Override
    public R<OfflineShipStatistics> offlineShipTotal(String shipId, String deptId) {
        try {
            OfflineShipStatistics offlineShipStatistics = shipStatusMapper.offlineShipTotal(shipId, deptId);

            int shipTotal = Integer.parseInt(offlineShipStatistics.getShipTotal());
            if (shipTotal>0) {
                offlineShipStatistics.setSmallDayRate(MathUtils.getHalfUp(Double.parseDouble(offlineShipStatistics.getSmallDay()) / shipTotal * 100, 2));
                offlineShipStatistics.setMiddleDayRate(MathUtils.getHalfUp(Double.parseDouble(offlineShipStatistics.getMiddleDay()) / shipTotal * 100, 2));
                offlineShipStatistics.setLargeDayRate(MathUtils.getHalfUp(Double.parseDouble(offlineShipStatistics.getLargeDay()) / shipTotal * 100, 2));
            }
            return R.ok(offlineShipStatistics);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }
}
