package cn.iocoder.yudao.module.system.service.houselocation;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseTakePointBindingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseTakePointBindingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseTakePointBindingDO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseTakePointDO;
import cn.iocoder.yudao.module.system.dal.mysql.houselocation.WareHouseTakePointBindingMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.HOUSE_TAKE_POINT_BINDING_NOT_EXISTS;

/**
 * 取货点位绑定 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class WareHouseTakePointBindingServiceImpl extends ServiceImpl<WareHouseTakePointBindingMapper, WareHouseTakePointBindingDO> implements WareHouseTakePointBindingService {

    @Resource
    private WareHouseTakePointBindingMapper houseTakePointBindingMapper;

    @Override
    public Long createHouseTakePointBinding(WareHouseTakePointBindingSaveReqVO createReqVO) {
        // 插入
        WareHouseTakePointBindingDO houseTakePointBinding = BeanUtils.toBean(createReqVO, WareHouseTakePointBindingDO.class);
        houseTakePointBindingMapper.insert(houseTakePointBinding);
        // 返回
        return houseTakePointBinding.getId();
    }

    @Override
    public void updateHouseTakePointBinding(WareHouseTakePointBindingSaveReqVO updateReqVO) {
        // 校验存在
        validateHouseTakePointBindingExists(updateReqVO.getId());
        // 更新
        WareHouseTakePointBindingDO updateObj = BeanUtils.toBean(updateReqVO, WareHouseTakePointBindingDO.class);
        houseTakePointBindingMapper.updateById(updateObj);
    }

    @Override
    public void deleteHouseTakePointBinding(Long id) {
        // 校验存在
        validateHouseTakePointBindingExists(id);
        // 删除
        houseTakePointBindingMapper.deleteById(id);
    }

    private void validateHouseTakePointBindingExists(Long id) {
        if (houseTakePointBindingMapper.selectById(id) == null) {
            throw exception(HOUSE_TAKE_POINT_BINDING_NOT_EXISTS);
        }
    }

    @Override
    public WareHouseTakePointBindingDO getHouseTakePointBinding(Long id) {
        return houseTakePointBindingMapper.selectById(id);
    }

    @Override
    public PageResult<WareHouseTakePointBindingDO> getHouseTakePointBindingPage(WareHouseTakePointBindingPageReqVO pageReqVO) {
        return houseTakePointBindingMapper.selectPage(pageReqVO);
    }

    @Override
    public void createHouseTakePointBindings(List<WareHouseTakePointBindingDO> list) {
        houseTakePointBindingMapper.insert(list);
    }

    @Override
    public void deleteByBindingId(Long id) {
        houseTakePointBindingMapper.deleteByBindingId(id);
    }

    @Override
    public List<WareHouseTakePointBindingDO> getByBindingId(Long bindingId) {
        return  houseTakePointBindingMapper.selectList(new LambdaQueryWrapperX<WareHouseTakePointBindingDO>()
                .eq(WareHouseTakePointBindingDO::getBindingId, bindingId));
    }

}
