package com.tmerclub.cloud.warehouse.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.warehouse.mapper.WarehouseMapper;
import com.tmerclub.cloud.warehouse.model.Warehouse;
import com.tmerclub.cloud.warehouse.model.dto.WarehouseDTO;
import com.tmerclub.cloud.warehouse.model.vo.WarehouseVO;
import com.tmerclub.cloud.warehouse.service.WarehouseService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 仓库Service实现类
 *
 * @author : frank
 * @create : 2025-04-14
 */
@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    WarehouseMapper warehouseMapper;

    @Override
    public PageVO<WarehouseVO> page(PageDTO pageDTO, WarehouseDTO warehouseDTO) {
        return PageUtil.doPage(pageDTO, () -> warehouseMapper.list(warehouseDTO));
    }

    @Override
    public WarehouseVO getById(Long id) {
        return warehouseMapper.getById(id);
    }

    @Override
    public int save(WarehouseDTO warehouseDTO) {
        Warehouse warehouse = BeanUtil.toBean(warehouseDTO, Warehouse.class);
        warehouse.setCreateTime(DateUtil.date());
        return warehouseMapper.save(warehouse);
    }

    @Override
    public int update(WarehouseDTO warehouseDTO) {
        Warehouse warehouse = BeanUtil.toBean(warehouseDTO, Warehouse.class);
        warehouse.setUpdateTime(DateUtil.date());
        return warehouseMapper.update(warehouse);
    }

    @Override
    public int delete(Long id) {
        WarehouseVO warehouseVO = warehouseMapper.getById(id);
        Warehouse warehouse = BeanUtil.toBean(warehouseVO, Warehouse.class);
        warehouse.setUpdateTime(DateUtil.date());
        warehouse.setDeleted(1);
        return warehouseMapper.update(warehouse);
    }
}