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

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.checkchannel.vo.CheckChannelPageReqVO;
import com.yc.ship.module.resource.controller.admin.checkchannel.vo.CheckChannelRespVO;
import com.yc.ship.module.resource.controller.admin.checkchannel.vo.CheckChannelSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.checkchannel.CheckChannelDO;
import com.yc.ship.module.resource.dal.mysql.checkchannel.CheckChannelMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.CHECK_CHANNEL_NOT_EXISTS;


/**
 * 产品检票通道 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CheckChannelServiceImpl implements CheckChannelService {

    @Resource
    private CheckChannelMapper checkChannelMapper;

    @Override
    public Long create(CheckChannelSaveReqVO createReqVO) {
        // 插入
        CheckChannelDO checkChannel = BeanUtils.toBean(createReqVO, CheckChannelDO.class);
        checkChannel.setDeleted(false);
        checkChannelMapper.insert(checkChannel);
        // 返回
        return checkChannel.getId();
    }

    @Override
    public void update(CheckChannelSaveReqVO updateReqVO) {
        // 校验存在
        validateCheckChannelExists(updateReqVO.getId());
        // 更新
        CheckChannelDO updateObj = BeanUtils.toBean(updateReqVO, CheckChannelDO.class);
        checkChannelMapper.updateById(updateObj);
    }

    @Override
    public void delete(Long id) {
        // 校验存在
        validateCheckChannelExists(id);
        // 删除
        checkChannelMapper.deleteById(id);
    }

    private void validateCheckChannelExists(Long id) {
        if (checkChannelMapper.selectById(id) == null) {
            throw exception(CHECK_CHANNEL_NOT_EXISTS);
        }
    }

    @Override
    public CheckChannelDO getCheckChannel(Long id) {
        return checkChannelMapper.selectById(id);
    }

    @Override
    public PageResult<CheckChannelDO> getCheckChannelPage(CheckChannelPageReqVO pageReqVO) {
        return checkChannelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CheckChannelRespVO> getCheckChannelByBase(Long baseId) {
        List<CheckChannelDO> list = checkChannelMapper.selectList(CheckChannelDO::getProductId, baseId);
        return BeanUtils.toBean(list, CheckChannelRespVO.class);
    }

    @Override
    public void deleteByProductId(Long productId) {
        checkChannelMapper.delete(CheckChannelDO::getProductId, productId);
    }

    @Override
    public CheckChannelDO getCheckChannelByChannelAndProduct(Long productId, Long channelId) {
        CheckChannelDO channelDO = checkChannelMapper.selectOne(CheckChannelDO::getChannelId, channelId, CheckChannelDO::getProductId, productId);
        return channelDO;
    }
}