package com.kingdee.eas.custom.esign.service;


import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.util.BOSUuid;


/**
 * 入职薪酬取数 + 产假工资
 * @author coyle
 * 202603
 * 备注: 与OnboardingSalaryDataService目前是一致的,暂不使用
 */
public class MaternityAllowanceDataService extends OtherESignConfigDataService {



	public String getSql(String id) {
    	StringBuilder sql = new StringBuilder();
    	
    	// -------------------------- SELECT子句 --------------------------
    	// 基础字段：人员姓名、身份证号、甲方名称、入职日期
    	// 薪资计算字段：固定工资、试用期固定工资、绩效奖金、各类补贴（均为税前）
    	sql.append("SELECT \n");
    	sql.append("    person.FNAME_L2 AS person_name ,\n");
    	sql.append("    person.FIDCardNO AS idcard ,\n");
    	sql.append("    firstParty.FName_l2 as firstPartyName ,\n");
    	sql.append("    elation.FEnterDate AS eDate ,\n");
    	// 1. 固定工资（税前）= 标准基本工资+综合工资+其他工资+加班工资
    	sql.append("    ISNULL(SUM(CASE WHEN sitem.FNUMBER IN ('TD01', 'T012', 'T013', 'TD02') THEN fas.FMoney ELSE 0 END), 0) AS fixed_salary ,\n");
    	// 2. 试用期固定工资（税前）= 标准基本工资+综合工资+其他工资+加班工资
    	sql.append("    ISNULL(SUM(CASE WHEN sitem.FNUMBER IN ('TD01', 'T012', 'T013', 'TD02') THEN fas.FMoney ELSE 0 END), 0) AS prob_fixed_salary ,\n");
    	// 3. 绩效奖金（税前）= 标准绩效奖金
    	sql.append("    ISNULL(MAX(CASE WHEN sitem.FNUMBER = 'TD04' THEN fas.FMoney ELSE 0 END), 0) AS perf_bonus ,\n");
    	// 4. 试用期绩效奖金（税前）= 试用期标准绩效奖金
    	sql.append("    ISNULL(MAX(CASE WHEN sitem.FNUMBER = 'TD74' THEN fas.FMoney ELSE 0 END), 0) AS prob_perf_bonus ,\n");
    	// 5. 国内餐补 = 餐补补贴
    	sql.append("    ISNULL(MAX(CASE WHEN sitem.FNUMBER = 'T014' THEN fas.FMoney ELSE 0 END), 0) AS domestic_meal_subsidy ,\n");
    	// 6. 艰苦补贴 = 艰苦补贴
    	sql.append("    ISNULL(MAX(CASE WHEN sitem.FNUMBER = 'TD05' THEN fas.FMoney ELSE 0 END), 0) AS hardship_subsidy ,\n");
    	// 7. 社保公积金补贴 = 社保公积金补贴
    	sql.append("    ISNULL(MAX(CASE WHEN sitem.FNUMBER = 'TD101' THEN fas.FMoney ELSE 0 END), 0) AS social_fund \n");

		// 8. 总部人员产假工资:
		sql.append("    ISNULL(SUM(CASE WHEN sitem.FNUMBER IN ('TD01', 'T012', 'TD101' ) THEN fas.FMoney ELSE 0 END), 0) AS fixed_salary ,\n");
		// 9. 月薪产假工资
		sql.append("    ISNULL(SUM(CASE WHEN sitem.FNUMBER IN ('TD01', 'TD03', 'TD02', 'TD06' , 'TD101') THEN fas.FMoney ELSE 0 END), 0) AS fixed_salary ,\n");
		// 10.日薪产假工资
		sql.append("    ISNULL(SUM(CASE WHEN sitem.FNUMBER IN ('TD01', 'TD06', 'TD07' ) THEN fas.FMoney ELSE 0 END), 0) AS fixed_salary ,\n");

    	// -------------------------- FROM子句 --------------------------
    	// 表关联逻辑：
    	// 1. T_bd_Person（人员表）关联T_HR_EmpLaborRelation（用工关系表）：取入职日期
    	// 2. 人员表关联T_HR_SFixAdjustSalary（固定薪资调整表）：取薪资金额
    	// 3. 薪资表关联T_HR_SCmpItem（薪资项目表）：按薪资项目编码过滤
    	// 4. 人员表关联T_HR_EmployeeContract（员工合同表）：关联甲方信息
    	// 5. 合同表关联T_HR_LabContractFirstParty（合同甲方表）：取甲方名称
    	sql.append("FROM \n");
    	sql.append("    T_bd_Person person \n");
    	sql.append("LEFT JOIN T_HR_EmpLaborRelation elation on person.fid = elation.FPersonID \n");
    	sql.append("LEFT JOIN T_HR_SFixAdjustSalary fas ON person.FID = fas.FPersonID\n");
    	sql.append("LEFT JOIN T_HR_SCmpItem sitem ON fas.FCmpItemID = sitem.FID\n");
    	sql.append("LEFT JOIN T_HR_EmployeeContract econtract on econtract.FEmployeeID = person.FID\n");
    	sql.append("LEFT JOIN T_HR_LabContractFirstParty firstParty on econtract.FContFirstPartyID = firstParty.FID \n");
    	
 
    	sql.append("WHERE \n");
    	sql.append("    sitem.FNUMBER IN ('TD01', 'T012', 'T013', 'TD02', 'TD04', 'TD74', 'T014', 'TD05', 'TD101' ,'TD03' ,'TD06' ,'TD07') \n");
    	sql.append("    and fas.FLeffectDay = '2199-12-31 00:00:00' \n");
    	sql.append("    AND  econtract. FState = '1' AND  econtract.FNewState  = '1' AND  econtract.FIsNewestContract = '1'   \n");
        BOSUuid read = BOSUuid.read(id);
        BOSObjectType type = read.getType();
    	 if (type.toString().equals("7BD37592")){//id类型为合同
         	sql.append("and  person.fid  in (select FEmployeeID from T_HR_EmployeeContract where fid = '"+id+"')" ) ;
         }else  if (type.toString().equals("80EF7DED")){//id类型为员工felse
         	sql.append("and person.fid = '"+id+"'");
         }
    	sql.append("GROUP BY \n");
    	sql.append("    person.FID, person.FNAME_L2 , elation.FEnterDate , person.FIDCardNO , firstParty.FName_l2 \n");
    	
    	// -------------------------- ORDER BY子句 --------------------------
    	// 排序规则：按人员姓名升序排列
    	sql.append("ORDER BY \n");
    	sql.append("    person.FNAME_L2");
    	
    	System.out.print("osfsql"+sql.toString());
    	return sql.toString();
    }


     
}
