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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationSaveReqVO;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
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.BusinessSampleEntrustRegistrationDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustRegistrationMapper;

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 BusinessSampleEntrustRegistrationServiceImpl implements BusinessSampleEntrustRegistrationService {

    @Resource
    private BusinessSampleEntrustRegistrationMapper businessSampleEntrustRegistrationMapper;

    @Override
    public BusinessSampleEntrustRegistrationRespVO createBusinessSampleEntrustRegistration(BusinessSampleEntrustRegistrationSaveReqVO createReqVO) {
        // 插入
        BusinessSampleEntrustRegistrationDO businessSampleEntrustRegistration = BeanUtils.toBean(createReqVO, BusinessSampleEntrustRegistrationDO.class);
        businessSampleEntrustRegistrationMapper.insert(businessSampleEntrustRegistration);
        // 返回
        return BeanUtils.toBean(businessSampleEntrustRegistration, BusinessSampleEntrustRegistrationRespVO.class);
    }

    @Override
    public void updateBusinessSampleEntrustRegistration(BusinessSampleEntrustRegistrationSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleEntrustRegistrationExists(updateReqVO.getId());
        // 更新
        BusinessSampleEntrustRegistrationDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleEntrustRegistrationDO.class);
        businessSampleEntrustRegistrationMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleEntrustRegistration(Long id) {
        // 校验存在
        validateBusinessSampleEntrustRegistrationExists(id);
        // 删除
        businessSampleEntrustRegistrationMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleEntrustRegistrationListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleEntrustRegistrationExists(ids);
        // 删除
        businessSampleEntrustRegistrationMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleEntrustRegistrationExists(List<Long> ids) {
        List<BusinessSampleEntrustRegistrationDO> list = businessSampleEntrustRegistrationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_REGISTRATION_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleEntrustRegistrationExists(Long id) {
        if (businessSampleEntrustRegistrationMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_REGISTRATION_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleEntrustRegistrationDO getBusinessSampleEntrustRegistration(Long id) {
        return businessSampleEntrustRegistrationMapper.selectById(id);
    }

    @Override
    public List<BusinessSampleEntrustRegistrationDO> listByIds(List<Long> ids) {
        return businessSampleEntrustRegistrationMapper.selectByIds(ids);
    }

    @Override
    public PageResult<BusinessSampleEntrustRegistrationExtendRespVO> getBusinessSampleEntrustRegistrationPage(BusinessSampleEntrustRegistrationPageReqVO pageReqVO) {
        return businessSampleEntrustRegistrationMapper.selectPage(pageReqVO);
    }

    //查询待编制数据
    @Override
    public PageResult<BusinessSampleEntrustRegistrationExtendRespVO> queryWaitingDataForReport(BusinessSampleEntrustRegistrationPageReqVO pageReqVO) {
        IPage<BusinessSampleEntrustRegistrationDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        pageReqVO.setAssayStatus(QmsCommonConstant.CHECKED);
        Page<BusinessSampleEntrustRegistrationDO> pageList = businessSampleEntrustRegistrationMapper.queryWaitingDataForReport(page, pageReqVO);
        PageResult<BusinessSampleEntrustRegistrationDO> pageResult = new PageResult<>(pageList.getRecords(), pageList.getTotal());
        return BeanUtils.toBean(pageResult, BusinessSampleEntrustRegistrationExtendRespVO.class);
    }
}