DriverTask.java 11.8 KB
package com.trash.quartz.task;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.trash.casefile.domain.KafkaCompensation;
import com.trash.casefile.kafka.Consumer;
import com.trash.casefile.mapper.KafkaCompensationMapper;
import com.trash.common.core.redis.RedisCache;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.trash.activiti.service.IActTaskService;
import com.trash.business.domain.CompanyCredit;
import com.trash.business.domain.DriverCredit;
import com.trash.business.domain.SupervisionThreestep;
import com.trash.business.domain.TruckCredit;
import com.trash.business.mapper.SupervisionThreestepMapper;
import com.trash.business.service.ICompanyCreditService;
import com.trash.business.service.IDriverCreditService;
import com.trash.business.service.ISupervisionThreestepService;
import com.trash.business.service.ITruckActivateService;
import com.trash.business.service.ITruckCreditService;
import com.trash.common.config.trashConfig;
import com.trash.common.utils.LogUtils;
import com.trash.common.utils.RemoteServerUtils;
import com.trash.common.utils.spring.SpringUtils;
import com.trash.workflow.service.IWorkflowService;

/**
 * 定时任务调度测试
 * 
 * @author trash
 */
@Component("DriverTask")
public class DriverTask
{	

	String TOKEN;

	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
    /** 系统基础配置 */
    @Autowired
    private trashConfig trashConfig;

	@Autowired
	private RedisCache redisCache;

    public void checkCredit(){

    	if(RemoteServerUtils.remote == null){
    		RemoteServerUtils.remote = trashConfig.getRemotePath();
    	}
    	if(TOKEN == null){
    		TOKEN = trashConfig.getToken();
    	}

        LogUtils.getBlock("=================== checkCredit 定时器执行 当前时间:      " + simpleDateFormat.format(new Date()));
        
        

    	try {
        	checkTruckActive();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    }
    

    @SuppressWarnings("deprecation")
	private void checkTruckActive() {
    	int hour = new Date().getHours();
    	int minute = new Date().getMinutes();
    	if(hour < 8 && (hour==3 && minute>55)){
    		return;
    	}
    	
    	SupervisionThreestep param =new SupervisionThreestep();
    	
    	param.setType(0L);
    	param.setStatus(1L);
    	
    	String start, end;

		Date date = new Date();

		if (new Date().getHours() < 8) {
			end = sdf.format(date);
			date.setDate(date.getDate() - 1);
			start = sdf.format(date);
		} else {
			start = sdf.format(date);
			date.setDate(date.getDate() + 1);
			end = sdf.format(date);
		}

		end += " 8:00:00";
		start += " 8:00:00";

		try {
			param.setWorkStartTime(sdf.parse(start));
			param.setWorkEndTime(sdf.parse(end));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
    	
    	List<SupervisionThreestep> cs = SpringUtils.getBean(SupervisionThreestepMapper.class).selectTodayDataList(param);
    	
    	for(SupervisionThreestep c:cs){
        	if(hour < 8 && (hour==3 && minute>55)){
        		return;
        	}
    		try {
     	       SpringUtils.getBean(ISupervisionThreestepService.class).checkDataToActiveTruck(c,TOKEN);
			} catch (Exception e) {
				// TODO: handle exception
			}
    	}

	}


    
	private void checkDriverCredit() {
		
    	if(RemoteServerUtils.remote == null){
    		RemoteServerUtils.remote = trashConfig.getRemotePath();
    	}
    	if(TOKEN == null){
    		TOKEN = trashConfig.getToken();
    	}

        JSONArray drivers = RemoteServerUtils.getDriverList(TOKEN);
        
        
        for(Object object :drivers){
        	
        	JSONObject json = (JSONObject)object;
        	
			try {
				if(sdf.parse(json.getString("endAt")).getTime() < sdf.parse(sdf.format(new Date())).getTime() || 
						sdf.parse(json.getString("qualificationCertValidEndAt")).getTime() < sdf.parse(sdf.format(new Date())).getTime()){
					DriverCredit driver = new DriverCredit();
					
					driver.setIdNumber(json.getString("identityNo"));
					driver.setName(json.getString("name"));
					driver.setCreateBy("定时器");
					driver.setReason("证件过期");
					driver.setTime(new Date());
					driver.setStatus(0L);
					driver.setObjectId(json.getString("id"));
					

					SpringUtils.getBean(IDriverCreditService.class).insertDriverCredit(driver);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        }
		
	}
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void checkTruckCredit() {
		Map map = new HashMap<>();
        map.put("size",9999);
        map.put("dishonestState",0);
        map.put("valid",0);
        map.put("page",1);
        
        JSONArray trucks = RemoteServerUtils.getTruckList(map,TOKEN);
    	
        List<Map> listParam = new ArrayList<Map>();
        
        if(trucks!=null){
        	for(Object jsonObject:trucks){
    			JSONObject truck = (JSONObject)jsonObject;
    			    			
    			try {
					if(sdf.parse(truck.getString("transportCertValid")).getTime() < sdf.parse(sdf.format(new Date())).getTime() || 
							sdf.parse(truck.getString("licenseValid")).getTime() < sdf.parse(sdf.format(new Date())).getTime()){
						
						TruckCredit truckCredit = new TruckCredit();
						truckCredit.setTime(new Date());
						truckCredit.setCreateBy("定时器");
						truckCredit.setReason("证件过期");
						truckCredit.setLostCredit(1L);
						truckCredit.setObjectId(truck.getString("id"));
						truckCredit.setLicensePlate(truck.getString("licenseplateNo"));
						truckCredit.setCompanyId(truck.getString("companyName"));
						truckCredit.setStatus(0L);
						
						Map param = new HashMap<>();
						
						param.put("id", truck.getString("id"));
						param.put("creditStatus", 1);
						
						listParam.add(param);
						
						SpringUtils.getBean(ITruckCreditService.class).insertTruckCredit(truckCredit);
					}
				} catch (BeansException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        	
        	if(listParam.size() > 0)
        		RemoteServerUtils.updateTruckList(listParam,TOKEN);
        	
        }
    	
    }
    
	public void checkCompanyCredit() {
    	try {
        	checkTruckCredit();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	try {
        	checkDriverCredit(); //检查驾驶员信用
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		
       	Map map = new HashMap<>();
        map.put("size",9999);
        map.put("dishonestState",0);
        map.put("page",1);
        
    	JSONArray companys = RemoteServerUtils.getCompanyList(map,TOKEN);
    	if(companys != null){
    		for(Object jsonObject:companys){
    			JSONObject company = (JSONObject)jsonObject;
    			try {
    				if(sdf.parse(company.getString("registrationValidTime")).getTime() < sdf.parse(sdf.format(new Date())).getTime() 
    						|| sdf.parse(company.getString("businessLicenseValidTime")).getTime() < sdf.parse(sdf.format(new Date())).getTime()){
        				CompanyCredit companyCredit = new CompanyCredit();
        				companyCredit.setTime(new Date());
        				companyCredit.setCreateBy("定时器");
        				companyCredit.setReason("证件过期");
        				companyCredit.setLostCredit(1L);
        				companyCredit.setObjectId(company.getString("id"));
        				companyCredit.setName(company.getString("name"));
        				companyCredit.setPlace(company.getString("areaName"));
        				companyCredit.setStatus(0L);
        		    	SpringUtils.getBean(ICompanyCreditService.class).insertCompanyCredit(companyCredit,TOKEN);
        			}
				} catch (Exception e) {
					e.printStackTrace();
				}
    			
    		}
    	}
    	
    	CompanyCredit companyCredit = new CompanyCredit();
    	companyCredit.setLostCredit(1L);
    	companyCredit.setStatus(0L);
    		
    	List<CompanyCredit> companyList = SpringUtils.getBean(ICompanyCreditService.class).selectCompanyCreditList(companyCredit);
    	
    	for(CompanyCredit c:companyList){
    		SpringUtils.getBean(ICompanyCreditService.class).updateRemoteCompanyAndTruck(c,TOKEN);
    	}
	}

	public void checkAllTask()
    {	
        LogUtils.getBlock("===================  删除超时报工数据     定时器执行 当前时间:      " + simpleDateFormat.format(new Date()));
        
    	if(TOKEN == null){
    		TOKEN = trashConfig.getToken();
    	}
    	SpringUtils.getBean(ISupervisionThreestepService.class).updateTodayData(TOKEN);
        
    	SpringUtils.getBean(IActTaskService.class).endAllThreesteptask("workflow_threestep");
    	
    	SpringUtils.getBean(IWorkflowService.class).deleteWorkflowByName("workflow_threestep");

        SpringUtils.getBean(ITruckActivateService.class).endAllTruckUnActive();
        
    	
    }

	/**
	 * kafka补偿机制,每半小时一次
	 * @throws InterruptedException
	 */
	public void kafkaCompensation() throws InterruptedException, IOException {
		KafkaCompensation kafkaCompensation = new KafkaCompensation();
		kafkaCompensation.setStatus(0);
		List<KafkaCompensation> kafkaCompensationList = SpringUtils.getBean(KafkaCompensationMapper.class).selectKafkaCompensationList(kafkaCompensation);
		for(KafkaCompensation k:kafkaCompensationList){
			SpringUtils.getBean(Consumer.class).autoViolationWarning(k.getData(),k.getId().toString());
		}
	}

	/**
	 * 每分钟更新一次公司列表
	 */
	public void getCompanyList(){
		if(RemoteServerUtils.remote == null){
			RemoteServerUtils.remote = trashConfig.getRemotePath();
		}
		Map<String, Object> map = new HashMap<>();
		map.put("size",9999);
		map.put("page",1);
		JSONArray jsonArray = RemoteServerUtils.getCompanyList(map, trashConfig.getToken());
		redisCache.setCacheObject("companyList",jsonArray);
	}
	
	public void getContractList(){
		if(RemoteServerUtils.remote == null){
			RemoteServerUtils.remote = trashConfig.getRemotePath();
		}
		Map<String, Object> map = new HashMap<>();
		map.put("size",9999);
		map.put("page",1);
		map.put("contractStatus", 1);
		map.put("auditStatus", 1);
		JSONArray jsonArray = RemoteServerUtils.getContractList(map, trashConfig.getToken());
		redisCache.setCacheObject("contractList",jsonArray);
	}

	/**
	 * 每分钟更新一次工地列表
	 */
	public void getConstructionList(){
		if(RemoteServerUtils.remote == null){
			RemoteServerUtils.remote = trashConfig.getRemotePath();
		}
		Map<String, Object> map = new HashMap<>();
		map.put("size",9999);
		map.put("page",1);
		JSONArray jsonArray = RemoteServerUtils.getConstructionList(map,trashConfig.getToken());
		redisCache.setCacheObject("constructionList",jsonArray);
	}

	/**
	 * 每分钟更新一次消纳场列表
	 */
	public void getEarthSitesList(){
		if(RemoteServerUtils.remote == null){
			RemoteServerUtils.remote = trashConfig.getRemotePath();
		}
		Map<String, Object> map = new HashMap<>();
		map.put("size",9999);
		map.put("page",1);
		JSONArray jsonArray = RemoteServerUtils.getEarthSitesList(map,trashConfig.getToken());
		redisCache.setCacheObject("earthSitesList",jsonArray);
	}

	/**
	 * 每分钟更新一次区域列表
	 */
	public void getAreaList(){
		if(RemoteServerUtils.remote == null){
			RemoteServerUtils.remote = trashConfig.getRemotePath();
		}
		Map<String, Object> map = new HashMap<>();
		map.put("size",9999);
		map.put("page",1);
		JSONArray jsonArray = RemoteServerUtils.getAreas(trashConfig.getToken());
		redisCache.setCacheObject("areaList",jsonArray);
	}
}