package com.jwipc.sys.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.inquiry.client.PriceInfoClient;
import com.jwipc.inquiry.dto.HistoryPriceInfoDTO;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.convert.ProductConvert;
import com.jwipc.sys.dto.ProductInfoDTO;
import com.jwipc.sys.dto.ProductStockDTO;
import com.jwipc.sys.entity.ProductInfo;
import com.jwipc.sys.entity.ProductStock;
import com.jwipc.sys.logic.ProductLogic;
import com.jwipc.sys.model.po.ProductInfoPO;
import com.jwipc.sys.model.query.ProductQuery;
import com.jwipc.sys.service.IProductInfoService;
import com.jwipc.sys.service.IProductInfoStockService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.baomidou.dynamic.datasource.support.DdConstants.SLAVE;

/**
 * @author shenyang.ou
 * @date 2024/2/3 22:54
 * @desc
 */
@Service
@RequiredArgsConstructor
public class ProductLogicImpl implements ProductLogic {
    private final IProductInfoService productInfoService;
    private final IProductInfoStockService productInfoStockService;
    private final ProductConvert productConvert;
    private final PriceInfoClient priceInfoClient;
    @Override
    public PageView findByPage(ProductQuery query) {
        final Page<ProductInfo> dbPage = PageUtil.toPage(query);
        List<ProductInfoPO> productInfoPOS;
        if(LoginUtil.isSupplier()){
             productInfoPOS = productInfoService.findByQuery1(dbPage,query);
        }else {
             productInfoPOS = productInfoService.findPageByQuery(dbPage, query);
        }
        List<ProductInfoDTO> productInfoDTOS = productConvert.po2DTOList(productInfoPOS);
        if (null!=query.getPriceFlag()&&query.getPriceFlag()==1){
            productInfoDTOS.forEach(productInfoDTO -> {
                //查询历史最低价
                HistoryPriceInfoDTO historyPriceInfoDTO = priceInfoClient.getHistoryLowPrice(productInfoDTO.getMaterialCode(),productInfoDTO.getFactoryCode());
                if (ObjectUtil.isNotNull(historyPriceInfoDTO)){
                    productInfoDTO.setHistoryPriceId(historyPriceInfoDTO.getId());
                    productInfoDTO.setHistoryPrice(historyPriceInfoDTO.getTaxPrice());
                }
                //查询上次中标价
                HistoryPriceInfoDTO historyPriceInfoDTO1 = priceInfoClient.getLastBidPrice(productInfoDTO.getMaterialCode(),productInfoDTO.getFactoryCode());
                if (ObjectUtil.isNotNull(historyPriceInfoDTO1)){
                    productInfoDTO.setLastBidPriceId(historyPriceInfoDTO.getId());
                    productInfoDTO.setLastBidPrice(historyPriceInfoDTO.getTaxPrice());
                }
            });
        }
        return PageUtil.getPV(productInfoDTOS, dbPage.getTotal());
    }

    /**
     * 根据物料编码和工厂编码获取物料
     *
     * @param materialCode 物料编码
     * @param factoryCode  工厂编码
     * @return 物料
     */
    @Override
    @DS(SLAVE)
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public ProductInfoDTO getByMaterialCodeAndFactoryCode(String materialCode, String factoryCode) {
        ProductInfo productInfo = productInfoService.getOne(Wrappers.lambdaQuery(ProductInfo.class).eq(ProductInfo::getMaterialCode,materialCode).eq(ProductInfo::getFactoryCode,factoryCode).last(" limit 1"));
        return productConvert.entity2DTO(productInfo);
    }

    /**
     * 根据物料编码和MES储位获取数据
     *
     * @param materialCode 物料编码
     * @return 物料
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public List<ProductStockDTO> getByMaterialCode(String materialCode) {
        List<ProductStock> productStocks = productInfoStockService.list(Wrappers.lambdaQuery(ProductStock.class).eq(ProductStock::getMaterialCode,materialCode));
        return productConvert.mesEntity2DTO(productStocks);
    }

}
