package com.ruoyi.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.Vo.BusinessBulletinResponse;
import com.ruoyi.mall.domain.Vo.BusinessBulletinVo;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.BusinessBulletinMapper;
import com.ruoyi.mall.domain.BusinessBulletin;
import com.ruoyi.mall.service.IBusinessBulletinService;

import javax.annotation.Resource;

/**
 * 公告Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-25
 */
@Service
public class BusinessBulletinServiceImpl implements IBusinessBulletinService 
{
    @Resource
    private BusinessBulletinMapper businessBulletinMapper;
    /**一周内 热度*/
    @Override
    public List<BusinessBulletinResponse> getHotList(BusinessBulletin businessBulletin){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        //过去七天
        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        String day = format.format(d);
        List<BusinessBulletinResponse> result = new ArrayList<>();
        List<BusinessBulletinVo> hotList = businessBulletinMapper.getHotList(day);
        List<String> hotTime = businessBulletinMapper.getHotTime(day);
        if(null != hotList && hotList.size()>0){
            for (String s : hotTime) {
                List<BusinessBulletinVo> stuList = hotList.stream().filter(o -> o.getTag() != null && o.getTag().equals(s)  ).collect(Collectors.toList());
                BusinessBulletinResponse item = new BusinessBulletinResponse();
                item.setList(stuList);
                item.setTime(s);
                result.add(item);
            }
        }
        return result;
    }
    /**
     * 查询公告
     * 
     * @param bulletinId 公告主键
     * @return 公告
     */
    @Override
    public BusinessBulletin selectBusinessBulletinByBulletinId(Long bulletinId)
    {
        return businessBulletinMapper.selectBusinessBulletinByBulletinId(bulletinId);
    }

    /**
     * 查询公告列表
     * 
     * @param businessBulletin 公告
     * @return 公告
     */
    @Override
    public List<BusinessBulletin> selectBusinessBulletinList(BusinessBulletin businessBulletin)
    {
        return businessBulletinMapper.selectBusinessBulletinList(businessBulletin);
    }

    /**
     * 新增公告
     * 
     * @param businessBulletin 公告
     * @return 结果
     */
    @Override
    public int insertBusinessBulletin(BusinessBulletin businessBulletin)
    {
        businessBulletin.setCreateTime(DateUtils.getNowDate());
        return businessBulletinMapper.insertBusinessBulletin(businessBulletin);
    }

    /**
     * 修改公告
     * 
     * @param businessBulletin 公告
     * @return 结果
     */
    @Override
    public int updateBusinessBulletin(BusinessBulletin businessBulletin)
    {
        businessBulletin.setUpdateTime(DateUtils.getNowDate());
        return businessBulletinMapper.updateBusinessBulletin(businessBulletin);
    }

    /**
     * 批量删除公告
     * 
     * @param bulletinIds 需要删除的公告主键
     * @return 结果
     */
    @Override
    public int deleteBusinessBulletinByBulletinIds(Long[] bulletinIds)
    {
        return businessBulletinMapper.deleteBusinessBulletinByBulletinIds(bulletinIds,DateUtils.getNowDate());
    }

    /**
     * 删除公告信息
     * 
     * @param bulletinId 公告主键
     * @return 结果
     */
    @Override
    public int deleteBusinessBulletinByBulletinId(Long bulletinId)
    {
       return businessBulletinMapper.deleteBusinessBulletinByBulletinId(bulletinId,DateUtils.getNowDate());
    }
}
