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.OtherDictionaryBusinessDO;
import com.zt.plat.module.qms.business.other.dal.mapper.OtherDictionaryBusinessMapper;

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 OtherDictionaryBusinessServiceImpl implements OtherDictionaryBusinessService {

    @Resource
    private OtherDictionaryBusinessMapper otherDictionaryBusinessMapper;

    @Override
    public OtherDictionaryBusinessRespVO createOtherDictionaryBusiness(OtherDictionaryBusinessSaveReqVO createReqVO) {
        // 插入
        OtherDictionaryBusinessDO otherDictionaryBusiness = BeanUtils.toBean(createReqVO, OtherDictionaryBusinessDO.class);
        otherDictionaryBusinessMapper.insert(otherDictionaryBusiness);
        // 返回
        return BeanUtils.toBean(otherDictionaryBusiness, OtherDictionaryBusinessRespVO.class);
    }

    @Override
    public void updateOtherDictionaryBusiness(OtherDictionaryBusinessSaveReqVO updateReqVO) {
        // 校验存在
        validateOtherDictionaryBusinessExists(updateReqVO.getId());
        // 更新
        OtherDictionaryBusinessDO updateObj = BeanUtils.toBean(updateReqVO, OtherDictionaryBusinessDO.class);
        otherDictionaryBusinessMapper.updateById(updateObj);
    }

    @Override
    public void deleteOtherDictionaryBusiness(Long id) {
        // 校验存在
        validateOtherDictionaryBusinessExists(id);
        // 删除
        otherDictionaryBusinessMapper.deleteById(id);
    }

    @Override
        public void deleteOtherDictionaryBusinessListByIds(List<Long> ids) {
        // 校验存在
        validateOtherDictionaryBusinessExists(ids);
        // 删除
        otherDictionaryBusinessMapper.deleteByIds(ids);
        }

    private void validateOtherDictionaryBusinessExists(List<Long> ids) {
        List<OtherDictionaryBusinessDO> list = otherDictionaryBusinessMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(OTHER_DICTIONARY_BUSINESS_NOT_EXISTS);
        }
    }

    private void validateOtherDictionaryBusinessExists(Long id) {
        if (otherDictionaryBusinessMapper.selectById(id) == null) {
            throw exception(OTHER_DICTIONARY_BUSINESS_NOT_EXISTS);
        }
    }

    @Override
    public OtherDictionaryBusinessDO getOtherDictionaryBusiness(Long id) {
        return otherDictionaryBusinessMapper.selectById(id);
    }

    @Override
    public PageResult<OtherDictionaryBusinessDO> getOtherDictionaryBusinessPage(OtherDictionaryBusinessPageReqVO pageReqVO) {
        return otherDictionaryBusinessMapper.selectPage(pageReqVO);
    }

}