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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseLocationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseLocationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseLocationSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseLocationVO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseLocationDO;
import cn.iocoder.yudao.module.system.dal.mysql.houselocation.WareHouseLocationMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import static cn.hutool.core.collection.CollUtil.isNotEmpty;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.HOUSE_LOCATION_NOT_EXISTS;

/**
 * 库位 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class HouseLocationServiceImpl extends ServiceImpl<WareHouseLocationMapper, WareHouseLocationDO> implements HouseLocationService {

    @Resource
    private WareHouseLocationMapper houseLocationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createHouseLocation(WareHouseLocationSaveReqVO createReqVO) {
        // 插入
        WareHouseLocationDO houseLocation = BeanUtils.toBean(createReqVO, WareHouseLocationDO.class);
        houseLocationMapper.insert(houseLocation);
        // 返回
        return houseLocation.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateHouseLocation(WareHouseLocationSaveReqVO updateReqVO) {
        // 校验存在
        validateHouseLocationExists(updateReqVO.getId());
        // 更新
        WareHouseLocationDO updateObj = BeanUtils.toBean(updateReqVO, WareHouseLocationDO.class);
        houseLocationMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHouseLocation(Long id) {
        // 校验存在
        validateHouseLocationExists(id);
        // 删除
        houseLocationMapper.deleteById(id);
    }

    private void validateHouseLocationExists(Long id) {
        if (houseLocationMapper.selectById(id) == null) {
            throw exception(HOUSE_LOCATION_NOT_EXISTS);
        }
    }

    @Override
    public WareHouseLocationDO getHouseLocation(Long id) {
        return houseLocationMapper.selectById(id);
    }

    @Override
    public PageResult<WareHouseLocationDO> getHouseLocationPage(WareHouseLocationPageReqVO pageReqVO) {
        return houseLocationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WareHouseLocationRespVO> getLocationByName(WareHouseLocationVO requestVO) {
        return houseLocationMapper.getLocationByName(requestVO);
    }

    @Override
    public List<WareHouseLocationDO> getByMapItemId(Long mapId, Long mapItemId) {
        return houseLocationMapper.selectList(new LambdaQueryWrapper<WareHouseLocationDO>()
                .eq(WareHouseLocationDO::getMapId, mapId)
                .eq(WareHouseLocationDO::getMapItemId, mapItemId));
    }

    @Override
    public List<WareHouseLocationDO> getLocationByLocationNumbers(List<Long> locationNumbers) {
        return houseLocationMapper.selectList(new LambdaQueryWrapper<WareHouseLocationDO>()
                .in(WareHouseLocationDO::getLocationNumber, locationNumbers));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveOrEditOrDel(Long positionMapId, List<List<WareHouseLocationDO>> list) {
        //批量添加、修改、删除
        if (isNotEmpty(list.get(0))) {
            houseLocationMapper.insertBatch(list.get(0));
        }
        if (isNotEmpty(list.get(1))) {
            houseLocationMapper.updateBatch(list.get(1));
        }
        if (isNotEmpty(list.get(2))) {
            houseLocationMapper.deleteByIds(convertList(list.get(2), WareHouseLocationDO::getId));
        }
    }

    @Override
    public List<WareHouseLocationDO> getByMapId(Long positionMapId) {
        return houseLocationMapper.selectList(new LambdaQueryWrapperX<WareHouseLocationDO>()
                .eq(WareHouseLocationDO::getMapId, positionMapId));
    }

}
