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 GroupDetailDeafultOutAction 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();
            //1.获取主次账号
            String useridAll = "";
            User user = new User(createrid);
            if ("1".equals(user.getAccount_type())){
                String sql = "select belongto from hrmresource where status < 4 and id ="+createrid;
                rs.execute(sql);
                if (rs.next()){
                    int belongto = Util.getIntValue(rs.getString("belongto"));
                    User userNew = new User(belongto);
                    useridAll = userNew.getUID() + "";
                    String belongtoids = userNew.getBelongtoids();
                    if (!"".equals(belongtoids)) {
                        useridAll += "," + belongtoids;
                    }
                }
            }else if ("0".equals(user.getAccount_type())){
                useridAll = user.getUID() + "";
                String belongtoids = user.getBelongtoids();
                if (!"".equals(belongtoids)) {
                    useridAll += "," + belongtoids;
                }
            }

            //根据requestid和creatorid获取该条数据的id
            RecordSet selectRs = new RecordSet();
            String idSql = "select id,hqbm from "+tablename+" where requestid = '"+requestid+"' and ngr in ("+useridAll+")";
            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 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"});
                        }
                    }
                }
            }
            restultObj.put("status","1");

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