/**
 * 
 */
package com.kingdee.shr.customer.gtiit.rpt;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.DEPCustomBillEditHandler;

/**
 * @author ISSUSER 工资单薪酬项目新增保存校验
 */
public class AddSalaryEditHandler extends DEPCustomBillEditHandler {
	Context ctx = SHRContext.getInstance().getContext();

	@Override
	public String saveAction(HttpServletRequest request, HttpServletResponse response, ModelMap modeMap)
			throws SHRWebException {
		CoreBaseInfo model = (CoreBaseInfo) request.getAttribute("dynamic_model");
		String depidentifying = model.getString("depidentifying");
		List<String> columnList = getColumn();
		List<String> list = getFiled();
		List<String> determineFieldsList = determineFields();
		if (list.contains(depidentifying)) {
			// 判断是否是中文环境
			if (ctx.getLocale().toString().equalsIgnoreCase("zh_CN")
					|| ctx.getLocale().toString().equalsIgnoreCase("l2")
					|| ctx.getLocale().toString().equalsIgnoreCase("L2")) {
				throw new ShrWebBizException("该dep标识已经在单据中存在");
			} else {
				throw new ShrWebBizException("The dep identifier already exists in the document");
			}
		}
//		String depFile = "cf" + depidentifying.toLowerCase();
//		if (!columnList.contains(depFile) && !columnList.contains(depFile + "_l1")) {
//			// 判断是否是中文环境
//			if (ctx.getLocale().toString().equalsIgnoreCase("zh_CN")
//					|| ctx.getLocale().toString().equalsIgnoreCase("l2")
//					|| ctx.getLocale().toString().equalsIgnoreCase("L2")) {
//				throw new ShrWebBizException("dep里面没有该字段,请先在dep里面创建字段");
//			} else {
//				throw new ShrWebBizException(
//						"This field is not available in the dep. Please create a field in the dep first");
//			}
//		}
		if (determineFieldsList.contains(depidentifying.toLowerCase())) {
			if (ctx.getLocale().toString().equalsIgnoreCase("zh_CN")
					|| ctx.getLocale().toString().equalsIgnoreCase("l2")
					|| ctx.getLocale().toString().equalsIgnoreCase("L2")) {
				throw new ShrWebBizException("dep已存在该字段标识");
			} else {
				throw new ShrWebBizException("The field identifier already exists in dep");
			}
		}

		return super.saveAction(request, response, modeMap);
	}

	/**
	 * 查询工资单薪酬项目新增单据里面的 dep字段
	 */
	public List<String> getFiled() {

		List<String> list = new ArrayList<>();
		String sql = "select* from CT_CUS_AddSalaryproject";
		try {
			IRowSet rs = DbUtil.executeQuery(ctx, sql);
			while (rs.next()) {
				list.add(rs.getString("cfdepidentifying"));
			}
		} catch (BOSException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 查询工资单套打里面的所有字段
	 *
	 */
	public List<String> getColumn() {
		List<String> list = new ArrayList<>();
		String sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS  WHERE TABLE_NAME = 'CT_SAL_WagesChromatography'";
		try {
			IRowSet rs = DbUtil.executeQuery(ctx, sql);
			while (rs.next()) {
				// 转换为小写
				list.add(rs.getString("COLUMN_NAME").toLowerCase());

			}
		} catch (BOSException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	/***
	 * 存储已经定死的字段
	 */
	public List<String> determineFields() {
		List<String> filedList = new ArrayList<>();
		filedList.add("ynssd");
		filedList.add("yfzj");
		filedList.add("deductionsfortaxRMB");
		filedList.add("deductionsfortax");
		filedList.add("Note");
		filedList.add("DeductionafterTax");
		filedList.add("PaymentafterTax");
		filedList.add("SalaryAdvance");
		filedList.add("UtilityCost");
		filedList.add("Rent");
		filedList.add("GrossSalary");
		filedList.add("rentorutility");
		filedList.add("Utilityfee");
		filedList.add("OtherDeduction");
		filedList.add("OtherRefund");
		filedList.add("UnpaidLeaveDeduction");
		filedList.add("UGSubsidyUG");
		filedList.add("AdmissionSubsidy");
		filedList.add("TaxableOtherBenefit");
		filedList.add("TaxableCommercial");
		filedList.add("TotalDeductionBeforeTax");
		filedList.add("DeductionBeforeTax");
		filedList.add("TotalAllowance");
		filedList.add("TeachersDayAllowance");
		filedList.add("TutoringAllowance2");
		filedList.add("TutoringAllowance");
		filedList.add("GrossWage2");
		filedList.add("DailyWage");
		filedList.add("leaveallowance");
		filedList.add("severancepay");
		filedList.add("annualleave");
		filedList.add("ERHousing");
		filedList.add("ERMedicalins");
		filedList.add("ERSocialins");
		filedList.add("TotalTax");
		filedList.add("SeveranceTax");
		filedList.add("NonResident");
		filedList.add("BonusTax1");
		filedList.add("AnnualBonus2");
		filedList.add("ForeignerAnnual");
		filedList.add("AnnualBonus1");
		filedList.add("Specialexpense");
		filedList.add("TaxableItem");
		filedList.add("PensionAllowance");
		filedList.add("HouseAllowance");
		filedList.add("CommunicationAllowance");
		filedList.add("OTPayment");
		filedList.add("HolidayOTPayment");
		filedList.add("TaxExemption");
		filedList.add("shifanetPayment");
		filedList.add("netPaymentnumber");
		filedList.add("socialinsuranceplanRMB");
		filedList.add("socialinsuranceplanRMBCode");
		filedList.add("IndividualIncomeTaxRMB");
		filedList.add("IndividualIncomeTaxRMBCode");
		filedList.add("HousingfundRMB");
		filedList.add("HousingfundRMBCode");
		filedList.add("MedicalinsuranceRMB");
		filedList.add("MedicalinsuranceRMBCode");
		filedList.add("SocialinsuranceRMB");
		filedList.add("SocialinsuranceRMBCode");
		filedList.add("SubsidyRMB");
		filedList.add("SubsidyRMBCode");
		filedList.add("WeekendOTPaymentRMB");
		filedList.add("WeekendOTPaymentRMBCode");
		filedList.add("WorkdayOTPaymentRMB");
		filedList.add("WorkdayOTPaymentRMBCode");
		filedList.add("PositionAllowanceRMB");
		filedList.add("PositionAllowanceRMBCode");
		filedList.add("netpaymentRMB");
		filedList.add("netpaymentRMBCode");
		filedList.add("YTDtaxRMB");
		filedList.add("YTDtaxRMBCode");
		filedList.add("YTDgrossRMB");
		filedList.add("YTDgrossRMBCode");
		filedList.add("HourlyRateRMB");
		filedList.add("HourlyRateRMBCode");
		filedList.add("ServiceTaxRMB");
		filedList.add("ServiceTaxRMBCode");
		filedList.add("PaymentbeforeRMB");
		filedList.add("PaymentbeforeRMBCode");
		filedList.add("GrossWageRMB");
		filedList.add("GrossWageRMBCode");
		filedList.add("MonthRMB");
		filedList.add("MonthRMBCode");
		filedList.add("paymentcurrency");
		filedList.add("insuranceplanCode");
		filedList.add("insuranceplan");
		filedList.add("insuranceplanId");
		filedList.add("individualIncomeTax");
		filedList.add("individualIncomeTaxCode");
		filedList.add("individualIncomeTaxId");
		filedList.add("housingfund");
		filedList.add("housingfundCode");
		filedList.add("housingfundId");
		filedList.add("medicalCode");
		filedList.add("medicalId");
		filedList.add("medical");
		filedList.add("socialinsurance");
		filedList.add("socialinsuranceCode");
		filedList.add("socialinsuranceId");
		filedList.add("subsidyId");
		filedList.add("subsidyCode");
		filedList.add("subsidyamount");
		filedList.add("weekendOTPaymentId");
		filedList.add("weekendOTPaymentCode");
		filedList.add("weekendOTPayment");
		filedList.add("workdayOTPaymentCode");
		filedList.add("workdayOTPaymentId");
		filedList.add("workdayOTPayment");
		filedList.add("allowanceCode");
		filedList.add("allowanceId");
		filedList.add("allowancecmount");
		filedList.add("settlementcyle");
		filedList.add("Nationalits");
		filedList.add("cuueryName");
		filedList.add("currencyId");
		filedList.add("MonthlysalaryCode");
		filedList.add("GrossWageCode");
		filedList.add("PaymentbeforeCode");
		filedList.add("grossincomeCode");
		filedList.add("ServiceTaxCode");
		filedList.add("YTDtaxCode");
		filedList.add("netpaymentCode");
		filedList.add("HourlyRateCode");
		filedList.add("WorkingHoursCode");
		filedList.add("AttendanceCode");
		filedList.add("department");
		filedList.add("peronid");
		filedList.add("Attendancecount");
		filedList.add("WorkingHourscount");
		filedList.add("WorkingHours");
		filedList.add("HourlyRateamount");
		filedList.add("HourlyRate");
		filedList.add("netpaymentamount");
		filedList.add("YTDnetpayment");
		filedList.add("YTDtaxamount");
		filedList.add("YTDtax");
		filedList.add("grossincome");
		filedList.add("ytdgrossincome");
		filedList.add("ServiceTaxamount");
		filedList.add("ServiceTax");
		filedList.add("beforeamcount");
		filedList.add("PaymentbeforeTax");
		filedList.add("grossamount");
		filedList.add("GrossWage");
		filedList.add("amount");
		filedList.add("Monthlysalary");
		filedList.add("Nationali");
		filedList.add("Attendancedays");
		// 将list元素都转换成小写
		List<String> lowerCaseList = filedList.stream().map(String::toLowerCase).collect(Collectors.toList());

		return lowerCaseList;

	}

}
