package com.engine.htxc.document;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htxc.document.service.impl.DocTemplateServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.hrm.HrmUserVarify;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

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.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810050
 * @version: 1.0
 * @CreateDate: 2022/8/18 18:06
 * @Description: 文档中心模板action
 * @ModifyLog: 修改路径名称，类名
 **/
public class DocTemplateAction {

    private static final Logger logger= LoggerFactory.getLogger(DocTemplateAction.class);

    private DocTemplateServiceImpl docTemplateService(){
        return ServiceUtil.getService(DocTemplateServiceImpl.class);
    }


    /**
     * 根据模板编号查询存储网关id
     * @param req  模板编号
     * @param rep  模板编号存储网关文件id
     * @return
     */
    @GET
    @Path("/getSgFileidByTemplateNos")
    @Produces({MediaType.APPLICATION_JSON})
    public String getSgFileidByTemplateNo(@Context HttpServletRequest req, @Context HttpServletResponse rep){
        HrmUserVarify.getUser(req, rep);
        String templateNos = req.getParameter("templateNos");
        logger.info("传入模板编号： [{}]",templateNos);
        try {
            List result = docTemplateService().getSgFileIdByTemplateNos(Stream.of(templateNos.split(",")).collect(Collectors.toList()));
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 200);
            map.put("msg", "success");
            map.put("data", result);
            return JSON.toJSONString(map);
        }catch (Exception e){
            logger.error("查询模板信息失败",e);
        }
        return JAVATools.Apiretrun("400","查询文档失败");

    }
}
