package com.engine.htsc.payment.dao;

import org.apache.ibatis.jdbc.SQL;

/**
 * author：k1810012
 * 通用付款sql
 */
public class BudgetDao {

	/**
	 * 更新中间表
	 * @param tableName 表名
	 * @return sql
	 */
	public String updateMiddleTable(String tableName) {
		return new SQL() {{
			UPDATE(tableName);
			SET("FBILLSTATUS=?,YSND=?,FBIZDATE=?,FSJAMOUNT=?,FNOTAXAMOUNT=?,FTAXAMOUNT=?," +
					"FACTUALAMOUNT=?,FSTANDARDAMOUNT=?，FOVERWEIGHT=?");
			WHERE("FITEMDETAILNUM=? and FNAME=?");
		}}.toString();
	}

	/**
	 * 插入中间表
	 * @param tableName 表名
	 * @return sql
	 */
	public String insertMiddleTable(String tableName) {
		return new SQL() {{
			INSERT_INTO(tableName);
			VALUES("FBILLSTATUS,FNAME,FBILLTITLE,FPESONNAME," +
											"YSND,FBIZDATE,FITEMDETAILNUM," +
							"FCOSTITEMNUM,FSJAMOUNT,FNOTAXAMOUNT,FTAXAMOUNT,FACTUALAMOUNT," +
											"FSTANDARDAMOUNT,FOVERWEIGHT,\n" +
							"ISITITEM,ITITEMCODE,ITITEMNAME,FDEPTNUMBER",
					"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?");
		}}.toString();
	}

	/**
	 * 是否it付款
	 *
	 * @param main        主表
	 * @param requestMark 流程编码
	 * @return string
	 */
	public String isItPay(String main, String requestMark) {
		return new SQL() {{
			SELECT("FKLX");
			FROM(main);
			WHERE("LCBH='" + requestMark + "'");
		}}.toString();
	}

	/**
	 * 获取预算科目信息
	 *
	 * @param budgetKmTable 预算表（dt8 or dt9）
	 * @param mainId        mainId
	 * @return sql
	 */
	public String budgetKmTableInfo(String budgetKmTable, String mainId, boolean isIt) {
		return new SQL() {{
			if (budgetKmTable.toLowerCase().contains("dt8")) {
				if (!isIt) {
					SELECT("FYSJCDBM,BHSJE,SJ,JSHJJE,CYS,YSKM");
				} else {
					SELECT("FYSJCDBM,BHSJE,SJ,JSHJJE,CYS,YSKM,PROJECTCODE,XMMC");
				}
			} else {
				SELECT("rmbje,CYS,YSKM,FYSJCDBM");
			}
			FROM(budgetKmTable);
			WHERE("MAINID=" + mainId);
		}}.toString();
	}

	/**
	 * 获取付款主表信息
	 *
	 * @param main        主表名称
	 * @param requestMark 流程编号
	 * @return 主表信息sql
	 */
	public String mainInfo(String main, String requestMark) {
		return new SQL() {{
			SELECT_DISTINCT("t1.ID,t1.LCBH,t2.REQUESTNAME,t3.LASTNAME,t4.DEPARTMENTCODE");
			FROM(main + " t1");
			LEFT_OUTER_JOIN("workflow_requestbase t2 on t1.requestid=t2.requestid");
			LEFT_OUTER_JOIN("hrmresource t3 on t1.ngr=t3.id");
			LEFT_OUTER_JOIN("hrmdepartment t4 on t3.departmentid=t4.id");
			WHERE("t1.lcbh='" + requestMark + "'");
		}}.toString();
	}

	/**
	 * 查询数据是否存在sql
	 *
	 * @param budgetTableName 数据表
	 * @param fName           流程编号
	 * @return 查询sql
	 */
	public String fNameIsExist(String budgetTableName, String fName) {
		return new SQL() {{
			SELECT_DISTINCT("FNAME");
			FROM(budgetTableName);
			WHERE("FNAME='" + fName + "'");
		}}.toString();
	}

	/**
	 * 通用付款 获取主表
	 * lcbh,ngr,id,requestname字段用于调用预算接口
	 *
	 * @return sql 占位符1 mainTable 占位符2 reqId
	 */
	public String mainFieldParam(String table, String rid) {
		return new SQL() {{
			SELECT("t1.lcbh,t1.ngr,t1.id,t1.fkxx,t2.requestname ");
			FROM(table + " t1");
			LEFT_OUTER_JOIN("workflow_requestbase t2 on t1.requestid=t2.requestid");
			WHERE("t1.requestid=" + rid);
		}}.toString();
	}


	/**
	 * 通用报销 获取主表
	 * lcbh,ngr,id,requestname字段用于调用预算接口
	 *
	 * @return sql 占位符1 mainTable 占位符2 reqId
	 */
	public String mainBXFieldParam(String table, String rid) {
		return new SQL() {{
			SELECT("t1.lcbh,t1.ngr,t1.id,t2.requestname ");
			FROM(table + " t1");
			LEFT_OUTER_JOIN("workflow_requestbase t2 on t1.requestid=t2.requestid");
			WHERE("t1.requestid=" + rid);
		}}.toString();
	}


	/**
	 * it付款 获取主表
	 * lcbh,ngr,id,requestname字段用于调用预算接口
	 *
	 * @return sql 占位符1 mainTable 占位符2 reqId
	 */
	public String mainFieldParamForIt(String table, String rid) {
		return new SQL() {{
			SELECT("t1.lcbh,t1.ngr,t1.id,t1.xzfklx,t2.requestname ");
			FROM(table + " t1");
			LEFT_OUTER_JOIN("workflow_requestbase t2 on t1.requestid=t2.requestid");
			WHERE("t1.requestid=" + rid);
		}}.toString();
	}

	/**
	 * 通用付款 获取预算明细数据
	 * fycdbmfzr,yskm,ccsqlc 字段用于调用预算接口
	 *
	 * @return sql 占位符1 yskmTable（明细8或明细9） 占位符2 mainId
	 */
	public String yskmTableFieldParam(String detailTable, String mainId) {
		return new SQL() {{
			SELECT("fycdbmfzr,fysjcdbm,yskm");
			FROM(detailTable);
			WHERE("mainid=" + mainId);
		}}.toString();
	}

	public String bxBBYskmTableFieldParam(String detailTable, String mainId){
		return new SQL() {{
			SELECT("fysjcdbm,yskmid,yskm,jshjje,bhsje,sj");
			FROM(detailTable);
			WHERE("mainid=" + mainId);
		}}.toString();
	}

	public String clbxBBYskmTableFieldParam(String detailTable, String mainId){
		return new SQL() {{
			SELECT("fycdbm,jshjje,bhsje,sj,qzzsbhsje,zssj,lkysbhsje,lkyssj,qt");
			FROM(detailTable);
			WHERE("mainid=" + mainId);
		}}.toString();
	}

	public String bxWBYskmTableFieldParam(String detailTable, String mainId){
		return new SQL() {{
			SELECT("fysjcdbm,yskm,rmbje");
			FROM(detailTable);
			WHERE("mainid=" + mainId);
		}}.toString();
	}

	/**
	 * IT付款 获取付款明细预算数据
	 * @param detailTable
	 * @param mainId
	 * @return
	 */
	public String ITyskmTableFieldParam(String detailTable, String mainId) {
		String sql= "select FYCDBMFZR,FYSJCDBM,YSKM,PROJECTCODE from " + detailTable + " where MAINID=" + mainId;
		return sql;
	}

	/**
	 * 通用付款，预算科目上级科目
	 *
	 * @return sql 占位符1  subBudgetCode 预算科目
	 */
	public String superiorBudgetSubject(String subBudgetCode) {
		return new SQL() {{
			SELECT_DISTINCT("sjyskm");
			FROM("uf_kmpz");
			WHERE("yskmbm=" + subBudgetCode);
		}}.toString();
	}


}
