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.DeviceConfigFlowPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceConfigFlowRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceConfigFlowSaveReqVO;
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.DeviceConfigFlowDO;
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.DeviceConfigFlowMapper;

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.*;

/**
 * 设备通用流程配置 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DeviceConfigFlowServiceImpl implements DeviceConfigFlowService {

    @Resource
    private DeviceConfigFlowMapper deviceConfigFlowMapper;

    @Override
    public List<DeviceConfigFlowDO> getEnableList(DeviceConfigFlowPageReqVO pageReqVO) {
        return deviceConfigFlowMapper.selectList(pageReqVO);
    }

    @Override
    public DeviceConfigFlowDO getByBusinessType(String code) {
        LambdaQueryWrapper<DeviceConfigFlowDO> query = new LambdaQueryWrapper<>();
        query.eq(DeviceConfigFlowDO::getBusinessType, code);
        query.orderByDesc(DeviceConfigFlowDO::getUpdateTime);
        query.last("limit 1");
        return deviceConfigFlowMapper.selectOne(query);
    }

    @Override
    public DeviceConfigFlowRespVO createDeviceConfigFlow(DeviceConfigFlowSaveReqVO createReqVO) {
        // 插入
        DeviceConfigFlowDO deviceConfigFlow = BeanUtils.toBean(createReqVO, DeviceConfigFlowDO.class);
        deviceConfigFlowMapper.insert(deviceConfigFlow);
        // 返回
        return BeanUtils.toBean(deviceConfigFlow, DeviceConfigFlowRespVO.class);
    }

    @Override
    public void updateDeviceConfigFlow(DeviceConfigFlowSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceConfigFlowExists(updateReqVO.getId());
        // 更新
        DeviceConfigFlowDO updateObj = BeanUtils.toBean(updateReqVO, DeviceConfigFlowDO.class);
        deviceConfigFlowMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceConfigFlow(Long id) {
        // 校验存在
        validateDeviceConfigFlowExists(id);
        // 删除
        deviceConfigFlowMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceConfigFlowListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceConfigFlowExists(ids);
        // 删除
        deviceConfigFlowMapper.deleteByIds(ids);
        }

    private void validateDeviceConfigFlowExists(List<Long> ids) {
        List<DeviceConfigFlowDO> list = deviceConfigFlowMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_CONFIG_FLOW_NOT_EXISTS);
        }
    }

    private void validateDeviceConfigFlowExists(Long id) {
        if (deviceConfigFlowMapper.selectById(id) == null) {
            throw exception(DEVICE_CONFIG_FLOW_NOT_EXISTS);
        }
    }

    @Override
    public DeviceConfigFlowDO getDeviceConfigFlow(Long id) {
        return deviceConfigFlowMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceConfigFlowDO> getDeviceConfigFlowPage(DeviceConfigFlowPageReqVO pageReqVO) {
        return deviceConfigFlowMapper.selectPage(pageReqVO);
    }

}