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

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

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.BusinessSampleEntrustDepartmentDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustDepartmentMapper;

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 BusinessSampleEntrustDepartmentServiceImpl implements BusinessSampleEntrustDepartmentService {

    @Resource
    private BusinessSampleEntrustDepartmentMapper businessSampleEntrustDepartmentMapper;

    @Override
    public BusinessSampleEntrustDepartmentRespVO createBusinessSampleEntrustDepartment(BusinessSampleEntrustDepartmentSaveReqVO createReqVO) {
        // 插入
        BusinessSampleEntrustDepartmentDO businessSampleEntrustDepartment = BeanUtils.toBean(createReqVO, BusinessSampleEntrustDepartmentDO.class);
        businessSampleEntrustDepartmentMapper.insert(businessSampleEntrustDepartment);
        // 返回
        return BeanUtils.toBean(businessSampleEntrustDepartment, BusinessSampleEntrustDepartmentRespVO.class);
    }

    @Override
    public void updateBusinessSampleEntrustDepartment(BusinessSampleEntrustDepartmentSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleEntrustDepartmentExists(updateReqVO.getId());
        // 更新
        BusinessSampleEntrustDepartmentDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleEntrustDepartmentDO.class);
        businessSampleEntrustDepartmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleEntrustDepartment(Long id) {
        // 校验存在
        validateBusinessSampleEntrustDepartmentExists(id);
        // 删除
        businessSampleEntrustDepartmentMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleEntrustDepartmentListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleEntrustDepartmentExists(ids);
        // 删除
        businessSampleEntrustDepartmentMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleEntrustDepartmentExists(List<Long> ids) {
        List<BusinessSampleEntrustDepartmentDO> list = businessSampleEntrustDepartmentMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_DEPARTMENT_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleEntrustDepartmentExists(Long id) {
        if (businessSampleEntrustDepartmentMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_DEPARTMENT_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleEntrustDepartmentDO getBusinessSampleEntrustDepartment(Long id) {
        return businessSampleEntrustDepartmentMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSampleEntrustDepartmentExtendRespVO> getBusinessSampleEntrustDepartmentPage(BusinessSampleEntrustDepartmentPageReqVO pageReqVO) {
        return businessSampleEntrustDepartmentMapper.selectPage(pageReqVO);
    }

}