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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.controller.admin.recordfileattachment.vo.RecordFileAttachmentPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordfileattachment.vo.RecordFileAttachmentRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordfileattachment.vo.RecordFileAttachmentSaveReqVO;
import com.zt.plat.module.qms.resource.record.dal.dataobject.recordfileattachment.RecordFileAttachmentDO;
import com.zt.plat.module.qms.resource.record.dal.mapper.RecordFileAttachmentMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.io.OutputStream;
import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.RECORD_FILE_ATTACHMENT_NOT_EXISTS;

/**
 * 文件记录附件 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class RecordFileAttachmentServiceImpl implements RecordFileAttachmentService {

    @Resource
    private RecordFileAttachmentMapper recordFileAttachmentMapper;

    @Override
    public RecordFileAttachmentRespVO createRecordFileAttachment(RecordFileAttachmentSaveReqVO createReqVO) {
        // 插入
        RecordFileAttachmentDO recordFileAttachment = BeanUtils.toBean(createReqVO, RecordFileAttachmentDO.class);
        recordFileAttachmentMapper.insert(recordFileAttachment);
        // 返回
        return BeanUtils.toBean(recordFileAttachment, RecordFileAttachmentRespVO.class);
    }

    @Override
    public void updateRecordFileAttachment(RecordFileAttachmentSaveReqVO updateReqVO) {
        // 校验存在
        validateRecordFileAttachmentExists(updateReqVO.getId());
        // 更新
        RecordFileAttachmentDO updateObj = BeanUtils.toBean(updateReqVO, RecordFileAttachmentDO.class);
        recordFileAttachmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecordFileAttachment(Long id) {
        // 校验存在
        validateRecordFileAttachmentExists(id);
        // 删除
        recordFileAttachmentMapper.deleteById(id);
    }

    @Override
        public void deleteRecordFileAttachmentListByIds(List<Long> ids) {
        // 校验存在
        validateRecordFileAttachmentExists(ids);
        // 删除
        recordFileAttachmentMapper.deleteByIds(ids);
        }

    private void validateRecordFileAttachmentExists(List<Long> ids) {
        List<RecordFileAttachmentDO> list = recordFileAttachmentMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RECORD_FILE_ATTACHMENT_NOT_EXISTS);
        }
    }

    private void validateRecordFileAttachmentExists(Long id) {
        if (recordFileAttachmentMapper.selectById(id) == null) {
            throw exception(RECORD_FILE_ATTACHMENT_NOT_EXISTS);
        }
    }

    @Override
    public RecordFileAttachmentDO getRecordFileAttachment(Long id) {
        return recordFileAttachmentMapper.selectById(id);
    }

    @Override
    public PageResult<RecordFileAttachmentDO> getRecordFileAttachmentPage(RecordFileAttachmentPageReqVO pageReqVO) {
        return recordFileAttachmentMapper.selectPage(pageReqVO);
    }

    @Override
    public int createRecordFileAttachmentBatch(List<RecordFileAttachmentSaveReqVO> createAttachmentReqVOList) {
        List<RecordFileAttachmentDO> bean = BeanUtils.toBean(createAttachmentReqVOList, RecordFileAttachmentDO.class);
        recordFileAttachmentMapper.insertBatch(bean);
        return bean.size();
    }

    @Override
    public void deleteRecordFileAttachmentByRecord(Long recordId) {
        LambdaQueryWrapper<RecordFileAttachmentDO> query = new LambdaQueryWrapper<>();
        query.eq(RecordFileAttachmentDO::getRecordId, recordId);
        recordFileAttachmentMapper.delete(query);
    }


}