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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.mybatis.core.util.MyBatisUtils;
import com.yc.ship.module.resource.controller.admin.route.vo.ResourceRoutePageReqVO;
import com.yc.ship.module.resource.controller.admin.route.vo.ResourceRouteSaveReqVO;
import com.yc.ship.module.resource.controller.app.route.vo.AppResourceRoutePageReqVO;
import com.yc.ship.module.resource.controller.app.route.vo.AppResourceRoutePriceVO;
import com.yc.ship.module.resource.controller.app.route.vo.AppResourceRouteRespVO;
import com.yc.ship.module.resource.dal.dataobject.route.ResourceRouteDO;
import com.yc.ship.module.resource.dal.mysql.route.ResourceRouteMapper;
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.resource.enums.ErrorCodeConstants.ROUTE_NOT_EXISTS;

/**
 * 航线管理 Service 实现类
 *
 * @author lfy
 */
@Service
@Validated
public class ResourceRouteServiceImpl implements ResourceRouteService {

    @Resource
    private ResourceRouteMapper routeMapper;

    @Override
    public Long createRoute(ResourceRouteSaveReqVO createReqVO) {
        // 插入
        ResourceRouteDO route = BeanUtils.toBean(createReqVO, ResourceRouteDO.class);
        routeMapper.insert(route);
        // 返回
        return route.getId();
    }

    @Override
    public void updateRoute(ResourceRouteSaveReqVO updateReqVO) {
        // 校验存在
        validateRouteExists(updateReqVO.getId());
        // 更新
        ResourceRouteDO updateObj = BeanUtils.toBean(updateReqVO, ResourceRouteDO.class);
        routeMapper.updateById(updateObj);
    }

    @Override
    public void deleteRoute(Long id) {
        // 校验存在
        validateRouteExists(id);
        // 删除
        routeMapper.deleteById(id);
    }

    private void validateRouteExists(Long id) {
        if (routeMapper.selectById(id) == null) {
            throw exception(ROUTE_NOT_EXISTS);
        }
    }

    @Override
    public ResourceRouteDO getRoute(Long id) {
        return routeMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceRouteDO> getRoutePage(ResourceRoutePageReqVO pageReqVO) {
        return routeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResourceRouteDO> getRouteList() {
        return routeMapper.selectList();
    }

    @Override
    public List<ResourceRouteDO> getEnableRouteList() {
        return routeMapper.selectEnableList();
    }

    @Override
    public List<ResourceRouteDO> getList(List<Long> routeIds) {
        if(routeIds.isEmpty()) {
            return Collections.emptyList();
        }
        return routeMapper.selectByIds(routeIds);
    }

    @Override
    public ResourceRouteDO getById(Long routeId) {
        return routeMapper.selectById(routeId);
    }

    @Override
    public PageResult<AppResourceRouteRespVO> getAppRoutePage(AppResourceRoutePageReqVO pageReqVO) {
        IPage<AppResourceRouteRespVO> ipage = MyBatisUtils.buildPage(pageReqVO);
        IPage<AppResourceRouteRespVO> result = routeMapper.selectAppRoutePage(ipage,pageReqVO);
        return new PageResult<>(result.getRecords(), result.getTotal());
    }

    @Override
    public AppResourceRoutePriceVO getMinAndMaxPrice(Long routeId, Long shipId) {
        return routeMapper.selectMinAndMaxPrice(routeId, shipId);
    }

}