package com.zcloud.basic.info.command;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.basic.info.command.convertor.ImgFilesCoConvertor;
import com.zcloud.basic.info.domain.gateway.ImgFilesGateway;
import com.zcloud.basic.info.domain.model.ImgFilesE;
import com.zcloud.basic.info.dto.ImgFilesAddCmd;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.zcloud.basic.info.dto.clientobject.ImgFilesCO;
import com.zcloud.basic.info.dto.clientobject.ImgFilesInfoCO;
import com.zcloud.basic.info.persistence.dataobject.ImgFilesDO;
import com.zcloud.basic.info.persistence.repository.ImgFilesRepository;
import lombok.AllArgsConstructor;
import org.apache.commons.math3.analysis.function.Sin;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * web-app
 *
 * @Author zhangyue
 * @Date 2025-10-30 16:10:00
 */
@Component
@AllArgsConstructor
public class ImgFilesAddExe {
    private final ImgFilesGateway imgFilesGateway;
    private final ImgFilesCoConvertor imgFilesCoConvertor;
    private final ImgFilesRepository imgFilesRepository;

    @Transactional(rollbackFor = Exception.class)
    public SingleResponse<ImgFilesCO> execute(ImgFilesAddCmd cmd) {
        SSOUser ssoUser = AuthContext.getCurrentUser();
        Long tenantId = ssoUser.getTenantId();
        ImgFilesE imgFilesE = new ImgFilesE();
        BeanUtils.copyProperties(cmd, imgFilesE);
        boolean res = false;
        ImgFilesCO imgFilesCO = new ImgFilesCO();
        try {
            String filePath = imgFilesE.initAdd(tenantId, imgFilesE);
            imgFilesCO.setFilePath(filePath);
            res = true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return SingleResponse.of(imgFilesCO);
    }

    @Transactional(rollbackFor = Exception.class)
    public SingleResponse<ImgFilesInfoCO> batchExecute(ImgFilesAddCmd cmd) {
        SSOUser ssoUser = AuthContext.getCurrentUser();
        Long tenantId = ssoUser.getTenantId();
        ImgFilesE imgFilesE = new ImgFilesE();
        BeanUtils.copyProperties(cmd, imgFilesE);
        boolean res = false;
        ImgFilesInfoCO imgFilesCO = new ImgFilesInfoCO();
        try {
            List<ImgFilesE> imgFilesEList = imgFilesE.initBatchAdd(tenantId, imgFilesE);
            List<ImgFilesDO> imgFilesDOList = imgFilesCoConvertor.converEsToDOs(imgFilesEList);
            imgFilesRepository.saveBatch(imgFilesDOList);
            List<ImgFilesCO> imgFilesCOList = imgFilesCoConvertor.converDOsToCOs(imgFilesDOList);
            imgFilesCO.setFileList(imgFilesCOList);
            imgFilesCO.setForeignKey(imgFilesE.getForeignKey());
            res = true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return SingleResponse.of(imgFilesCO);
    }
}

