package com.engine.htxc.tycgsq.web;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;

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-09-05 16:35
 * @Description:通用采购相关接口 ，原来的jsp转java
 * @ModifyLog:
 **/
public class GetDeptShortName {
    @GET
    @Path("/getDeptShortName")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDeptShortName(@Context HttpServletRequest request, @Context HttpServletResponse response){
        String fieldvalue = Util.null2String(request.getParameter("fieldvalue"));
        String type = Util.null2String(request.getParameter("type"));
        Map<String,Object> dataMap = new HashMap<String,Object>();
        List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
        ResourceComInfo resourceComInfo = null;
        DepartmentComInfo departmentComInfo = null;
        try {
             resourceComInfo = new ResourceComInfo();
             departmentComInfo = new DepartmentComInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String[] idArr = fieldvalue.split(",");
        for(int i=0;i<idArr.length;i++){

            String showname = "";
            String shortname = "";
            if("hrm".equals(type)){
                String deptid = resourceComInfo.getDepartmentID(idArr[i]);
                shortname = getTopDeptShortName(deptid);
                showname = resourceComInfo.getResourcename(idArr[i])+shortname;
            }else{
                shortname = getTopDeptShortName(idArr[i]);
                showname = departmentComInfo.getDepartmentname(idArr[i])+shortname;
            }

            Map<String,String> itemMap = new HashMap<String,String>();
            itemMap.put("id",idArr[i]);
            itemMap.put("name",showname);
            dataList.add(itemMap);


        }
        dataMap.put("code",200);
        dataMap.put("msg","SUCCESS");
        dataMap.put("value",fieldvalue);
        dataMap.put("specialobj",dataList);

        return JSONObject.toJSONString(dataMap);
    }
    /**
     * 根据部门查询一级部门简称
     * @param deptid
     * @return
     */
    public String getTopDeptShortName(String deptid){
        String shortName = "";
        String sql = " select deptid,cmpAbbre from hrmdepartment a,hrmdepartmentdefined b " +
                " where a.id=b.deptid and a.id in(select id from ( SELECT id,departmentname,supdepid,level " +
                "  FROM hrmdepartment " +
                " START WITH id=? "+
                " connect by nocycle prior supdepid=id ) " +
                " where supdepid is null or supdepid=0)  ";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql,new Object[]{deptid});
        if(rs.next()){
            String cdeptid = Util.null2String(rs.getString("deptid"));
            shortName = Util.null2String(rs.getString("cmpAbbre"));
            if(deptid.equals(cdeptid)){
                shortName = "("+shortName+")";
            }else{
                DepartmentComInfo deptComInfo = new DepartmentComInfo();
                String deptname =  deptComInfo.getDepartmentname(deptid);
                shortName = "("+shortName+"/"+deptname+")";
            }
        }
        return shortName;
    }
}
