package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import shop.alien.entity.store.AiIntelligentAssistant;
import shop.alien.entity.store.StoreCustomerService;
import shop.alien.mapper.AiIntelligentAssistantMapper;
import shop.alien.mapper.StoreCustomerServiceMapper;
import shop.alien.store.service.StoreCustomerServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 门店客服 服务实现类
 * </p>
 *
 * @author ssk
 * @since 2024-12-30
 */
@Service
@RequiredArgsConstructor
public class StoreCustomerServiceServiceImpl extends ServiceImpl<StoreCustomerServiceMapper, StoreCustomerService> implements StoreCustomerServiceService {

    private final StoreCustomerServiceMapper storeCustomerServiceMapper;

    private final AiIntelligentAssistantMapper aiIntelligentAssistantMapper;

    @Override
    public StoreCustomerService getByQuestion(String question) {
        LambdaQueryWrapper<StoreCustomerService> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<StoreCustomerService> list = storeCustomerServiceMapper.selectList(lambdaQueryWrapper);
        return list.stream().filter(item -> item.getQuestion().contains(question)).findFirst().orElse(null);
    }

    @Override
    public StoreCustomerService saveStoreCustomerService(StoreCustomerService storeCustomerService) {
        storeCustomerService.setUpdatedTime(new Date());
        if (ObjectUtils.isNotEmpty(storeCustomerService.getId())) {
            storeCustomerServiceMapper.updateById(storeCustomerService);
        } else {
            storeCustomerService.setCreatedTime(new Date());
            storeCustomerServiceMapper.insert(storeCustomerService);
        }
        return storeCustomerService;
    }

    @Override
    public void delStoreCustomerService(String id) {
        storeCustomerServiceMapper.deleteById(id);
    }

    @Override
    public IPage<StoreCustomerService> getStoreCustomerServicePage(int page, int size, String question, String type) {
        LambdaQueryWrapper<StoreCustomerService> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(question), StoreCustomerService::getQuestion, question)
                .eq(StringUtils.isNotEmpty(type), StoreCustomerService::getType, type)
                .eq(StoreCustomerService::getDeleteFlag, 0)
                .orderByDesc(StoreCustomerService::getCreatedTime);
        IPage<StoreCustomerService> storeCustomerServiceIPage = new Page<>(page, size);
        IPage<StoreCustomerService> selectPage = storeCustomerServiceMapper.selectPage(storeCustomerServiceIPage, lambdaQueryWrapper);
        return selectPage;
    }

    @Override
    public StoreCustomerService getById(String id) {
        return storeCustomerServiceMapper.selectById(id);
    }

    @Override
    public List<StoreCustomerService> getRandList(String type, Integer limit) {
        LambdaQueryWrapper<StoreCustomerService> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreCustomerService::getType, type)
                .eq(StoreCustomerService::getDeleteFlag, 0)
                .last("order by rand() LIMIT " + limit);
        return storeCustomerServiceMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<AiIntelligentAssistant> saveAiIntelligentAssistant(List<AiIntelligentAssistant> aiIntelligentAssistants) {
        for (AiIntelligentAssistant aiIntelligentAssistant : aiIntelligentAssistants) {
            aiIntelligentAssistant.setDeleteFlag(0);
            if (aiIntelligentAssistant.getType() != 0) {
                aiIntelligentAssistant.setCreatedTime(new Date());
                aiIntelligentAssistant.setUpdatedTime(new Date());
            }
        }
        aiIntelligentAssistantMapper.insertList(aiIntelligentAssistants);
        return aiIntelligentAssistants;
    }

    @Override
    public List<AiIntelligentAssistant> selectAiIntelligentAssistant(String userId,String time,Integer talkSource) {
        LambdaQueryWrapper<AiIntelligentAssistant> last = new LambdaQueryWrapper<AiIntelligentAssistant>()
                .lt(AiIntelligentAssistant::getCreatedTime, time)
                .eq(AiIntelligentAssistant::getDeleteFlag, 0)
                .eq(AiIntelligentAssistant::getType, 0).eq(AiIntelligentAssistant::getTalkSource, talkSource).eq(AiIntelligentAssistant::getUserId, userId).orderByDesc(AiIntelligentAssistant::getCreatedTime).last("limit 1");
        AiIntelligentAssistant aiIntelligentAssistant = aiIntelligentAssistantMapper.selectOne(last);
        if (ObjectUtils.isNotEmpty(aiIntelligentAssistant)) {
            LambdaQueryWrapper<AiIntelligentAssistant> queryWrapper = new LambdaQueryWrapper<AiIntelligentAssistant>()
                    .ge(AiIntelligentAssistant::getCreatedTime, aiIntelligentAssistant.getCreatedTime())
                    .lt(AiIntelligentAssistant::getCreatedTime, time)
                    .eq(AiIntelligentAssistant::getDeleteFlag, 0)
                    .eq(AiIntelligentAssistant::getUserId, userId)
                    .eq(AiIntelligentAssistant::getTalkSource, talkSource)
                    .orderByAsc(AiIntelligentAssistant::getCreatedTime);
            return aiIntelligentAssistantMapper.selectList(queryWrapper);
        }
        return new ArrayList<>();
    }
}
