package com.ruoyi.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.VO.SeckillTimeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallSeckillTimeMapper;
import com.ruoyi.mall.domain.MallSeckillTime;
import com.ruoyi.mall.service.IMallSeckillTimeService;

import javax.annotation.Resource;

/**
 * 秒杀时间Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-01
 */
@Service
public class MallSeckillTimeServiceImpl implements IMallSeckillTimeService
{
    @Resource
    private MallSeckillTimeMapper mallSeckillTimeMapper;

    /**
     * 秒杀时间列表
     * @return
     */
    @Override
    public ApiResult getSeckillTimeList(){
        try {

            List<SeckillTimeVO> seckillTimeList = mallSeckillTimeMapper.getSeckillTimeList();
            if (seckillTimeList.size() > 0){
                for (SeckillTimeVO seckillTimeVO : seckillTimeList) {
                    //开始时间
                    String timeHour = seckillTimeVO.getTimeHour();
                    //结束时间
                    String timeEnd = seckillTimeVO.getTimeEnd();

                    //转换类型
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                    //开始时间
                    Date parseBegin = simpleDateFormat.parse(timeHour);
                    //结束时间
                    Date parseEnd = simpleDateFormat.parse(timeEnd);
                    //此时
                    Date parseNow = simpleDateFormat.parse(DateUtils.dateTimeNow("HH:mm"));

                    //比较,秒杀状态(0-未开始，1-秒杀中，2-秒杀结束)
                    if (parseNow.before(parseBegin)){
                        seckillTimeVO.setSeckillStatus(0);
                    }else if(parseNow.after(parseBegin) && parseNow.before(parseEnd)){
                        seckillTimeVO.setSeckillStatus(1);
                    }else if(parseNow.after(parseEnd)){
                        seckillTimeVO.setSeckillStatus(2);
                    }
                }
            }
            return ApiResult.ok().put("data",seckillTimeList);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("日期转换异常");
        }
    }

    /**
     * 查询秒杀时间
     *
     * @return 秒杀时间
     */
    @Override
    public List<SeckillTimeVO> seckillTimeSelectList()
    {
        return mallSeckillTimeMapper.getSeckillTimeList();
    }

    /**
     * 查询秒杀时间
     *
     * @param id 秒杀时间主键
     * @return 秒杀时间
     */
    @Override
    public MallSeckillTime selectMallSeckillTimeById(Integer id)
    {
        return mallSeckillTimeMapper.selectMallSeckillTimeById(id);
    }

    /**
     * 查询秒杀时间列表
     *
     * @param mallSeckillTime 秒杀时间
     * @return 秒杀时间
     */
    @Override
    public List<MallSeckillTime> selectMallSeckillTimeList(MallSeckillTime mallSeckillTime)
    {
        return mallSeckillTimeMapper.selectMallSeckillTimeList(mallSeckillTime);
    }

    /**
     * 新增秒杀时间
     *
     * @param mallSeckillTime 秒杀时间
     * @return 结果
     */
    @Override
    public int insertMallSeckillTime(MallSeckillTime mallSeckillTime)
    {
        mallSeckillTime.setCreateAt(DateUtils.getTime());
        mallSeckillTime.setDelFlag(0);
        return mallSeckillTimeMapper.insertMallSeckillTime(mallSeckillTime);
    }

    /**
     * 修改秒杀时间
     *
     * @param mallSeckillTime 秒杀时间
     * @return 结果
     */
    @Override
    public int updateMallSeckillTime(MallSeckillTime mallSeckillTime)
    {
        return mallSeckillTimeMapper.updateMallSeckillTime(mallSeckillTime);
    }

    /**
     * 批量删除秒杀时间
     *
     * @param ids 需要删除的秒杀时间主键
     * @return 结果
     */
    @Override
    public int deleteMallSeckillTimeByIds(Integer[] ids)
    {
        return mallSeckillTimeMapper.deleteMallSeckillTimeByIds(ids);
    }

    /**
     * 删除秒杀时间信息
     *
     * @param id 秒杀时间主键
     * @return 结果
     */
    @Override
    public int deleteMallSeckillTimeById(Integer id)
    {
        return mallSeckillTimeMapper.deleteMallSeckillTimeById(id);
    }
}
