package com.fzm.mall.server.admin.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.SystemClock;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.marketing.model.Coupon;
import com.fzm.mall.server.admin.message.entity.dto.NoticeDTO;
import com.fzm.mall.server.admin.message.entity.dto.NoticeSaveDTO;
import com.fzm.mall.server.admin.message.entity.dto.NoticeUpdateDTO;
import com.fzm.mall.server.admin.message.mapper.NoticeMapper;
import com.fzm.mall.server.admin.message.model.Notice;
import com.fzm.mall.server.admin.message.service.INoticeService;
import com.fzm.mall.server.admin.util.CopyUtil;
import com.fzm.mall.server.admin.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 消息中心-公告 服务实现类
 * </p>
 *
 * @author wulixian
 * @since 2021-03-09
 */
@Service
@Transactional
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {
    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public void save(NoticeSaveDTO noticeSaveDTO) {
        Notice notice = CopyUtil.to(noticeSaveDTO, Notice.class);
        notice.setNoticeId(UUIDUtil.getUUID());
        checkStartTimeAndEndTime(notice);
        noticeMapper.insert(notice);
    }

    @Override
    public void update(NoticeUpdateDTO noticeUpdateDTO) {
        Notice notice = CopyUtil.to(noticeUpdateDTO, Notice.class);
        checkStartTimeAndEndTime(notice);
        UpdateWrapper<Notice> wrapper = new UpdateWrapper<>();
        wrapper.eq("notice_id", notice.getNoticeId())
                .eq("update_time", notice.getUpdateTime());
        int update = noticeMapper.update(notice, wrapper);
        if(update == 0){
            throw new MyException(MallResponseError.UPDATE_FAIL);
        }
    }

    @Override
    public void delete(String noticeId) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_id", noticeId);
        noticeMapper.delete(queryWrapper);
    }

    @Override
    public NoticeDTO read(String noticeId) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_id", noticeId);
        Notice notice = noticeMapper.selectOne(queryWrapper);

        NoticeDTO result = CopyUtil.to(notice, NoticeDTO.class);
        Long currentTime = SystemClock.now();
        Boolean validStatus = currentTime <= result.getEndTime();
        result.setValidStatus(validStatus);
        return result;
    }

    @Override
    public IPage<NoticeDTO> page(Page<NoticeDTO> page, String content, String merchantId) {
        IPage<NoticeDTO> result = noticeMapper.page(page, content, merchantId, MALLGlobalConfig.PLATFORM_MERCHANT_ID);
        Long currentTime = SystemClock.now();
        result.getRecords().forEach(noticeDTO -> {
            Boolean validStatus = currentTime <= noticeDTO.getEndTime();
            noticeDTO.setValidStatus(validStatus);
        });
        return result;
    }

    /**
     * 活动开始时间必须小于结束时间
     * @param notice
     */
    private void checkStartTimeAndEndTime(Notice notice) {
        if (notice.getEndTime() <= notice.getStartTime()) {
            throw new MyException(MallResponseError.START_TIME_ERROR);
        }
    }
}
