package com.engine.htxc.htqc.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htxc.common.EcologyResponseDTO;
import com.engine.htxc.htqc.service.HtqcYwService;
import com.engine.htxc.htqc.service.impl.HtqcYwServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
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.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810039
 * @version: 1.0
 * @CreateDate: 2022-09-14 16:35
 * @Description: 合同模块相关接口
 * @ModifyLog:
 **/
@Slf4j
public class HtqcYwAction {
    public HtqcYwService getHtqcYwService (){
        return ServiceUtil.getService(HtqcYwServiceImpl.class);
    }
    /**
     * 通过合同相关产品的id，获取产品对应的对手方编码
     */
    @POST
    @Path("/getDsf")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDsf(@Context HttpServletRequest request, @Context HttpServletResponse response){
        try{
            String productIds = request.getParameter("productIds");
            log.info("getDsf--begin--传入数据productIds:[{}]",productIds);
            if (StringUtils.isBlank(productIds)){
                log.error("productIds is blank");
                return EcologyResponseDTO.failJson("productIds为空，请检查");
            }
            Map<String, String> dsfMap = getHtqcYwService().getDsf(productIds);
            if (dsfMap.isEmpty()){
                return EcologyResponseDTO.failJson("返回数据dsfMap为空，请检查");
            }
            log.info("getDsf--end--返回数据dsfMap:[{}]", JSON.toJSONString(dsfMap));
            return EcologyResponseDTO.successJson(dsfMap);
        }catch (Exception e){
            log.error("getDsf 接口异常，请检查：",e);
            return EcologyResponseDTO.failJson("getDsf 接口异常，请检查");
        }
    }

    @GET
    @Path("/getContractInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getContractInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        try{
            String htbhs = request.getParameter("htbhs");
            log.info("getContractInfo--begin--htbhs:[{}]",htbhs);
            if (StringUtils.isBlank(htbhs)){
                log.error("htbhs is blank");
                return EcologyResponseDTO.failJson("htbhs为空，请检查");
            }
            Map<String, Object> resMap = getHtqcYwService().getContractInfo(htbhs);
            log.info("getContractInfo--end--返回数据resMap:[{}]", JSON.toJSONString(resMap));
            return EcologyResponseDTO.successJson(resMap);
        }catch (Exception e){
            log.error("getContractInfo 接口异常，请检查：",e);
            return EcologyResponseDTO.failJson("getContractInfo 接口异常，请检查");
        }
    }

    /**
     * 获取当前用户的身份
     */
    @GET
    @Path("/getCurrentUserRole")
    @Produces({MediaType.TEXT_PLAIN})
    public String getCurrentUserRole(@Context HttpServletRequest request, @Context HttpServletResponse response){
        try{
            String currentUserId = request.getParameter("currentUserId");
            log.info("getCurrentUserRole--begin--currentUserId:[{}]",currentUserId);
            if (StringUtils.isBlank(currentUserId)){
                log.error("currentUserId is blank");
                return EcologyResponseDTO.failJson("currentUserId为空，请检查");
            }
            int role = getHtqcYwService().getCurrentUserRole(currentUserId);
            if (role < 0){
                log.error("role < 0,请检查");
                return EcologyResponseDTO.failJson("role < 0,请检查");
            }
            log.info("getCurrentUserRole--end--返回数据role:[{}]", role);
            return EcologyResponseDTO.successJson(role);
        }catch (Exception e){
            log.error("getCurrentUserRole 接口异常，请检查：",e);
            return EcologyResponseDTO.failJson("getCurrentUserRole 接口异常，请检查");
        }
    }

}
