package com.engine.htsc.htxw.web;


import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.htxw.util.HtXwEntity;
import com.engine.htsc.htxw.util.HtXwPageUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
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 HtXwControlAction {

    /**
     * 查询新闻列表
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getXwList")
    @Produces({MediaType.TEXT_PLAIN})
    public String getXwList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String ecologyContentPath = GCONST.getContextPath();
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        RecordSet sqlFindXwStyle = new RecordSet();
        RecordSet sqlFindImage = new RecordSet();
        RecordSet sqlFindDoc = new RecordSet();

        RecordSet sqlTotal = new RecordSet();

        HtXwPageUtil htXwPageUtil = new HtXwPageUtil();

        Integer pageNo = Util.getIntValue(request.getParameter("pageNo"),1);
        Integer pageSize = Util.getIntValue(request.getParameter("pageSize"),5);
        String order = Util.null2String(request.getParameter("order"),"cjrq");
        String xwlxNew = Util.null2String(request.getParameter("xwlx"),"");


        String sql = "SELECT id,xwbt,cjrq,cjsj,fj,nr,yxj,xwfj,xwlx FROM uf_xwwh ";
        if(!xwlxNew.equals("")){
            sql = sql +" "+ "where xwlx = " + xwlxNew;
        }
        String orderSql = " "+" ORDER BY "+order+" DESC";
        if(order.equals("cjrq")){
            orderSql = " "+"ORDER BY cjrq DESC,cjsj DESC";
        }
        //判断超过最后一页

        String sqlAll = "select count(*)as total from uf_xwwh";
        if(!xwlxNew.equals("")){
            sqlAll = sqlAll  +" "+ "where xwlx = " + xwlxNew;
        }
        sqlTotal.execute(sqlAll);
        String total = "";
        if (sqlTotal.next())
        {
            total = Util.null2String(sqlTotal.getString("total"));
            Integer numAll = pageNo*pageSize;
            if(numAll>Integer.valueOf(total)){
                Integer pageNoNew = Integer.valueOf(total)/pageSize + 1;
                pageNo = pageNoNew;
            }
        }

        HtXwEntity htXwEntity = new HtXwEntity();
        JSONObject jsonObject = new JSONObject();
        htXwEntity.setPageNo(pageNo);
        htXwEntity.setPageSize(pageSize);
        htXwEntity.setTotal(Integer.valueOf(total));
        List<Map<String,Object>> listNew = new ArrayList<Map<String,Object>>();
        Map<String,Object> selectMap = new HashMap<String,Object>();

        try{

            String fieldid = "" ;
            String sql_v =  " select k.id from workflow_billfield k\n" +
                            " inner join workflow_bill l on l.id = k.billid\n" +
                            " where l.tablename='uf_xwwh'\n" +
                            " and fieldname='xwlx'\n " ;
            rs.execute(sql_v);
            if(rs.next())
            {
                fieldid = Util.null2String(rs.getString("id"));
            }

            if(!"".equals(fieldid))
            {
                sql_v = " select selectvalue,selectname from workflow_selectitem where fieldid ="+fieldid;
                rs.execute(sql_v);
                while (rs.next())
                {
                    String selectvalue = Util.null2String(rs.getString("selectvalue"));
                    String selectname = Util.null2String(rs.getString("selectname"));
                    bb.infoLog("selectname",selectname);
                    bb.infoLog("selectvalue",selectvalue);
                    selectMap.put(selectvalue,selectname);
                }
            }

            String resultSql = HtXwPageUtil.getPaginationSql(sql,orderSql,pageNo,pageSize);
            bb.infoLog("resultSql:",resultSql);
            rs.execute(resultSql);
            while (rs.next())
            {
                Map<String,Object> printMap = new HashMap<String,Object>();
                String id = Util.null2String(rs.getString("id"));
                String xwbt = Util.null2String(rs.getString("xwbt"));
                String cjrq = Util.null2String(rs.getString("cjrq"));
                String cjsj = Util.null2String(rs.getString("cjsj"));
                String fj = Util.null2String(rs.getString("fj"));
                String nr = Util.null2String(rs.getString("nr"));
                String nrNew = nr.replaceAll("/weaver/weaver",ecologyContentPath+"/weaver/weaver");
                nr = nrNew;
                String yxj = Util.null2String(rs.getString("yxj"));
                if(yxj.equals("")){
                    yxj = "0";
                }
                String xwfj = Util.null2String(rs.getString("xwfj"));
                String xwlx = Util.null2String(rs.getString("xwlx"));
                bb.infoLog("xwlx",xwlx);
                if(xwlx.equals("")){
                    xwlx = "0";
                }

                ArrayList imageUrlList = new ArrayList();
                if(!"".equals(fj)){
                    for(String fjId:fj.split(",")){
                        sqlFindImage.execute("select imagefileid from DocImageFile where docid = "+fjId);
                        if(sqlFindImage.next()){
                            String imagefileid = Util.null2String(sqlFindImage.getString("imagefileid"));
                            String imageUrl = "";
                            imageUrl = ecologyContentPath+"/weaver/weaver.file.FileDownload?"+"fileid="+imagefileid;
                            imageUrlList.add(imageUrl);
                        }
                    }
                }


                ArrayList docUrlList = new ArrayList();
                if(!"".equals(xwfj)){

                    for(String xwfjId:xwfj.split(",")){
                        sqlFindDoc.execute("select imagefileid from DocImageFile where docid = "+xwfjId);
                        if(sqlFindDoc.next()){
                            String docfileid = Util.null2String(sqlFindDoc.getString("imagefileid"));
                            String docUrl = "";
                            docUrl = ecologyContentPath+"/weaver/weaver.file.FileDownload?"+"fileid="+docfileid;
                            docUrlList.add(docUrl);
                        }
                    }

                }



                String xwlxValue = Util.null2String(selectMap.get(xwlx));
                bb.infoLog("xwlxValue",xwlxValue);
//                if(sqlFindXwStyle.next()){
//                    xwlxValue = Util.null2String(sqlFindXwStyle.getString("xwlxValue"));
//                }

                printMap.put("id",Integer.valueOf(id));
                printMap.put("xwbt",xwbt);
                printMap.put("cjrq",cjrq);
                printMap.put("cjsj",cjsj);
                printMap.put("imageUrl",imageUrlList);
                printMap.put("nr",nr);
                printMap.put("yxj",Integer.valueOf(yxj));
                printMap.put("docUrl",docUrlList);
                printMap.put("xwlx",Integer.valueOf(xwlx));
                printMap.put("xwlxValue",xwlxValue);
                listNew.add(printMap);
            }
            htXwEntity.setList(listNew);
            jsonObject.put("code","200");
            jsonObject.put("msg","success");
            jsonObject.put("data",htXwEntity);
        }catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code","500");
            jsonObject.put("msg","fail");
        }
        return JSONObject.toJSONString(jsonObject);
    }

    /**
     * 查询新闻详细列表
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getXwListById")
    @Produces({MediaType.TEXT_PLAIN})
    public String getXwListById(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String ecologyContentPath = GCONST.getContextPath();
        RecordSet rs = new RecordSet();
        Integer idSelect = Util.getIntValue(request.getParameter("Id"),1);
        String sql = "SELECT id,xwbt,cjrq,cjsj,fj,nr,yxj,xwfj,xwlx FROM uf_xwwh where id ="+idSelect;
        rs.execute(sql);
        Map<String,Object> printMap = new HashMap<String,Object>();
        JSONObject jsonObject = new JSONObject();
        try{
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String xwbt = Util.null2String(rs.getString("xwbt"));
                String cjrq = Util.null2String(rs.getString("cjrq"));
                String cjsj = Util.null2String(rs.getString("cjsj"));
                String fj = Util.null2String(rs.getString("fj"));
                String nr = Util.null2String(rs.getString("nr"));
                String nrNew = nr.replaceAll("/weaver/weaver",ecologyContentPath+"/weaver/weaver");
                nr = nrNew;
                String yxj = Util.null2String(rs.getString("yxj"));
                if(yxj.equals("")){
                    yxj = "0";
                }
                String xwfj = Util.null2String(rs.getString("xwfj"));
                String xwlx = Util.null2String(rs.getString("xwlx"));
                if(xwlx.equals("")){
                    xwlx = "0";
                }
                RecordSet sqlFindImage = new RecordSet();
                ArrayList imageUrlList = new ArrayList();
                if(!"".equals(fj)){
                    for(String fjId:fj.split(",")){
                        sqlFindImage.execute("select imagefileid from DocImageFile where docid = "+fjId);
                        if(sqlFindImage.next()){
                            String imagefileid = Util.null2String(sqlFindImage.getString("imagefileid"));
                            String imageUrl = "";
                            imageUrl = ecologyContentPath+"/weaver/weaver.file.FileDownload?"+"fileid="+imagefileid;
                            imageUrlList.add(imageUrl);
                        }
                    }
                }
                RecordSet sqlFindDoc = new RecordSet();
                ArrayList docUrlList = new ArrayList();
                if(!"".equals(xwfj)){
                    for(String xwfjId:xwfj.split(",")){
                        sqlFindDoc.execute("select imagefileid from DocImageFile where docid = "+xwfjId);
                        if(sqlFindDoc.next()){
                            String docfileid = Util.null2String(sqlFindDoc.getString("imagefileid"));
                            String docUrl = "";
                            docUrl = ecologyContentPath+"/weaver/weaver.file.FileDownload?"+"fileid="+docfileid;
                            docUrlList.add(docUrl);
                        }
                    }
                }
                RecordSet sqlFindXwStyle = new RecordSet();
                sqlFindXwStyle.execute("select (select selectname from workflow_selectitem where xwlx = selectvalue " +
                        "and fieldid = 274067)xwlxValue from uf_xwwh WHERE id = "+id);
                String xwlxValue = "";
                if(sqlFindXwStyle.next()){
                    xwlxValue = Util.null2String(sqlFindXwStyle.getString("xwlxValue"));
                }
                printMap.put("id",Integer.valueOf(id));
                printMap.put("xwbt",xwbt);
                printMap.put("cjrq",cjrq);
                printMap.put("cjsj",cjsj);
                printMap.put("imageUrl",imageUrlList);
                printMap.put("nr",nr);
                printMap.put("yxj",Integer.valueOf(yxj));
                printMap.put("docUrl",docUrlList);
                printMap.put("xwlx",Integer.valueOf(xwlx));
                printMap.put("xwlxValue",xwlxValue);
            }
            jsonObject.put("code","200");
            jsonObject.put("msg","success");
            jsonObject.put("data",printMap);
        }catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code","500");
            jsonObject.put("msg","fail");
        }
        return JSONObject.toJSONString(jsonObject);
    }

    /**
     * 预留新闻地址获取
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getXwUrl")
    @Produces({MediaType.TEXT_PLAIN})
    public String getXwUrl(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        String getUrl = Util.null2String(request.getParameter("url"));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("getUrl",getUrl);
        return JSONObject.toJSONString(jsonObject);
    }

}
