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.DeviceRepairApplyPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceRepairApplyRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceRepairApplySaveReqVO;
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.DeviceRepairApplyDO;
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.DeviceRepairApplyMapper;

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

/**
 * 维修申请 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DeviceRepairApplyServiceImpl implements DeviceRepairApplyService {

    @Resource
    private DeviceRepairApplyMapper deviceRepairApplyMapper;

    @Override
    public DeviceRepairApplyRespVO createDeviceRepairApply(DeviceRepairApplySaveReqVO createReqVO) {
        // 插入
        DeviceRepairApplyDO deviceRepairApply = BeanUtils.toBean(createReqVO, DeviceRepairApplyDO.class);
        deviceRepairApplyMapper.insert(deviceRepairApply);
        // 返回
        return BeanUtils.toBean(deviceRepairApply, DeviceRepairApplyRespVO.class);
    }

    @Override
    public void updateDeviceRepairApply(DeviceRepairApplySaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceRepairApplyExists(updateReqVO.getId());
        // 更新
        DeviceRepairApplyDO updateObj = BeanUtils.toBean(updateReqVO, DeviceRepairApplyDO.class);
        deviceRepairApplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceRepairApply(Long id) {
        // 校验存在
        validateDeviceRepairApplyExists(id);
        // 删除
        deviceRepairApplyMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceRepairApplyListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceRepairApplyExists(ids);
        // 删除
        deviceRepairApplyMapper.deleteByIds(ids);
        }

    private void validateDeviceRepairApplyExists(List<Long> ids) {
        List<DeviceRepairApplyDO> list = deviceRepairApplyMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_REPAIR_APPLY_NOT_EXISTS);
        }
    }

    private void validateDeviceRepairApplyExists(Long id) {
        if (deviceRepairApplyMapper.selectById(id) == null) {
            throw exception(DEVICE_REPAIR_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public DeviceRepairApplyDO getDeviceRepairApply(Long id) {
        return deviceRepairApplyMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceRepairApplyDO> getDeviceRepairApplyPage(DeviceRepairApplyPageReqVO pageReqVO) {
        return deviceRepairApplyMapper.selectPage(pageReqVO);
    }

}