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 org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.infra.api.file.FileApi;
import com.zt.plat.module.infra.api.file.dto.FileRespDTO;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustFileDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustFileMapper;

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 BusinessSampleEntrustFileServiceImpl implements BusinessSampleEntrustFileService {
	
	@Resource
	private FileApi fileApi;

    @Resource
    private BusinessSampleEntrustFileMapper businessSampleEntrustFileMapper;

    @Override
    public BusinessSampleEntrustFileRespVO createBusinessSampleEntrustFile(BusinessSampleEntrustFileSaveReqVO createReqVO) {
        // 插入
        BusinessSampleEntrustFileDO businessSampleEntrustFile = BeanUtils.toBean(createReqVO, BusinessSampleEntrustFileDO.class);
        businessSampleEntrustFileMapper.insert(businessSampleEntrustFile);
        // 返回
        return BeanUtils.toBean(businessSampleEntrustFile, BusinessSampleEntrustFileRespVO.class);
    }

    @Override
    public void updateBusinessSampleEntrustFile(BusinessSampleEntrustFileSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleEntrustFileExists(updateReqVO.getId());
        // 更新
        BusinessSampleEntrustFileDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleEntrustFileDO.class);
        businessSampleEntrustFileMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleEntrustFile(Long id) {
        // 校验存在
        validateBusinessSampleEntrustFileExists(id);
        // 删除
        businessSampleEntrustFileMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleEntrustFileListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleEntrustFileExists(ids);
        // 删除
        businessSampleEntrustFileMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleEntrustFileExists(List<Long> ids) {
        List<BusinessSampleEntrustFileDO> list = businessSampleEntrustFileMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_FILE_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleEntrustFileExists(Long id) {
        if (businessSampleEntrustFileMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_ENTRUST_FILE_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleEntrustFileDO getBusinessSampleEntrustFile(Long id) {
        return businessSampleEntrustFileMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSampleEntrustFileExtendRespVO> getBusinessSampleEntrustFilePage(BusinessSampleEntrustFilePageReqVO pageReqVO) {
        PageResult<BusinessSampleEntrustFileExtendRespVO> pageResult = businessSampleEntrustFileMapper.selectPage(pageReqVO);
        List<BusinessSampleEntrustFileExtendRespVO> list = pageResult.getList();
		for (BusinessSampleEntrustFileExtendRespVO businessSampleEntrustFileExtendRespVO : list) {
			CommonResult<FileRespDTO> fileInfoResult = fileApi.getFileInfo(businessSampleEntrustFileExtendRespVO.getFileId());
			FileRespDTO fileRespDTO = fileInfoResult.getCheckedData();
			if (fileInfoResult.isSuccess() && fileRespDTO != null) {
				businessSampleEntrustFileExtendRespVO.setUrl(fileRespDTO.getUrl());
				businessSampleEntrustFileExtendRespVO.setPreviewUrl(fileRespDTO.getPreviewUrl());
			}
		}
        return pageResult;
    }

	@Override
	public List<BusinessSampleEntrustFileExtendRespVO> getBusinessSampleEntrustFileList(BusinessSampleEntrustFilePageReqVO reqVO) {
		List<BusinessSampleEntrustFileExtendRespVO> list = businessSampleEntrustFileMapper.selectList(reqVO);
		for (BusinessSampleEntrustFileExtendRespVO businessSampleEntrustFileExtendRespVO : list) {
			CommonResult<FileRespDTO> fileInfoResult = fileApi.getFileInfo(businessSampleEntrustFileExtendRespVO.getFileId());
			FileRespDTO fileRespDTO = fileInfoResult.getCheckedData();
			if (fileInfoResult.isSuccess() && fileRespDTO != null) {
				businessSampleEntrustFileExtendRespVO.setUrl(fileRespDTO.getUrl());
				businessSampleEntrustFileExtendRespVO.setPreviewUrl(fileRespDTO.getPreviewUrl());
			}
		}
		return list;
	}

}