package com.shkj.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shkj.common.core.domain.Result;
import com.shkj.common.utils.DateUtils;
import com.shkj.common.utils.SecurityUtils;
import com.shkj.wms.bo.BasePointQueryBo;
import com.shkj.wms.domain.BasePoint;
import com.shkj.wms.domain.BasePointGroup;
import com.shkj.wms.mapper.BasePointMapper;
import com.shkj.wms.service.IBasePointService;
import com.shkj.wms.vo.BasePointVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 点位管理Service业务层处理
 *
 * @author zjx
 * @date 2023-04-20
 */
@Service
public class BasePointServiceImpl extends ServiceImpl<BasePointMapper, BasePoint> implements IBasePointService {

    @Autowired
    private BasePointMapper basePointMapper;

    /**
     * 查询点位管理
     *
     * @param id 点位管理主键
     * @return 点位管理
     */
    @Override
    public BasePoint selectBasePointById(Long id) {
        return basePointMapper.selectBasePointById(id);
    }

    /**
     * 查询点位管理列表
     *
     * @param basePoint 点位管理
     * @return 点位管理
     */
    @Override
    public List<BasePoint> selectBasePointList(BasePoint basePoint) {
        return basePointMapper.selectBasePointList(basePoint);
    }

    /**
     * 新增点位管理
     *
     * @param basePoint 点位管理
     * @return 结果
     */
    @Override
    public Result<Boolean> insertBasePoint(BasePoint basePoint) {

        LambdaQueryWrapper<BasePoint> wrapper = new LambdaQueryWrapper<BasePoint>()
                .eq(BasePoint::getPointNo, basePoint.getPointNo())
                .or().eq(BasePoint::getPointName,basePoint.getPointName()).last("limit 1");
        BasePoint one = getOne(wrapper);
        if ( one!= null && one.getPointNo().equals(basePoint.getPointNo())) {
            return Result.err().msg("点位编号已经存在");
        }
        if ( one!= null && one.getPointName().equals(basePoint.getPointName())) {
            return Result.err().msg("点位名称已经存在");
        }
        basePoint.setCreateTime(DateUtils.getNowDate());
        basePoint.setBranchId(SecurityUtils.getCurrentBranchId());
        this.baseMapper.insert(basePoint);
        return Result.ok();
    }

    /**
     * 修改点位管理
     *
     * @param basePoint 点位管理
     * @return 结果
     */
    @Override
    public Result<Boolean> updateBasePoint(BasePoint basePoint) {
        basePoint.setUpdateTime(DateUtils.getNowDate());
        this.baseMapper.updateById(basePoint);
        return Result.ok();
    }

    /**
     * 删除点位管理
     *
     * @param id 需要删除的点位管理主键
     * @return 结果
     */
    @Override
    public Result<Boolean> deleteBasePointById(Long id) {
        this.baseMapper.deleteBasePointById(id);
        return Result.ok();
    }

    @Override
    public  BasePointVo selectBasePointByQuery(BasePointQueryBo bo){
        return this.baseMapper.selectBasePointByQuery(bo);
    }

    @Override
    public BasePoint selectBasePoint(BasePoint basePoint) {
        return basePointMapper.selectBasePoint(basePoint);
    }

    @Override
    public BasePoint selectBasePointByPointNo(String pointNo) {
        return basePointMapper.selectBasePointByPointNo(pointNo);
    }


}
