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.wms.domain.BasePunch;
import com.shkj.wms.domain.WcsPlcConnect;
import com.shkj.wms.mapper.BasePunchMapper;
import com.shkj.wms.service.IBasePunchService;
import com.shkj.wms.vo.BasePunchRecordVo;
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 BasePunchServiceImpl extends ServiceImpl<BasePunchMapper, BasePunch> implements IBasePunchService {
    @Autowired
    private BasePunchMapper basePunchMapper;

    /**
     * 查询冲孔机设定
     *
     * @param id 冲孔机设定主键
     * @return 冲孔机设定
     */
    @Override
    public BasePunch selectBasePunchById(Long id) {
        return basePunchMapper.selectBasePunchById(id);
    }

    /**
     * 查询冲孔机设定列表
     *
     * @param basePunch 冲孔机设定
     * @return 冲孔机设定
     */
    @Override
    public List<BasePunch> selectBasePunchList(BasePunch basePunch) {
        return basePunchMapper.selectBasePunchList(basePunch);
    }

    @Override
    public BasePunchRecordVo selectBasePunchRecord(BasePunch basePunch) {
        return basePunchMapper.selectBasePunchRecord(basePunch);
    }

    /**
     * 新增冲孔机设定
     *
     * @param basePunch 冲孔机设定
     * @return 结果
     */
    @Override
    public Result<Boolean> insertBasePunch(BasePunch basePunch) {
        LambdaQueryWrapper<BasePunch> wrapper = new LambdaQueryWrapper<BasePunch>()
                .eq(BasePunch::getPunchNo, basePunch.getPunchNo())
                .or().eq(BasePunch::getPunchName,basePunch.getPunchName()).last("limit 1");
        BasePunch one = getOne(wrapper);
        if ( one!= null && one.getPunchNo().equals(basePunch.getPunchNo())) {
            return Result.err().msg("冲孔机编号已经存在");
        }
        if ( one!= null && one.getPunchName().equals(basePunch.getPunchName())) {
            return Result.err().msg("冲孔机名称已经存在");
        }

        basePunch.setCreateTime(DateUtils.getNowDate());
        this.baseMapper.insert(basePunch);
        return Result.ok();
    }

    /**
     * 修改冲孔机设定
     *
     * @param basePunch 冲孔机设定
     * @return 结果
     */
    @Override
    public Result<Boolean> updateBasePunch(BasePunch basePunch) {
        basePunch.setUpdateTime(DateUtils.getNowDate());
        this.baseMapper.updateById(basePunch);
        return Result.ok();
    }

    /**
     * 删除冲孔机设定
     *
     * @param id 需要删除的冲孔机设定主键
     * @return 结果
     */
    @Override
    public Result<Boolean> deleteBasePunchById(Long id) {
        BasePunch basePunch = this.baseMapper.selectById(id);
        basePunch.setIsDel("1");
        basePunch.setUpdateTime(DateUtils.getNowDate());
        this.updateById(basePunch);
        return Result.ok();
    }
}
