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

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.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.otc.controller.admin.otclevel.vo.OtcLevelPageReqVO;
import com.yc.ship.module.otc.controller.admin.otclevel.vo.OtcLevelSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.otclevel.OtcLevelDO;
import com.yc.ship.module.otc.dal.mysql.otclevel.OtcLevelMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.Collections;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.otc.enums.ErrorCodeConstants.LEVEL_NOT_EXISTS;

/**
 * 旅行社等级管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class OtcLevelServiceImpl implements OtcLevelService {

    @Resource
    private OtcLevelMapper levelMapper;

    @Override
    public Long createLevel(OtcLevelSaveReqVO createReqVO) {
        // 插入
        OtcLevelDO level = BeanUtils.toBean(createReqVO, OtcLevelDO.class);
        level.setId(IdWorker.getId(level));
        level.setHotelRoomType(createReqVO.getHotelRoomType().toString()) ;
        levelMapper.insert(level);
        // 返回
        return level.getId();
    }

    @Override
    public void updateLevel(OtcLevelSaveReqVO updateReqVO) {
        // 校验存在
        validateLevelExists(updateReqVO.getId());
        // 更新
        OtcLevelDO updateObj = BeanUtils.toBean(updateReqVO, OtcLevelDO.class);
        levelMapper.updateById(updateObj);
    }

    @Override
    public void deleteLevel(Long id) {
        // 校验存在
        validateLevelExists(id);
        // 删除
        levelMapper.deleteById(id);
    }

    private void validateLevelExists(Long id) {
        if (levelMapper.selectById(id) == null) {
            throw exception(LEVEL_NOT_EXISTS);
        }
    }

    @Override
    public OtcLevelDO getLevel(Long id) {
        return levelMapper.selectById(id);
    }

    @Override
    public OtcLevelDO getLevelByTravlId(Long id) {
        List<OtcLevelDO> levelList = levelMapper.selectList(new LambdaQueryWrapperX<OtcLevelDO>().eq(OtcLevelDO::getId, id));
        if(levelList!=null && !levelList.isEmpty()){
            return levelList.get(0);
        }
        return null;
    }

    @Override
    public PageResult<OtcLevelDO> getLevelPage(OtcLevelPageReqVO pageReqVO) {
        return levelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OtcLevelDO> getLevelList() {
        return levelMapper.selectList();
    }
}