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

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.resource.controller.admin.productRoute.vo.ProductRoutePageReqVO;
import com.yc.ship.module.resource.controller.admin.productRoute.vo.ProductRouteSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.productRoute.ProductRouteDO;
import com.yc.ship.module.resource.dal.mysql.productRoute.ProductRouteMapper;
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.ROUTE_NOT_EXISTS;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.ROUTE_REPEAT_EXCEPTION;


/**
 * 基础产品路线 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ProductRouteServiceImpl implements ProductRouteService {

    @Resource
    private ProductRouteMapper routeMapper;

    @Override
    public Long createRoute(ProductRouteSaveReqVO createReqVO) {
        List<ProductRouteDO> repeatNameList = routeMapper.selectList(new LambdaQueryWrapperX<ProductRouteDO>()
                .eq(ProductRouteDO::getName, createReqVO.getName())
                .eq(ProductRouteDO::getEndPoint, createReqVO.getEndPoint())
                .eq(ProductRouteDO::getStartPoint, createReqVO.getStartPoint())
        );
        if (repeatNameList != null && !repeatNameList.isEmpty()) {
            throw exception(ROUTE_REPEAT_EXCEPTION);
        }
        // 插入
        ProductRouteDO route = BeanUtils.toBean(createReqVO, ProductRouteDO.class);
        route.setId(IdWorker.getId(route));
        routeMapper.insert(route);
        // 返回
        return route.getId();
    }

    @Override
    public void updateRoute(ProductRouteSaveReqVO updateReqVO) {
        // 校验存在
        validateRouteExists(updateReqVO.getId());
        List<ProductRouteDO> repeatNameList = routeMapper.selectList(new LambdaQueryWrapperX<ProductRouteDO>()
                .ne(ProductRouteDO::getId, updateReqVO.getId())
                .eq(ProductRouteDO::getName, updateReqVO.getName())
                .eq(ProductRouteDO::getEndPoint, updateReqVO.getEndPoint())
                .eq(ProductRouteDO::getStartPoint, updateReqVO.getStartPoint())
        );
        if (repeatNameList != null && !repeatNameList.isEmpty()) {
            throw exception(ROUTE_REPEAT_EXCEPTION);
        }
        // 更新
        ProductRouteDO updateObj = BeanUtils.toBean(updateReqVO, ProductRouteDO.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 ProductRouteDO getRoute(Long id) {
        return routeMapper.selectById(id);
    }

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

}