package com.softwarebr.nunu.service.oper.impl;

import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import com.brframework.commonweb.exception.HandleException;
import com.softwarebr.nunu.dao.oper.AdvertisementDao;
import com.softwarebr.nunu.dto.oper.AdvertisementParamDTO;
import com.softwarebr.nunu.entity.oper.Advertisement;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.service.oper.AdvertisementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author lilin
 * @date 2020-02-18 10:58:32
 */

@Slf4j
@Service
public class AdvertisementServiceImpl extends AbstractEntityService<Advertisement, Long, Object> implements AdvertisementService {

    @Autowired
    private AdvertisementDao advertisementDao;

    @Override
    public CommonRepository<Long, Advertisement> getRepository() {
        return advertisementDao;
    }

    @Override
    public List<Advertisement> getAdvertisementList(Integer position) {
        return advertisementDao.findByStatusDateOrderBySortValue(SystemConst.STATUS_UPPER_SHELF, LocalDateTime.now(), position);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAdvertisement(AdvertisementParamDTO paramDTO) {
        Advertisement advertisement = new Advertisement();
        advertisement.setAdvertUri(paramDTO.getAdvertUri());
        advertisement.setName(paramDTO.getName());
        advertisement.setEndDate(paramDTO.getEndDate());
        advertisement.setStartDate(paramDTO.getStartDate());
        advertisement.setLinkType(paramDTO.getLinkType());
        advertisement.setLinkUri(paramDTO.getLinkUri());
        advertisement.setSortValue(paramDTO.getSortValue());
        advertisement.setType(paramDTO.getType());
        advertisement.setPosition(paramDTO.getPosition());
        advertisement.setCreateDate(LocalDateTime.now());
        advertisement.setStatus(SystemConst.STATUS_LOWER_SHELF);
        advertisementDao.save(advertisement);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAdvertisement(Long advertisementId, AdvertisementParamDTO paramDTO) {
        Advertisement advertisement = accessObject(advertisementId);
        advertisement.setAdvertUri(paramDTO.getAdvertUri());
        advertisement.setName(paramDTO.getName());
        advertisement.setEndDate(paramDTO.getEndDate());
        advertisement.setStartDate(paramDTO.getStartDate());
        advertisement.setLinkType(paramDTO.getLinkType());
        advertisement.setLinkUri(paramDTO.getLinkUri());
        advertisement.setSortValue(paramDTO.getSortValue());
        advertisement.setType(paramDTO.getType());
        advertisement.setPosition(paramDTO.getPosition());
        advertisementDao.save(advertisement);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void advertisementLowerShelf(Long advertisementId) {
        Advertisement advertisement = accessObject(advertisementId);
        if (advertisement.equals(SystemConst.STATUS_LOWER_SHELF)) {
            throw new HandleException("广告已下架，请勿重复操作!");
        }
        advertisement.setStatus(SystemConst.STATUS_LOWER_SHELF);
        advertisementDao.save(advertisement);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void advertisementUpperShelf(Long advertisementId) {
        Advertisement advertisement = accessObject(advertisementId);
        if (advertisement.equals(SystemConst.STATUS_UPPER_SHELF)) {
            throw new HandleException("广告已上架，请勿重复操作!");
        }
        advertisement.setStatus(SystemConst.STATUS_UPPER_SHELF);
        advertisementDao.save(advertisement);
    }


}