package com.xingxi.api.service.impl;

import com.xingxi.api.data.mapper.WxApiProductMapper;
import com.xingxi.api.data.domain.MercProdRequest;
import com.xingxi.api.data.domain.MercProdResponse;
import com.xingxi.api.model.ProdAttrResponse;
import com.xingxi.api.service.ProdServiceI;
import com.xingxi.common.enums.EDelFlag;
import com.xingxi.common.enums.ERoleKey;
import com.xingxi.common.utils.StringUtils;
import com.xingxi.master.merchant.domain.MerchantProd;
import com.xingxi.master.product.domain.*;
import com.xingxi.master.product.service.IProdAttrJointService;
import com.xingxi.master.product.service.IProdAttrPriceService;
import com.xingxi.master.product.service.IProdAttrService;
import com.xingxi.master.product.service.IProdPicService;
import com.xingxi.system.domain.SysConfig;
import com.xingxi.system.mapper.SysConfigMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@RequiredArgsConstructor
@Service("ProdServiceI")
class ProdApiServiceImpl implements ProdServiceI {

    @Autowired
    WxApiProductMapper productMapper;

    @Autowired
    IProdPicService prodPicService;

    @Autowired
    IProdAttrService prodAttrService;

    @Autowired
    IProdAttrPriceService prodAttrPriceService;

    @Autowired
    IProdAttrJointService prodAttrJointService;

    @Autowired
    private SysConfigMapper configMapper;

    /**
     * 根据键名查询参数配置信息
     *
     * @param configKey 参数key
     * @return 参数键值
     */
    @Override
    public String selectConfigByKey(String configKey)
    {
        SysConfig config = new SysConfig();
        config.setConfigKey(configKey);
        SysConfig retConfig = configMapper.selectConfig(config);
        if (StringUtils.isNotNull(retConfig))
        {
            return retConfig.getConfigValue();
        }
        return StringUtils.EMPTY;
    }

    @Override
    public List<MercProdResponse> selectMercProdList(MercProdRequest request) {
        return productMapper.selectProductList(request);
    }

    @Override
    public MercProdResponse selectMercProdById(Long mercProdId) {
        MercProdResponse rtn = new MercProdResponse();

        MerchantProd mercProd = productMapper.selectMerchantProdByMercProdId(mercProdId);

        rtn.setMercProdId(mercProd.getMercProdId());
        rtn.setMercId(mercProd.getMercId());
        rtn.setProdId(mercProd.getProdId());
        Prod prod = productMapper.selectProdById(mercProd.getProdId());
        rtn.setProdClassId(prod.getProdClassId());
        rtn.setProdName(prod.getProdName());
        rtn.setIpId(prod.getIpId());
        rtn.setDesignerId(prod.getDesignerId());
        rtn.setDescription(prod.getDescription());
        rtn.setThirdIdConfirmFlag(prod.getThirdIdConfirmFlag());
        rtn.setProdStatus(prod.getProdStatus());

        ProdPic condPic = new ProdPic();
        condPic.setProdId(mercProd.getProdId());
        condPic.setDelFlag(EDelFlag.NO.getVal());

        List<ProdPic> prodPicList = prodPicService.selectProdPicList(condPic);
        rtn.setProdPicList(prodPicList);

        ProdAttr condAttr = new ProdAttr();
        condAttr.setProdId(prod.getId());
        condAttr.setDelFlag(EDelFlag.NO.getVal());

        List<ProdAttr> prodAttrList = prodAttrService.selectProdAttrList(condAttr);

        List<ProdAttrResponse> prodAttrResponseList = new ArrayList<>();

        for (ProdAttr prodAttr : prodAttrList) {
            ProdAttrResponse prodAttrResponse = new ProdAttrResponse();

            prodAttrResponse.setProdAttrId(prodAttr.getProdAttrId());
            prodAttrResponse.setProdId(prodAttr.getProdId());
            prodAttrResponse.setAttrName(prodAttr.getAttrName());
            prodAttrResponse.setAttrType(prodAttr.getAttrType());
            prodAttrResponse.setDelFlag(prodAttr.getDelFlag());

            ProdAttrPrice condPrice = new ProdAttrPrice();
            condPrice.setProdAttrId(prodAttr.getProdAttrId());
            condPrice.setProdId(prodAttr.getProdId());
            condPrice.setDelFlag(EDelFlag.NO.getVal());
            condPrice.setBuyerRoleKey(ERoleKey.CUSTOMER.getCode());
            condPrice.setSellerId(mercProd.getMercId());

            List<ProdAttrPrice> priceList = prodAttrPriceService.selectProdAttrPriceList(condPrice);
            if (priceList != null && !priceList.isEmpty()) {
                prodAttrResponse.setProdAttrPrice(priceList.get(0));
            }

            ProdAttrJoint condJoint = new ProdAttrJoint();
            condJoint.setDelFlag(EDelFlag.NO.getVal());
            condJoint.setProdId(prodAttr.getProdId());
            condJoint.setProdAttrId(prodAttr.getProdAttrId());

            List<ProdAttrJoint> prodAttrJointList = prodAttrJointService.selectProdAttrJointList(condJoint);
            prodAttrResponse.setProdAttrJointList(prodAttrJointList);
        }

        rtn.setProdAttrList(prodAttrResponseList);

        return rtn;
    }
}
