package com.yc.ship.module.resource.service.site;

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.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.site.vo.ScenicSitePageReqVO;
import com.yc.ship.module.resource.controller.admin.site.vo.ScenicSiteSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.site.ScenicSiteDO;
import com.yc.ship.module.resource.dal.mysql.site.ScenicSiteMapper;
import com.yc.ship.module.resource.enums.ErrorCodeConstants;
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.Map;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 站点 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ScenicSiteServiceImpl implements ScenicSiteService {

    @Resource
    private ScenicSiteMapper scenicSiteMapper;

    @Override
    public Long createScenicSite(ScenicSiteSaveReqVO createReqVO) {
        // 插入
        ScenicSiteDO scenicSite = BeanUtils.toBean(createReqVO, ScenicSiteDO.class);
        Long id = IdWorker.getId(scenicSite);
        scenicSite.setId(id);
        scenicSite.setDeleted(false);
        scenicSiteMapper.insert(scenicSite);
        // 返回
        return scenicSite.getId();
    }

    @Override
    public void updateScenicSite(ScenicSiteSaveReqVO updateReqVO) {
        // 校验存在
        validateScenicSiteExists(updateReqVO.getId());
        // 更新
        ScenicSiteDO updateObj = BeanUtils.toBean(updateReqVO, ScenicSiteDO.class);
        scenicSiteMapper.updateById(updateObj);
    }

    @Override
    public void deleteScenicSite(Long id) {
        // 校验存在
        validateScenicSiteExists(id);
        // 删除
        scenicSiteMapper.deleteById(id);
    }

    private void validateScenicSiteExists(Long id) {
        if (scenicSiteMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.SCENIC_SITE_NOT_EXISTS);
        }
    }

    @Override
    public ScenicSiteDO getScenicSite(Long id) {
        return scenicSiteMapper.selectById(id);
    }

    @Override
    public PageResult<ScenicSiteDO> getScenicSitePage(ScenicSitePageReqVO pageReqVO) {
        return scenicSiteMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ScenicSiteDO> getSiteListByStatus(Integer siteType) {
        if (siteType == null) {
            return scenicSiteMapper.selectList(ScenicSiteDO::getIsUse, 1);
        } else {
            return scenicSiteMapper.selectList(ScenicSiteDO::getIsUse, 1, ScenicSiteDO::getSiteType, siteType);
        }
    }

    @Override
    public List<ScenicSiteDO> getSiteList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return scenicSiteMapper.selectBatchIds(ids);
    }

    /**
     * 获得用户 Map
     *
     * @param ids 站点编号数组
     * @return 站点 Map
     */
    @Override
    public Map<Long, ScenicSiteDO> getSiteMap(Collection<Long> ids) {
        List<ScenicSiteDO> users = getSiteList(ids);
        return CollectionUtils.convertMap(users, ScenicSiteDO::getId);
    }
}
