package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.db.entity.PackageInfo;
import com.jeequan.jeepay.db.entity.PayInterfaceDefine;
import com.jeequan.jeepay.db.entity.ProductInfo;
import com.jeequan.jeepay.service.mapper.PackageInfoMapper;
import com.jeequan.jeepay.service.mapper.PayInterfaceDefineMapper;
import com.jeequan.jeepay.service.mapper.ProductInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 方案表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2023-12-12
 */
@Service
public class PackageInfoService extends ServiceImpl<PackageInfoMapper, PackageInfo> {

    @Autowired private PackageInfoMapper packageInfoMapper;
    @Autowired private ProductInfoMapper productInfoMapper;
    @Autowired private PayInterfaceDefineMapper payInterfaceDefineMapper;
    @Autowired private PackageOrderService packageOrderService;

    public PackageInfo getDetailById(String packageId) {
        PackageInfo packageInfoById = packageInfoMapper.getDetailById(packageId);
        StringBuilder productIdsName= new StringBuilder();
        StringBuilder packageIdsName=new StringBuilder();
        StringBuilder payInterfaceCodesName=new StringBuilder();
        if (StringUtils.isNotEmpty(packageInfoById.getProductIds())){
            //关联产品不为空，组装关联的产品信息
            String[] productIdSplit = packageInfoById.getProductIds().split(",");
            for (String id : productIdSplit) {
                ProductInfo productInfo = productInfoMapper.selectById(id);
                if (productInfo != null){
                    productIdsName.append(productInfo.getProductName()).append(",");
                }
            }
            packageInfoById.setProductIdsName(String.valueOf(productIdsName));
        }
        if (StringUtils.isNotEmpty(packageInfoById.getPackageIds())){
            //关联方案不为空，组装关联的方案信息
            String[] packageIdSplit = packageInfoById.getPackageIds().split(",");
            for (String id : packageIdSplit) {
                PackageInfo packageInfo = packageInfoMapper.selectById(id);
                if (packageInfo != null){
                    packageIdsName.append(packageInfo.getName()).append(",");
                }
            }
            packageInfoById.setPackageIdsName(String.valueOf(packageIdsName));
        }
        if (StringUtils.isNotEmpty(packageInfoById.getPayInterfaceCodes())){
            //关联通道配置不为空，组装关联的通道配置信息
            String[] codeSplit = packageInfoById.getPayInterfaceCodes().split(",");
            for (String code : codeSplit) {
                PayInterfaceDefine payInterfaceDefine = payInterfaceDefineMapper.selectById(code);
                if (payInterfaceDefine != null){
                    payInterfaceCodesName.append(payInterfaceDefine.getIfName()).append(",");
                }
            }
            packageInfoById.setPayInterfaceCodesName(String.valueOf(payInterfaceCodesName));
        }



        return packageInfoById;
    }

    public IPage<PackageInfo> getPackageCheckList(Page page, String packageName, Integer state, String mchName) {
        return packageInfoMapper.getPackageCheckList(page,packageName,state,mchName);
    }

    public List<PackageInfo> getPackageList(String packageName, Integer state, Integer packageType) {
        return packageInfoMapper.getPackageList(packageName,state,packageType);
    }

    public List<ProductInfo> getProductList(List<String> collection) {
        return packageInfoMapper.getProductList(collection);
    }

    public PackageInfo getPackageById(String packageId) {
        PackageInfo resultInfo = packageInfoMapper.getPackageById(packageId);
        List<ProductInfo> productInfoList=new ArrayList<>();
        List<PackageInfo> packageInfoList=new ArrayList<>();
        List<PayInterfaceDefine> payInterfaceDefineList=new ArrayList<>();
        if (StringUtils.isNotEmpty(resultInfo.getProductIds())){
            //关联产品不为空，组装关联的产品信息
            String[] productIdSplit = resultInfo.getProductIds().split(",");
            for (String id : productIdSplit) {
                ProductInfo productInfo = productInfoMapper.selectById(id);
                if (productInfo !=null){
                    ProductInfo productInfoModel =new ProductInfo();
                    productInfoModel.setProductId(productInfo.getProductId());
                    productInfoModel.setProductName(productInfo.getProductName());
                    productInfoModel.setProductUrl(productInfo.getProductUrl());
                    productInfoList.add(productInfoModel);
                }
            }
        }
        if (StringUtils.isNotEmpty(resultInfo.getPackageIds())){
            //关联方案不为空，组装关联的方案信息
            String[] packageIdSplit = resultInfo.getPackageIds().split(",");
            for (String id : packageIdSplit) {
                PackageInfo packageInfo = packageInfoMapper.selectById(id);
                if (packageInfo != null){
                    PackageInfo packageInfoModel =new PackageInfo();
                    packageInfoModel.setPackageId(packageInfo.getPackageId());
                    packageInfoModel.setName(packageInfo.getName());
                    packageInfoModel.setPackageUrl(packageInfo.getPackageUrl());
                    packageInfoList.add(packageInfoModel);
                }
            }
        }
        if (StringUtils.isNotEmpty(resultInfo.getPayInterfaceCodes())){
            //关联通道配置不为空，组装关联的通道配置信息
            String[] codeSplit = resultInfo.getPayInterfaceCodes().split(",");
            for (String code : codeSplit) {
                PayInterfaceDefine payInterfaceDefine = payInterfaceDefineMapper.selectById(code);
                if (payInterfaceDefine != null){
                    PayInterfaceDefine payInterfaceDefineModel =new PayInterfaceDefine();
                    payInterfaceDefineModel.setIfCode(payInterfaceDefine.getIfCode());
                    payInterfaceDefineModel.setIfName(payInterfaceDefine.getIfName());
                    payInterfaceDefineList.add(payInterfaceDefineModel);
                }
            }
        }
        resultInfo.setProductInfoRelevancyList(productInfoList);
        resultInfo.setPackageInfoRelevancyList(packageInfoList);
        resultInfo.setPayInterRelevancyList(payInterfaceDefineList);

        return resultInfo;
    }

    public IPage<PackageInfo> getList(IPage<PackageInfo> page, LambdaQueryWrapper<PackageInfo> queryWrapper,PackageInfo packageInfo){

        // 时间搜索
        Date[] searchDateRange = packageInfo.buildQueryDateRange();
        if (searchDateRange[0] != null) {
            packageInfo.setFirstDate(searchDateRange[0]);
        }
        if (searchDateRange[0] != null) {
            packageInfo.setLastDate(searchDateRange[1]);
        }
        IPage<PackageInfo> mgrPackageInfoList = packageInfoMapper.getMgrPackageInfoList(page, packageInfo);

        return mgrPackageInfoList;
    }

    public boolean dataCheckout(String productIds,String packageIds,String payInterfaceDefineIds){

        if (StringUtils.isNotEmpty(productIds) && productIds.startsWith(",")) {
            return false;
        }
        if (StringUtils.isNotEmpty(packageIds) && packageIds.startsWith(",")) {
            return false;
        }
        if (StringUtils.isNotEmpty(payInterfaceDefineIds) && payInterfaceDefineIds.startsWith(",")) {
            return false;
        }
        return true;
    }
}
