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.BusinessSampleEntrustProjectPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustProjectRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustProjectSaveReqVO;
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.BusinessSampleEntrustProjectDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustProjectMapper;

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 BusinessSampleEntrustProjectServiceImpl implements BusinessSampleEntrustProjectService {

    @Resource
    private BusinessSampleEntrustProjectMapper businessSampleEntrustProjectMapper;

    @Override
    public BusinessSampleEntrustProjectRespVO createBusinessSampleEntrustProject(BusinessSampleEntrustProjectSaveReqVO createReqVO) {
        // 插入
        BusinessSampleEntrustProjectDO businessSampleEntrustProject = BeanUtils.toBean(createReqVO, BusinessSampleEntrustProjectDO.class);
        businessSampleEntrustProjectMapper.insert(businessSampleEntrustProject);
        // 返回
        return BeanUtils.toBean(businessSampleEntrustProject, BusinessSampleEntrustProjectRespVO.class);
    }

    @Override
    public void updateBusinessSampleEntrustProject(BusinessSampleEntrustProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleEntrustProjectExists(updateReqVO.getId());
        // 更新
        BusinessSampleEntrustProjectDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleEntrustProjectDO.class);
        businessSampleEntrustProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleEntrustProject(Long id) {
        // 校验存在
        validateBusinessSampleEntrustProjectExists(id);
        // 删除
        businessSampleEntrustProjectMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleEntrustProjectListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleEntrustProjectExists(ids);
        // 删除
        businessSampleEntrustProjectMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleEntrustProjectExists(List<Long> ids) {
        List<BusinessSampleEntrustProjectDO> list = businessSampleEntrustProjectMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_PROJECT_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleEntrustProjectExists(Long id) {
        if (businessSampleEntrustProjectMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleEntrustProjectDO getBusinessSampleEntrustProject(Long id) {
        return businessSampleEntrustProjectMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSampleEntrustProjectDO> getBusinessSampleEntrustProjectPage(BusinessSampleEntrustProjectPageReqVO pageReqVO) {
        return businessSampleEntrustProjectMapper.selectPage(pageReqVO);
    }

}