package com.mortals.xhx.module.app.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.code.AppTypeEnum;
import com.mortals.xhx.module.app.dao.AppPublishDao;
import com.mortals.xhx.module.app.model.AppPublishEntity;
import com.mortals.xhx.module.app.service.AppPublishService;
import com.mortals.xhx.utils.EncodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * AppPublishService
 * 应用发布部署 service实现
 *
 * @author zxfei
 * @date 2023-02-01
 */
@Service("appPublishService")
public class AppPublishServiceImpl extends AbstractCRUDServiceImpl<AppPublishDao, AppPublishEntity, Long> implements AppPublishService {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private ParamService paramService;

    @Override
    protected void saveBefore(AppPublishEntity entity, Context context) throws AppException {
        String targetPath = "/home/mortals/apps/";
        String fileNewName = entity.getAppCode() + "-" + entity.getVersion();

        //将zip包移动到指定目录 并更改名称
        String realFilePath = uploadService.getFilePath(entity.getFilePath());
        if (FileUtil.isEmpty(new File(realFilePath))) {
            throw new AppException("部署文件不存在！");
        }
        if (!FileUtil.getSuffix(entity.getFilePath()).equals("zip")) {
            throw new AppException("部署只支持zip文件！");
        }
        //移动文件并重命名
        FileUtil.move(new File(realFilePath), new File(targetPath + fileNewName), true);
        Map<String, String> appPublish = paramService.getParamByFirstOrganize("appPublish");

        String appCode = appPublish.get(entity.getAppName());
        entity.setAppCode(appCode);
        super.saveBefore(entity, context);
    }


    @Override
    protected void updateBefore(AppPublishEntity entity, Context context) throws AppException {
        super.updateBefore(entity, context);
        AppPublishEntity appPublishEntity = this.get(entity.getId());
        if (entity.getFilePath().equals(appPublishEntity.getFilePath())) {
            String targetPath = "/home/mortals/apps/";
            String fileNewName = entity.getAppCode() + "-" + entity.getVersion();
            //将zip包移动到指定目录 并更改名称
            String realFilePath = uploadService.getFilePath(entity.getFilePath());
            if (FileUtil.isEmpty(new File(realFilePath))) {
                throw new AppException("部署文件不存在！");
            }
            if (!FileUtil.getSuffix(entity.getFilePath()).equals("zip")) {
                throw new AppException("部署只支持zip文件！");
            }
            //移动文件并重命名
            FileUtil.move(new File(realFilePath), new File(targetPath + fileNewName), true);
            Map<String, String> appPublish = paramService.getParamByFirstOrganize("appPublish");

            String appCode = appPublish.get(entity.getAppName());
            entity.setAppCode(appCode);
        }


    }

    @Override
    public void active(AppPublishEntity appPublishEntity, Context context) {
        if (AppTypeEnum.前端.getValue() == appPublishEntity.getAppType()) {
            String targetFilePath = uploadService.getFilePath(appPublishEntity.getFilePath());
            String disPath = "/home/publish/" + appPublishEntity.getAppCode();
            boolean directory = FileUtil.isDirectory(new File(disPath));
            if (directory) {
                FileUtil.del(new File(disPath));
            }
            String fileEncode = "UTF-8";
            try {
                fileEncode = EncodeUtil.getEncode(targetFilePath, true);
            } catch (Exception e) {
                log.error("异常", e);
            }
            ZipUtil.unzip(targetFilePath, disPath, Charset.forName(fileEncode));
        } else {
//后端部署 执行sh 脚本程序部署
        }


    }
}