YlxxbServiceImpl.java 9.63 KB
package com.bsth.service.oil.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
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 javax.transaction.Transactional;

import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.common.ResponseCode;
import com.bsth.entity.excep.Offline;
import com.bsth.entity.oil.Ylb;
import com.bsth.entity.oil.Ylxxb;
import com.bsth.entity.oil.YlxxbUpdate;
import com.bsth.entity.search.CustomerSpecs;
import com.bsth.repository.oil.YlbRepository;
import com.bsth.repository.oil.YlxxbRepository;
import com.bsth.repository.oil.YlxxbUpdateRepository;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.oil.YlxxbService;
import com.bsth.util.PageHelper;
import com.bsth.util.PageObject;

@Service
public class YlxxbServiceImpl extends BaseServiceImpl<Ylxxb,Integer> implements YlxxbService
{
	Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	YlxxbRepository repository;
	@Autowired
	YlxxbUpdateRepository updateRepository;
	@Autowired
	YlbRepository ylbRepository;
	@Autowired
	JdbcTemplate jdbcTemplate;
	@Override
	public PageObject<Ylxxb> Pagequery(Map<String, Object> map) {

		String rq=map.get("yyrq").toString();
		String gsdm=map.get("gsdm_like").toString();
		String fgsdm=map.get("fgsdm_like").toString();

		String sql="	select * from ("
				+ "select *,CONCAT(nbbm,jsy)  as nj "
				+ " from bsth_c_ylxxb where yyrq='"+rq+"' "
				+ " and gsdm ='"+gsdm+"' and jylx !='1') x where x.nj not in ("
				+ " select CONCAT(nbbm,jsy)  from bsth_c_ylb "
				+ " where rq='"+rq+"'"
				+ " and ssgsdm='"+gsdm+"' and fgsdm ='"+fgsdm+"')"
				+ " and x.nbbm in (select nbbm  from bsth_c_ylb "
				+ " where rq='"+rq+"'"
				+ " and ssgsdm='"+gsdm+"' and fgsdm ='"+fgsdm+"') "
				+ " and x.nbbm not in (select nbbm from bsth_c_ylxxb"
				+ " where yyrq='"+rq+"' and gsdm ='"+gsdm+"' AND jylx !='0')";


		/*String sql= "select v.*,u.jsy as ldgh  from "
				+ " ( select *  from   bsth_c_ylxxb x  where  "
				+ " DATE_FORMAT(x.yyrq,'%Y-%m-%d')='"+rq+"'  and x.gsdm='"+gsdm+"'"
				+ " and x.jsy not  in  ("
				+ " select jsy  from bsth_c_ylb "
				+ "  where DATE_FORMAT(rq,'%Y-%m-%d')='"+rq+"' "
				+ " and ssgsdm='"+gsdm+"'  and fgsdm='"+fgsdm+"')"
				+ " and x.nbbm  in (select nbbm  from bsth_c_ylb  "
				+ " where DATE_FORMAT(rq,'%Y-%m-%d')='"+rq+"' "
				+ " and ssgsdm='"+gsdm+"'  and fgsdm='"+fgsdm+"' )) v "
				+ " left join (select * from  bsth_c_ylb "
				+ " where DATE_FORMAT(rq,'%Y-%m-%d')='"+rq+"'"
				+ " and ssgsdm='"+gsdm+"'  and fgsdm='"+fgsdm+"' )  u "
				+ " on  v.nbbm=u.nbbm ";*/

		//根具条件查询指定日期Ylxxb的数据
//		List<Ylxxb> iterator=repository.checkYlxx(rq,gsdm);
		/*if(map.get("gsdm_in")!=null){
			map.put("ssgsdm_in", map.get("gsdm_in"));
			map.remove("gsdm_in");

		}else{
			map.put("ssgsdm_like", map.get("gsdm_like"));
			map.remove("gsdm_like");
		}*/

		//根具条件查询指定日期Ylb的数据
//		List<Ylb> ylbIterator=ylbRepository.checkYl(rq,gsdm,fgsdm);
		List<Ylxxb> list=jdbcTemplate.query(sql,
				new RowMapper<Ylxxb>(){
					@Override
					public Ylxxb  mapRow(ResultSet rs, int rowNum) throws SQLException {
						Ylxxb s  = new Ylxxb();
						s.setId(rs.getInt("id"));
						s.setYyrq(rs.getDate("yyrq"));
						s.setNbbm(rs.getString("nbbm"));
						s.setGsdm(rs.getString("gsdm"));
						s.setFgsdm(rs.getString("fgsdm"));
						s.setJsy(rs.getString("jsy"));
						s.setJzl(rs.getDouble("jzl"));
						s.setStationid(rs.getString("stationid"));
						s.setNylx(rs.getInt("nylx"));
						s.setJyggh(rs.getString("jyggh"));
						s.setYj(rs.getDouble("yj"));
//            	s.setLdgh(rs.getString("ldgh"));
						s.setBz(rs.getString("bz"));
						return s;
					}
				});


		List<Ylb> listylb=ylbRepository.obtainYl(rq, gsdm, fgsdm, "", "", "nbbm");
		for (int i = 0; i < list.size(); i++) {
			String ldgh="";
			for (int j = 0; j < listylb.size(); j++) {
				Ylb ylb =listylb.get(j);
				if(list.get(i).getNbbm().equals(ylb.getNbbm())){
					if(ldgh.equals(""))
						ldgh +=ylb.getJsy();
					else
						ldgh += ","+ylb.getJsy();
				}
			}
			list.get(i).setLdgh(ldgh);

		}
		//正式使用用下面代码
//		for (int i = 0; i < iterator.size(); i++) {
//			Ylxxb y1=iterator.get(i);
//			boolean fage=true;
//			String ldgh="";
//			for (int j = 0; j < ylbIterator.size(); j++) {
//				Ylb	 y2 = ylbIterator.get(j);
//				if(y1.getNbbm().equals(y2.getNbbm())){
//					if(y1.getJsy().equals(y2.getJsy())){
//						fage=false;
//					}
//					ldgh +=y2.getJsy()+"/";
//				}
//			}
//			if(fage){
//				y1.setLdgh(ldgh);
//				list.add(y1);
//			}
//		}



		PageHelper pageHelper = new PageHelper(list.size(), map);
		pageHelper.getMap();
		PageObject<Ylxxb> pageObject = pageHelper.getPageObject();
		pageObject.setDataList(list);
		return pageObject;
	}
	@Transactional
	@Override
	public Map<String, Object> checkJsy(Map<String, Object> map) throws Exception{
		Map<String, Object> newMap=new HashMap<String,Object>();
		// TODO Auto-generated method stub
		try {
//		int id=Integer.parseInt(map.get("id").toString());
//		String jsy=map.get("jsy").toString();
//		Ylxxb ylxxb=repository.findOne(id);
//		ylxxb.setJsy(jsy);
//		repository.save(ylxxb);
			String json =StringEscapeUtils.unescapeHtml4(map.get("ylbList").toString());
			JSONArray jsonArray=JSONArray.parseArray(json);
			JSONObject jsonObject;
			for (int x = 0; x < jsonArray.size(); x++) {
				jsonObject=jsonArray.getJSONObject(x);
				Integer id =jsonObject.getInteger("id");
				String jsy =jsonObject.getString("jsy");
				Ylxxb ylxxb=repository.findOne(id);
				if(!ylxxb.getJsy().equals(jsy)){
					ylxxb.setJsy(jsy);
					ylxxb.setJylx(3);
					repository.save(ylxxb);
				}
			}
			newMap.put("status", ResponseCode.SUCCESS);
		}catch(Exception e){
			newMap.put("status", ResponseCode.ERROR);
			logger.error("save erro.", e);
			throw e;
		}
		return newMap;
	}
	@Override
	public List<Ylxxb> Pagequery_nbbm(Map<String, Object> map) {
		// TODO Auto-generated method stub
		String rq=map.get("yyrq").toString();
		String gsdm=map.get("gsdm_like").toString();
		String nbbm=map.get("nbbm").toString();
		
	String sql="select * from (select *,CONCAT(t.nbbm,'_',t.jsy,'_',t.jzl) as tj "
			+ " from bsth_c_ylxxb t where t.yyrq='"+rq+"' and t.gsdm='"+gsdm+"' and "
			+ " t.nylx=0 and t.jylx=0 and t.nbbm like '%"+nbbm+"%') y "
			+ " where y.nbbm not in (select c.inside_code from bsth_c_cars c "
			+ " where c.business_code ='05' and c.sfdc=0) and y.tj not in ("
			+ " select x.tj from bsth_c_ylxxb_update x)";
		
		//根具条件查询指定日期Ylb的数据
//		List<Ylb> ylbIterator=ylbRepository.checkYl(rq,gsdm,fgsdm);
		List<Ylxxb> list=jdbcTemplate.query(sql,  
		        new RowMapper<Ylxxb>(){  
            @Override  
            public Ylxxb  mapRow(ResultSet rs, int rowNum) throws SQLException {  
            	Ylxxb s  = new Ylxxb(); 
            	s.setId(rs.getInt("id"));
            	s.setYyrq(rs.getDate("yyrq"));
            	s.setNbbm(rs.getString("nbbm"));
            	s.setGsdm(rs.getString("gsdm"));
            	s.setJsy(rs.getString("jsy"));
            	s.setJzl(rs.getDouble("jzl"));
            	s.setStationid(rs.getString("stationid"));
            	s.setNylx(rs.getInt("nylx"));
            	s.setJyggh(rs.getString("jyggh"));
            	s.setYj(rs.getDouble("yj"));
//            	s.setLdgh(rs.getString("ldgh"));
            	s.setBz(rs.getString("bz"));
                return s;  
            }  
		});  
		return list;
	}


	@Transactional  
	@Override
	public Map<String, Object> checkNbbm(Map<String, Object> map) throws Exception{
		Map<String, Object> newMap=new HashMap<String,Object>();
		// TODO Auto-generated method stub
		try {
//		int id=Integer.parseInt(map.get("id").toString());
//		String jsy=map.get("jsy").toString();
//		Ylxxb ylxxb=repository.findOne(id);
//		ylxxb.setJsy(jsy);
//		repository.save(ylxxb);
			String json =StringEscapeUtils.unescapeHtml4(map.get("ylbList").toString());
			JSONArray jsonArray=JSONArray.parseArray(json);
			JSONObject jsonObject;
			for (int x = 0; x < jsonArray.size(); x++) {
				jsonObject=jsonArray.getJSONObject(x);
				Integer id =jsonObject.getInteger("id");
				String nbbm =jsonObject.getString("nbbm").trim();
				Ylxxb ylxxb=repository.findOne(id);
				if(!ylxxb.getNbbm().equals(nbbm)){
					ylxxb.setNbbm(nbbm);
					ylxxb.setJylx(2);
					repository.save(ylxxb);
					YlxxbUpdate ylxxbUpdate=new YlxxbUpdate();
					ylxxbUpdate.setGsdm(ylxxb.getGsdm());
					ylxxbUpdate.setJsy(ylxxb.getJsy());
					ylxxbUpdate.setJzl(ylxxb.getJzl());
					ylxxbUpdate.setJlrq(ylxxb.getJlrq());
					ylxxbUpdate.setNbbm(ylxxb.getNbbm());
					ylxxbUpdate.setYyrq(ylxxb.getYyrq());
					ylxxbUpdate.setCreatetime(new Date());
					ylxxbUpdate.setTj(nbbm+"_"+ylxxb.getJsy()+"_"+ylxxb.getJzl());
					updateRepository.save(ylxxbUpdate);
				}
			}
		newMap.put("status", ResponseCode.SUCCESS);
		}catch(Exception e){
			newMap.put("status", ResponseCode.ERROR);
			logger.error("save erro.", e);
			throw e;
		}
		return newMap;
	}
}