package com.engine.htxc.tycgsq.web;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-08-24 13:58
 * @Description:采购模块相关接口
 * @ModifyLog:
 **/
public class GetHqbmAjax {
    @GET
    @Path("/getHqbmAjax")
    @Produces({MediaType.TEXT_PLAIN})
    public String getHqbmAjax(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        int requestid = Util.getIntValue(Util.null2String(request.getParameter("requestid")));    //
        int workflowid = Util.getIntValue(Util.null2String(request.getParameter("workflowid")));  //
        String zbhqbm = Util.null2String(request.getParameter("hqbm"));
        PropBean pb = new PropBean();
        String fieldid = pb.getPropName("zbzysx_hqbm_hqyy_fieldid"); // "11014";
        String formTable = "" ;
        String sql = " select abs(formid) as formid from workflow_base where id = "+workflowid;
        rs.execute(sql);
        if(rs.next()){
            formTable = "formtable_main_"+Util.null2String(rs.getString("formid"));
        }
        Map<String,Object> dataMap = new HashMap<String,Object>();
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        Map<String,String> map = null;
        String hqbms = "" ;

        if(!"".equals(zbhqbm))
        {
            if(!"".equals(formTable))
            {
                sql = " select h.id,h.departmentname,l.hqbm,l.bmzz,k.selectname\n" +
                        " from hrmdepartment h " +
                        " left join "+formTable+"_dt2 l on l.hqbm = h.id \n" +
                        " left join "+formTable+" t on l.mainid = t.id\n" +
                        " left join workflow_selectitem k on k.selectvalue = l.bmzz  and k.fieldid= "+fieldid+
                        " where requestid=" + requestid +
                        " and h.id in("+zbhqbm+") ";

                bb.writeLog("sql:"+sql);
                rs.execute(sql);

                while (rs.next()){
                    map = new HashMap<String,String>();
                    String hqbm = Util.null2String(rs.getString("id"));
                    String departmentname = Util.null2String(rs.getString("departmentname"));
                    String selectname = Util.null2String(rs.getString("selectname"));
                    if(!"".equals(selectname)){
                        map.put("name",departmentname+"("+selectname+")");
                    }else{
                        map.put("name",departmentname);
                    }
                    hqbms += hqbms == "" ? hqbm : ","+hqbm ;
                    map.put("id",hqbm);

                    list.add(map);
                }
            }
        }

        dataMap.put("value",hqbms);
        dataMap.put("specialobj",list);
        dataMap.put("code",200);
        return JSONObject.toJSONString(dataMap);
    }
}
