# ItInventoryServiceImpl.java

    package cloud.wconnect.it.service.itinventory.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.it.entity.itinventory.ItInventory;
    import cloud.wconnect.it.mapper.itinventory.ItInventoryMapper;
    import cloud.wconnect.it.pojo.dto.itinventory.ItInventoryDTO;
    import cloud.wconnect.it.pojo.query.itinventory.ItInventoryQuery;
    import cloud.wconnect.it.service.itinventory.IItInventoryService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.Objects;

    /**
     * <p>
     * IT Inventory 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2024-03-02
     */
    @Service
    @DS("it_manage")
    public class ItInventoryServiceImpl extends ServiceImpl<ItInventoryMapper, ItInventory> implements IItInventoryService {

        @Autowired
        private BeanMapper beanMapper;

        /**
         * 检查数据是否存在
         *
         * @param id inventory id
         * @return inventory
         * @author yyt
         * @date 2024/3/2
         **/
        @Override
        public ItInventory checkExist(long id) {
            ItInventory itInventory = getById(id);
            if (Objects.isNull(itInventory)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the it inventory is not found;id:" + id);
            }

            return itInventory;
        }

        /**
         * 分页查询数据
         *
         * @param propertyId 资产 id
         * @param query      查询条件
         * @return inventory
         * @author yyt
         * @date 2025/10/30
         **/
        @Override
        public ListPagedDTO<ItInventoryDTO> listPage(Integer propertyId, ItInventoryQuery query) {
            LambdaQueryWrapper<ItInventory> sql = new QueryWrapper<ItInventory>().lambda()
                    .eq(ItInventory::getPropertyId, propertyId)
                    .between(Objects.nonNull(query.getStartDate()) && Objects.nonNull(query.getEndDate()),
                            ItInventory::getPurchaseDate, query.getStartDate(), query.getEndDate())
                    .like(StringUtils.isNotBlank(query.getDeviceName()), ItInventory::getDeviceName, query.getDeviceName())
                    .like(StringUtils.isNotBlank(query.getUserName()), ItInventory::getUserName, query.getUserName())
                    .like(StringUtils.isNotBlank(query.getSerialNumber()), ItInventory::getSerialNumber, query.getSerialNumber())
                    .like(StringUtils.isNotBlank(query.getOs()), ItInventory::getOs, query.getOs())
                    .eq(ItInventory::getIsDelete, false);
            if (CollectionUtils.isNotEmpty(query.getStatus())) {
                sql.in(ItInventory::getStatus, query.getStatus());
            }

            sql.orderByDesc(ItInventory::getId);

            Page<ItInventory> page = page(new Page<>(query.getPage(), query.getSize()), sql);

            return new ListPagedDTO<>(page.getTotal(), beanMapper.mapList(page.getRecords(), ItInventoryDTO.class));
        }

        /**
         * 新增 it inventory
         *
         * @param propertyId     资产 id
         * @param itInventoryDTO inventory 信息
         * @param userId         操作人
         * @author yyt
         * @date 2026/1/16
         **/
        @Override
        public void save(Integer propertyId, ItInventoryDTO itInventoryDTO, int userId) {
            ItInventory itInventory = beanMapper.map(itInventoryDTO, ItInventory.class);
            itInventory.setPropertyId(propertyId).setIsDelete(false).setUpdatedBy(userId).setUpdatedTime(DateTimeUtils.nowTimestamp());
            save(itInventory);
        }

        /**
         * 更新 it inventory
         *
         * @param id             inventory id
         * @param itInventoryDTO inventory 信息
         * @param userId         操作人
         * @author yyt
         * @date 2026/1/16
         **/
        @Override
        public void update(long id, ItInventoryDTO itInventoryDTO, int userId) {
            ItInventory itInventory = beanMapper.map(itInventoryDTO, ItInventory.class);
            itInventory.setId(id);
            updateById(itInventory);
        }

        /**
         * 删除 it inventory
         *
         * @param id inventory id
         * @author yyt
         * @date 2026/1/16
         **/
        @Override
        public void delete(long id) {
            updateById(new ItInventory().setId(id).setIsDelete(true));
        }

    }