package com.fjhx.wms.service.warehouse.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.wms.entity.warehouse.dto.WarehouseDto;
import com.fjhx.wms.entity.warehouse.dto.WarehouseSelectDto;
import com.fjhx.wms.entity.warehouse.po.Warehouse;
import com.fjhx.wms.entity.warehouse.vo.WarehouseVo;
import com.fjhx.wms.mapper.warehouse.WarehouseMapper;
import com.fjhx.wms.service.warehouse.WarehouseService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;


/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author
 * @since 2023-03-20
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {

    @Override
    public Page<WarehouseVo> getPage(WarehouseSelectDto dto) {
        IWrapper<Warehouse> wrapper = getWrapper();
        wrapper.orderByDesc("w", Warehouse::getId);
        wrapper.like("w", Warehouse::getName, dto.getKeyword());
        wrapper.eq("w", Warehouse::getType, dto.getType());
        Page<WarehouseVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        //赋值仓管员名称
        UserUtil.assignmentNickName(page.getRecords(), Warehouse::getKeeperId, WarehouseVo::setKeeperName);
        return page;
    }

    @Override
    public WarehouseVo detail(Long id) {
        Warehouse Warehouse = this.getById(id);
        WarehouseVo result = BeanUtil.toBean(Warehouse, WarehouseVo.class);
        //赋值仓管员名称
        Map<Long, String> nickNameMapByIds = UserUtil.getNickNameMapByIds(Arrays.asList(result.getKeeperId()));
        result.setKeeperName(nickNameMapByIds.get(result.getKeeperId()));
        return result;
    }

    @Override
    public void add(WarehouseDto warehouseDto) {
        this.nameDuplication(Warehouse::getName, warehouseDto.getName(), "仓库名称重复");
        this.save(warehouseDto);
    }

    @Override
    public void edit(WarehouseDto warehouseDto) {
        this.nameDuplication(Warehouse::getName, warehouseDto.getName(), warehouseDto.getId(), "仓库名称重复");
        this.updateById(warehouseDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
