package cn.socket.quartz;


import cn.base.util.Struts2Utils;
import cn.socket.socket.WebSocketServer;
import cn.yw.repairmanage.service.YwrepairjobinfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: 清风一阵吹我心
 * @ProjectName: socket
 * @Package: com.socket.web.quartz
 * @ClassName: TestJob
 * @Description:
 * @CreateDate: 2018/11/12 12:50
 * @Version: 1.0
 **/
public class repaireInfoJob {

    private static Logger iLog = Logger.getLogger(repaireInfoJob.class);

    @Autowired
    private YwrepairjobinfoService service;
    public void doSomething() {

        //记录查询总数
        Integer  total=0;

        //获取WebSocketServer对象的映射。
        ConcurrentHashMap<String, WebSocketServer> map = WebSocketServer.getWebSocketMap();

        if (map.size() != 0) {
            for (Map.Entry<String, WebSocketServer> entry : map.entrySet()) {
                WebSocketServer webSocketServer = entry.getValue();
                    //置空
                    total=0;
                    //查询该用户下是否有未处理单号
                    Map repairejobmap = new HashMap();
                    String comprehensive = " (extend1='" + webSocketServer.getYhbh() + "') "
                            + " and jobprocessstate in ('901','902','904','906','908','913')";
                    repairejobmap.put("comprehensive", comprehensive);
                    //查询该用户名下单子，有就推送，没有不推送
                    try {
                        total= this.service.count_Ywrepairjobinfo(repairejobmap);
                        if(total.intValue()>0){
                            //向客户端推送消息
                            webSocketServer.getSession().getBasicRemote().sendText(total.toString());
                        }

                    }catch(Exception e){
                        e.printStackTrace();
                        iLog.error("websocket 查询用户待办单子" + e);
                    }

            }
        } else {
            iLog.info("WebSocket未连接" );
        }
    }
}
