package com.zt.plat.module.qms.resource.device.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceRepairDetailPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceRepairDetailRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceRepairDetailSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceRepairDetailDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.resource.device.dal.mapper.DeviceRepairDetailMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.DEVICE_REPAIR_DETAIL_NOT_EXISTS;

/**
 * 设备维修明细 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DeviceRepairDetailServiceImpl implements DeviceRepairDetailService {

    @Resource
    private DeviceRepairDetailMapper deviceRepairDetailMapper;

    @Override
    public DeviceRepairDetailRespVO createDeviceRepairDetail(DeviceRepairDetailSaveReqVO createReqVO) {
        // 插入
        DeviceRepairDetailDO deviceRepairDetail = BeanUtils.toBean(createReqVO, DeviceRepairDetailDO.class);
        deviceRepairDetailMapper.insert(deviceRepairDetail);
        // 返回
        return BeanUtils.toBean(deviceRepairDetail, DeviceRepairDetailRespVO.class);
    }

    @Override
    public void updateDeviceRepairDetail(DeviceRepairDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceRepairDetailExists(updateReqVO.getId());
        // 更新
        DeviceRepairDetailDO updateObj = BeanUtils.toBean(updateReqVO, DeviceRepairDetailDO.class);
        deviceRepairDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceRepairDetail(Long id) {
        // 校验存在
        validateDeviceRepairDetailExists(id);
        // 删除
        deviceRepairDetailMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceRepairDetailListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceRepairDetailExists(ids);
        // 删除
        deviceRepairDetailMapper.deleteByIds(ids);
        }

    private void validateDeviceRepairDetailExists(List<Long> ids) {
        List<DeviceRepairDetailDO> list = deviceRepairDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_REPAIR_DETAIL_NOT_EXISTS);
        }
    }

    private void validateDeviceRepairDetailExists(Long id) {
        if (deviceRepairDetailMapper.selectById(id) == null) {
            throw exception(DEVICE_REPAIR_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public DeviceRepairDetailDO getDeviceRepairDetail(Long id) {
        return deviceRepairDetailMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceRepairDetailDO> getDeviceRepairDetailPage(DeviceRepairDetailPageReqVO pageReqVO) {
        return deviceRepairDetailMapper.selectPage(pageReqVO);
    }

}