package weaver.interfaces.htsc.TrealReimTools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.ReimbursementFlow.Dao.UfZbbxandclsplDaoImpl;
import com.engine.htsc.ReimbursementFlow.Service.GettravelsubsidyService;
import com.engine.htsc.ReimbursementFlow.Service.GettravlapplctionService;
import com.engine.htsc.ReimbursementFlow.Service.Impl.GettravelsubsidyServiceImpl;
import com.engine.htsc.ReimbursementFlow.Service.Impl.GettravlapplctionServiceImpl;
import com.engine.htsc.ReimbursementFlow.Service.Impl.UfZbbxandclsplServiceImpl;
import com.engine.htsc.ReimbursementFlow.Service.UfZbbxandclsplService;
import com.engine.htsc.ReimbursementFlow.model.TravelLinkreq;
import com.engine.htsc.ReimbursementFlow.tool.Apireturn;
import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class ReimburseAction extends BaseBean implements Action {

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    private  String status;

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = Util.null2String(requestInfo.getRequestid());
        String tablename = Util.null2String(requestInfo.getRequestManager().getBillTableName());//主表
      //  String userid = Util.null2String(String.valueOf(requestInfo.getRequestManager().getUserId()));
        if("".equals(Util.null2String(status))||"".equals(requestid)||"".equals(tablename)){
            JAVATools.SetCmdwritelog("总部差旅报销","总部差旅报销action数据传参有误");
            JAVATools.SetCmdwritelog(" requestid:"+requestid+" tablename"+tablename+" status:"+status);
        }
        //status=0，将表里数据插入到建模表中
        if(status.equals("0")){
            inserttable(requestid,tablename);

        }else if(status.equals("1")){
            //删除操作
            deletetable(requestid,tablename);
        }
        //不管成功还是失败。不影响流程走向
        return  Action.SUCCESS;
    }


    private  void inserttable(String requestid,String tablename){
        if("".equals(requestid)||"".equals(tablename)){
            return ;
        }
        String sql="     select distinct dt.lcbh,dt12.cclc from "+tablename+"_dt12 dt12 left join "+tablename+" dt on dt.id=dt12.mainid " +
                "     where dt.requestid='"+requestid+"'and dt12.cclc is not null" ;
        List<Map<String,Object>> list=   JAVATools.GetTableToListMap(sql,null);
         if(list==null|| list.size()<=0){
             return;
         }
        String uftable="uf_zbclbxandccsqgx";
        for (Map<String,Object> map:list){
            String treavelcode=Util.null2String(map.get("cclc"));
            String lcbh=Util.null2String(map.get("lcbh"));
            if(!"".equals(treavelcode)&&!"".equals(lcbh)){
                Map<String,String> mapvalue=new HashMap();
                mapvalue.put("requestid",requestid);
                mapvalue.put("treavelcode",treavelcode);
                mapvalue.put("lcbh",lcbh);
                mapvalue.put("opdate",JAVATools.getDatetime());
                JAVATools.Excetesql(mapvalue,uftable);
            }
         }
    }

    private  void deletetable(String requestid,String tablename){
        if("".equals(requestid)||"".equals(tablename)){
            return ;
        }
        String sql="delete from  uf_zbclbxandccsqgx where requestid='"+requestid+"'" ;
        RecordSet rs=new RecordSet();
        rs.executeUpdate(sql) ;
    }

}
