package com.engine.htsc.glpt.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.integration.util.StringUtils;
import com.weaver.general.BaseBean;
import org.apache.commons.collections.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.workflow.action.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.util.*;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810039
 * @version: 1.0
 * @CreateDate: 2022-06-13
 * @Description: 对接IT需求管理平台，验证流程状态
 * @ModifyLog:
 **/
public class CheckStateAction {
    /***
     * 对接IT需求管理平台，验证流程状态
     */
    @POST
    @Path("/checkState")
    @Produces({MediaType.TEXT_PLAIN})
    public String checkState( @Context HttpServletRequest request, @Context HttpServletResponse response) {
        JAVATools.writelog("-------CheckStateAction------");
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String xqbhStr = Util.null2String(request.getParameter("demandcodeList"));

            if (StringUtils.isEmpty(xqbhStr)){
                JAVATools.writelog("xqbhStr is null");
                apidatas.put("success",false);
                apidatas.put("msg", "接收到的参数为空");
                return JSONObject.toJSONString(apidatas);
            }
            JAVATools.writelog("xqbhStr:"+xqbhStr);
            String sql = "";
            RecordSet rs = new RecordSet();
            sql = "select distinct d.XQBH from formtable_main_548 t join FORMTABLE_MAIN_548_DT7 d\n" +
                    "    on t.ID in (d.MAINID) join WORKFLOW_REQUESTBASE a on a.REQUESTID = t.REQUESTID\n" +
                    "where d.XQBH in ("+xqbhStr+") and a.CURRENTNODETYPE in (1,2) and a.ISSTOPPED IS NUll ";
            JAVATools.writelog(sql);
            List<String> resList = new ArrayList<>();
            rs.execute(sql);
            while (rs.next()){
                resList.add(Util.null2String(rs.getString("XQBH")));
            }
            apidatas.put("success",true);
            apidatas.put("msg", "查询成功");
            apidatas.put("demandcodeList",resList);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success",false);
            apidatas.put("msg", "系统内部错误");
        }

        return JSONObject.toJSONString(apidatas);

    }

    /**
     * 触发IT需求管理平台流程拟稿节点前Action
     */
    @POST
    @Path("/ItxqlcDeleteJira")
    @Produces({MediaType.TEXT_PLAIN})
    public void ItxqlcDeleteJira( @Context HttpServletRequest request, @Context HttpServletResponse response) {
        String requestid = Util.null2String(request.getParameter("requestid"));
        JAVATools.writelog("-------ItxqlcDeleteJira------" + requestid );
        RecordSetDataSource rsd = new RecordSetDataSource("ALM");
        BaseBean bb = new BaseBean();
        try {
            String sql = " DELETE FROM T_ITAPPLYREQUIREMENT_DATA WHERE WORKFLOWNUM= '"+requestid+"' AND STATUS='未办结'";
            JAVATools.writelog(sql);
            rsd.execute(sql);
        } catch (Exception e) {
            e.printStackTrace();
            JAVATools.writelog("error:ItxqlcDeleteJira");
            JAVATools.writelog( "异常参考:" + e );
        }
    }


    /**
     * 将List<String>集合转化为带引号的String
     * 如{"1","2"} To '1','2'
     * @param strList ⼊参
     * @return String
     */
    public static String convertListToString(List<String> strList) {
        StringBuffer sb = new StringBuffer();
        if (CollectionUtils.isNotEmpty(strList)) {
            for (int i = 0; i < strList.size(); i++) {
                if (i == 0) {
                    sb.append("'").append(strList.get(i)).append("'");
                } else {
                    sb.append(",").append("'").append(strList.get(i)).append("'");
                }
            }
        }
        return sb.toString();
    }

}
