package com.engine.htxc.contract.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htxc.common.dto.EcologyCommonResponseDTO;
import com.engine.htxc.contract.dto.ProductCounterPartyDTO;
import com.engine.htxc.contract.dto.ProductCounterpartyEnum;
import com.engine.htxc.contract.service.ProductCounterpartyService;
import com.engine.htxc.contract.service.impl.ProductCounterpartyServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810050
 * @version: 1.0
 * @CreateDate: 2022/8/2315:19
 * @Description:  添加产品对手方关系action
 * @ModifyLog:
 **/
public class ProductCounterpartyAction {
    private static final Logger logger= LoggerFactory.getLogger(ProductCounterpartyAction.class);

    private ProductCounterpartyService productCounterpartyService=ServiceUtil.getService(ProductCounterpartyServiceImpl.class);

    @POST
    @Path("/addProductCounterParty")
    @Produces({MediaType.TEXT_PLAIN})
    public String addProductCounterParty(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try{
            User user = HrmUserVarify.getUser(request, response);
            final String productCounterPartyJson = Util.null2String(request.getParameter("productCounterParty"));
            logger.info("传入合同流程数据：[{}]",productCounterPartyJson);
            final ProductCounterPartyDTO productCounterParty = JSON.parseObject(productCounterPartyJson, ProductCounterPartyDTO.class);
            ProductCounterpartyEnum result=productCounterpartyService.addProductCounterParty(user,productCounterParty);
            logger.info("添加产品对手方结果：[{}]",result);
            if(result==ProductCounterpartyEnum.PRODUCT_OTHER_COUNTERPARTY_EXIST){
               return EcologyCommonResponseDTO.failJson(ProductCounterpartyEnum.PRODUCT_OTHER_COUNTERPARTY_EXIST.name());
            }
            return  EcologyCommonResponseDTO.successJson(result.name());

        }catch (Exception e){
            logger.error("创建产品对手方发生异常",e);
        }
        return EcologyCommonResponseDTO.failJson("创建产品对手方发生错误");
    }
}
