package weaver.interfaces.htsc.payment;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * k1810012
 * 商旅结算付款节点前附加操作
 * 更新明细表8数据到明细表12
 */
public class BTSUpdateAccountDetail implements Action {
	
	private final Log log = LogFactory.getLog(BTSUpdateAccountDetail.class);
	
	private final String JXS = "2221340001"; //进项税
	private final String DK_JXS = "2221340005"; //抵扣进项税
	
	
	@Override
	public String execute(RequestInfo requestInfo) {
		RequestManager rm = requestInfo.getRequestManager();
		String tableName = rm.getBillTableName();
		String rid = requestInfo.getRequestid();
		
		try {
			updateCostDetail(tableName, rid);
			return SUCCESS;
		} catch (Exception e) {
			rm.setMessagecontent("流程流转异常!自动更新会计明细表失败,原因:" + e.getMessage());
			rm.setMessageid("500");
			return FAILURE_AND_CONTINUE;
		}
	}
	
	/**
	 * 批量更新商旅结算会计表
	 *
	 * @param tableName 主表
	 * @param rid       流程id
	 * @throws Exception 异常
	 */
	private void updateCostDetail(String tableName, String rid) throws Exception {
		
		RecordSetTrans irs = new RecordSetTrans();
		PropBean pp = new PropBean();
		irs.setAutoCommit(false);
		
		List<List<Object>> paramsList = new LinkedList<>();
		String pre_hj = pp.getPropName("TSP_KJ_PRE"); //会计科目前缀
		
		//汇总明细8数据到list
		String sql = "select MAINID,FYSJCDBM,BHSJE,SJ,YSKM,YSKMID,SY from " + tableName + "_DT8 t1\n" +
								 "left join " + tableName + " t2\n" +
								 "on t1.MAINID=t2.ID\n" +
								 "where t2.REQUESTID=? order by t1.ID";
		try {
			irs.executeQuery(sql, rid);
			while (irs.next()) {
				List<Object> valueList = new ArrayList<>();
				valueList.add(irs.getString("MAINID"));
				valueList.add(irs.getString("FYSJCDBM"));
				String yskm = Util.null2String(irs.getString("YSKM"));
				valueList.add(yskm);
				List<Object> list = convertBudgetToAccount(yskm);
				String yskmmc = (String) list.get(0);
				valueList.add(yskmmc); //预算科目名称
				
				double bhsje = irs.getDouble("BHSJE");
				valueList.add(bhsje); //有税金时取税金 3+
				String hjkmbm = (String) list.get(1);
				valueList.add(hjkmbm);
				valueList.add(String.join("_", pre_hj, hjkmbm));
				
				valueList.add(bhsje); //有税金时取税金 3+
				paramsList.add(valueList);
				
				double sj = irs.getDouble("SJ");
				int sy = Util.getIntValue(irs.getInt("SY"));
				
				if (sj > 0) {
					List<Object> secList = new ArrayList<>(4);
					Collections.addAll(secList,  new String[valueList.size()]);
					Collections.copy(secList, valueList);
					secList = secList.subList(0, 4);
					secList.add(sj);
					if (sy == 0 || sy == 1) {
						secList.add(DK_JXS);
						secList.add(String.join("_", pre_hj, DK_JXS));
					} else if(sy == 2 || sy == 3 || sy == 5) {
						secList.add(JXS);
						secList.add(String.join("_", pre_hj, JXS));
					}
					secList.add(sj);
					paramsList.add(secList);
				}
				
			}
			
			//插入明细表12
			String isql = "insert into " + tableName + "_DT12 (MAINID,FYSJCDBM,YSKM,YSKMMC,JE," +
										"HJKMBM,HJKMID,SJBZ,HL,ZHRMB) values (?,?,?,?,?,?,?,0,1,?)";
			irs.executeBatchSql(isql, paramsList);
			irs.commit();
		} catch (Exception e) {
			irs.rollback();
			log.info("执行更新sql报错," + e.getMessage());
			throw new Exception("执行更新sql报错");
		}
		
		
	}
	
	/**
	 * 预算编码对应会计名称
	 *
	 * @param yskm 预算编码
	 * @return string
	 */
	private List<Object> convertBudgetToAccount(String yskm) {
		RecordSet rs = new RecordSet();
		List<Object> list = new ArrayList<>();
		String sql = "SELECT t1.HJKMBM,t2.YSKMMC\n" +
								 "FROM UF_KJKMPZ t1\n" +
								 "LEFT OUTER JOIN UF_KJKMPZ_DT1 t2 on t1.id=t2.mainid\n" +
								 "WHERE t2.yskm='" + yskm + "'";
		rs.executeQuery(sql);
		if (rs.next()) {
			list.add(rs.getString("YSKMMC"));
			list.add(rs.getString("HJKMBM"));
		} else {
			list.add("");
			list.add("");
		}
		return list;
	}
	
	
}
