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

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 BusinessStandardSampleServiceImpl implements BusinessStandardSampleService {

    @Resource
    private BusinessStandardSampleMapper businessStandardSampleMapper;

    @Override
    public BusinessStandardSampleRespVO createBusinessStandardSample(BusinessStandardSampleSaveReqVO createReqVO) {
        // 插入
        BusinessStandardSampleDO businessStandardSample = BeanUtils.toBean(createReqVO, BusinessStandardSampleDO.class);
        businessStandardSampleMapper.insert(businessStandardSample);
        // 返回
        return BeanUtils.toBean(businessStandardSample, BusinessStandardSampleRespVO.class);
    }

    @Override
    public void updateBusinessStandardSample(BusinessStandardSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessStandardSampleExists(updateReqVO.getId());
        // 更新
        BusinessStandardSampleDO updateObj = BeanUtils.toBean(updateReqVO, BusinessStandardSampleDO.class);
        businessStandardSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessStandardSample(Long id) {
        // 校验存在
        validateBusinessStandardSampleExists(id);
        // 删除
        businessStandardSampleMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessStandardSampleListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessStandardSampleExists(ids);
        // 删除
        businessStandardSampleMapper.deleteByIds(ids);
        }

    private void validateBusinessStandardSampleExists(List<Long> ids) {
        List<BusinessStandardSampleDO> list = businessStandardSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_STANDARD_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateBusinessStandardSampleExists(Long id) {
        if (businessStandardSampleMapper.selectById(id) == null) {
            throw exception(BUSINESS_STANDARD_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public BusinessStandardSampleDO getBusinessStandardSample(Long id) {
        return businessStandardSampleMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessStandardSampleDO> getBusinessStandardSamplePage(BusinessStandardSamplePageReqVO pageReqVO) {
        return businessStandardSampleMapper.selectPage(pageReqVO);
    }

//	@Override
//	public List<BusinessStandardSampleExtendRespVO> getBusinessStandardSampleByConfigQCSampleMethodId(Long configQCSampleMethodId) {
//		return businessStandardSampleMapper.selectByConfigQCSampleMethodId(configQCSampleMethodId);
//	}

	@Override
	public List<BusinessStandardSampleExtendRespVO> getBusinessStandardSampleLeafList() {
		return businessStandardSampleMapper.getBusinessStandardSampleLeafList();
	}

}