package com.engine.jucailinkq.attendance.workflow.action;

import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 调动分组流程归档action
 * @author sy
 */
@Slf4j
public class AdjustPersonGroupArchivingAction implements Action {


    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        // 流程表单主表数据
        HashMap<String,String> mainTableData = CommonUtil.getMainTableInfo(requestInfo);

        //调动人员
        String ddry = Util.null2String(mainTableData.get("ddry"));
        //调入日期
        String adjustDate = Util.null2String(mainTableData.get("drrq"));
        //调入分组
        String adjustToGroup = Util.null2String(mainTableData.get("drfz"));
        //调出分组
        String adjustFromGroup = Util.null2String(mainTableData.get("dcfz"));
        //调回日期
        String adjustBackDate = Util.null2String(mainTableData.get("dhrq"));
        //调动说明
        String ddsm = Util.null2String(mainTableData.get("ddsm"));

        try {
            String sql = "";
            if (!"".equals(adjustFromGroup)) {
                sql = "select id, bdate, edate from uf_ryqz_dt1 where mainid = " + adjustFromGroup + " and empid = " + ddry + " and delete_type = 0";
                List<Map<String, Object>> data = DbTools.getSqlToList(sql);
                data = data.stream().
                        filter(f -> Util.null2String(f.get("bdate")).compareTo(adjustDate) < 0
                                && ("".equals(Util.null2String(f.get("edate"))) || Util.null2String(f.get("edate")).compareTo(adjustDate) >= 0))
                        .collect(Collectors.toList());
                if (data.size() > 2) {
                    log.error("更新人员分组数据失败: 调出分组中目标数据不唯一！");
                    requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                    requestInfo.getRequestManager().setMessagecontent("更新人员分组数据失败: 调出分组中目标数据不唯一！");
                    return Action.FAILURE_AND_CONTINUE;
                } else if (data.size() == 1) {
                    //修改调出分组中目标数据的失效日期为调入日期前一天，todo

                    //新增一条数据在调入分组中。生效日期为调入日期，失效日期为调回日期

                    //新增一条数据在调出分组中。生效日期为调回日期，失效日期为调回日期
                } else if (data.size() == 0) {

                }
            }
            if ("".equals(adjustBackDate)) {

            } else {

            }


        }catch (Exception e){
            log.error("AdjustPersonGroupArchivingAction error : [{}]",e);
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
