package com.cim.idm.mwmsextend.material.oqa.service;


import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.orm.service.CommonServiceDAO;
import com.cim.idm.wmspackage.material.oqa.data.MaterialOQANums;
import com.cim.idm.wmspackage.material.oqa.data.MaterialOQANumsKey;

public class MaterialOQANumsServiceImpl extends CommonServiceDAO<MaterialOQANumsKey, MaterialOQANums>  {

//	private static Log log = LogFactory.getLog(MaterialOQANumsServiceImpl.class);
	private String getSqlFromDB = "select querystring from bs_customquery where queryid=?";
	
	public MaterialOQANums createMaterialOQANums(String site,String materialSpecName, String supplierNo, String materialOQANum){
		double nums = 0;
		//"^[0-9]+([.]{1}[0-9]+){0,1}$" 数字验证
		if(materialOQANum != null){
			if(Pattern.compile("-{0,1}[0-9]+([.]{0,1}[0-9]+){0,1}").matcher(materialOQANum).matches()){
				nums = Double.valueOf(materialOQANum);
			}
		}
		return new MaterialOQANums(site,materialSpecName,supplierNo,nums);
	}

	/**
	 * save or update
	 * @param materialOQANums
	 */
	public String SaveOrUpdateMaterialOQANums(MaterialOQANums materialOQANums){
		if(materialOQANums != null){
			if(materialOQANums.getKey().getMaterialSpecName() != null){
				if(materialOQANums.getKey().getSupplierNo() != null){
					String sql = "";
					
					if(isExist(materialOQANums)){
						sql = IDMFrameServiceProxy.getSqlTemplate().queryForObject(getSqlFromDB, String.class,"UpdateMaterialOQANums");
						IDMFrameServiceProxy.getSqlTemplate().update(sql,materialOQANums.getMaterialOQANum(), materialOQANums.getKey().getSite(),materialOQANums.getKey().getMaterialSpecName(),
								materialOQANums.getKey().getSupplierNo());
					}else{
						sql = IDMFrameServiceProxy.getSqlTemplate().queryForObject(getSqlFromDB, String.class,"SaveMaterialOQANums");
						IDMFrameServiceProxy.getSqlTemplate().update(sql, materialOQANums.getKey().getSite(),materialOQANums.getKey().getMaterialSpecName(),
								materialOQANums.getKey().getSupplierNo(),materialOQANums.getMaterialOQANum());
					}
					  
					return "success";
				}
			}
		}
		
		return "failed";
	}	
	
	/**
	 * 返回所有数据
	 * @return
	 */
	public  List<Map<String, Object>> getMaterialOQANumsList(){
		String sql = IDMFrameServiceProxy.getSqlTemplate().queryForObject(getSqlFromDB, String.class,"GetMaterialOQANumsList");
		return IDMFrameServiceProxy.getSqlTemplate().queryForList(sql);
	}
	
	/**
	 * 根据key删除数据
	 * @param materialOQANums
	 */
	public String deleteData(MaterialOQANums materialOQANums){
		if(materialOQANums != null){
			if(materialOQANums.getKey().getMaterialSpecName() != null){
				if(materialOQANums.getKey().getSupplierNo() != null){
					String sql = "";
					
					if(isExist(materialOQANums)){
						sql = IDMFrameServiceProxy.getSqlTemplate().queryForObject(getSqlFromDB, String.class,"DeleteMaterialOQANums");
						IDMFrameServiceProxy.getSqlTemplate().update(sql, materialOQANums.getKey().getSite(),materialOQANums.getKey().getMaterialSpecName(),
								materialOQANums.getKey().getSupplierNo());
						
						return "success";
					}					  
					
				}
			}
		}
		
		return "failed";
	}
	/**
	 * 根据key判断是否已存在
	 * @param materialOQANums
	 * @return
	 */
	public boolean isExist(MaterialOQANums materialOQANums){
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList("select * from  MATERIALOQANUMS where SITE=? AND MATERIALSPECNAME=? and SUPPLIERNO=?",
				materialOQANums.getKey().getSite(),materialOQANums.getKey().getMaterialSpecName(),materialOQANums.getKey().getSupplierNo());
		if(!list.isEmpty()){
			return true;
		}
		return false;
	}
	
/*	public static void main(String[] args) {
		MaterialOQANumsServiceImpl materialOQANumsServiceImpl = new MaterialOQANumsServiceImpl();
		materialOQANumsServiceImpl.SaveOrUpdateMaterialOQANums(materialOQANumsServiceImpl.createMaterialOQANums("1", "123", "123"));
		List<Map<String, Object>> list = materialOQANumsServiceImpl.getMaterialOQANumsList();
		for(int i=0; i<list.size();i++){
			System.out.println(list.get(i).get("materialSpecname") + " " + list.get(i).get("supplierno") + " " + list.get(i).get("materialOQANum"));
		}
	}*/
}
