package weaver.interfaces.htxc.contract;

import com.engine.htxc.commonutil.base.BaseUtil;
import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.commonutil.log.CustomizeLog;
import com.engine.integration.util.StringUtils;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName ContractdetailAction.java
 * @createTime 2022年07月11日 13:38:00
 */
public class ContractdetailAction extends BaseBean implements Action {

    public   DbTools dbTools=new DbTools();
    public String tablename;

    @Override
    public String execute(RequestInfo requestInfo) {
        CustomizeLog.printLog("ContractdetailAction");

        String requestid = requestInfo.getRequestid();
         tablename = requestInfo.getRequestManager().getBillTableName();//主表
        CustomizeLog.printLog("requestid:" + requestid + "tablename:" + tablename);

        if(StringUtils.isBlank(requestid) && StringUtils.isBlank(tablename)){
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败参数有误" );
            return Action.FAILURE_AND_CONTINUE;
        }
        String sql = " select id,kgsldsp,hqbm  from " + tablename + " where requestid='" + requestid + "'";
        Map<String,Object> retmap=dbTools.getsqltomap(sql,(Object) null);
        if(retmap==null || retmap.size()<=0){
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败查询数据有误" );
            return Action.FAILURE_AND_CONTINUE;
        }
        String mainid = Util.null2String(retmap.get( "id" ) );//mainid
        String hqbms = Util.null2String(retmap.get( "hqbm" ) );//会签部门
        String kgsldsps = Util.null2String(retmap.get( "kgsldsp" ) );//跨公司会签
        if(StringUtils.isBlank(mainid)){
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败mainid获取有误" );
            return Action.FAILURE_AND_CONTINUE;
        }
        detail(2,mainid);
        if(!StringUtils.isBlank(hqbms)){
            inertdt2(retmap,tablename);
        }
        detail(3,mainid);
        if(!StringUtils.isBlank(kgsldsps)){
            inertdt3(retmap,tablename);
        }
            return Action.SUCCESS;
    }
    public void inertdt2(Map<String,Object> map,String tablename){
        String mainid = Util.null2String(map.get( "id" ) );//mainid
        String hqbms = Util.null2String(map.get( "hqbm" ) );//会签部门
        String sql = " select wm_concat_old(hqbm) as hqbm  from "+tablename+"_dt2 where mainid="+mainid+" and yhq=1 ";
        Map<String,Object> retmap=  dbTools.getsqltomap(sql);
        Set<String> newset=new HashSet<>();
        String hqbm="";
        if(retmap!=null && retmap.size()>=0){
            hqbm= Util.null2String(retmap.get("hqbm"));
        }
        Set<String> oldset= Arrays.stream(hqbm.split(",")).collect(Collectors.toSet());


        Arrays.stream(hqbms.split(",")).forEach((value)->{
            if(!StringUtils.isBlank(value)){

                if(oldset!=null && !oldset.contains(value)){
                    String deptdirectors= BaseUtil.getdeptdirectors(Util.getIntValue(value,-1 ));
                    String insert_sql = "insert into " + tablename + "_dt2 (mainid,hqbm,bmfzr,sfhq)values('" + mainid + "','" + value + "','" + deptdirectors + "','1')";
                     dbTools.execteSql(insert_sql);
                }

            }
        });
    }
    public void inertdt3(Map<String,Object> map,String tablename){
        String mainid = Util.null2String(map.get( "id" ) );//mainid
        String kgsldsp = Util.null2String(map.get( "kgsldsp" ) );//会签部门
        String sql = " select wm_concat_old(bmfzr) as hqbm  from "+tablename+"_dt3 where mainid="+mainid+" and yhq=1 ";
        Map<String,Object> retmap=  dbTools.getsqltomap(sql);
        Set<String> newset=new HashSet<>();
        String hqbm="";
        if(retmap!=null && retmap.size()>=0){
            hqbm= Util.null2String(retmap.get("hqbm"));
        }
        Set<String> oldset= Arrays.stream(hqbm.split(",")).collect(Collectors.toSet());

        Arrays.stream(kgsldsp.split(",")).forEach((value)->{
            if(oldset!=null && oldset.size()>0 && !oldset.contains(value)){
                if(!StringUtils.isBlank(value)){
                    User user=new User(Util.getIntValue(value,-1));
                    if(user!=null && user.getUserDepartment()>=0){
                      //  String deptdirectors= BaseUtil.getdeptdirectors(user.getUserDepartment());
                        String insert_sql = "insert into " + tablename + "_dt3 (mainid,hqbm,bmfzr,sfhq)values('" + mainid + "','" +  user.getUserDepartment() + "','" + value + "','1')";
                        dbTools.execteSql(insert_sql);
                    }

                }

            }
        });
    }
    public void detail(int number,String mainid){
        if(StringUtils.isBlank(mainid) || number<=0){
            return ;
        }
        dbTools.execteSql( "delete " + tablename + "_dt"+number+" where mainid='" + mainid + "' and (yhq is null or yhq = 0) " );
   }

}
