package com.engine.htsc.htxw.web;

import com.engine.htsc.payment.util.CommonAPI;
import com.engine.htsc.trip.util.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.general.GCONST;
import weaver.general.Util;

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;

public class HtXwShowControlAction {

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

    private final String IMAGE_URL_PREFIX = GCONST.getContextPath()+"/weaver/weaver.file.FileDownload?fileid=";

    /**
     * 查询新闻列表
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getXinWenList")
    @Produces({MediaType.APPLICATION_JSON})
    public AjaxResult<List<Map<String, String>>> getXinWenList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            List<Map<String, String>> dts = CommonAPI.query("select  m3.id, m3.cjrq, m3.tzlj, m3.fj, m3.cjsj, m3.tpms, m3.tpwz, f1.imagefileid from (\n" +
                    "select id,cjrq,tzlj,fj,cjsj,tpms,tpwz from (\n" +
                    "select t1.id,t1.cjrq,t1.tzlj,t1.fj,t1.cjsj,t1.tpms,t1.tpwz from uf_htpd t1 \n" +
                    "where t1.tpwz=0 \n" +
                    "order by t1.cjrq desc,t1.cjsj desc\n" +
                    ")m1 where rownum=1\n" +
                    "union all\n" +
                    "select id,cjrq,tzlj,fj,cjsj,tpms,tpwz from (\n" +
                    "select t2.id,t2.cjrq,t2.tzlj,t2.fj,t2.cjsj,t2.tpms,t2.tpwz from uf_htpd t2 \n" +
                    "where t2.tpwz=1 \n" +
                    "order by t2.cjrq desc,t2.cjsj desc \n" +
                    ")m2 where rownum=1\n" +
                    ")m3 \n" +
                    "left join DocImageFile f1 on m3.fj=f1.docid");
            List<Map<String, String>> resp = new ArrayList<>();
            dts.forEach(item->{
                Map<String, String> dt= new HashMap<>();
                dt.put("id", Util.null2String(item.get("id")));//id
                dt.put("cjrq", Util.null2String(item.get("cjrq")));//创建日期
                dt.put("tzlj", Util.null2String(item.get("tzlj")));//跳转链接
                dt.put("fj", Util.null2String(item.get("fj")));//封面图片
                dt.put("cjsj", Util.null2String(item.get("cjsj")));//创建时间
                dt.put("tpms", Util.null2String(item.get("tpms")));//图片描述
                dt.put("tpwz", Util.null2String(item.get("tpwz")));//图片描述
                dt.put("imageUrl", Util.null2String(IMAGE_URL_PREFIX + item.get("imagefileid")));//图片下载地址
                resp.add(dt);
            });
            return AjaxResult.ok(resp);
        } catch (Exception e) {
            return AjaxResult.error("catch error:" + e.getMessage());
        }
    }

}
