package com.engine.htxc.contract.service.impl;

import com.engine.core.impl.Service;
import com.engine.htxc.commonutil.db.EntityCrudUtil;
import com.engine.htxc.contract.dao.ContractDao;
import com.engine.htxc.contract.dao.ProductDao;
import com.engine.htxc.contract.dto.ProductCounterPartyDTO;
import com.engine.htxc.contract.dto.ProductCounterpartyEnum;
import com.engine.htxc.contract.entity.UfDsfgl;
import com.engine.htxc.contract.entity.UfProduct;
import com.engine.htxc.contract.entity.UfProductCounterParty;
import com.engine.htxc.contract.service.ProductCounterpartyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810050
 * @version: 1.0
 * @CreateDate: 2022/8/2315:35
 * @Description:
 * @ModifyLog:
 **/
public class ProductCounterpartyServiceImpl extends Service implements ProductCounterpartyService {

    private static final Logger logger= LoggerFactory.getLogger(ProductCounterpartyServiceImpl.class);

    @Override
    public ProductCounterpartyEnum addProductCounterParty(User user, ProductCounterPartyDTO productCounterParty) {
        // 插入产品关系
        //根据产品编码查对手方和产品关系表   判断是否和对手方编码一致
        ProductCounterpartyEnum result=ProductCounterpartyEnum.CREATE_SUCCESS;
        final UfProductCounterParty ufProductCounterParty = ProductDao.selectUfProductCounterpartyByProductid(productCounterParty.getProductCode());
        if(null!=ufProductCounterParty){
            if(null!=ufProductCounterParty.getSocialcreditcode()&&ufProductCounterParty.getSocialcreditcode().equals(productCounterParty.getCertNo())){
                logger.info("产品已存在和该对手方关系");
                result= ProductCounterpartyEnum.PRODUCT_COUNTERPARTY_EXIST;
            }else{
                logger.error("产品已存在和其他对手方关系");
                result= ProductCounterpartyEnum.PRODUCT_OTHER_COUNTERPARTY_EXIST;
            }
            return result;
        }
        final String certNo = productCounterParty.getCertNo();
        final List<String> usccList = new ArrayList<>();
        usccList.add(certNo);
        final List<String> cpList = ContractDao.selectCounterPartyByCertCode(usccList);
        // 不存在对手方 创建对手方
        if(cpList.isEmpty()){
            logger.info("对手方不存在,新建对手方");
            final UfDsfgl ufDsfgl = EntityCrudUtil.createModeBaseAttributes("uf_dsfgl", UfDsfgl.class);
            ufDsfgl.setModedatacreater(user.getUID());
            ufDsfgl.setDsfmc(productCounterParty.getCounterPartyName());
            ufDsfgl.setDqbb(1);
            EntityCrudUtil.insertEntity("uf_dsfgl",ufDsfgl,UfDsfgl.class);
        }else{
            result=ProductCounterpartyEnum.COUNTERPARTY_EXIST;
        }

        UfProduct ufProduct = ProductDao.selectProductByProductId(productCounterParty.getProductCode());
        if(null==ufProduct){
            logger.info("产品信息不存在,新建产品");
            final UfProduct ufProduct1 = new UfProduct();
            ufProduct1.setProductid(productCounterParty.getProductCode());
            ufProduct1.setProductname(productCounterParty.getProductName());
            EntityCrudUtil.insertEntity("uf_product",ufProduct1,UfProduct.class);
        }else{
            if(ProductCounterpartyEnum.COUNTERPARTY_EXIST==result){
                result=ProductCounterpartyEnum.PRODUCT_COUNTERPARTY_EXIST;
            }else{
                result =ProductCounterpartyEnum.PRODUCT_EXIST;
            }
        }
        logger.info("创建产品对手方关系 productid=[{}],socialcreditcode=[{}]",productCounterParty.getProductCode(),productCounterParty.getProductName());
        final UfProductCounterParty ufProductCounterParty1 = new UfProductCounterParty();
        ufProductCounterParty1.setSocialcreditcode(productCounterParty.getCertNo());
        ufProductCounterParty1.setProductid(productCounterParty.getProductCode());
        EntityCrudUtil.insertEntity("uf_prodcontrelation",ufProductCounterParty1,UfProductCounterParty.class);
        return result;

    }
}
