package com.ruoyi.zhushi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.zhushi.entity.ProductRanking;
import com.ruoyi.zhushi.mapper.ProductRankingMapper;
import com.ruoyi.zhushi.service.ProductRankingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class ProductRankingServiceImpl implements ProductRankingService {
    @Autowired
    private ProductRankingMapper productRankingMapper;

    //批量插入
    @Transactional(rollbackFor = Exception.class)
    public void insertProductRankings(List<ProductRanking> productRankings) throws Exception{
        String date = getDate();
        String time = getTime();
        if (productRankings!=null&&productRankings.size()>0) {
            ProductRanking pr = productRankings.get(0);
            String website = pr.getWebsite();
            String category = pr.getCategory();
            //先删除当天的   前段是 按照 website  和 category  分批上传的
            QueryWrapper<ProductRanking> delWrapper = new QueryWrapper();
            delWrapper.eq("createdate", date);
            delWrapper.eq("website", website);
            delWrapper.eq("category", category);
            int i =  productRankingMapper.delete(delWrapper);
            //插入当天数据
            for (ProductRanking productRanking : productRankings) {
                productRanking.setCreatedate(date);
                productRanking.setCreatetime(time);
                productRankingMapper.insert(productRanking);
            }
        }

    }

    //根据条件查询商品
    @Override
    public List<ProductRanking> getProductRankingList(String createDate,
                                                      Integer website,
                                                      String category,
                                                      String keyword) throws Exception {
        QueryWrapper<ProductRanking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("website", website);
        queryWrapper.eq("createdate", createDate);
        if (!StringUtils.isEmpty(category)) {
            queryWrapper.eq("category", category);
        }
        if (!StringUtils.isEmpty(keyword)) {
            // 添加嵌套的条件组合
            queryWrapper.nested(wrapper -> {
                wrapper.like("sn",keyword).or().like("name",keyword);
            });
        }
        List<ProductRanking> list = productRankingMapper.selectList(queryWrapper);
        return list;
    }
    // 获取当前 最大日期的商品
    @Override
    public List<ProductRanking> getProductRankingListCurrent(Integer website,String category) throws Exception {

        List<ProductRanking> list = productRankingMapper.getProductRankingListCurrent(website,category);
        return list;
    }

    //查询商品的历史信息
    @Override
    public List<ProductRanking> getProductRankingByParam(String sn, String shop, Integer website,String category) throws Exception {
        QueryWrapper<ProductRanking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sn", sn);
        queryWrapper.eq("shop", shop);
        queryWrapper.eq("website", website);
        queryWrapper.eq("category", category);
        queryWrapper.orderByDesc("createdate");
        List<ProductRanking> list = productRankingMapper.selectList(queryWrapper);
        return list;
    }

    public String getDate() {
        LocalDate today = LocalDate.now();

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 格式化日期为字符串
        String formattedDate = today.format(formatter);
        return formattedDate;
    }

    public String getTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = new Date();
        return sdf.format(currentDate);
    }


}
