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

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.routepoint.vo.RoutePointPageReqVO;
import com.yc.ship.module.resource.controller.admin.routepoint.vo.RoutePointSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.routepoint.RoutePointDO;
import com.yc.ship.module.resource.dal.mysql.routepoint.RoutePointMapper;
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.POINT_REPEAT_EXCEPTION;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.ROUTE_POINT_NOT_EXISTS;


/**
 * 产品路线点位 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class RoutePointServiceImpl implements RoutePointService {

    @Resource
    private RoutePointMapper routePointMapper;

    @Override
    public Long createRoutePoint(RoutePointSaveReqVO createReqVO) {
        List<RoutePointDO> repeatNameList = routePointMapper.selectList(new LambdaQueryWrapperX<RoutePointDO>()
                .eq(RoutePointDO::getName, createReqVO.getName()));
        if (repeatNameList != null && !repeatNameList.isEmpty()) {
            throw exception(POINT_REPEAT_EXCEPTION);
        }
        // 插入
        RoutePointDO routePoint = BeanUtils.toBean(createReqVO, RoutePointDO.class);
        Long id= IdWorker.getId(routePoint);
        routePoint.setId(id);
        routePoint.setDeleted(false);
        routePointMapper.insert(routePoint);
        // 返回
        return id;
    }

    @Override
    public void updateRoutePoint(RoutePointSaveReqVO updateReqVO) {
        // 校验存在
        validateRoutePointExists(updateReqVO.getId());

        List<RoutePointDO> repeatNameList = routePointMapper.selectList(new LambdaQueryWrapperX<RoutePointDO>()
                .eq(RoutePointDO::getName, updateReqVO.getName())
                .ne(RoutePointDO::getId, updateReqVO.getId())
        );
        if (repeatNameList != null && !repeatNameList.isEmpty()) {
            throw exception(POINT_REPEAT_EXCEPTION);
        }
        // 更新
        RoutePointDO updateObj = BeanUtils.toBean(updateReqVO, RoutePointDO.class);
        routePointMapper.updateById(updateObj);
    }

    @Override
    public void deleteRoutePoint(Long id) {
        // 校验存在
        validateRoutePointExists(id);
        // 删除
        routePointMapper.deleteById(id);
    }

    private void validateRoutePointExists(Long id) {
        if (routePointMapper.selectById(id) == null) {
            throw exception(ROUTE_POINT_NOT_EXISTS);
        }
    }

    @Override
    public RoutePointDO getRoutePoint(Long id) {
        return routePointMapper.selectById(id);
    }

    @Override
    public PageResult<RoutePointDO> getRoutePointPage(RoutePointPageReqVO pageReqVO) {
        return routePointMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RoutePointDO> selectAll() {
        return routePointMapper.selectList();
    }

}