package com.zfire.mall.manager.logic.engin.info;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.engin.info.EnginInfoBean;
import com.zfire.mall.manager.bean.engin.info.EnginInfoItemBean;
import com.zfire.mall.manager.bean.engin.info.EnginInfoListBean;
import com.zfire.mall.manager.bean.engin.order.EnginOrderBean;
import com.zfire.mall.manager.bean.retail.ProductPriceBean;
import com.zfire.mall.manager.commonmapper.engin.info.EnginInfoReserveMapper;
import com.zfire.mall.manager.enums.OrderTypeEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.engin.order.EnginOrderLogic;
import com.zfire.mall.manager.logic.retail.RetailOrderLogic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class EnginInfoLogic {

    @Resource
    EnginInfoReserveMapper enginInfoReserveMapper;
    @Resource
    CommonLogic commonLogic;
    @Resource
    EnginOrderLogic enginOrderLogic;
    @Resource
    RetailOrderLogic retailOrderLogic;

    public IPage<EnginInfoListBean> listPage(String enginInfoNo, String productCategoryId, String customerKeyWord,
                                             String projectName, String machineType, String useUnit, String installAddress,
                                             String projectNo, String enginFactoryNo, String enginSignType,
                                             String startContractExpireDate, String endContractExpireDate, String createName,
                                             String confirmName, String examineStatus,
                                             String serviceId, String specification,Boolean isClose, Integer pageNum, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        String customerId = adminUser.getIsCustomer() ? adminUser.getCustomerId() : "";
        return enginInfoReserveMapper.listPage(new Page<>(pageNum, pageSize), enginInfoNo, productCategoryId, customerKeyWord, projectName, machineType,
                useUnit, installAddress, projectNo, enginFactoryNo, enginSignType, startContractExpireDate, endContractExpireDate, createName,
                confirmName, examineStatus, serviceId, specification, adminUser.getAdminCompanyIds(), adminUser.getServiceIds(), customerId,isClose);
    }

    public EnginInfoBean detail(String id) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        String customerId = adminUser.getIsCustomer() ? adminUser.getCustomerId() : "";

        return enginInfoReserveMapper.detail(id, adminUser.getAdminCompanyIds(), adminUser.getServiceIds(), customerId);
    }

    public EnginInfoBean queryDetailByRecordNo(String recordNo, String enginOrderId, String customerId) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        customerId = adminUser.getIsCustomer() ? adminUser.getCustomerId() : customerId;

        // 查询引用单号历史
        EnginOrderBean enginOrderBean = new EnginOrderBean();
        enginOrderBean.setEnginOrderId(enginOrderId);
        enginOrderBean.setAdminCompanyId(adminUser.getAdminCompanyId());
        enginOrderBean.setRefEnginRecordNo(recordNo);
        enginOrderBean.setEnginOrderType(OrderTypeEnum.TRADE.getKey());
        enginOrderBean.setCustomerId(adminUser.getCustomerId());
//        Integer refCount = enginOrderLogic.checkEnginOrderRecord(enginOrderBean);
        enginOrderBean.setRefCount(0);

        EnginInfoBean info = enginInfoReserveMapper.queryDetailByRecordNo(recordNo, adminUser.getAdminCompanyIds(), customerId);

        queryItemDataHandle(info, 0);

        return info;
    }

    private void queryItemDataHandle(EnginInfoBean info, Integer refCount) {
        List<String> oldNumbers = info.getItems().stream()
                .map(EnginInfoItemBean::getMaterialOldNumber)
                .collect(Collectors.toList());
        // 获取产品价格列表
        IPage<ProductPriceBean> productPricePage = retailOrderLogic.queryProductList(null, null, null,
                null, null, null, null, info.getCustomerId(), oldNumbers,
                OrderTypeEnum.GENERAL.getKey(), 1, -1);
        List<ProductPriceBean> records = productPricePage.getRecords();
        Map<String, ProductPriceBean> priceBeanMap = records.stream()
                .collect(Collectors.toMap(v -> v.getSaleCode() + v.getMaterialOldNumber(), Function.identity(), (key1, key2) -> key1));
        for (EnginInfoItemBean item : info.getItems()) {
            ProductPriceBean priceBean = priceBeanMap.get(item.getSaleTypeCode() + item.getMaterialOldNumber());
            if (Objects.isNull(priceBean)) {
                throw new RemoteServiceException(item.getMaterialOldNumber() + item.getSpecification() + "：没有找到关联价格产品编码");
            }
            item.setUseRefCount(refCount);
            item.getCustomerWalletList().addAll(priceBean.getRebateWallets());
            item.getCustomerWalletList().addAll(priceBean.getWallets());
        }
    }

}
