package weaver.interfaces.htsc.cus;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * Author:DTY-PC
 * Date:2020/11/6 15:47
 * Description: 联合差旅报销流程退回，重置影响和票据关系
 * Copyright:上海泛微网络科技股份有限公司
 */
public class ResetImageAndBill4RejectAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = Util.null2String(requestInfo.getRequestid());
        String src = requestInfo.getRequestManager().getSrc();
        HTUtil ht = new HTUtil();
        String tableName = ht.getTableNameByRequestid(requestid,"0");//主表
        String tableDt4Name = tableName+"_dt4";//明细4
        try {
            if(src.equals("reject")){//退回
                ResourceComInfo rc = new ResourceComInfo();
                String sql = "select id,lcbh,ngr from " + tableName + " where requestid = " + requestid;
                RecordSet rs = new RecordSet();
                rs.execute(sql);
                rs.next();
                int mianid = Util.getIntValue(rs.getString("id"));//主表id
                String lcbh = Util.null2String(rs.getString("lcbh"));//流程编号
                String ngr = Util.null2String(rs.getString("ngr"));//拟稿人
                String workcode = rc.getWorkcode(ngr);//工号
                sql = "select wjid,fphm,fpdm from " + tableDt4Name + " where mainid = "+mianid;
                rs.execute(sql);
                int i = 1;
                while(rs.next()){
                    String wjid = Util.null2String(rs.getString("wjid"));//文件id
                    String fphm = Util.null2String(rs.getString("fphm"));//发票号码
                    String fpdm = Util.null2String(rs.getString("fpdm"));//发票代码
                    Map<String,Object> params = new HashMap<>();
                    params.put("UserCode",workcode);
                    params.put("FileID",wjid);
                    params.put("InvoiceNum",fphm);
                    params.put("InvoiceCode",fpdm);
                    params.put("BillID",lcbh);
                    String res  = ht.resetImageAndBill(params);
                    JSONObject resObj = JSONObject.parseObject(res);
                    String code = Util.null2String(resObj.get("ReCode"));//1：成功
                    String msg = Util.null2String(resObj.get("ReMsg"));
                    if(!"1".equals(code)){
                        requestInfo.getRequestManager().setMessageid("11111111");
                        requestInfo.getRequestManager().setMessagecontent("重置第 " + i + " 行【增值税发票】明细影像票据关系出错！ERROR:"+msg);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                    i++;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("11111111");
            requestInfo.getRequestManager().setMessagecontent("关联重置影像关系接口捕获异常，请联系管理员处理！");
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
