package com.nmgs.service.impl;

import com.nmgs.mapper.Mapper;
import com.nmgs.service.VehicleScoreService;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PropertiesUtil;
import com.nmgs.util.PubTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VehicleScoreServiceImpl implements VehicleScoreService {
    @Autowired
    public Mapper mapperI;
    private static String  DBType= PropertiesUtil.getValue("DBType");
    private static String sql="select " +
            "  vs.PASSID ,CONCAT(substr( vs.vehicleId, 1,instr(vs.vehicleId,'_')-1) || '_',(select text from PLATECOLOR_TEXT where value=substr( vs.vehicleId, instr(vs.vehicleId,'_')+1))) AS vehicleId ,vs.enname ,vs.exname ," +
            " TO_CHAR(vs.entime,'YYYY-MM-DD HH24:MI:SS') as entime,TO_CHAR(vs.extime,'YYYY-MM-DD HH24:MI:SS') as extime,ROUND((vs.length/1000),1) as length," +
            " (vs.SCORE) AS SCORE,vs.iscompute,vs.INTEGRATIONLISTID " +
            " from vehicle_score vs " +
            " where 1=1 ";

    static{
        if("4".equals(DBType)){
            sql="select " +
                    "  vs.PASSID ,CONCAT(substr( vs.vehicleId, 1,instr(vs.vehicleId,'_')-1) , '_',(select text from PLATECOLOR_TEXT where value=substr( vs.vehicleId, instr(vs.vehicleId,'_')+1))) AS vehicleId ,vs.enname ,vs.exname ," +
                    " date_format(vs.entime,'%Y-%m-%d %H:%i:%s') as entime," +
                    " date_format(vs.extime,'%Y-%m-%d %H:%i:%s') as extime,ROUND((vs.length/1000),1) as length," +
                    " (vs.SCORE) AS SCORE,vs.iscompute,vs.INTEGRATIONLISTID " +
                    " from vehicle_score vs " +
                    " where 1=1 ";
        }
    }
    @Override
    public List getVehicleScoreSList(String vehicleId, String entime, String extime,String type) {
        List list=null;
        String sqlTemp=sql;
        try{
            if(!PubTools.isNull(vehicleId)){
                sqlTemp +=" and vs.vehicleId ='"+vehicleId+"'";
            }
            if(!PubTools.isNull(entime)){
                sqlTemp +=" and SUBSTR(to_char(vs.entime,'yyyy-MM-dd'),1,10)='"+entime+"'";
                if("4".equals(DBType)){
                    sqlTemp +=" and SUBSTR(date_format(vs.entime,'%Y-%m-%d'),1,10)='"+entime+"'";

                }
            }
            if(!PubTools.isNull(extime)){
                sqlTemp +=" and SUBSTR(to_char(vs.extime,'yyyy-MM-dd'),1,10)='"+extime+"'";
                if("4".equals(DBType)){
                    sqlTemp +=" and SUBSTR(date_format(vs.extime,'%Y-%m-%d'),1,10)='"+extime+"'";

                }
            }
            if(!PubTools.isNull(type)){
                if("VEHICLE_LENGTH".equals(type)){
                    sqlTemp +=" and vs.iscompute='Y' ";
                }
            }
            sqlTemp += " order by vs.extime";
            list= this.mapperI.selectforlist(sqlTemp);
        }catch (Exception e){
            LogUtil.WriteLog_Error("查询车辆里程积分失败===>"+e.getMessage(),"VehicleScoreServiceImpl");
            return list=null;
        }
        return list;
    }
}
