package weaver.interfaces.htsc.workflow.odoc;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by K1810001 on 2020/11/17.
 */
public class DeptDetailDeafultOutAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet dtRs = new RecordSet();
        RecordSet deptRs = new RecordSet();
        JSONObject restultObj = new JSONObject();
        BaseBean bs = new BaseBean();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String requestid = requestInfo.getRequestid();
        User requser = requestInfo.getRequestManager().getUser();
        int createrid = requser.getUID();


        ResourceComInfo resourceComInfo = null;
        try{
            resourceComInfo = new ResourceComInfo();
            //根据requestid和creatorid获取该条数据的id
            RecordSet selectRs = new RecordSet();
            String idSql = "select id,hqbm,kgsldsp from "+tablename+" where requestid = "+requestid;
            rs.executeQuery(idSql);
            List<String> rejectTwoList = new ArrayList<>();
            if(rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String hqbm = Util.null2String(rs.getString("hqbm"));
                String kgsldsp = Util.null2String(rs.getString("kgsldsp"));
                //拆分会签部门字段
                String detailtableTwo = tablename+"_dt2";
                //1.退回流程 插入数据前将未勾选的历史明细数据清空;
                String selectSql = "select id,yhq,hqbm from "+detailtableTwo+" where mainid = "+id;
                selectRs.execute(selectSql);
                while (selectRs.next()) {
                    int yhq = Util.getIntValue(selectRs.getString("yhq"));
                    int detailId = Util.getIntValue(selectRs.getString("id"));
                    String detailHqbm = Util.null2String(selectRs.getString("hqbm"));
                    if (yhq != 1) {
                        String deleteSqlTwo = "delete from " + detailtableTwo + " where mainid =" + id + " and id =" + detailId;
                        dtRs.executeUpdate(deleteSqlTwo);
                    }else {
                        rejectTwoList.add(detailHqbm);
                    }
                }
                //遍历hqbm字段插入明细表
                if (!"".equals(hqbm)){
                    String dtTwoSql = "insert into "+detailtableTwo+"(mainid,hqbm,bmfzr,sfhq) values(?,?,?,?)";
                    String[] hqbmsplit = hqbm.split(",");
                    for (String hqbmid : hqbmsplit){
                        //2.获取部门负责人
                        if (!rejectTwoList.contains(hqbmid)){
                            List<String> list = new ArrayList<>();
                            String deptSql = "select deptdirectors from hrmdepartmentdefined where deptid ="+hqbmid;
                            deptRs.execute(deptSql);
                            while (deptRs.next()){
                                String deptdirectors = Util.null2String(deptRs.getString("deptdirectors"));
                                list.add(deptdirectors);
                            }
                            String bmfzr = StringUtils.join(list, ",");
                            dtRs.executeUpdate(dtTwoSql,new Object[]{id,hqbmid,bmfzr,"1"});
                        }
                    }
                }


                //跨公司领导会签
                RecordSet selectkgsRs = new RecordSet();
                String detailtableThree = tablename+"_dt3";
                List<String> rejectThreeList = new ArrayList<>();
                //1.退回流程 插入数据前将历史明细数据清空;
                String selectkgsSql = "select id,yhq,bmfzr from "+detailtableThree+" where mainid = "+id;
                selectkgsRs.execute(selectkgsSql);
                while (selectkgsRs.next()){
                    int yhq = Util.getIntValue(selectkgsRs.getString("yhq"));
                    int detailId = Util.getIntValue(selectkgsRs.getString("id"));
                    String bmfzr = Util.null2String(selectkgsRs.getString("bmfzr"));
                    if (yhq != 1){
                        String deleteSqlThree = "delete from "+detailtableThree+" where mainid ="+id+" and id ="+detailId;
                        dtRs.executeUpdate(deleteSqlThree);
                    }else {
                        rejectThreeList.add(bmfzr);
                    }
                }
                if (!"".equals(kgsldsp)){
                    String dtThreeSql = "insert into "+detailtableThree+"(mainid,hqbm,bmfzr,sfhq) values(?,?,?,?)";
                    String[] kgshqsplit = kgsldsp.split(",");
                    for (String  kgshqid : kgshqsplit){
                        if (!rejectThreeList.contains(kgshqid)) {
                            String departmentID = resourceComInfo.getDepartmentID(kgshqid);
                            dtRs.executeUpdate(dtThreeSql, new Object[]{id, departmentID, kgshqid, "1"});
                        }
                    }
                }
            }
            restultObj.put("status","1");

        }catch (Exception e){
            e.printStackTrace();
            restultObj.put("status","-1");
        }
        return null;
    }
}
