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.SupplierEvaluationAtPageReqVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierEvaluationAtRespVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierEvaluationAtSaveReqVO;
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.SupplierEvaluationAtDO;
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.SupplierEvaluationAtMapper;

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 SupplierEvaluationAtServiceImpl implements SupplierEvaluationAtService {

    @Resource
    private SupplierEvaluationAtMapper supplierEvaluationAtMapper;

    @Override
    public SupplierEvaluationAtRespVO createSupplierEvaluationAt(SupplierEvaluationAtSaveReqVO createReqVO) {
        // 插入
        SupplierEvaluationAtDO supplierEvaluationAt = BeanUtils.toBean(createReqVO, SupplierEvaluationAtDO.class);
        supplierEvaluationAtMapper.insert(supplierEvaluationAt);
        // 返回
        return BeanUtils.toBean(supplierEvaluationAt, SupplierEvaluationAtRespVO.class);
    }

    @Override
    public void updateSupplierEvaluationAt(SupplierEvaluationAtSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplierEvaluationAtExists(updateReqVO.getId());
        // 更新
        SupplierEvaluationAtDO updateObj = BeanUtils.toBean(updateReqVO, SupplierEvaluationAtDO.class);
        supplierEvaluationAtMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplierEvaluationAt(Long id) {
        // 校验存在
        validateSupplierEvaluationAtExists(id);
        // 删除
        supplierEvaluationAtMapper.deleteById(id);
    }

    @Override
        public void deleteSupplierEvaluationAtListByIds(List<Long> ids) {
        // 校验存在
        validateSupplierEvaluationAtExists(ids);
        // 删除
        supplierEvaluationAtMapper.deleteByIds(ids);
        }

    private void validateSupplierEvaluationAtExists(List<Long> ids) {
        List<SupplierEvaluationAtDO> list = supplierEvaluationAtMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SUPPLIER_EVALUATION_AT_NOT_EXISTS);
        }
    }

    private void validateSupplierEvaluationAtExists(Long id) {
        if (supplierEvaluationAtMapper.selectById(id) == null) {
            throw exception(SUPPLIER_EVALUATION_AT_NOT_EXISTS);
        }
    }

    @Override
    public SupplierEvaluationAtDO getSupplierEvaluationAt(Long id) {
        return supplierEvaluationAtMapper.selectById(id);
    }

    @Override
    public PageResult<SupplierEvaluationAtDO> getSupplierEvaluationAtPage(SupplierEvaluationAtPageReqVO pageReqVO) {
        return supplierEvaluationAtMapper.selectPage(pageReqVO);
    }

}