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.WareHouseTakePointPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseTakePointSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseTakePointDO;
import cn.iocoder.yudao.module.system.dal.dataobject.parkingspot.ParkingSpotDO;
import cn.iocoder.yudao.module.system.dal.mysql.houselocation.WareHouseTakePointMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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


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_TAKE_POINT_NOT_EXISTS;

/**
 * 取货点位 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class WareHouseTakePointServiceImpl extends ServiceImpl<WareHouseTakePointMapper, WareHouseTakePointDO> implements WareHouseTakePointService {

    @Resource
    private WareHouseTakePointMapper houseTakePointMapper;

    @Override
    public Long createHouseTakePoint(WareHouseTakePointSaveReqVO createReqVO) {
        // 插入
        WareHouseTakePointDO houseTakePoint = BeanUtils.toBean(createReqVO, WareHouseTakePointDO.class);
        houseTakePointMapper.insert(houseTakePoint);
        // 返回
        return houseTakePoint.getId();
    }

    @Override
    public void updateHouseTakePoint(WareHouseTakePointSaveReqVO updateReqVO) {
        // 校验存在
        validateHouseTakePointExists(updateReqVO.getId());
        // 更新
        WareHouseTakePointDO updateObj = BeanUtils.toBean(updateReqVO, WareHouseTakePointDO.class);
        houseTakePointMapper.updateById(updateObj);
    }

    @Override
    public void deleteHouseTakePoint(Long id) {
        // 校验存在
        validateHouseTakePointExists(id);
        // 删除
        houseTakePointMapper.deleteById(id);
    }

    private void validateHouseTakePointExists(Long id) {
        if (houseTakePointMapper.selectById(id) == null) {
            throw exception(HOUSE_TAKE_POINT_NOT_EXISTS);
        }
    }

    @Override
    public WareHouseTakePointDO getHouseTakePoint(Long id) {
        return houseTakePointMapper.selectById(id);
    }

    @Override
    public PageResult<WareHouseTakePointDO> getHouseTakePointPage(WareHouseTakePointPageReqVO pageReqVO) {
        return houseTakePointMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WareHouseTakePointDO> getHouseTakePointList(WareHouseTakePointSaveReqVO reqVO) {
        return houseTakePointMapper.selectList(new LambdaQueryWrapperX<WareHouseTakePointDO>()
                .likeIfPresent(WareHouseTakePointDO::getPointName, reqVO.getPointName())
                .eqIfPresent(WareHouseTakePointDO::getPointType, reqVO.getPointType())
                .inIfPresent(WareHouseTakePointDO::getId, reqVO.getIds())
                .eqIfPresent(WareHouseTakePointDO::getPointStatus, reqVO.getPointStatus())
                .eqIfPresent(WareHouseTakePointDO::getPositionMapId, reqVO.getPositionMapId())
                .eqIfPresent(WareHouseTakePointDO::getLocationStorey, reqVO.getLocationStorey())
                .eqIfPresent(WareHouseTakePointDO::getPositionMapItemId, reqVO.getPositionMapItemId()));
    }

    @Override
    public void setTakePointMapIdEmpty(Long mapId,List<Long> ids) {
        houseTakePointMapper.setTakePointMapIdEmpty(mapId,ids);
    }

    @Override
    public void batchEditOrDel(List<List<WareHouseTakePointDO>> list) {
        //批量添加、修改、删除
        if (isNotEmpty(list.get(0))) {
            houseTakePointMapper.insertBatch(list.get(0));
        }
        if (isNotEmpty(list.get(1))) {
            houseTakePointMapper.updateBatch(list.get(1));
        }
        if (isNotEmpty(list.get(2))) {
            houseTakePointMapper.deleteByIds(convertList(list.get(2), WareHouseTakePointDO::getId));
        }
    }

    @Override
    public WareHouseTakePointDO getHouseTakePointByItemIdAndStorey(Long positionMapItemId, int locationStorey) {
        return houseTakePointMapper.selectOne(new LambdaQueryWrapperX<WareHouseTakePointDO>()
                .eq(WareHouseTakePointDO::getPositionMapItemId, positionMapItemId)
                .eq(WareHouseTakePointDO::getLocationStorey, locationStorey));
    }

    @Override
    public void updateHouseTakePointByItemIdAndStorey(WareHouseTakePointDO pointDO) {
        houseTakePointMapper.updateHouseTakePointByItemIdAndStorey(pointDO);
    }


}
