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.DeviceCalibrationPlanPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceCalibrationPlanRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceCalibrationPlanSaveReqVO;
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.DeviceCalibrationPlanDO;
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.DeviceCalibrationPlanMapper;

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

/**
 * 设备-检定校准计划 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DeviceCalibrationPlanServiceImpl implements DeviceCalibrationPlanService {

    @Resource
    private DeviceCalibrationPlanMapper deviceCalibrationPlanMapper;

    @Override
    public DeviceCalibrationPlanRespVO createDeviceCalibrationPlan(DeviceCalibrationPlanSaveReqVO createReqVO) {
        // 插入
        DeviceCalibrationPlanDO deviceCalibrationPlan = BeanUtils.toBean(createReqVO, DeviceCalibrationPlanDO.class);
        deviceCalibrationPlanMapper.insert(deviceCalibrationPlan);
        // 返回
        return BeanUtils.toBean(deviceCalibrationPlan, DeviceCalibrationPlanRespVO.class);
    }

    @Override
    public void updateDeviceCalibrationPlan(DeviceCalibrationPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceCalibrationPlanExists(updateReqVO.getId());
        // 更新
        DeviceCalibrationPlanDO updateObj = BeanUtils.toBean(updateReqVO, DeviceCalibrationPlanDO.class);
        deviceCalibrationPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceCalibrationPlan(Long id) {
        // 校验存在
        validateDeviceCalibrationPlanExists(id);
        // 删除
        deviceCalibrationPlanMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceCalibrationPlanListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceCalibrationPlanExists(ids);
        // 删除
        deviceCalibrationPlanMapper.deleteByIds(ids);
        }

    private void validateDeviceCalibrationPlanExists(List<Long> ids) {
        List<DeviceCalibrationPlanDO> list = deviceCalibrationPlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_CALIBRATION_PLAN_NOT_EXISTS);
        }
    }

    private void validateDeviceCalibrationPlanExists(Long id) {
        if (deviceCalibrationPlanMapper.selectById(id) == null) {
            throw exception(DEVICE_CALIBRATION_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public DeviceCalibrationPlanDO getDeviceCalibrationPlan(Long id) {
        return deviceCalibrationPlanMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceCalibrationPlanDO> getDeviceCalibrationPlanPage(DeviceCalibrationPlanPageReqVO pageReqVO) {
        return deviceCalibrationPlanMapper.selectPage(pageReqVO);
    }

}