package com.ruoyi.ai.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ai.domain.AiImage;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ai.mapper.AiCustomerMapper;
import com.ruoyi.ai.domain.AiCustomer;
import com.ruoyi.ai.service.IAiCustomerService;

/**
 * AI-客户配置Service业务层处理
 * 
 * @author shi
 * @date 2025-11-26
 */
@Service
public class AiCustomerServiceImpl implements IAiCustomerService {

    @Autowired
    private AiCustomerMapper aiCustomerMapper;

    /**
     * 查询AI-客户配置
     * 
     * @param id AI-客户配置主键
     * @return AI-客户配置
     */
    @Override
    public AiCustomer selectAiCustomerById(Long id) {
        AiCustomer customer = aiCustomerMapper.selectById(id);
        if (StringUtils.isNotEmpty(customer.getBannerUrl())) {
            customer.setBannerUrls(AiImage.getList(customer.getBannerUrl()));
            customer.setBannerUrl(null);
        }
        if (StringUtils.isNotEmpty(customer.getBanner1Url())) {
            customer.setBanner1Urls(AiImage.getList(customer.getBanner1Url()));
            customer.setBanner1Url(null);
        }
        return customer;
    }

    /**
     * 查询AI-客户配置列表
     * 
     * @param aiCustomer AI-客户配置
     * @return AI-客户配置
     */
    @Override
    public List<AiCustomer> selectAiCustomerList(AiCustomer aiCustomer) {
        LambdaQueryWrapper<AiCustomer> query = Wrappers.lambdaQuery(aiCustomer);
        query.orderByDesc(AiCustomer::getId);
        query.eq(AiCustomer::getDelFlag, "0");
        List<AiCustomer> aiCustomers = aiCustomerMapper.selectList(query);
        for (AiCustomer customer : aiCustomers) {
            if (StringUtils.isNotEmpty(customer.getBannerUrl())) {
                customer.setBannerUrls(AiImage.getList(customer.getBannerUrl()));
            }
            if (StringUtils.isNotEmpty(customer.getBanner1Url())) {
                customer.setBannerUrls(AiImage.getList(customer.getBanner1Url()));
            }
        }
        return aiCustomers;
    }

    /**
     * 分页查询AI-客户配置列表
     *
     * @param aiCustomer AI-客户配置
     * @return AI-客户配置
     */
    @Override
    public IPage<AiCustomer> selectAiCustomerPage(Page page, AiCustomer aiCustomer) {
        LambdaQueryWrapper<AiCustomer> query = Wrappers.lambdaQuery(aiCustomer);
        query.eq(AiCustomer::getDelFlag, "0");
        return aiCustomerMapper.selectPage(page, query);
    }

    /**
     * 新增AI-客户配置
     * 
     * @param aiCustomer AI-客户配置
     * @return 结果
     */
    @Override
    public int insertAiCustomer(AiCustomer aiCustomer) {
        aiCustomer.setDelFlag("0");
        aiCustomer.setCreateBy(SecurityUtils.getUsername());
        aiCustomer.setCreateTime(DateUtils.getNowDate());
        if (aiCustomer.getBannerUrls() != null && aiCustomer.getBannerUrls().size() > 0) {
            String bannerUrl = AiImage.getUrl(aiCustomer.getBannerUrls());
            aiCustomer.setBannerUrl(bannerUrl);
        }
        if (aiCustomer.getBanner1Urls() != null && aiCustomer.getBanner1Urls().size() > 0) {
            String banner1Url = AiImage.getUrl(aiCustomer.getBanner1Urls());
            aiCustomer.setBanner1Url(banner1Url);
        }
        return aiCustomerMapper.insert(aiCustomer);
    }

    /**
     * 修改AI-客户配置
     * 
     * @param aiCustomer AI-客户配置
     * @return 结果
     */
    @Override
    public int updateAiCustomer(AiCustomer aiCustomer) {
        aiCustomer.setUpdateBy(SecurityUtils.getUsername());
        aiCustomer.setUpdateTime(DateUtils.getNowDate());
        if (aiCustomer.getBannerUrls() != null && aiCustomer.getBannerUrls().size() > 0) {
            String bannerUrl = AiImage.getUrl(aiCustomer.getBannerUrls());
            aiCustomer.setBannerUrl(bannerUrl);
        }
        if (aiCustomer.getBanner1Urls() != null && aiCustomer.getBanner1Urls().size() > 0) {
            String banner1Url = AiImage.getUrl(aiCustomer.getBanner1Urls());
            aiCustomer.setBanner1Url(banner1Url);
        }
        return aiCustomerMapper.updateById(aiCustomer);
    }

    /**
     * 批量删除AI-客户配置
     * 
     * @param ids 需要删除的AI-客户配置主键
     * @return 结果
     */
    @Override
    public int deleteAiCustomerByIds(Long[] ids)
    {
        return aiCustomerMapper.deleteAiCustomerByIds(ids);
    }

    /**
     * 删除AI-客户配置信息
     * 
     * @param id AI-客户配置主键
     * @return 结果
     */
    @Override
    public int deleteAiCustomerById(Long id)
    {
        return aiCustomerMapper.deleteAiCustomerById(id);
    }
}
