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.DeviceRepairResultDetailPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceRepairResultDetailRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceRepairResultDetailSaveReqVO;
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.DeviceRepairResultDetailDO;
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.DeviceRepairResultDetailMapper;

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_RESULT_DETAIL_NOT_EXISTS;

/**
 * 故障处理明细 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DeviceRepairResultDetailServiceImpl implements DeviceRepairResultDetailService {

    @Resource
    private DeviceRepairResultDetailMapper deviceRepairResultDetailMapper;

    @Override
    public DeviceRepairResultDetailRespVO createDeviceRepairResultDetail(DeviceRepairResultDetailSaveReqVO createReqVO) {
        // 插入
        DeviceRepairResultDetailDO deviceRepairResultDetail = BeanUtils.toBean(createReqVO, DeviceRepairResultDetailDO.class);
        deviceRepairResultDetailMapper.insert(deviceRepairResultDetail);
        // 返回
        return BeanUtils.toBean(deviceRepairResultDetail, DeviceRepairResultDetailRespVO.class);
    }

    @Override
    public void updateDeviceRepairResultDetail(DeviceRepairResultDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceRepairResultDetailExists(updateReqVO.getId());
        // 更新
        DeviceRepairResultDetailDO updateObj = BeanUtils.toBean(updateReqVO, DeviceRepairResultDetailDO.class);
        deviceRepairResultDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceRepairResultDetail(Long id) {
        // 校验存在
        validateDeviceRepairResultDetailExists(id);
        // 删除
        deviceRepairResultDetailMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceRepairResultDetailListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceRepairResultDetailExists(ids);
        // 删除
        deviceRepairResultDetailMapper.deleteByIds(ids);
        }

    private void validateDeviceRepairResultDetailExists(List<Long> ids) {
        List<DeviceRepairResultDetailDO> list = deviceRepairResultDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_REPAIR_RESULT_DETAIL_NOT_EXISTS);
        }
    }

    private void validateDeviceRepairResultDetailExists(Long id) {
        if (deviceRepairResultDetailMapper.selectById(id) == null) {
            throw exception(DEVICE_REPAIR_RESULT_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public DeviceRepairResultDetailDO getDeviceRepairResultDetail(Long id) {
        return deviceRepairResultDetailMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceRepairResultDetailDO> getDeviceRepairResultDetailPage(DeviceRepairResultDetailPageReqVO pageReqVO) {
        return deviceRepairResultDetailMapper.selectPage(pageReqVO);
    }

}