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.BusinessStandardSampleProjectPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessStandardSampleProjectRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessStandardSampleProjectSaveReqVO;
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.BusinessStandardSampleProjectDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessStandardSampleProjectMapper;

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 BusinessStandardSampleProjectServiceImpl implements BusinessStandardSampleProjectService {

    @Resource
    private BusinessStandardSampleProjectMapper businessStandardSampleProjectMapper;

    @Override
    public BusinessStandardSampleProjectRespVO createBusinessStandardSampleProject(BusinessStandardSampleProjectSaveReqVO createReqVO) {
        // 插入
        BusinessStandardSampleProjectDO businessStandardSampleProject = BeanUtils.toBean(createReqVO, BusinessStandardSampleProjectDO.class);
        businessStandardSampleProjectMapper.insert(businessStandardSampleProject);
        // 返回
        return BeanUtils.toBean(businessStandardSampleProject, BusinessStandardSampleProjectRespVO.class);
    }

    @Override
    public void updateBusinessStandardSampleProject(BusinessStandardSampleProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessStandardSampleProjectExists(updateReqVO.getId());
        // 更新
        BusinessStandardSampleProjectDO updateObj = BeanUtils.toBean(updateReqVO, BusinessStandardSampleProjectDO.class);
        businessStandardSampleProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessStandardSampleProject(Long id) {
        // 校验存在
        validateBusinessStandardSampleProjectExists(id);
        // 删除
        businessStandardSampleProjectMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessStandardSampleProjectListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessStandardSampleProjectExists(ids);
        // 删除
        businessStandardSampleProjectMapper.deleteByIds(ids);
        }

    private void validateBusinessStandardSampleProjectExists(List<Long> ids) {
        List<BusinessStandardSampleProjectDO> list = businessStandardSampleProjectMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_STANDARD_SAMPLE_PROJECT_NOT_EXISTS);
        }
    }

    private void validateBusinessStandardSampleProjectExists(Long id) {
        if (businessStandardSampleProjectMapper.selectById(id) == null) {
            throw exception(BUSINESS_STANDARD_SAMPLE_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public BusinessStandardSampleProjectDO getBusinessStandardSampleProject(Long id) {
        return businessStandardSampleProjectMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessStandardSampleProjectDO> getBusinessStandardSampleProjectPage(BusinessStandardSampleProjectPageReqVO pageReqVO) {
        return businessStandardSampleProjectMapper.selectPage(pageReqVO);
    }

}