package com.engine.htsc.payment.web;

import com.engine.common.util.ServiceUtil;
import com.engine.htsc.payment.service.SaveIntoFinancePaymentService;
import com.engine.htsc.payment.service.impl.SaveIntoFinancePaymentServiceImpl;
import com.engine.htsc.trip.util.AjaxResult;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.cus.SaveTrip4SubAction;
import weaver.interfaces.htsc.cus.UpdateReimbursement4SubAction;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.Arrays;

public class SaveIntoFinancePaymentAction {


    private SaveIntoFinancePaymentService getServices() {
        return ServiceUtil.getService(SaveIntoFinancePaymentServiceImpl.class);
    }
    /**
     * 根据requestid写入资金系统
     * @param requestids
     * @return
     */
    @GET
    @Path("/saveIntoFinance")
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<String> saveIntoFinanceDB(@QueryParam("requestid") String requestids) {
        String result = "";
        try {
            if(!"".equals(Util.null2String(requestids))){
                JAVATools.writelog("根据requestid写入资金系统 saveIntoFinance 入参: "+ requestids);
                result = this.getServices().saveIntoFinance(Arrays.asList(requestids.split(",")));
            }else{
                result = "请选择需要写入资金系统的流程！";
            }
            return AjaxResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据requestid写入资金系统
     * @return
     */
    @GET
    @Path("/saveLhbxIntoJourney")
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<String> saveHtlhBxIntoJourney() {
        String result = "";
        try {
            UpdateReimbursement4SubAction action1 = new UpdateReimbursement4SubAction();
            String sql = " select * from uf_dsrwdb t where t.rwlx='LHBX'";
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            rs.executeQuery(sql);
            while (rs.next()){
                String reqid = Util.null2String(rs.getString("reqid"));
                String sql1 = "select s.requestname from workflow_requestbase s where s.requestid='"+reqid+"'";
                rs1.executeQuery(sql1);
                String reqName = "";
                if(rs1.next()){
                    reqName = Util.null2String(rs1.getString("requestname"));
                }
                RequestInfo requestInfo = new RequestInfo();
                RequestManager requestManager = new RequestManager();
                requestManager.setRequestname(reqName);
                requestInfo.setRequestid(reqid);
                requestInfo.setRequestManager(requestManager);
                action1.setStatus("FINISH");
                action1.execute(requestInfo);
            }
            return AjaxResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据requestid写入资金系统
     * @return
     */
    @GET
    @Path("/saveLhccIntoJourney")
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<String> saveHtlhCcIntoJourney() {
        String result = "";
        try {
            SaveTrip4SubAction action1 = new SaveTrip4SubAction();
            String sql = " select * from uf_dsrwdb t where t.rwlx='LHCC'";
            RecordSet rs = new RecordSet();
            rs.executeQuery(sql);
            while (rs.next()){
                String reqid = Util.null2String(rs.getString("reqid"));
                RequestInfo requestInfo = new RequestInfo();
                RequestManager requestManager = new RequestManager();
                requestManager.setBilltablename("formtable_main_43");
                requestInfo.setRequestid(reqid);
                requestInfo.setRequestManager(requestManager);
                action1.execute(requestInfo);
            }
            return AjaxResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
