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

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.content.vo.ContentPageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.content.vo.ContentSaveReqVO;
import com.yc.ship.module.miniapplet.controller.app.content.vo.AppContentPageReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.catalogue.CatalogueDO;
import com.yc.ship.module.miniapplet.dal.dataobject.content.ContentDO;
import com.yc.ship.module.miniapplet.dal.mysql.catalogue.CatalogueMapper;
import com.yc.ship.module.miniapplet.dal.mysql.content.ContentMapper;
import com.yc.ship.module.miniapplet.service.catalogue.CatalogueService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

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


/**
 * 目录内容 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ContentServiceImpl implements ContentService {

    @Resource
    private ContentMapper contentMapper;

    @Resource
    private CatalogueMapper catalogueMapper;

    @Override
    public Long createContent(ContentSaveReqVO createReqVO) {
        // 插入
        ContentDO content = BeanUtils.toBean(createReqVO, ContentDO.class);
        content.setId(IdWorker.getId(content));
        if (content.getReleaseStatus() == 1) {
            content.setReleaseTime(LocalDateTime.now());
        }
        contentMapper.insert(content);
        // 返回
        return content.getId();
    }

    @Override
    public void updateContent(ContentSaveReqVO updateReqVO) {
        // 校验存在
        validateContentExists(updateReqVO.getId());
        // 更新
        ContentDO updateObj = BeanUtils.toBean(updateReqVO, ContentDO.class);
        if (updateObj.getReleaseStatus() == 1) {
            updateObj.setReleaseTime(LocalDateTime.now());
        }
        contentMapper.updateById(updateObj);
    }

    @Override
    public void deleteContent(Long id) {
        // 校验存在
        validateContentExists(id);
        // 删除
        contentMapper.deleteById(id);
    }

    private void validateContentExists(Long id) {
        if (contentMapper.selectById(id) == null) {
            throw exception(CONTENT_NOT_EXISTS);
        }
    }

    @Override
    public ContentDO getContent(Long id) {
        return contentMapper.selectById(id);
    }

    @Override
    public PageResult<ContentDO> getContentPage(ContentPageReqVO pageReqVO) {
        return contentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ContentDO> getContentList(Long catalogueId, Integer releaseStatus) {
        return contentMapper.getContentList(catalogueId, releaseStatus);
    }

    @Override
    public PageResult<ContentDO> getAppContentPage(AppContentPageReqVO reqVO) {
        CatalogueDO catalogue = catalogueMapper.selectByCode(reqVO.getCode());
        if(catalogue == null) {
            throw exception(CATALOGUE_NOT_EXISTS);
        }
        return contentMapper.selectAppContentPage(reqVO, catalogue.getId());
    }

    @Override
    public PageResult<ContentDO> getAppContentPageByCatalogueId(AppContentPageReqVO reqVO) {
        return contentMapper.selectAppContentPage(reqVO, reqVO.getCatalogueId());
    }

}
