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.BusinessHandoverRecordSubExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessHandoverRecordSubPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessHandoverRecordSubRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessHandoverRecordSubSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
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.BusinessHandoverRecordSubDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessHandoverRecordSubMapper;

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 BusinessHandoverRecordSubServiceImpl implements BusinessHandoverRecordSubService {

    @Resource
    private BusinessHandoverRecordSubMapper businessHandoverRecordSubMapper;

    @Override
    public BusinessHandoverRecordSubRespVO createBusinessHandoverRecordSub(BusinessHandoverRecordSubSaveReqVO createReqVO) {
        // 插入
        BusinessHandoverRecordSubDO businessHandoverRecordSub = BeanUtils.toBean(createReqVO, BusinessHandoverRecordSubDO.class);
        businessHandoverRecordSubMapper.insert(businessHandoverRecordSub);
        // 返回
        return BeanUtils.toBean(businessHandoverRecordSub, BusinessHandoverRecordSubRespVO.class);
    }

    @Override
    public void updateBusinessHandoverRecordSub(BusinessHandoverRecordSubSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessHandoverRecordSubExists(updateReqVO.getId());
        // 更新
        BusinessHandoverRecordSubDO updateObj = BeanUtils.toBean(updateReqVO, BusinessHandoverRecordSubDO.class);
        businessHandoverRecordSubMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessHandoverRecordSub(Long id) {
        // 校验存在
        validateBusinessHandoverRecordSubExists(id);
        // 删除
        businessHandoverRecordSubMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessHandoverRecordSubListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessHandoverRecordSubExists(ids);
        // 删除
        businessHandoverRecordSubMapper.deleteByIds(ids);
        }

    private void validateBusinessHandoverRecordSubExists(List<Long> ids) {
        List<BusinessHandoverRecordSubDO> list = businessHandoverRecordSubMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_HANDOVER_RECORD_SUB_NOT_EXISTS);
        }
    }

    private void validateBusinessHandoverRecordSubExists(Long id) {
        if (businessHandoverRecordSubMapper.selectById(id) == null) {
            throw exception(BUSINESS_HANDOVER_RECORD_SUB_NOT_EXISTS);
        }
    }

    @Override
    public BusinessHandoverRecordSubDO getBusinessHandoverRecordSub(Long id) {
        return businessHandoverRecordSubMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessHandoverRecordSubExtendRespVO> getBusinessHandoverRecordSubPage(BusinessHandoverRecordSubPageReqVO pageReqVO) {
        return businessHandoverRecordSubMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<BusinessHandoverRecordSubDO> list) {
        businessHandoverRecordSubMapper.insertBatch(list);
    }
}