package abacus.springboot.example.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import abacus.springboot.example.dao.JcBill;
import abacus.springboot.example.dao.JcBillHg;
import abacus.springboot.example.dao.JcBillPhoto;
import abacus.springboot.example.dao.JcBillSourceItem;
import abacus.springboot.example.dao.JcSourceItem;
import abacus.springboot.example.pi.JcBillAccessIF;
import abacus.springboot.example.util.DateUtil;
import abacus.springboot.example.vo.BaiduToken;
import abacus.springboot.example.vo.SiDepositlocation;

/**
 * <p>Title:ApiJcBillAccess</p>
   <p>Description: 底层实现</p>
   @author chuanZeng 
   @date 2026年8月18日
 */
@Repository
public class JcBillAccess extends MyJpaUtils implements JcBillAccessIF {

	private static final Logger LOG = LoggerFactory.getLogger(JcBillAccess.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@PersistenceContext
	private EntityManager em;

	@Override
	public List<JcBillPhoto> queryJcBillPhoto(String srcid, String srcitemid, String typephoto)
			throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			List<Object> args = new ArrayList<Object>();
			StringBuffer sb = new StringBuffer("select id, version, srcid, srcitemid, typephoto, urlphoto, dt from jc_bill_photo a");
			
			if(StringUtils.isNotBlank(srcid)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.srcid = ? ");
				args.add(srcid);
				params.put("srcid", srcid);
			}
			if(StringUtils.isNotBlank(srcitemid)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.srcitemid = ? ");
				args.add(srcitemid);
				params.put("srcitemid", srcitemid);
			}
			if(StringUtils.isNotBlank(typephoto)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.typephoto = ? ");
				args.add(typephoto);
				params.put("typephoto", typephoto);
			}
			
			LOG.info("queryJcBillPhoto>>>SQL>>>{}", sb.toString());
			
			List<JcBillPhoto> lists = jdbcTemplate.query( sb.toString(), args.toArray(), new BeanPropertyRowMapper<JcBillPhoto>(JcBillPhoto.class));
			
			return lists;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	public List<JcBillPhoto> queryJcBillPhotoIn(List<String> srcids) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			List<Object> args = new ArrayList<Object>();
			StringBuffer sb = new StringBuffer("select id, version, srcid, srcitemid, typephoto, urlphoto, dt from jc_bill_photo a");
			
			if(srcids != null && srcids.size() >= 1) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.srcid in (");
				for(int i = 0; i < srcids.size(); i++){
					if(i > 0) sb.append(",");
					sb.append("?");
					args.add(srcids.get(i));
				}
				sb.append(") ");
			}
			
			LOG.info("queryJcBillPhotoIn>>>SQL>>>{}", sb.toString());
			
			List<JcBillPhoto> lists = jdbcTemplate.query( sb.toString(), args.toArray(), new BeanPropertyRowMapper<JcBillPhoto>(JcBillPhoto.class));
			
			return lists;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public void updateJcBill(JcBill bill) throws RuntimeException {
		try {
			List<Object> args = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer(" update jc_bill ");
			sql.append(" set status = ?,");
			sql.append(" warehouse = ?,");
			sql.append(" warehousename = ?,");
			sql.append(" receivedt = ?,");
			sql.append(" quantity = ?,");
			sql.append(" subtotal = ?,");
			sql.append(" applicant = ?,");
			sql.append(" applicantname = ?,");
			sql.append(" applicantdt = ?");
			sql.append(" where id = ?");
			
			args.add(bill.getStatus());
			args.add(bill.getWarehouse());
			args.add(bill.getWarehousename());
			args.add(bill.getReceivedt());
			args.add(bill.getQuantity());
			args.add(bill.getSubtotal());
			args.add(bill.getApplicant());
			args.add(bill.getApplicantname());
			args.add(DateUtil.formatDateTime(bill.getApplicantdt()));
			args.add(bill.getId());
			
			this.jdbcTemplate.update(sql.toString(), args.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public void updateJcBillSourceItem(List<JcBillSourceItem> items) throws RuntimeException {
		try {
			String sql = "update jc_billsource_item set recsource = ?, recplatform = ?, channel = ?, price = ?, quantity = ?,"
					+ " numbers = ?, subtotal = ?, goodsid = ?, logisticsid = ?"
					+ " where id = ?";
			
			this.jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
				
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					JcBillSourceItem item = items.get(i);
					ps.setString(1, item.getRecsource());
					ps.setString(2, item.getRecplatform());
					ps.setString(3, item.getChannel());
					ps.setDouble(4, item.getPrice());
					ps.setDouble(5, item.getQuantity());
					
					ps.setDouble(6, item.getNumbers());
					ps.setDouble(7, item.getSubtotal());
					ps.setString(8, item.getGoodsid());
					ps.setString(9, item.getLogisticsid());
					
					ps.setLong(10, item.getId());
					
				}
				
				public int getBatchSize() {
					return items.size();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public void updateJcBillPhoto(List<JcBillPhoto> photos) throws RuntimeException {
		try {
			String sql = "update jc_bill_photo set urlphoto = ?, dt = ? where srcid = ? and srcitemid = ? and typephoto = ?";
			
			this.jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
				
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					JcBillPhoto item = photos.get(i);
					ps.setString(1, item.getUrlphoto());
					ps.setDate(2, new java.sql.Date(item.getDt().getTime()));
					ps.setString(3, item.getSrcid());
					ps.setString(4, item.getSrcitemid());
					ps.setString(5, item.getTypephoto());
					
				}
				
				public int getBatchSize() {
					return photos.size();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public int countJcBillItemIndexno(String billId) throws RuntimeException {
		try {
			StringBuffer sb = new StringBuffer(" select max(indexno) from jc_billsource_item a where a.billid = ?");
			
			Query q = this.em.createNativeQuery(sb.toString()).setParameter(1, billId);
			
			return q.getSingleResult() == null ? 0 : Integer.valueOf(q.getSingleResult().toString());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public String lastBillHgId() throws RuntimeException {
		try {
			StringBuffer sb = new StringBuffer(" select top 1 id from jc_billhg a where a.status = 1 order by founddt asc ");
			
			List<JcBill> lists = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<JcBill>(JcBill.class));
			
			return (lists != null && lists.size() >= 1) ? lists.get(0).getId() : "";
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<JcSourceItem> queryJcSourceItemByBillId(String billId) {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			List<Object> args = new ArrayList<Object>();
			StringBuffer sb = new StringBuffer("select id, version, indexno, billid, srcitemid,");
			sb.append(" goodsid, logisticsid, billout, billoutdt, billoutwh,");
			sb.append(" billoutwhname, product, productname, account, accountname,");
			sb.append(" operator, operatorname, groupid, groupname, salearea,");
			sb.append(" saleareaname, source, dt");
			sb.append(" from jc_source_item a");
			
			if(StringUtils.isNotBlank(billId)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billid = ? ");
				args.add(billId);
				params.put("billId", billId);
			}
			
			LOG.info("queryJcSourceItemByBillId>>>SQL>>>{}", sb.toString());
			
			List<JcSourceItem> lists = jdbcTemplate.query( sb.toString(), args.toArray(), new BeanPropertyRowMapper<JcSourceItem>(JcSourceItem.class));
			
			return lists;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public void deleteJcSourceItemBySource(String billId) throws RuntimeException {
		try {
			StringBuffer sb = new StringBuffer(" delete from jc_source_item where billid = ? and source in ('业务中台', '二维码')");
			
			LOG.info("deleteJcSourceItemBySource>>>SQL>>>{}", sb.toString());
			
			jdbcTemplate.update(sb.toString(), billId);
			
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public Page<JcBill> queryJcBill(String fromDate, String thruDate, String applicant, String goods, String id,
			String billin, String goodsid, String product, String status, int typ, Pageable pageable) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			List<Object> args = new ArrayList<Object>();
			StringBuffer sb = new StringBuffer("");
			
			if(StringUtils.isNotBlank(fromDate)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.applicantdt >= ? ");
				args.add(fromDate);
				params.put("fromDate", fromDate);
			}
			if(StringUtils.isNotBlank(thruDate)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.applicantdt <= ? ");
				args.add(thruDate);
				params.put("thruDate", thruDate);
			}
			if(StringUtils.isNotBlank(applicant)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.applicant = ? ");
				args.add(applicant);
				params.put("applicant", applicant);
			}
			if(StringUtils.isNotBlank(goods)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.goods = ? ");
				args.add(goods);
				params.put("goods", goods);
			}
			if(StringUtils.isNotBlank(id)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.id = ? ");
				args.add(id);
				params.put("id", id);
			}
			if(StringUtils.isNotBlank(billin)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.billin = ? ");
				args.add(billin);
				params.put("billin", billin);
			}
			if(StringUtils.isNotBlank(status)) {
				if(typ == 1) {
					sb.append(params.size() > 0 ? " and ":" where ");
					sb.append(" a.status = ? ");
					args.add(Integer.parseInt(status));
					params.put("status", Integer.parseInt(status));
				} else {
					
					if("1".equals(status) || "2".equals(status)) {
						sb.append(params.size() > 0 ? " and ":" where ");
						sb.append(" a.hgstatus = ? ");
						args.add(Integer.parseInt(status));
						params.put("hgstatus", Integer.parseInt(status));
					} else {
						sb.append(params.size() > 0 ? " and ":" where ");
						sb.append(" a.status = ? ");
						args.add(Integer.parseInt(status));
						params.put("status", Integer.parseInt(status));
					}
					
				}
				
			}else {
				if(typ == 1) {
					sb.append(params.size() > 0 ? " and ":" where ");
					sb.append(" a.status in (-3, -2, 2, 3, 4, 5) ");
					params.put("status", status);
				} else {
					sb.append(params.size() > 0 ? " and ":" where ");
					sb.append(" (a.status in (3, 4, 5) or a.hgstatus in (1, 2))");
					params.put("status", status);
				}
			}
			if(StringUtils.isNotBlank(goodsid)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.id in (select b.billid from jc_billsource_item b where b.goods like ? ) ");
				args.add("%" + goodsid + "%");
				params.put("goodsid", goodsid);
			}
			if(StringUtils.isNotBlank(product)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.id in (select b.billid from jc_billsource_item b where b.product = ? ) ");
				args.add(product);
				params.put("product", product);
			}
			
			StringBuffer orderBy = new StringBuffer("");
			
			orderBy.append(" order by dt desc ");
			
			orderBy.append(" offset "+ (pageable.getPageNumber() * pageable.getPageSize())  + " rows fetch next "+ pageable.getPageSize() + " rows only ");

			
			StringBuffer sql = new StringBuffer("select id, version, status, hgstatus, goods, warehouse, warehousename,");
			sql.append(" receivedt, quantity, subtotal, billin, billindt, applicant, applicantname, applicantdt, dt");
			sql.append(" from jc_bill a ");
			sql.append(sb.toString());
			sql.append(orderBy.toString());
			
			LOG.info("queryJcBill>>>SQL>>>{}", sql.toString());
			
			List<JcBill> lists = jdbcTemplate.query(sql.toString(), args.toArray(), new BeanPropertyRowMapper<JcBill>(JcBill.class));
			
			Long count = jdbcTemplate.queryForObject("select count(a.id) from jc_bill a " + sb.toString(), args.toArray(), Long.class);

			Page<JcBill> scorePage = new PageImpl<JcBill>(lists, pageable, count);
			return scorePage;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public void saveEvent(String srcid, String typ, String billtyp) throws RuntimeException {
		try {
			String sql="insert into si_siwdevent(srcid, typ, billtyp, deptid, stauts,"
					+ " node, remake, dt, inputdate)"
					+ " values (?, ?, ?, ?, ?,"
					+ " ?, ?, ?, ?) ";
			Query query = em.createNativeQuery(sql);
			query.setParameter(1, srcid);
			query.setParameter(2, typ);
			query.setParameter(3, billtyp);
			query.setParameter(4, "");
			query.setParameter(5, 0);
			
			query.setParameter(6, 0);
			query.setParameter(7, "");
			query.setParameter(8, new Date());
			query.setParameter(9, new Date());
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public Page<JcBillHg> queryJcBillHg(String fromDate, String thruDate, String account, String salearea, String id,
			String billId, String status, String product, String manager, int typ, Pageable pageable)
			throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			List<Object> args = new ArrayList<Object>();
			StringBuffer sb = new StringBuffer("");
			
			if(StringUtils.isNotBlank(fromDate)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.founddt >= ? ");
				args.add(fromDate);
				params.put("fromDate", fromDate);
			}
			if(StringUtils.isNotBlank(thruDate)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.founddt <= ? ");
				args.add(thruDate);
				params.put("thruDate", thruDate);
			}
			if(StringUtils.isNotBlank(account)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.account = ? ");
				args.add(account);
				params.put("account", account);
			}
			if(StringUtils.isNotBlank(salearea)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.salearea = ? ");
				args.add(salearea);
				params.put("salearea", salearea);
			}
			if(StringUtils.isNotBlank(id)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.id = ? ");
				args.add(id);
				params.put("id", id);
			}
			if(StringUtils.isNotBlank(billId)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.billid = ? ");
				args.add(billId);
				params.put("billId", billId);
			}
			if(StringUtils.isNotBlank(status)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.status = ? ");
				args.add(Integer.parseInt(status));
				params.put("status", Integer.parseInt(status));
			} else {
				if(typ == 1) {
					sb.append(params.size() > 0 ? " and ":" where ");
					sb.append(" a.status in (1, 2, 3) ");
					params.put("status", status);
				} else {
					sb.append(params.size() > 0 ? " and ":" where ");
					sb.append(" a.status in (4, 5, 6) ");
					params.put("status", status);
				}
			}
			
			if(StringUtils.isNotBlank(product)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.id in (select b.billhg from jc_billin_item b where b.product = ? ) ");
				args.add(product);
				params.put("product", product);
			}
			if(StringUtils.isNotBlank(manager)) {
				sb.append(params.size() > 0 ? " and ":" where ");
				sb.append(" a.manager = ? ");
				args.add(manager);
				params.put("manager", manager);
			}
			
			
			StringBuffer orderBy = new StringBuffer("");
			
			orderBy.append(" order by founddt desc ");
			
			orderBy.append(" offset "+ (pageable.getPageNumber() * pageable.getPageSize())  + " rows fetch next "+ pageable.getPageSize() + " rows only ");

			
			StringBuffer sql = new StringBuffer("select id, version, billid, status, source,");
			sql.append(" account, accountname, quantity, subtotal, freight,");
			sql.append(" manager, managername, consignee, consigneephone, province,");
			sql.append(" provincename, city, cityname, area, areaname,");
			sql.append(" address, receiveway, remark, remark1, salearea,");
			sql.append(" saleareaname, groupid, groupname, logisticsname, logisticscode,");
			sql.append(" logisticstype, logisticsno, found, foundname,");
			sql.append(" founddt, submit, submitname, submitdt, audit,");
			sql.append(" auditname, auditdt, dt, fhwarehouse, fhwarehousename,");
			sql.append(" wdtorder, wdtbillout, wdtbillouttyp");
			sql.append(" ,(select b.status from jc_bill b where b.id = a.billid ) as jcstatus");
			sql.append(" from jc_billhg a");
			sql.append(sb.toString());
			sql.append(orderBy.toString());
			
			LOG.info("queryJcBillHg>>>SQL>>>{}", sb.toString());
			
			List<JcBillHg> lists = jdbcTemplate.query(sql.toString(), args.toArray(), new BeanPropertyRowMapper<JcBillHg>(JcBillHg.class));
			
			Long count = jdbcTemplate.queryForObject("select count(a.id) from jc_billhg a " + sb.toString(), args.toArray(), Long.class);

			Page<JcBillHg> scorePage = new PageImpl<JcBillHg>(lists, pageable, count);
			return scorePage;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public void deleteJcSourceItem(List<Long> ids) throws RuntimeException {
		if(ids==null||ids.isEmpty()) return;
		for(int i=0;i<ids.size();i+=500){
			int end=(i+500)>ids.size()?ids.size():(i+500);
			em.createNativeQuery("delete from jc_source_item where id in (:ids) ").setParameter("ids", ids.subList(i, end)).executeUpdate();
		}
		
	}

	@Override
	public void deleteJcBillPhoto(String srcid, String srcitemid, String type) throws RuntimeException {
		try {
			StringBuffer sb = new StringBuffer(" delete from jc_bill_photo where"
					+ " srcid = ?"
//					+ " and srcitemid = ?"
					+ " and typephoto = ?");
			
			LOG.info("deleteJcBillPhoto>>>SQL>>>{}", sb.toString());
			
			jdbcTemplate.update(sb.toString(), srcid, type);
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public String getBaiduToken() throws RuntimeException {
		try {
			StringBuffer sb = new StringBuffer("select token from si_baidu_token");
			
			List<BaiduToken> lists = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<BaiduToken>(BaiduToken.class));
			
			return (lists != null && lists.size() >= 1) ? lists.get(0).getToken() : "";
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<JcBillSourceItem> queryJcBillSourceItemInBillId(List<String> billIds) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			List<Object> args = new ArrayList<Object>();
			StringBuffer sb = new StringBuffer(" select billid, status, recsource, recplatform, channel ");
			sb.append(" from jc_billsource_item a ");
			
			if(billIds != null && billIds.size() >= 1) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billid in (");
				for(int i = 0; i < billIds.size(); i++){
					if(i > 0) sb.append(",");
					sb.append("?");
					args.add(billIds.get(i));
				}
				sb.append(") ");
			}
			
			LOG.info("queryJcBillSourceItemInBillId>>>SQL>>>{}", sb.toString());
			
			List<JcBillSourceItem> lsit = jdbcTemplate.query(sb.toString(), args.toArray(), new BeanPropertyRowMapper<JcBillSourceItem>(JcBillSourceItem.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public SiDepositlocation querySiDepositlocation(String srcid) throws Exception {
		try {
			List<Object> args = new ArrayList<Object>();
			StringBuffer sb = new StringBuffer("select a.id, a.push from wms_depositlocation a");
			sb.append(" where a.id in (select d.physicaldept from dept d where d.id in");
			sb.append(" (select c.warehouse from jc_bill c where c.id = ?))");
			args.add(srcid);
			
			LOG.info("querySiDepositlocation>>>SQL>>>{}", sb.toString());
			
			List<SiDepositlocation> lists = jdbcTemplate.query( sb.toString(), args.toArray(), new BeanPropertyRowMapper<SiDepositlocation>(SiDepositlocation.class));
			
			return (lists != null && lists.size() >= 1) ? lists.get(0) : null;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public SiDepositlocation querySiDepositlocationHGfc(String srcid) throws Exception {
		try {
			List<Object> args = new ArrayList<Object>();
			StringBuffer sb = new StringBuffer("select a.id, a.push from wms_depositlocation a");
			sb.append(" where a.id in (select d.physicaldept from dept d where d.id in");
			sb.append(" (select c.fhwarehouse from jc_billhg c where c.id = ?))");
			args.add(srcid);
			
			LOG.info("querySiDepositlocationHGfc>>>SQL>>>{}", sb.toString());
			
			List<SiDepositlocation> lists = jdbcTemplate.query( sb.toString(), args.toArray(), new BeanPropertyRowMapper<SiDepositlocation>(SiDepositlocation.class));
			
			return (lists != null && lists.size() >= 1) ? lists.get(0) : null;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public void deleteEvent(String srcid, String typ, String billtyp) throws RuntimeException {
		try {
			String sql="delete from si_siwdevent where srcid = ? and typ = ? and billtyp = ?";
			Query query = em.createNativeQuery(sql);
			query.setParameter(1, srcid);
			query.setParameter(2, typ);
			query.setParameter(3, billtyp);
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

}
