package com.zt.plat.module.qms.business.other.service;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.qms.business.other.controller.vo.*;
import com.zt.plat.module.qms.business.other.dal.dataobject.OtherSystemConfigDO;
import com.zt.plat.module.qms.business.other.dal.mapper.OtherSystemConfigMapper;

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.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 第三方系统配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class OtherSystemConfigServiceImpl implements OtherSystemConfigService {

    @Resource
    private OtherSystemConfigMapper otherSystemConfigMapper;

    @Override
    public OtherSystemConfigRespVO createOtherSystemConfig(OtherSystemConfigSaveReqVO createReqVO) {
        // 插入
        OtherSystemConfigDO otherSystemConfig = BeanUtils.toBean(createReqVO, OtherSystemConfigDO.class);
        otherSystemConfigMapper.insert(otherSystemConfig);
        // 返回
        return BeanUtils.toBean(otherSystemConfig, OtherSystemConfigRespVO.class);
    }

    @Override
    public void updateOtherSystemConfig(OtherSystemConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateOtherSystemConfigExists(updateReqVO.getId());
        // 更新
        OtherSystemConfigDO updateObj = BeanUtils.toBean(updateReqVO, OtherSystemConfigDO.class);
        otherSystemConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteOtherSystemConfig(Long id) {
        // 校验存在
        validateOtherSystemConfigExists(id);
        // 删除
        otherSystemConfigMapper.deleteById(id);
    }

    @Override
        public void deleteOtherSystemConfigListByIds(List<Long> ids) {
        // 校验存在
        validateOtherSystemConfigExists(ids);
        // 删除
        otherSystemConfigMapper.deleteByIds(ids);
        }

    private void validateOtherSystemConfigExists(List<Long> ids) {
        List<OtherSystemConfigDO> list = otherSystemConfigMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(OTHER_SYSTEM_CONFIG_NOT_EXISTS);
        }
    }

    private void validateOtherSystemConfigExists(Long id) {
        if (otherSystemConfigMapper.selectById(id) == null) {
            throw exception(OTHER_SYSTEM_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public OtherSystemConfigDO getOtherSystemConfig(Long id) {
        return otherSystemConfigMapper.selectById(id);
    }

    @Override
    public PageResult<OtherSystemConfigDO> getOtherSystemConfigPage(OtherSystemConfigPageReqVO pageReqVO) {
        return otherSystemConfigMapper.selectPage(pageReqVO);
    }

}