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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceApplyDetailPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceApplyDetailRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceApplyDetailSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceApplyDetailDO;
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.DeviceApplyDetailMapper;

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

/**
 * 设备通用流程明细 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DeviceApplyDetailServiceImpl implements DeviceApplyDetailService {

    @Resource
    private DeviceApplyDetailMapper deviceApplyDetailMapper;

    @Override
    public List<DeviceApplyDetailDO> selectList(DeviceApplyDetailPageReqVO reqVO) {
        return deviceApplyDetailMapper.selectList(reqVO);
    }

    @Override
    public List<DeviceApplyDetailRespVO> selectListWithDeviceInfo(DeviceApplyDetailPageReqVO reqVO) {
        return deviceApplyDetailMapper.selectListWithDeviceInfo(reqVO);
    }

    @Override
    public List<DeviceApplyDetailDO> selectListByApplyId(Long applyId) {
        LambdaQueryWrapper<DeviceApplyDetailDO> query = new LambdaQueryWrapper<>();
        query.eq(DeviceApplyDetailDO::getApplyId, applyId);
        return deviceApplyDetailMapper.selectList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<DeviceApplyDetailDO> list) {
        deviceApplyDetailMapper.insertBatch(list);
    }

    @Override
    public DeviceApplyDetailRespVO createDeviceApplyDetail(DeviceApplyDetailSaveReqVO createReqVO) {
        // 插入
        DeviceApplyDetailDO deviceApplyDetail = BeanUtils.toBean(createReqVO, DeviceApplyDetailDO.class);
        deviceApplyDetailMapper.insert(deviceApplyDetail);
        // 返回
        return BeanUtils.toBean(deviceApplyDetail, DeviceApplyDetailRespVO.class);
    }

    @Override
    public void updateDeviceApplyDetail(DeviceApplyDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceApplyDetailExists(updateReqVO.getId());
        // 更新
        DeviceApplyDetailDO updateObj = BeanUtils.toBean(updateReqVO, DeviceApplyDetailDO.class);
        deviceApplyDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceApplyDetail(Long id) {
        // 校验存在
        validateDeviceApplyDetailExists(id);
        // 删除
        deviceApplyDetailMapper.deleteById(id);
    }

    @Override
    public void deleteDeviceApplyDetailListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceApplyDetailExists(ids);
        // 删除
        deviceApplyDetailMapper.deleteByIds(ids);
    }

    private void validateDeviceApplyDetailExists(List<Long> ids) {
        List<DeviceApplyDetailDO> list = deviceApplyDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_APPLY_DETAIL_NOT_EXISTS);
        }
    }

    private void validateDeviceApplyDetailExists(Long id) {
        if (deviceApplyDetailMapper.selectById(id) == null) {
            throw exception(DEVICE_APPLY_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public DeviceApplyDetailDO getDeviceApplyDetail(Long id) {
        return deviceApplyDetailMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceApplyDetailRespVO> getDeviceApplyDetailPage(DeviceApplyDetailPageReqVO pageReqVO) {
        return deviceApplyDetailMapper.selectPage(pageReqVO);
    }

}