package com.zcloud.basic.info.domain.model;

import com.jjb.saas.framework.domain.model.BaseE;
import com.zcloud.basic.info.domain.utils.ImageCompressUtil;
import com.zcloud.basic.info.domain.utils.Smb;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.gbscommon.utils.Tools;
import lombok.Data;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * web-domain
 *
 * @Author zhangyue
 * @Date 2025-10-30 16:10:06
 */
@Data
public class ImgFilesE extends BaseE {
    //主键
    private Long id;
    //业务主键id
    private String imgFilesId;
    //路径
    private String filePath;
    //类型,参考 com.zcloud.imgfiles.enums.filetype
    private Integer type;
    //外键
    private String foreignKey;
    //附件名称
    private String fileName;
    //企业id
    private Long corpinfoId;
    // 文件存储路径
    private String path;

    private MultipartFile[] files;

    private String md5;
    //乐观锁
    private Integer version;
    //创建人
    private Long createId;
    //创建人姓名
    private String createName;
    //创建时间
    private LocalDateTime createTime;
    //更新人
    private Long updateId;
    //修改人名称
    private String updateName;
    //更新时间
    private LocalDateTime updateTime;
    //描述
    private String remarks;
    //是否删除
    private String deleteEnum;
    //租户id
    private Long tenantId;
    //机构id
    private Long orgId;
    //环境
    private String env;

    private String daHuaFilePath;

    private Map<String, MultipartFile> filesMap;


    public String initAdd(Long tenantId, ImgFilesE baseImgFilesE) throws Exception {

        MultipartFile[] files = baseImgFilesE.getFiles();
        Long corpinfoId = ObjectUtils.isEmpty(baseImgFilesE.getCorpinfoId()) ? tenantId : baseImgFilesE.getCorpinfoId();
        if (baseImgFilesE.getType() == 608 || baseImgFilesE.getType() == 13){
            String resultFilePath = Smb.saveDaHuaFile(files[0], baseImgFilesE.getDaHuaFilePath());
            return  resultFilePath;
        } else {
            String filePath = corpinfoId.toString() + "/" + DateUtil.getMonth() + "/" + baseImgFilesE.getPath();
            // 文件上传并获取上传路径
            String resultFilePath = Smb.saveFile(files[0], filePath);
            return  resultFilePath;
        }

    }
    public String initAdd(Integer dockFlag, Long tenantId, ImgFilesE baseImgFilesE) throws Exception {

        MultipartFile[] files = baseImgFilesE.getFiles();
        Long corpinfoId = ObjectUtils.isEmpty(baseImgFilesE.getCorpinfoId()) ? tenantId : baseImgFilesE.getCorpinfoId();
        if (dockFlag == 1 && (baseImgFilesE.getType() == 608 || baseImgFilesE.getType() == 13)){
            MultipartFile targetFile = ImageCompressUtil.compressToJpg(files[0]);
            String resultFilePath = Smb.saveDaHuaFile(targetFile, baseImgFilesE.getDaHuaFilePath());
            return  resultFilePath;
        } else {
            String filePath = corpinfoId.toString() + "/" + DateUtil.getMonth() + "/" + baseImgFilesE.getPath();
            // 文件上传并获取上传路径
            String resultFilePath = Smb.saveFile(files[0], filePath);
            return  resultFilePath;
        }

    }

    public List<ImgFilesE> initBatchAdd(Long tenantId, ImgFilesE baseImgFilesE) throws Exception {
        MultipartFile[] files = baseImgFilesE.getFiles();
        List<ImgFilesE> imgFilesList = new ArrayList<ImgFilesE>();
        String foreignKey = ObjectUtils.isEmpty(baseImgFilesE.getForeignKey()) ? Tools.get32UUID() : baseImgFilesE.getForeignKey();
        Long corpinfoId = ObjectUtils.isEmpty(baseImgFilesE.getCorpinfoId()) ? tenantId : baseImgFilesE.getCorpinfoId();
        String filePath = corpinfoId.toString() + "/" + DateUtil.getMonth() + "/" + baseImgFilesE.getPath();

        for (MultipartFile file : files) {
            ImgFilesE imgFile = new ImgFilesE();
            BeanUtils.copyProperties(imgFile, baseImgFilesE);
            // 文件上传并获取上传路径
            String resultFilePath = Smb.saveFile(file, filePath);
            imgFile.setImgFilesId(Tools.get32UUID());
            imgFile.setFilePath(resultFilePath);
            imgFile.setType(baseImgFilesE.getType());
            imgFile.setForeignKey(foreignKey);
            imgFile.setFileName(file.getOriginalFilename());
            imgFile.setCorpinfoId(corpinfoId);
//            imgFile.setMd5(FileUtil.getMD5(file));
            imgFilesList.add(imgFile);
        }
        baseImgFilesE.setForeignKey(foreignKey);
        return imgFilesList;
    }

    public List<ImgFilesE> initDaHuaBatchAdd( Long tenantId, ImgFilesE baseImgFilesE) throws Exception {
        MultipartFile[] files = baseImgFilesE.getFiles();
        List<ImgFilesE> imgFilesList = new ArrayList<ImgFilesE>();
        String foreignKey = ObjectUtils.isEmpty(baseImgFilesE.getForeignKey()) ? Tools.get32UUID() : baseImgFilesE.getForeignKey();

        Long corpinfoId = ObjectUtils.isEmpty(baseImgFilesE.getCorpinfoId()) ? tenantId : baseImgFilesE.getCorpinfoId();
        Map<String, MultipartFile> filesMap = baseImgFilesE.getFilesMap();
        for (Map.Entry<String, MultipartFile> entry : filesMap.entrySet()) {
            ImgFilesE imgFile = new ImgFilesE();
            BeanUtils.copyProperties(imgFile, baseImgFilesE);
            // 生成文件名
            int lastSlash = entry.getKey().lastIndexOf("/");
//            String dirPath = entry.getKey().substring(0, lastSlash + 1);
            String fileName = entry.getKey().substring(lastSlash + 1);
            MultipartFile targetFile = ImageCompressUtil.compressToJpg(entry.getValue());
            // 文件上传并获取上传路径
            String resultFilePath = Smb.saveDaHuaFile(targetFile, entry.getKey());
            imgFile.setImgFilesId(Tools.get32UUID());
            imgFile.setFilePath(resultFilePath);
            imgFile.setType(baseImgFilesE.getType());
            imgFile.setForeignKey(foreignKey);
            imgFile.setFileName(fileName);
            imgFile.setCorpinfoId(corpinfoId);
//            imgFile.setMd5(FileUtil.getMD5(file));
            imgFilesList.add(imgFile);

        }
        baseImgFilesE.setForeignKey(foreignKey);
        return imgFilesList;
    }


    public void deleteFile(String filePath) throws Exception {
        Smb.deleteFile(filePath);
    }

    public void deleteFileList(List<ImgFilesE> imgFilesEList) throws Exception {
        for (ImgFilesE imgFilesE : imgFilesEList) {
            Smb.deleteFile(imgFilesE.getFilePath());
        }
    }
}

