package com.zt.plat.module.qms.resource.record.service.recordapplydetail;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailSaveReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailJoinVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.resource.record.dal.dataobject.recordapplydetail.RecordApplyDetailDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.resource.record.dal.mapper.RecordApplyDetailMapper;

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.module.qms.enums.ErrorCodeConstants.*;

/**
 * 文件记录通用申请明细 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class RecordApplyDetailServiceImpl implements RecordApplyDetailService {

    @Resource
    private RecordApplyDetailMapper recordApplyDetailMapper;

    @Override
    public RecordApplyDetailRespVO createRecordApplyDetail(RecordApplyDetailSaveReqVO createReqVO) {
        // 插入
        RecordApplyDetailDO recordApplyDetail = BeanUtils.toBean(createReqVO, RecordApplyDetailDO.class);
        recordApplyDetailMapper.insert(recordApplyDetail);
        // 返回
        return BeanUtils.toBean(recordApplyDetail, RecordApplyDetailRespVO.class);
    }

    @Override
    public void updateRecordApplyDetail(RecordApplyDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateRecordApplyDetailExists(updateReqVO.getId());
        // 更新
        RecordApplyDetailDO updateObj = BeanUtils.toBean(updateReqVO, RecordApplyDetailDO.class);
        recordApplyDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecordApplyDetail(Long id) {
        // 校验存在
        validateRecordApplyDetailExists(id);
        // 删除
        recordApplyDetailMapper.deleteById(id);
    }

    @Override
        public void deleteRecordApplyDetailListByIds(List<Long> ids) {
        // 校验存在
        validateRecordApplyDetailExists(ids);
        // 删除
        recordApplyDetailMapper.deleteByIds(ids);
        }

    private void validateRecordApplyDetailExists(List<Long> ids) {
        List<RecordApplyDetailDO> list = recordApplyDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RECORD_APPLY_DETAIL_NOT_EXISTS);
        }
    }

    private void validateRecordApplyDetailExists(Long id) {
        if (recordApplyDetailMapper.selectById(id) == null) {
            throw exception(RECORD_APPLY_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public RecordApplyDetailDO getRecordApplyDetail(Long id) {
        return recordApplyDetailMapper.selectById(id);
    }

    @Override
    public PageResult<RecordApplyDetailDO> getRecordApplyDetailPage(RecordApplyDetailPageReqVO pageReqVO) {
        return recordApplyDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RecordApplyDetailDO> selectList(RecordApplyDetailPageReqVO pageReqVO) {
        PageResult<RecordApplyDetailDO> pageResult = recordApplyDetailMapper.selectPage(pageReqVO);
        return pageResult.getList();
    }

    @Override
    public List<RecordApplyDetailJoinVO> getRecordApplyDetailJoinList(Long documentId, String businessType) {
        return recordApplyDetailMapper.selectJoinByDocumentIdAndBusinessType(documentId, businessType);
    }

    @Override
    public boolean createRecordApplyDetailBatch(List<RecordApplyDetailDO> detailSaveReqVOList) {
        return recordApplyDetailMapper.insertBatch(detailSaveReqVOList);
    }

    @Override
    public List<RecordApplyDetailDO> selectAllList(Long applyId) {
        return recordApplyDetailMapper.selectList(RecordApplyDetailDO::getApplyId, applyId);
    }

}