package abacus.springboot.example.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import abacus.springboot.example.api.view.ApiBillHg;
import abacus.springboot.example.api.view.ApiBillHgItem;
import abacus.springboot.example.api.view.ApiBillHgPayment;
import abacus.springboot.example.api.view.ApiJcBill;
import abacus.springboot.example.api.view.ApiJcBillHgSearchItem;
import abacus.springboot.example.api.view.ApiJcBillItem;
import abacus.springboot.example.api.view.ApiJcBillSearch;
import abacus.springboot.example.api.view.JcBillCount;
import abacus.springboot.example.api.view.JcCodeFlow;
import abacus.springboot.example.api.view.JcProduct;
import abacus.springboot.example.api.view.JcProductBillSouce;
import abacus.springboot.example.dao.JcBillHg;
import abacus.springboot.example.pi.ApiJcBillAccessIF;

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

	private static final Logger LOG = LoggerFactory.getLogger(ApiJcBillAccess.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Page<ApiJcBillSearch> queryApiJcBillSearch(String keyword, String employee, String status, Pageable pageable)
			throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer("");
			
			if(StringUtils.isNotBlank(keyword)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" (a.id like '%" + keyword + "%' ");
				sb.append(" or a.goods like '%" + keyword + "%'");
				sb.append(" or a.id in (select b.billid from jc_billsource_item b where b.goodsid like'%" + keyword + "%'))");
				
				params.put("id", "%"+keyword+"%");
			}
			
			if(StringUtils.isNotBlank(employee)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" (a.dept in (");
				sb.append(" select deptid from (");
				sb.append(" select dataid as deptid  from distributionmapping where erid ='" + employee + "' and rtype = 1 and ftype = 1 and dtype = 1");
				sb.append(" union");
				sb.append(" select a.dataid as deptid  from distributionmapping a ,acas_assign b where a.erid =b.rolez and a.rtype = 0 and a.ftype = 1 and a.dtype = 1 and b.userz ='" + employee + "'");
				sb.append(" ) adept)");
				sb.append(" or");
				sb.append(" a.applicant in (");
				sb.append(" select dataid from (");
				sb.append(" select dataid from distributionmapping where erid ='" + employee + "' and rtype = 1 and ftype = 1 and dtype = 2");
				sb.append(" union");
				sb.append(" select dataid  from distributionmapping a ,acas_assign b where a.erid =b.rolez and a.rtype = 0 and a.ftype = 1 and a.dtype = 2 and b.userz ='" + employee + "'");
				sb.append(" union");
				sb.append(" select '" + employee + "'");
				sb.append(" ) adept )");
				sb.append(" )");
				params.put("employee", employee);
//				sb.append(" a.dept in (");
//				sb.append(" select deptid from (");
//				sb.append(" select dataid as deptid  from distributionmapping where erid ='" + employee + "' and rtype = 1 and ftype = 1 and dtype = 1");
//				sb.append(" union ");
//				sb.append(" select a.dataid as deptid  from distributionmapping a ,acas_assign b where a.erid =b.rolez and a.rtype = 0 and a.ftype = 1 and a.dtype = 1 and b.userz ='" + employee + "'");
//				sb.append(" union ");
//				sb.append("select (select dept as deptid from employee e where e.id = dataid) as deptid  from distributionmapping where erid ='" + employee + "' and rtype = 1 and ftype = 1 and dtype = 2");
//				sb.append(" union ");
//				sb.append("select (select dept as deptid from employee e where e.id = a.dataid) as deptid  from distributionmapping a ,acas_assign b where a.erid =b.rolez and a.rtype = 0 and a.ftype = 1 and a.dtype = 2 and b.userz ='" + employee + "'");
//				sb.append(" union ");
//				sb.append("select dept as deptid from employee e where e.id = '" + employee + "'");
//				sb.append(" ) adept");
//				sb.append(" )");
//				params.put("employee", employee);
			}
			
			if(StringUtils.isNotBlank(status)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				int statusInt = Integer.parseInt(status);
				if(statusInt == 0) {// 待提交：已录入或者录入完成提交验证
					sb.append(" a.status in(0, 1) ");
					params.put("status", statusInt);
				} else if(statusInt == 2) {// 待溯源
					sb.append(" a.status = 2 ");
					params.put("status", statusInt);
				} else if(statusInt == -2) {// 人工溯源驳回
					sb.append(" a.status = -2 ");
					params.put("status", statusInt);
				} else if(statusInt == 3) {// 待入库，待入库审核
					sb.append(" a.status in(3, 4) ");
					params.put("status", statusInt);
				} else if(statusInt == 5) {// 已入库
					sb.append(" a.status = 5 ");
					params.put("status", statusInt);
				} else if(statusInt == 6 || statusInt == 8) {// 部分回购，回购完成
					sb.append(" a.hgstatus in(1, 2) ");
					params.put("status", statusInt);
				}
			} else {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.status in(-2, 0, 1, 2, 3, 4, 5) ");
				params.put("status", 1);
			}
			
			StringBuffer orderBy = new StringBuffer("");
			
			orderBy.append(" order by dt desc ");
			
			orderBy.append(" offset "+ (pageable.getPageNumber() * pageable.getPageSize())  + " rows fetch next "+ pageable.getPageSize() + " rows only ");
			
			String sql = " select a.id as billid, receivedt as receivedt, a.goods as goods, "
						+ " a.quantity as quantity, a.status as status, a.hgstatus as hgstatus from jc_bill a "
						+ sb.toString() + orderBy.toString();
			LOG.info("queryApiJcBillSearch>>>SQLgetPageNumber>>>{}", pageable.getPageNumber());
			LOG.info("queryApiJcBillSearch>>>SQLgetPageSize>>>{}", pageable.getPageSize());
			LOG.info("queryApiJcBillSearch>>>SQL>>>{}", sql);
			
			List<ApiJcBillSearch> lists = jdbcTemplate.query(sql, new BeanPropertyRowMapper<ApiJcBillSearch>(ApiJcBillSearch.class));
			
			Long count = jdbcTemplate.queryForObject("select count(a.id) from jc_bill a " + sb.toString(), Long.class);

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

	@Override
	public ApiJcBill queryApiJcBillById(String id) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer(" select a.id, a.status, a.goods, a.warehouse, a.warehousename, a.receivedt,");
			sb.append(" a.quantity, a.subtotal, a.applicant, a.applicantname, a.applicantdt");
			sb.append(" from jc_bill a");
			
			if(StringUtils.isNotBlank(id)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.id = '" + id + "' ");
				params.put("id", id);
			}
			
			LOG.info("queryApiJcBillById>>>SQL>>>{}", sb.toString());
			
			List<ApiJcBill> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<ApiJcBill>(ApiJcBill.class));
			
			return (lsit != null && lsit.size() >= 1) ? lsit.get(0) : null;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<ApiJcBillItem> queryApiJcBillItemByBIllId(String billId) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer("select a.id,a.status,a.recsource,a.channel,a.price,a.quantity,");
			sb.append(" a.numbers,a.subtotal,a.goodsid,a.logisticsid ");
			sb.append(" from jc_billsource_item a ");
			
			if(StringUtils.isNotBlank(billId)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billid = '" + billId + "' ");
				params.put("billId", billId);
			}
			
			LOG.info("queryApiJcBillItemByBIllId>>>SQL>>>{}", sb.toString());
			
			List<ApiJcBillItem> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<ApiJcBillItem>(ApiJcBillItem.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public Page<ApiJcBillHgSearchItem> queryApiJcBillHgSearchItem(String keyword, String employee, String status, Pageable pageable)
			throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer("");
			
			if(StringUtils.isNotBlank(keyword)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" (a.id like '%" + keyword + "%' ");
				sb.append(" or a.account like '%" + keyword + "%'");
				sb.append(" or a.accountname like '%" + keyword + "%'");
				sb.append(" or a.consigneephone like '%" + keyword + "%'");
				sb.append(" or a.id in (select b.billhg from jc_billin_item b where b.product like '%" + keyword + "%' and b.productname like'%" + keyword + "%'))");
				
				params.put("id", "%"+keyword+"%");
			}
			
			if(StringUtils.isNotBlank(employee)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" (");
				sb.append(" a.account in (");
				sb.append(" select sa.id from sams_account sa where sa.groupid in (select pc.id from pms_customergroup pc where pc.grouperid='" + employee + "' or ',' + groupempid + ',' like '%," + employee + ",%')");
				sb.append(" )");
				sb.append(" or ");
				sb.append(" a.account in (");
				sb.append(" select customerid from ws_wsempmgcustomer where wsempid ='" + employee + "' ");
				sb.append(" )");
				sb.append(" )");
//				sb.append(" a.account in (select sa.id from sams_account sa where sa.groupid in (select pc.id from pms_customergroup pc where pc.grouperid='" + employee + "' or ',' + groupempid + ',' like '%," + employee + ",%')) ");
				params.put("employee", employee);
			}
			
			if(StringUtils.isNotBlank(status)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				int statusInt = Integer.parseInt(status);
				if(statusInt == 1) {// 待提交
					sb.append(" a.status = 1 ");
					params.put("status", statusInt);
				} else if(statusInt == 2) {// 待付款
					sb.append(" a.status = 2 ");
					params.put("status", statusInt);
				} else if(statusInt == 3) {// 待分仓
					sb.append(" a.status in(3, 4) ");
					params.put("status", statusInt);
				} else if(statusInt == 5) {// 待出库
					sb.append(" a.status = 5 ");
					params.put("status", statusInt);
				} else if(statusInt == 6) {// 已出库
					sb.append(" a.status = 6 ");
					params.put("status", statusInt);
				}
			}
			
			StringBuffer orderBy = new StringBuffer("");
			
			orderBy.append(" order by dt desc ");
			
			orderBy.append(" offset "+ (pageable.getPageNumber() * pageable.getPageSize())  + " rows fetch next "+ pageable.getPageSize() + " rows only ");
			
			String sql = " select a.id as billHg, a.account, a.accountname, a.manager, a.managername, a.status, "
						+ " a.logisticsname, a.logisticsno, (a.subtotal+a.freight) as subtotal from jc_billhg a "
						+ sb.toString() + orderBy.toString();
			
			LOG.info("queryApiJcBillHgSearchItem>>>SQL>>>{}", sql);
			
			List<ApiJcBillHgSearchItem> lists = jdbcTemplate.query(sql, new BeanPropertyRowMapper<ApiJcBillHgSearchItem>(ApiJcBillHgSearchItem.class));
			
			Long count = jdbcTemplate.queryForObject("select count(a.id) from jc_billhg a " + sb.toString(), Long.class);

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

	@Override
	public List<JcBillCount> queryJcBillCount(List<String> ids) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer("select billhg as id, count(product) as breednum, sum(numbers) as numbers ");
			sb.append(" from jc_billin_item a ");
			
			if(ids != null && ids.size() >= 1) {
				StringBuffer idIn = new StringBuffer();
				for(String key : ids){
					idIn.append("'" + key + "',");
	            }
				String idIns = idIn.toString().substring(0, idIn.length() - 1);
				
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billhg in (" + idIns + ") ");
				params.put("billhg", idIn);
			}
			
			sb.append(" group by billhg");
			
			LOG.info("queryJcBillCount>>>SQL>>>{}", sb.toString());
			
			List<JcBillCount> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<JcBillCount>(JcBillCount.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public ApiBillHg queryApiBillHgById(String hgId) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer(" select a.id, a.billid, a.status, a.source, a.account,");
			sb.append(" a.accountname, a.quantity, a.subtotal, a.freight, a.manager,");
			sb.append(" a.managername, a.consignee, a.consigneephone, a.province, a.provincename,");
			sb.append(" a.city, a.cityname, a.area, a.areaname, a.address,");
			sb.append(" a.receiveway, a.remark, a.remark1, a.logisticsname, a.logisticscode,");
			sb.append(" a.logisticstype, a.logisticsno");
			sb.append(" from jc_billhg a");
			
			if(StringUtils.isNotBlank(hgId)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.id = '" + hgId + "' ");
				params.put("id", hgId);
			}
			
			LOG.info("queryApiBillHgById>>>SQL>>>{}", sb.toString());
			
			List<ApiBillHg> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<ApiBillHg>(ApiBillHg.class));
			
			return (lsit != null && lsit.size() >= 1) ? lsit.get(0) : null;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<ApiBillHgItem> queryApiBillHgItemByHgId(String hgId) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer("select a.id, a.status, a.product, a.productname, b.spec, a.price,	");
			sb.append(" a.quantity, a.numbers, a.hgprice, a.hgamount");
			sb.append(" from jc_billin_item a");
			sb.append(" left join pms_product b on a.product = b.id");
			
			if(StringUtils.isNotBlank(hgId)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billhg = '" + hgId + "' ");
				params.put("billhg", hgId);
			}
			
			LOG.info("queryApiBillHgItemByHgId>>>SQL>>>{}", sb.toString());
			
			List<ApiBillHgItem> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<ApiBillHgItem>(ApiBillHgItem.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<ApiBillHgPayment> queryApiBillHgPaymentByHgId(String hgId) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer(" select a.billhg, a.payment, a.amount, a.zhmc, a.khyh, a.yhzh from jc_billhg_pay a ");
			
			if(StringUtils.isNotBlank(hgId)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billhg = '" + hgId + "' ");
				params.put("billhg", hgId);
			}
			
			LOG.info("queryApiBillHgPaymentByHgId>>>SQL>>>{}", sb.toString());
			
			List<ApiBillHgPayment> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<ApiBillHgPayment>(ApiBillHgPayment.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<JcCodeFlow> queryJcCodeFlow(List<String> codes) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer("select b.id, b.deposit, b.depositname, b.dt as tdate, b.orderid,");
			sb.append(" c.customerid, c.customername, c.aprssalemanid, c.aprssalemanname, c.bizgroupid,");
			sb.append(" c.bizgroupname, c.wsareaid, c.wsareaname,");
			sb.append(" a.code, a.productid, a.productname");
			sb.append(" from bcs_codeflow a");
			sb.append(" left join wmsn_despatch b on b.id = a.srcid ");
			sb.append(" left join ws_wholesalebill c on c.id = b.orderid");
			
			if(codes != null && codes.size() >= 1) {
				StringBuffer idIn = new StringBuffer();
				for(String key : codes){
					idIn.append("'" + key + "',");
	            }
				String idIns = idIn.toString().substring(0, idIn.length() - 1);
				
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.code in (" + idIns + ") ");
				params.put("id", idIn);
			}
			
			LOG.info("queryJcCodeFlow>>>SQL>>>{}", sb.toString());
			
			List<JcCodeFlow> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<JcCodeFlow>(JcCodeFlow.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<ApiBillHgPayment> queryApiBillHgPaymentByHgId(List<String> hgIds) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer(" select a.billhg, a.payment, a.amount, a.zhmc, a.khyh, a.yhzh from jc_billhg_pay a ");
			
			if(hgIds != null && hgIds.size() >= 1) {
				StringBuffer idIn = new StringBuffer();
				for(String key : hgIds){
					idIn.append("'" + key + "',");
	            }
				String idIns = idIn.toString().substring(0, idIn.length() - 1);
				
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billhg in (" + idIns + ") ");
				params.put("id", idIn);
			}
			
			LOG.info("queryApiBillHgPaymentByHgId>>>SQL>>>{}", sb.toString());
			
			List<ApiBillHgPayment> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<ApiBillHgPayment>(ApiBillHgPayment.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<JcProduct> queryJcProduct(String product, String stockInId, String hgId) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer(" select a.billid,a.billin,a.billhg,a.account,a.accountname,a.product,a.productname,");
			sb.append(" sum(a.quantity) as quantity ,sum(a.numbers) as numbers,");
			sb.append(" (select h.wdtbillout from jc_billhg h where h.billid = a.billid and h.id = a.billhg) as wdtbillout");
			sb.append(" from jc_billin_item a");
			
			if(StringUtils.isNotBlank(product)) {
				sb.append(params.size() > 0 ?" and ":" where ");
//				sb.append(" a.product ='" + product + "' ");
				sb.append(" a.billid in (select jbi.billid from jc_billsource_item jbi where jbi.goodsid ='" + product + "' and jbi.status = 3) ");
				params.put("product", product);
			}
			if(StringUtils.isNotBlank(stockInId)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billin ='" + stockInId + "' ");
				params.put("billin", stockInId);
			}
			if(StringUtils.isNotBlank(hgId)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billhg ='" + hgId + "' ");
				params.put("billhg", hgId);
			}
			
			sb.append(" group by a.billid,a.billin,a.billhg,a.account,a.accountname,a.product,a.productname");
			
			LOG.info("queryJcProduct>>>SQL>>>{}", sb.toString());
			
			List<JcProduct> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<JcProduct>(JcProduct.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<JcProductBillSouce> queryJcProductBillSouce(List<String> billIds) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer(" select indexno,billid,account,product,goodsid from jc_billsource_item a ");
			
			if(billIds != null && billIds.size() >= 1) {
				StringBuffer idIn = new StringBuffer();
				for(String key : billIds){
					idIn.append("'" + key + "',");
	            }
				String idIns = idIn.toString().substring(0, idIn.length() - 1);
				
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billid in (" + idIns + ") ");
				params.put("id", idIn);
			}
			
			LOG.info("queryJcProductBillSouce>>>SQL>>>{}", sb.toString());
			
			List<JcProductBillSouce> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<JcProductBillSouce>(JcProductBillSouce.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<JcProduct> queryJcProduct(String product, String stockInId) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			StringBuffer sb = new StringBuffer(" select a.billid,a.billin,a.billhg,a.account,a.accountname,a.product,a.productname,");
			sb.append(" sum(a.quantity) as quantity ,sum(a.numbers) as numbers,b.goodsid,");
			sb.append(" (select h.wdtbillout from jc_billhg h where h.billid = a.billid and h.id = a.billhg) as wdtbillout");
			sb.append(" from jc_billin_item a");
			sb.append(" left join jc_billsource_item b on a.billid = b.billid and a.account = b.account and a.product = b.product ");
			
			if(StringUtils.isNotBlank(product)) {
				sb.append(params.size() > 0 ?" and ":" where ");
//				sb.append(" a.product ='" + product + "' ");
				sb.append(" a.billid in (select jbi.billid from jc_billsource_item jbi where jbi.goodsid ='" + product + "' and jbi.status = 3) ");
				params.put("product", product);
			}
			if(StringUtils.isNotBlank(stockInId)) {
				sb.append(params.size() > 0 ?" and ":" where ");
				sb.append(" a.billin ='" + stockInId + "' ");
				params.put("billin", stockInId);
			}
			
			sb.append(" group by a.billid,a.billin,a.billhg,a.account,a.accountname,a.product,a.productname,b.goodsid");
			
			LOG.info("queryJcProduct>>>SQL>>>{}", sb.toString());
			
			List<JcProduct> lsit = jdbcTemplate.query(sb.toString(), new BeanPropertyRowMapper<JcProduct>(JcProduct.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<JcBillHg> qeryJcBillHgs(List<String> billids) throws RuntimeException {
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			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(" from jc_billhg a");
			
			if(billids != null && billids.size() >= 1) {
				StringBuffer idIn = new StringBuffer();
				for(String key : billids){
					idIn.append("'" + key + "',");
	            }
				String idIns = idIn.toString().substring(0, idIn.length() - 1);
				
				sql.append(params.size() > 0 ?" and ":" where ");
				sql.append(" a.billid in (" + idIns + ") ");
				params.put("billid", idIn);
			}
			
			LOG.info("qeryJcBillHgs>>>SQL>>>{}", sql.toString());
			
			List<JcBillHg> lsit = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<JcBillHg>(JcBillHg.class));
			
			return lsit;
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
}
