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

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
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, CONCAT(nbbm,'_',jsy,'_',jzl) as njj "
				+ " 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.nj not in (select CONCAT(nbbm,jsy) from bsth_c_ylxxb"
				+ " where yyrq='"+rq+"' and gsdm ='"+gsdm+"' AND jylx in (1,3))"
				+ " and x.njj not in (SELECT tj FROM bsth_c_ylxxb_update  "
				+ " where yyrq='"+rq+"' and zt='3')";

		//根具条件查询指定日期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);

		}



		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);
	        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");  

			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");
				String lsjsy =jsonObject.getString("lsjsy");
				String ldgh =jsonObject.getString("ldgh");
				if(!lsjsy.equals(jsy)){
					if(ldgh.indexOf(jsy)>-1){
						Ylxxb ylxxb=repository.findOne(id);
						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(ylxxb.getNbbm()+"_"+ylxxb.getJsy()+"_"+decimalFormat.format(ylxxb.getJzl()));
						ylxxbUpdate.setZt(3);
						updateRepository.save(ylxxbUpdate);
						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 fgsdm=map.get("fgsdm_like").toString();

		String nbbm=map.get("nbbm").toString();
		
	String sql="select * from ("
			+ " select t.*,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 in(0,2)  "
			+ " and t.nbbm  in (select c.inside_code from bsth_c_cars c "
			+ " where c.business_code ='"+gsdm+"' and c.sfdc=0 and c.branche_company_code ='"+fgsdm+"')"
			+ " and t.nbbm not in (select nbbm from bsth_c_ylb y "
			+ " where y.rq= '"+rq+"' and ssgsdm='"+gsdm+"' and fgsdm='"+fgsdm+"') "
			+ " union "
			+ " select t.*,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 in(0,2) "
			+ " and t.nbbm not in (	select c.inside_code from bsth_c_cars c  "
			+ " where c.business_code ='"+gsdm+"' and c.sfdc=0 )"
			+ " ) z"
			+ " where z.tj not in ("
			+ " SELECT x.tj FROM bsth_c_ylxxb_update x where x.yyrq='"+rq+"' and zt=2"
			+ " ) and z.nbbm like '%"+nbbm+"%'";
		
		//根具条件查询指定日期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);
	        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");  

			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(nbbm);
					ylxxbUpdate.setYyrq(ylxxb.getYyrq());
					ylxxbUpdate.setCreatetime(new Date());
					ylxxbUpdate.setTj(nbbm+"_"+ylxxb.getJsy()+"_"+decimalFormat.format(ylxxb.getJzl()));
					ylxxbUpdate.setZt(2);
					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;
	}
	@Override
	public Map<String, Object> saveYlxxb(Map<String, Object> map) {
		// TODO Auto-generated method stub
		Map<String, Object> newMap=new HashMap<String,Object>();
		try{
			Integer id=Integer.parseInt(map.get("id").toString());
			Ylxxb t=repository.findOne(id);
			t.setJzl(Double.parseDouble(map.get("jzl").toString().trim()));
			t.setNbbm(map.get("nbbm").toString().trim());
			t.setJsy(map.get("jsy").toString().trim());
			repository.save(t);
			newMap.put("status", ResponseCode.SUCCESS);
		}catch(Exception e){
			newMap.put("status", ResponseCode.ERROR);
			logger.error("save erro.", e);
			throw e;
		}
		return newMap;
	}
}