package com.njzscloud.dispose.wh.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.dispose.wh.mapper.WarehouseMapper;
import com.njzscloud.dispose.wh.pojo.entity.WarehouseEntity;
import com.njzscloud.dispose.wh.pojo.param.AddWarehouseParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 仓库信息
 *
 * @author ljw
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WarehouseService extends ServiceImpl<WarehouseMapper, WarehouseEntity> {

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(AddWarehouseParam addWarehouseParam) {
        WarehouseEntity entity = BeanUtil.copyProperties(addWarehouseParam, WarehouseEntity.class);
        this.save(entity);
    }

    /**
     * 修改
     */
    public void modify(WarehouseEntity warehouseEntity) {
        this.updateById(warehouseEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public WarehouseEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<WarehouseEntity> paging(PageParam pageParam, WarehouseEntity warehouseEntity) {
        String warehouseName = warehouseEntity.getWarehouseName();
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.query(warehouseEntity)
            .eq("deleted", Boolean.FALSE)
            .like(StrUtil.isNotBlank(warehouseName), "warehouse_name", warehouseName)));
    }

}
