package com.engine.organization.web;

import com.engine.common.util.ServiceUtil;
import com.engine.organization.entity.config.params.QTXConfigParam;
import com.engine.organization.entity.config.po.QTXConfigPO;
import com.engine.organization.service.ConfigService;
import com.engine.organization.service.impl.ConfigServiceImpl;
import com.engine.organization.util.response.ReturnResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;

/**
 * @Author liang.cheng
 * @Date 2023/8/18 2:29 PM
 * @Description: 配置入口
 * @Version 1.0
 */
public class ConfigController {


    public ConfigService getConfigService(User user) {
        return ServiceUtil.getService(ConfigServiceImpl.class,user);
    }




    @GET
    @Path("/ssoLogin")
    public void ssoLogin(@Context HttpServletRequest request, @Context HttpServletResponse response,
                               @QueryParam("type") String type,@QueryParam("domainIndex") String domainIndex,@QueryParam("whole") String whole) throws IOException {
        User user = HrmUserVarify.getUser(request, response);
        QTXConfigParam qtxConfigParam = QTXConfigParam.builder().loginId(user.getLoginid())
                .type(type == null ? "1" : type)
                .domainIndex(domainIndex == null ? "0" : domainIndex)
                .whole(whole == null ? "0" : whole)
                .userAgent(request.getHeader("user-agent")).build();
        String url = getConfigService(user).ssoLogin(qtxConfigParam);
        response.sendRedirect(url);
    }


    @GET
    @Path("/selectConfigInfo")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult selectConfigInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(user,getConfigService(user).selectConfigInfo());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }


    @POST
    @Path("/saveConfigInfo")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveConfigInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            String editTable = request.getParameter("editTable");
            String isopenconfig = request.getParameter("isopenconfig");
            ObjectMapper objectMapper = new ObjectMapper();
            QTXConfigPO qtxConfigPO = objectMapper.readValue(editTable, QTXConfigPO.class);
            return ReturnResult.successed(user,getConfigService(user).saveConfigInfo(qtxConfigPO,isopenconfig));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }
}
