package com.engine.htsc.mpsq.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.mpsq.MpsqEntity;
import com.engine.htsc.mpsq.service.MpsqService;
import com.engine.htsc.mpsq.service.impl.MpsqServiceImpl;
import oracle.jdbc.proxy.annotation.Post;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

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 javax.ws.rs.core.Response;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MpsqAction {
    private MpsqService getService(User user) {
        return ServiceUtil.getService(MpsqServiceImpl.class, user);
    }
    @GET
    @Path("/getDetailByExcel")
    @Produces({MediaType.APPLICATION_JSON})
    public String getDetailByExcel(@Context HttpServletRequest request,
                                               @Context HttpServletResponse response){
        HashMap<String, Object> map = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            List<MpsqEntity> list = this.getService(user).getDetailByExcel(ParamUtil.request2Map(request));
//            List<MpsqEntity> collect = list.stream().filter(distinctByKey(MpsqEntity::getSj)).collect(Collectors.toList());
            map.put("code",200);
            map.put("msg","success");
            map.put("data",list);

            return JSON.toJSONString(map);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg",e.getMessage());
            map.put("data",null);

            return JSON.toJSONString(map);
        }

    }
    private  <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }


    @GET
    @Path("/getExportExcel")
    @Produces({MediaType.APPLICATION_JSON})
//    @Produces({MediaType.APPLICATION_OCTET_STREAM})
    public String getExportExcel(@Context HttpServletRequest request,
                                   @Context HttpServletResponse response){

        //添加响应头信息
//        response.setHeader("Content-disposition", "attachment; filename=" + "mpsq.xlsx");
//        response.setContentType("application/vnd.ms-excel;charset=UTF-8");//设置类型
//        response.setHeader("Pragma", "No-cache");//设置头
//        response.setHeader("Cache-Control", "no-cache");//设置头
//        response.setDateHeader("Expires", 0);//设置日期头
        HashMap<String, String> map = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            String path = this.getService(user).getExportExcel(ParamUtil.request2Map(request), response);
            map.put("code","200");
            map.put("msg","success");
            map.put("data",path);
        }catch (Exception e){
            map.put("code","500");
            map.put("msg",e.getMessage());
            map.put("data","");
        }
        return JSONObject.toJSONString(map);


    }
    @GET
    @Path("/deleteFile")
    @Produces({MediaType.APPLICATION_JSON})
    public String deleteFile(@Context HttpServletRequest request,
                                 @Context HttpServletResponse response){
        HashMap<String, String> map = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            String path = request.getParameter("path");
            boolean flag  = this.getService(user).deleteFile(path);
            if(flag){
                map.put("code","200");
                map.put("msg","success");
            }else{
                map.put("code","500");
                map.put("msg","error");
            }


        }catch (Exception e){
            map.put("code","500");
            map.put("msg",e.getMessage());

        }
        return JSONObject.toJSONString(map);


    }
}
