package com.yc.ship.module.miniapplet.service.adsense;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.miniapplet.controller.admin.adsense.vo.AdsensePageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.adsense.vo.AdsenseSaveReqVO;
import com.yc.ship.module.miniapplet.controller.app.advert.vo.AppAdvertReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.adsense.AdsenseDO;
import com.yc.ship.module.miniapplet.dal.dataobject.advert.AdvertDO;
import com.yc.ship.module.miniapplet.dal.mysql.adsense.AdsenseMapper;
import com.yc.ship.module.miniapplet.service.advert.AdvertService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.miniapplet.enums.ErrorCodeConstants.*;


/**
 * 广告位管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class AdsenseServiceImpl implements AdsenseService {

    @Resource
    private AdvertService advertService;

    @Resource
    private AdsenseMapper adsenseMapper;

    @Override
    public Long createAdsense(AdsenseSaveReqVO createReqVO) {
        // 插入
        AdsenseDO adsense = BeanUtils.toBean(createReqVO, AdsenseDO.class);
        adsense.setId(IdWorker.getId(adsense));
        AdsenseDO adsenseDO = getAdsenseByCodeN(createReqVO.getAdsenseCode());
        if (adsenseDO != null) {
            throw exception(ADSENSE_CODE_EXISTS);
        }
        adsenseMapper.insert(adsense);
        // 返回
        return adsense.getId();
    }

    @Override
    public void updateAdsense(AdsenseSaveReqVO updateReqVO) {
        // 校验存在
        validateAdsenseExists(updateReqVO.getId());
        // 更新
        AdsenseDO updateObj = BeanUtils.toBean(updateReqVO, AdsenseDO.class);
        AdsenseDO adsenseDO = getAdsenseByCodeN(updateReqVO.getAdsenseCode());
        if (adsenseDO != null) {
            if (!Objects.equals(adsenseDO.getId(), updateReqVO.getId())) {
                throw exception(ADSENSE_CODE_EXISTS);
            }
        }
        adsenseMapper.updateById(updateObj);
    }

    @Override
    public void deleteAdsense(Long id) {
        // 校验存在
        validateAdsenseExists(id);
        List<AdvertDO> advertList = advertService.getAdvertList(id);
        if (!advertList.isEmpty()) {
            throw exception(ADSENSE_NOT_DELETE);
        }
        // 删除
        adsenseMapper.deleteById(id);
    }

    private void validateAdsenseExists(Long id) {
        if (adsenseMapper.selectById(id) == null) {
            throw exception(ADSENSE_NOT_EXISTS);
        }
    }

    private AdsenseDO getAdsenseByCodeN(String code) {
        AdsenseDO adsenseDO = adsenseMapper.selectByCode(code);
        return adsenseDO;
    }
    @Override
    public AdsenseDO getAdsense(Long id) {
        return adsenseMapper.selectById(id);
    }

    @Override
    public PageResult<AdsenseDO> getAdsensePage(AdsensePageReqVO pageReqVO) {
        return adsenseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AdsenseDO> getAdsenseListByStatus(Integer status) {
        return adsenseMapper.selectList(status);
    }

    @Override
    public List<AdsenseDO> getAdsenseList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return adsenseMapper.selectBatchIds(ids);
    }

    @Override
    public List<AdsenseDO> getAdsenseList(AppAdvertReqVO reqVO) {
        return adsenseMapper.getAdsenseList(reqVO.getAdsenseId(), reqVO.getAdsenseCode());
    }

    @Override
    public AdsenseDO getAdsenseByCode(String code) {
        AdsenseDO adsenseDO = adsenseMapper.selectByCode(code);
        if(adsenseDO == null) {
            throw exception(ADSENSE_NOT_EXISTS);
        }
        return adsenseDO;
    }

}
