package weaver.interfaces.htsc.workflow;

import com.engine.htsc.payment.util.PayUtil;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.UUID;

/**
 * Created by K1810006 on 2020/9/17.
 */
public class BankAccountHistoryHKAction extends BaseBean implements Action{


    @Override
    public String execute(RequestInfo requestInfo) {
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String requestid = requestInfo.getRequestid();
        int workflowid = Util.getIntValue(requestInfo.getWorkflowid());

        RecordSet rs = new RecordSet();
        String wfunnumber = PayUtil.getUnNumberByWorkFlowId(workflowid);
        if("xgtybx".equals(wfunnumber)){
              String dataSql = " select ngr,ygxm,bm,yxzh,khx,'' skdw from "+tablename+" a,"+tablename+"_dt11 b \n" +
                    "where a.id=b.mainid and a.requestid=? ";
            rs.executeQuery(dataSql,requestid);
        }

        if("xgtyfk".equals(wfunnumber)){
            String dataSql = " select ngr,'' ygxm,'' bm,yxzh,khx,skdw from "+tablename+" a,"+tablename+"_dt11 b \n" +
                    "where a.id=b.mainid and a.requestid=? ";
            rs.executeQuery(dataSql,requestid);
        }

        inserBankAccount(rs);
        return Action.SUCCESS;
    }


    /**
     * 是否重复,银行卡号付款单位,创建人,为唯一区别
     *
     * @return boolean
     */
    public boolean isRepeat(String yxzh) {
        boolean flag = false;
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from uf_xgyhlsjl where yxzh=? ",yxzh);
        if (rs.next()) {
            flag = true;
        }
        return flag;
    }


    public void inserBankAccount(RecordSet rs){
        try{
            ResourceComInfo resComInfo = new ResourceComInfo();
            RecordSet rs2 = new RecordSet();
            int formModeId = PayUtil.getFormModeIdByCubeName("uf_xgyhlsjl");
            String uuid = UUID.randomUUID().toString();
            String createdate = TimeUtil.getCurrentDateString();
            String createtime = TimeUtil.getOnlyCurrentTimeString();
            String insertSql = "insert into uf_xgyhlsjl(ygxm,bm,yxzh,khx,skdw,workcode,formmodeid,modedatacreater," +
                    "modedatacreatertype,modedatacreatedate,modedatacreatetime,modeuuid) \n" +
                    "values(?,?,?,?,?,?,?,?,?,?,?,?)";
            while (rs.next()){
                String yxzh = Util.null2String(rs.getString("yxzh"));
                if(isRepeat(yxzh)){
                    continue;
                }
                int ngr = Util.getIntValue(rs.getString("ngr"));
                String workcode = resComInfo.getWorkcode(ngr+"");
                String ygxm = Util.null2String(rs.getString("ygxm"));
                String bm = Util.null2String(rs.getString("bm"));
                String skdw = Util.null2String(rs.getString("skdw"));
                String khx = Util.null2String(rs.getString("khx"));
                boolean result = rs2.executeUpdate(insertSql,new Object[]{ygxm,bm,yxzh,khx,skdw,workcode,formModeId,ngr,0,createdate,createtime,uuid});
                if(result){
                    rebuildRight(ngr,formModeId,uuid);
                }
            }
        }catch (Exception e) {

        }


    }


    /**
     * 建模数据权限重构
     * @param creater
     * @param formModeId
     * @param uuid
     */
    public void rebuildRight(int creater,int formModeId, String uuid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery(" select id from uf_xgyhlsjl where modeuuid=?",uuid);
        rs.next();
        int id = rs.getInt("id");
        if(id>0){
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.editModeDataShare(creater, formModeId, id);
        }
    }



}
