package com.shkj.wcs.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.enums.HttpStatus;
import com.shkj.common.utils.DateUtils;
import com.shkj.wcs.domain.WcsDevice;
import com.shkj.wcs.domain.WcsDevice;
import com.shkj.wcs.mapper.WcsDeviceMapper;
import com.shkj.wcs.mapper.WcsDeviceMapper;
import com.shkj.wcs.service.IWcsDeviceService;
import com.shkj.wcs.service.IWcsDeviceService;
import com.shkj.wcs.vo.BaseDeviceVo;
import com.shkj.wms.domain.BaseLogicArea;
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 WcsDeviceServiceImpl extends ServiceImpl<WcsDeviceMapper, WcsDevice> implements IWcsDeviceService {
    @Autowired
    private WcsDeviceMapper wcsDeviceMapper;

    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public WcsDevice selectWcsDeviceById(Long id) {
        return wcsDeviceMapper.selectWcsDeviceById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param wcsDevice 设备管理
     * @return 设备管理
     */
    @Override
    public List<WcsDevice> selectWcsDeviceList(WcsDevice wcsDevice) {
        return wcsDeviceMapper.selectWcsDeviceList(wcsDevice);
    }

    /**
     * 新增设备管理
     *
     * @param wcsDevice 设备管理
     * @return 结果
     */
    @Override
    public Result<Boolean> insertWcsDevice(WcsDevice wcsDevice) {

        Result<Boolean> booleanResult = checkDevice(wcsDevice);
        if(booleanResult.getCode() !=HttpStatus.SUCCESS.getCode()){
            return booleanResult;
        }
        wcsDevice.setCreateTime(DateUtils.getNowDate());
        this.baseMapper.insert(wcsDevice);
        return Result.ok();
    }

    /**
     * 修改设备管理
     *
     * @param wcsDevice 设备管理
     * @return 结果
     */
    @Override
    public Result<Boolean> updateWcsDevice(WcsDevice wcsDevice) {
        wcsDevice.setUpdateTime(DateUtils.getNowDate());
        this.baseMapper.updateById(wcsDevice);
        return Result.ok();
    }

    public Result<Boolean> checkDevice(WcsDevice wcsDevice){
        LambdaQueryWrapper<WcsDevice> wrapper = new LambdaQueryWrapper<WcsDevice>()
                .eq(WcsDevice::getDevNo, wcsDevice.getDevNo())
                .or().eq(WcsDevice::getDevName,wcsDevice.getDevName()).last("limit 1");
        WcsDevice one = getOne(wrapper);
        if ( one!= null && one.getDevNo().equals(wcsDevice.getDevNo())) {
            return Result.err().msg("设备编号已经存在");
        }
        if ( one!= null && one.getDevName().equals(wcsDevice.getDevName())) {
            return Result.err().msg("设备名称已经存在");
        }
        return Result.ok();
    }

    /**
     * 删除设备管理
     *
     * @param id 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public Result<Boolean> deleteWcsDeviceById(Long id) {
        WcsDevice WcsDevice = this.baseMapper.selectById(id);
        WcsDevice.setIsDel("1");
        WcsDevice.setUpdateTime(DateUtils.getNowDate());
        this.updateById(WcsDevice);
        return Result.ok();
    }

    @Override
    public List<BaseDeviceVo> selectAllDeviceList(WcsDevice wcsDevice) {
        return this.baseMapper.selectAllDeviceList(wcsDevice);
    }
}
