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.domain.BasePointGroup;
import com.shkj.wms.domain.BasePunch;
import com.shkj.wms.mapper.BasePointGroupMapper;
import com.shkj.wms.service.IBasePointGroupService;
import com.shkj.wms.vo.BasePointGroupVo;
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 BasePointGroupServiceImpl extends ServiceImpl<BasePointGroupMapper, BasePointGroup> implements IBasePointGroupService {
    @Autowired
    private BasePointGroupMapper basePointGroupMapper;

    /**
     * 查询点位分组
     *
     * @param id 点位分组主键
     * @return 点位分组
     */
    @Override
    public BasePointGroup selectBasePointGroupById(Long id) {
        return basePointGroupMapper.selectBasePointGroupById(id);
    }

    /**
     * 查询点位分组列表
     *
     * @param basePointGroup 点位分组
     * @return 点位分组
     */
    @Override
    public List<BasePointGroup> selectBasePointGroupList(BasePointGroup basePointGroup) {
        return basePointGroupMapper.selectBasePointGroupList(basePointGroup);
    }

    /**
     * 新增点位分组
     *
     * @param basePointGroup 点位分组
     * @return 结果
     */
    @Override
    public Result<Boolean> insertBasePointGroup(BasePointGroup basePointGroup) {

        LambdaQueryWrapper<BasePointGroup> wrapper = new LambdaQueryWrapper<BasePointGroup>()
                .eq(BasePointGroup::getGroupNo, basePointGroup.getGroupNo())
                .or().eq(BasePointGroup::getGroupName,basePointGroup.getGroupName()).last("limit 1");
        BasePointGroup one = getOne(wrapper);
        if ( one!= null && one.getGroupNo().equals(basePointGroup.getGroupNo())) {
            return Result.err().msg("分组编号已经存在");
        }
        if ( one!= null && one.getGroupName().equals(basePointGroup.getGroupName())) {
            return Result.err().msg("分组名称已经存在");
        }

        basePointGroup.setCreateTime(DateUtils.getNowDate());
        basePointGroup.setBranchId(SecurityUtils.getCurrentBranchId());
        this.baseMapper.insert(basePointGroup);
        return Result.ok();
    }

    /**
     * 修改点位分组
     *
     * @param basePointGroup 点位分组
     * @return 结果
     */
    @Override
    public Result<Boolean> updateBasePointGroup(BasePointGroup basePointGroup) {
        basePointGroup.setUpdateTime(DateUtils.getNowDate());
        this.baseMapper.updateById(basePointGroup);
        return Result.ok();
    }

    /**
     * 删除点位分组
     *
     * @param id 需要删除的点位分组主键
     * @return 结果
     */
    @Override
    public Result<Boolean> deleteBasePointGroupById(Long id) {
        BasePointGroup basePointGroup = this.baseMapper.selectById(id);
        basePointGroup.setIsDel("1");
        basePointGroup.setUpdateTime(DateUtils.getNowDate());
        this.updateById(basePointGroup);
        return Result.ok();
    }

    @Override
    public List<BasePointGroupVo> getBasePointGroup() {
        BasePointGroup basePointGroup = new BasePointGroup();
        basePointGroup.setIsActivity("1");
        return this.baseMapper.selectBasePointGroupListVo(basePointGroup);

    }


}
