package com.yc.ship.module.otc.service.cooperate;

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.otc.controller.admin.cooperate.vo.CooperatePageReqVO;
import com.yc.ship.module.otc.controller.admin.cooperate.vo.CooperateSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.cooperate.CooperateDO;
import com.yc.ship.module.otc.dal.mysql.cooperate.CooperateMapper;
import com.yc.ship.module.otc.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

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

/**
 * 合作旅行社 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CooperateServiceImpl implements CooperateService {

    @Resource
    private CooperateMapper cooperateMapper;

    @Override
    public Long createCooperate(CooperateSaveReqVO createReqVO) {
        // 插入
        CooperateDO cooperate = BeanUtils.toBean(createReqVO, CooperateDO.class);
        cooperate.setId(IdWorker.getId(cooperate));
        cooperateMapper.insert(cooperate);
        // 返回
        return cooperate.getId();
    }

    @Override
    public void updateCooperate(CooperateSaveReqVO updateReqVO) {
        // 校验存在
        validateCooperateExists(updateReqVO.getId());
        // 更新
        CooperateDO updateObj = BeanUtils.toBean(updateReqVO, CooperateDO.class);
        cooperateMapper.updateById(updateObj);
    }

    @Override
    public void deleteCooperate(Long id) {
        // 校验存在
        validateCooperateExists(id);
        // 删除
        cooperateMapper.deleteById(id);
    }

    private void validateCooperateExists(Long id) {
        if (cooperateMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.COOPERATE_NOT_EXISTS);
        }
    }

    @Override
    public CooperateDO getCooperate(Long id) {
        return cooperateMapper.selectById(id);
    }

    @Override
    public PageResult<CooperateDO> getCooperatePage(CooperatePageReqVO pageReqVO) {
        return cooperateMapper.selectPage(pageReqVO);
    }

}
