package com.kingdee.shr.customer.gtiit.osf;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.customer.gtiit.osf.util.DateUtils;

/**
 * 获取职位的接口
 * @author cb
 *
 */
public class GtiitOrgPositionService implements IHRMsfService {

    private static final Logger logger = LoggerFactory.getLogger(GtiitOrgPositionService.class);

    @Override
    public Object process(Context ctx, Map param) throws EASBizException, BOSException {
        // 开始日期
        String startDate = (String) param.get("startDate");
        // 截止日期
        String endDate = (String) param.get("endDate");
        String sql="SELECT\r\n" + 
        		"	op.FID ID,\r\n" + 
        		"	op.fnumber POSITION_CODE,\r\n" + 
        		"	op.FNAME_L1 POSITION_NAME_EN,\r\n" + 
        		"	op.FNAME_L2 POSITION_NAME_CN,\r\n" + 
        		"	op.feffdt EFFECTIVE_START_DATE,\r\n" + 
        		"	op.fleffdt EFFECTIVE_END_DATE,\r\n" + 
        		"	ob.FNUMBER ORGANIZATION_CODE,\r\n" + 
        		"	ob.FNAME_L1 ORGANIZATION_CODE_NAME,\r\n" + 
        		"	oj.FNUMBER JOB_CODE,\r\n" + 
        		"	oj.FNAME_L1 JOB_NAME_EN,\r\n" + 
        		"CASE\r\n" + 
        		"		\r\n" + 
        		"		WHEN op.FDELETEDSTATUS= '1' THEN\r\n" + 
        		"		'是' \r\n" + 
        		"		WHEN op.FDELETEDSTATUS= '2' THEN\r\n" + 
        		"		'否' \r\n" + 
        		"	END ACTIVE_STATUS,\r\n" + 
        		"CASE\r\n" + 
        		"	\r\n" + 
        		"	WHEN op.FISRESPPOSITION= '0' THEN\r\n" + 
        		"	'否' \r\n" + 
        		"	WHEN op.FISRESPPOSITION= '1' THEN\r\n" + 
        		"	'是' \r\n" + 
        		"	END DEPARTMENT_HEAD,\r\n" + 
        		"	op.FCREATETIME ERP_CREATION_DATE,\r\n" + 
        		"	op.FLASTUPDATETIME ERP_LAST_UPDATE_DATE \r\n" + 
        		"FROM\r\n" + 
        		"	T_ORG_Position op\r\n" + 
        		"	LEFT JOIN T_ORG_BaseUnit ob ON op.FADMINORGUNITID = ob.FID\r\n" + 
        		"	LEFT JOIN T_ORG_Job oj ON oj.FID = op.FJOBID ";
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            startDate = DateUtils.formatDate(startDate, true);
            endDate = DateUtils.formatDate(endDate, false);
            sql += " where op.FLASTUPDATETIME >= '" + startDate + "' and op.FLASTUPDATETIME <= '" + endDate + "'";
        }
        List<String> fieldList = initMappingField();
        logger.info("GtiitOrgPositionService->excute->sql->"+sql.toString());
        IRowSet rs = DbUtil.executeQuery(ctx, sql.toString());
        List<Map<String, String>> dataList = this.getRsListData(fieldList, rs);
        return dataList;
    }

    private List<String> initMappingField() {
        List<String> list = new ArrayList<>();
        list.add("ID");
        list.add("POSITION_CODE");
        list.add("POSITION_NAME_EN");
        list.add("POSITION_NAME_CN");
        list.add("EFFECTIVE_START_DATE");
        list.add("EFFECTIVE_END_DATE");
        list.add("ORGANIZATION_CODE");
        list.add("ORGANIZATION_CODE_NAME");
        list.add("JOB_CODE");
        list.add("JOB_NAME_EN");
        list.add("ACTIVE_STATUS");
        list.add("DEPARTMENT_HEAD");
        list.add("ERP_CREATION_DATE");
        list.add("ERP_LAST_UPDATE_DATE");
        return list;
    }

    private List<Map<String, String>> getRsListData(List<String> fieldList, IRowSet rs) throws BOSException {
        List<Map<String, String>> dataList = new ArrayList<>();
        try {
            while (rs.next()) {
                HashMap<String, String> dataMap = new HashMap<>();
                int i = 0;
                for (int size = fieldList.size(); i < size; ++i) {
                    String field = fieldList.get(i);
                    if ("ACTIVE_STATUS".equals(field) || "DEPARTMENT_HEAD".equals(field)) {
                        dataMap.put(field, "1".equals(rs.getString(field)) ? "是" : "否");
                    } else {
                        dataMap.put(field, rs.getString(field));
                    }
                }
                dataList.add(dataMap);
            }
            return dataList;
        } catch (SQLException ex) {
            throw new BOSException(ex);
        }
    }
}