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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessQualityControlSampleProjectPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessQualityControlSampleProjectRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessQualityControlSampleProjectSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessQualityControlSampleProjectDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessQualityControlSampleProjectMapper;

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 BusinessQualityControlSampleProjectServiceImpl implements BusinessQualityControlSampleProjectService {

    @Resource
    private BusinessQualityControlSampleProjectMapper businessQualityControlSampleProjectMapper;

    @Override
    public BusinessQualityControlSampleProjectRespVO createBusinessQualityControlSampleProject(BusinessQualityControlSampleProjectSaveReqVO createReqVO) {
        // 插入
        BusinessQualityControlSampleProjectDO businessQualityControlSampleProject = BeanUtils.toBean(createReqVO, BusinessQualityControlSampleProjectDO.class);
        businessQualityControlSampleProjectMapper.insert(businessQualityControlSampleProject);
        // 返回
        return BeanUtils.toBean(businessQualityControlSampleProject, BusinessQualityControlSampleProjectRespVO.class);
    }

    @Override
    public void updateBusinessQualityControlSampleProject(BusinessQualityControlSampleProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQualityControlSampleProjectExists(updateReqVO.getId());
        // 更新
        BusinessQualityControlSampleProjectDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQualityControlSampleProjectDO.class);
        businessQualityControlSampleProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQualityControlSampleProject(Long id) {
        // 校验存在
        validateBusinessQualityControlSampleProjectExists(id);
        // 删除
        businessQualityControlSampleProjectMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQualityControlSampleProjectListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQualityControlSampleProjectExists(ids);
        // 删除
        businessQualityControlSampleProjectMapper.deleteByIds(ids);
        }

    private void validateBusinessQualityControlSampleProjectExists(List<Long> ids) {
        List<BusinessQualityControlSampleProjectDO> list = businessQualityControlSampleProjectMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QUALITY_CONTROL_SAMPLE_PROJECT_NOT_EXISTS);
        }
    }

    private void validateBusinessQualityControlSampleProjectExists(Long id) {
        if (businessQualityControlSampleProjectMapper.selectById(id) == null) {
            throw exception(BUSINESS_QUALITY_CONTROL_SAMPLE_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQualityControlSampleProjectDO getBusinessQualityControlSampleProject(Long id) {
        return businessQualityControlSampleProjectMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQualityControlSampleProjectDO> getBusinessQualityControlSampleProjectPage(BusinessQualityControlSampleProjectPageReqVO pageReqVO) {
        return businessQualityControlSampleProjectMapper.selectPage(pageReqVO);
    }

}