package com.gci.boot.modules.dataview.service.impl;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.*;

import com.gci.boot.modules.dataview.vo.GisRoadMileVo;
import com.gci.boot.modules.gis.service.GisRoadMileLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.common.util.DateUtils;
import com.gci.boot.modules.basedata.entity.TbVehicle;
import com.gci.boot.modules.basedata.mapper.TbVehicleMapper;
import com.gci.boot.modules.dataview.service.ITbAssetCenterService;
import com.gci.boot.modules.dataview.vo.VehicleFaultAlarmVo;
import com.gci.boot.modules.dataview.vo.VehicleTotalVo;
import com.gci.boot.modules.dataview.vo.VehicleTypeVo;
import com.gci.boot.modules.driver.entity.TbDriver;
import com.gci.boot.modules.driver.mapper.TbDriverMapper;
import com.gci.boot.modules.gis.mapper.TbGisVehicleFaultAlarmMapper;
import com.gci.boot.modules.vehicles.vo.TbInsuranceRemindVo;

/**
 * <p>
 * 数据看板-资产中心 服务实现类
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-04
 */
@Service
public class TbAssetCenterImpl extends ServiceImpl<TbDriverMapper, TbDriver> implements ITbAssetCenterService {

    @Autowired
    private TbVehicleMapper tbVehicleMapper;
    @Autowired
    private TbGisVehicleFaultAlarmMapper tbGisVehicleFaultAlarmMapper;

    @Autowired
    private GisRoadMileLogService gisRoadMileLogService;

    DecimalFormat df2 = new DecimalFormat("#.0");

    //总资产统计
    @Override
    public VehicleTotalVo getVehicleTotal() {
        double totalPrice = 0d;
        List<TbVehicle> bus = tbVehicleMapper.getProjectVehicle();
        List<VehicleTypeVo> pageList = getVehicleProjectCount(new VehicleTypeVo());
        for (VehicleTypeVo tbVehicle : pageList) {
            totalPrice += tbVehicle.getTotalPrice();
        }
        VehicleTotalVo vo = new VehicleTotalVo();
        vo.setTotal(bus.size());
        vo.setTotalPrice(new Double(df2.format(totalPrice)));
        return vo;
    }

    //按车辆种类统计资产
    @Override
    public List<VehicleTypeVo> getVehicleTypeCount(VehicleTypeVo vo) {
        List<VehicleTypeVo> list = tbVehicleMapper.getVehicleTypeCount(vo.getProjectId());
		/*double totalPrice = 0d;
		for (VehicleTypeVo tbVehicle : list) {
			totalPrice += tbVehicle.getTotalPrice();
		}*/
        for (VehicleTypeVo tbVehicle : list) {
        	/*if(totalPrice!=0d) {
        		tbVehicle.setTotalPricePercent(new Double(df2.format(tbVehicle.getTotalPrice()*100/totalPrice)));
        	}*/
            tbVehicle.setTotalPrice(new Double(df2.format(tbVehicle.getTotalPrice())));
        }
        return list;
    }

    //按项目统计资产
	@Override
    public List<VehicleTypeVo> getVehicleProjectCount(VehicleTypeVo vo) {
        List<VehicleTypeVo> list = tbVehicleMapper.getVehicleProjectCount();
        for (VehicleTypeVo tbVehicle : list) {
            if(Objects.nonNull(tbVehicle.getTotalPrice())){
                tbVehicle.setTotalPrice(new Double(df2.format(tbVehicle.getTotalPrice())));
            }else{
                tbVehicle.setTotalPrice(0.0);
            }
        }
        return list;
    }

    /**
     * 获取半年故障告警统计（三电）
     *
     * @return
     */
    @Override
    public List<VehicleFaultAlarmVo> getFaultAlarmMonth() {
        Date alarmStartTime = new Date();
        alarmStartTime = DateUtils.addMonth(alarmStartTime, -5);
        alarmStartTime = DateUtils.getFirstDayForMonth(alarmStartTime);
        List<VehicleFaultAlarmVo> list = tbGisVehicleFaultAlarmMapper.getFaultAlarmMonth(alarmStartTime);
        Date today = new Date();
        while (alarmStartTime.before(today)) {
            boolean has = false;
            String month = DateUtils.formatDate(alarmStartTime, DateUtils.HYPHENS_MONTH);
            for (VehicleFaultAlarmVo vehicleFaultAlarmVo : list) {
                if (month.equals(vehicleFaultAlarmVo.getAlarmMonth())) {
                    has = true;
                    break;
                }
            }
            if (!has) {
                VehicleFaultAlarmVo vo = new VehicleFaultAlarmVo();
                vo.setAlarmMonth(month);
                vo.setTotal(0);
                list.add(vo);
            }
            alarmStartTime = DateUtils.addMonth(alarmStartTime, 1);
        }

        Collections.sort(list, new Comparator<VehicleFaultAlarmVo>() {
            @Override
            public int compare(VehicleFaultAlarmVo u1, VehicleFaultAlarmVo u2) {
                return u1.getAlarmMonth().compareTo(u2.getAlarmMonth());
            }
        });

        return list;
    }

    @Override
    public List<GisRoadMileVo> countByTime(String startTimeStr, String endTimeStr) {
        Date endTime = DateUtils.convertLocalDateTimeToDate(LocalDateTime.now());
        Date startTime = DateUtils.convertLocalDateTimeToDate(LocalDateTime.now().minusMonths(5));
        return gisRoadMileLogService.countByTime(startTime,endTime);
    }

}
