package com.zt.plat.module.qms.office.supplier.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierPropertiesPageReqVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierPropertiesRespVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierPropertiesSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.office.supplier.dal.dataobject.SupplierPropertiesDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.office.supplier.dal.mapper.SupplierPropertiesMapper;

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 后台管理
 */
@Service
@Validated
public class SupplierPropertiesServiceImpl implements SupplierPropertiesService {

    @Resource
    private SupplierPropertiesMapper supplierPropertiesMapper;

    @Override
    public SupplierPropertiesRespVO createSupplierProperties(SupplierPropertiesSaveReqVO createReqVO) {
        // 插入
        SupplierPropertiesDO supplierProperties = BeanUtils.toBean(createReqVO, SupplierPropertiesDO.class);
        supplierPropertiesMapper.insert(supplierProperties);
        // 返回
        return BeanUtils.toBean(supplierProperties, SupplierPropertiesRespVO.class);
    }

    @Override
    public SupplierPropertiesRespVO updateSupplierProperties(SupplierPropertiesSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplierPropertiesExists(updateReqVO.getId());
        // 更新
        SupplierPropertiesDO updateObj = BeanUtils.toBean(updateReqVO, SupplierPropertiesDO.class);
        supplierPropertiesMapper.updateById(updateObj);
        return BeanUtils.toBean(updateObj, SupplierPropertiesRespVO.class);
    }

    @Override
    public void deleteSupplierProperties(Long id) {
        // 校验存在
        validateSupplierPropertiesExists(id);
        // 删除
        supplierPropertiesMapper.deleteById(id);
    }

    @Override
        public void deleteSupplierPropertiesListByIds(List<Long> ids) {
        // 校验存在
        validateSupplierPropertiesExists(ids);
        // 删除
        supplierPropertiesMapper.deleteByIds(ids);
        }

    private void validateSupplierPropertiesExists(List<Long> ids) {
        List<SupplierPropertiesDO> list = supplierPropertiesMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SUPPLIER_PROPERTIES_NOT_EXISTS);
        }
    }

    private void validateSupplierPropertiesExists(Long id) {
        if (supplierPropertiesMapper.selectById(id) == null) {
            throw exception(SUPPLIER_PROPERTIES_NOT_EXISTS);
        }
    }

    @Override
    public SupplierPropertiesDO getSupplierProperties(Long id) {
        return supplierPropertiesMapper.selectById(id);
    }

    @Override
    public PageResult<SupplierPropertiesDO> getSupplierPropertiesPage(SupplierPropertiesPageReqVO pageReqVO) {
        return supplierPropertiesMapper.selectPage(pageReqVO);
    }

}