package com.hzya.frame.seeyon.service.impl;

import cn.hutool.core.date.DateUtil;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.seeyon.dao.ICtpAttachmentDao;
import com.hzya.frame.seeyon.dao.ICtpFileDao;
import com.hzya.frame.seeyon.entity.CtpAttachmentEntity;
import com.hzya.frame.seeyon.entity.CtpFileEntity;
import com.hzya.frame.seeyon.service.ICtpAttachmentService;
import com.hzya.frame.seeyon.service.ICtpFileService;
import com.hzya.frame.sysnew.comparison.controlsLog.dao.impl.ControlsLogDaoImpl;
import com.hzya.frame.uuid.UUIDLong;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author xiangerlin
 * @Date 2024/6/17 15:31
 **/
@Service(value = "ctpAttachmentService")
public class CtpAttachmentServiceImpl extends BaseService<CtpAttachmentEntity, String> implements ICtpAttachmentService {

    private ICtpAttachmentDao ctpAttachmentDao;
    @Autowired
    private ICtpFileService ctpFileService;

    @Autowired
    private ICtpFileDao ctpFileDao;
    @Autowired
    public void setCtpAttachmentDao(ICtpAttachmentDao dao) {
        this.ctpAttachmentDao = dao;
        this.dao = dao;
    }

    /**
     * 保存附件关系表
     *
     * @param fileUrl        ctp_file id
     * @param col_summary_id col_summary id
     * @param sub_reference  随机uuid
     * @return
     */
    @Override
    public CtpAttachmentEntity saveAttachment(String fileUrl, String col_summary_id, String sub_reference, String dataSourceCode)throws Exception {
        //查一下附件
        CtpFileEntity ctpFileEntity = new CtpFileEntity();
        ctpFileEntity.setId(fileUrl);
        ctpFileEntity.setDataSourceCode(dataSourceCode);
        List<CtpFileEntity> ctpFileList = ctpFileDao.queryFile(ctpFileEntity);
        if (CollectionUtils.isNotEmpty(ctpFileList)){
            CtpFileEntity ctpFile = ctpFileList.get(0);
            if (null != ctpFile){
                CtpAttachmentEntity ctpAttachmentEntity = new CtpAttachmentEntity();
                ctpAttachmentEntity.setDataSourceCode(dataSourceCode);
                ctpAttachmentEntity.setId(String.valueOf(UUIDLong.longUUID()));
                ctpAttachmentEntity.setFile_url(ctpFile.getId());//ctp_file表的id
                ctpAttachmentEntity.setAtt_reference(col_summary_id);//业务表单的id
                ctpAttachmentEntity.setSub_reference(sub_reference);//这个字段要保存到业务表附件到字段上
                ctpAttachmentEntity.setCategory(ctpFile.getCategory());//这里写66 才可以显示图片
                ctpAttachmentEntity.setFilename(ctpFile.getFilename());
                ctpAttachmentEntity.setType(ctpFile.getType());
                ctpAttachmentEntity.setMime_type(ctpFile.getMime_type());
                ctpAttachmentEntity.setAttachment_size(ctpFile.getFile_size());
                ctpAttachmentEntity.setCreatedate(new Date());
                ctpAttachmentDao.saveCtpAttachment(ctpAttachmentEntity);
                return ctpAttachmentEntity;
            }
        }
        return null;
    }
}
