package com.seeyon.apps.src_gettaxrate.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.src_gettaxrate.util.BaseUtil;
import com.seeyon.apps.src_gettaxrate.util.CommonResult;
import com.seeyon.cap4.util.StringUtil;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.util.JDBCAgent;

/**
 * @author zhaoyang:
 * @version 创建时间：2024年11月30日 下午10:11:07 类说明
 */
public class IGetTaxRateDaoImpl extends BaseUtil implements IGetTaxRateDao {
	private static final Log log = CtpLogFactory.getLog(IGetTaxRateDaoImpl.class);

	@Override
	public CommonResult getTaxRateExecute(String taxnum, String taxtype) {
		JSONArray jsonArray = new JSONArray();
		try {
			String sql = "";
			ResultSet rs = null;
			// 如果纳税人识别号或者税目都有数据
//			System.out.println(taxnum+taxtype);
			if (!StringUtil.isEmpty(taxnum) && !StringUtil.isEmpty(taxtype)) {
				Object[] params = new Object[] { taxnum, taxtype };
				sql = "SELECT * FROM SXLY_PUB_ORG_TAXRATE WHERE ORGTAXNUMBER= ? AND TAXTYPECODE= ? ";
				rs = super.executeQuery(sql, params);
//				System.out.println(sql);
			} else {// 两个参数有一个存在空，获取所有数据
				sql = "SELECT * FROM SXLY_PUB_ORG_TAXRATE";
				rs = super.executeQuery2(sql);
//				System.out.println(sql);
			}
			while (rs.next()) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("DAPK", checkData(rs, "DAPK"));
				jsonObject.put("ORGTAXNUMBER", checkData(rs, "ORGTAXNUMBER"));
				jsonObject.put("ORGTAXTYPE", checkData(rs, "ORGTAXTYPE"));
				jsonObject.put("ORGTAXTYPENAME", checkData(rs, "ORGTAXTYPENAME"));
				jsonObject.put("ORGNAME", checkData(rs, "ORGNAME"));
				jsonObject.put("TAXTYPECODE", checkData(rs, "TAXTYPECODE"));
				jsonObject.put("TAXTYPENAME", checkData(rs, "TAXTYPENAME"));
				jsonObject.put("TAXRATE", checkData(rs, "TAXRATE"));
				jsonArray.add(jsonObject);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			return new CommonResult("fail", "税率查询失败", e.getMessage());
		}
		return new CommonResult("success", "税率查询成功", jsonArray);
	}
	
	
	public String checkData(ResultSet rs,String str) {
		try {
			if(rs.getString(str)!=null) {
				return rs.getString(str);
			}else {
				return "";
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return "";
		}
	}


	@Override
	public CommonResult getTaxRateExecute2(String taxnum, String taxtype) {
		JSONArray jsonArray = new JSONArray();
		try {
			JDBCAgent agent = new JDBCAgent();
			List<Object> p = new ArrayList<Object>();
			String sql = "";
			List<Map> list = null;
			if (!StringUtil.isEmpty(taxnum) && !StringUtil.isEmpty(taxtype)) {
				sql = "SELECT * FROM SXLY_PUB_ORG_TAXRATE WHERE ORGTAXNUMBER= '" + taxnum + "' AND TAXTYPECODE= '"
						+ taxtype + "' ";
				agent.execute(sql.toString(), p);
				list = agent.resultSetToList();
				for (Map map : list) {
					JSONObject json = new JSONObject();
					json.put("taxrate",checkMapData(map,"taxrate"));
					jsonArray.add(json);
				}
			} else {// 两个参数有一个存在空
				String res = "两个必填参数都不能为空!请检查参数:";
				if(StringUtil.isEmpty(taxnum)) {
					res = res+" taxnum(纳税人识别号)为空！";
				}
				if(StringUtil.isEmpty(taxtype)) {
					res = res+" taxtype(税目)为空！";
				}
				return new CommonResult("fail", res, null);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			return new CommonResult("fail", "税率查询失败", e.getMessage());
		}
		return new CommonResult("success", "税率查询成功", jsonArray);
	}
	
	
	public String checkMapData(Map map,String str) {
		try {
			if(map.get(str)!=null) {
				return map.get(str).toString();
			}else {
				return "";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
}
