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

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.mortals.framework.ap.GlobalSysInfo;
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.upload.service.UploadService;
import com.mortals.xhx.common.code.ImageReEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.utils.ZipUtils;
import com.mortals.xhx.module.device.dao.DeviceModuleDistributeDao;
import com.mortals.xhx.module.device.model.DeviceModuleDistributeEntity;
import com.mortals.xhx.module.device.service.DeviceModuleDistributeService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;

/**
 * DeviceModuleDistributeService
 * 设备前端模块部署 service实现
 *
 * @author zxfei
 * @date 2022-08-02
 */
@Service("deviceModuleDistributeService")
public class DeviceModuleDistributeServiceImpl extends AbstractCRUDServiceImpl<DeviceModuleDistributeDao, DeviceModuleDistributeEntity, Long> implements DeviceModuleDistributeService {
    @Autowired
    private ProductService productService;
    @Autowired
    private UploadService uploadService;

    @Override
    protected void saveBefore(DeviceModuleDistributeEntity entity, Context context) throws AppException {
        ProductEntity productEntity = productService.get(entity.getProductId(), context);
        entity.setProductCode(productEntity.getProductCode());
        entity.setProductName(productEntity.getProductName());

        entity.setImageResolutionValue(ImageReEnum.getByValue(Integer.parseInt(entity.getImageResolution())).getDesc());

        super.saveBefore(entity, context);
    }


    @Override
    protected void updateBefore(DeviceModuleDistributeEntity entity, Context context) throws AppException {
        ProductEntity productEntity = productService.get(entity.getProductId(), context);
        entity.setProductCode(productEntity.getProductCode());
        entity.setProductName(productEntity.getProductName());

        entity.setImageResolutionValue(ImageReEnum.getByValue(Integer.parseInt(entity.getImageResolution())).getDesc());

        super.updateBefore(entity, context);
    }

    @Override
    public void active(DeviceModuleDistributeEntity distributeEntity, Context context) {
        //判断文件是否存在

        String targetFilePath = uploadService.getFilePath(distributeEntity.getFilePath());
        if (FileUtil.isEmpty(new File(targetFilePath))) {
            throw new AppException("部署文件不存在！");
        }

        if (!FileUtil.getSuffix(distributeEntity.getFilePath()).equals("zip")) {
            throw new AppException("部署只支持zip文件！");
        }

        //部署路径是否存在 如果不存在 创建目录，
        String path = GlobalSysInfo.getParamValue(Constant.DISTRIBUTE_PATH, "/home/publish/device-new-manager-ui/homeDeviceUrl");

        String code = distributeEntity.getProductCode();

        String imageRe = distributeEntity.getImageResolutionValue();

        String disPath = "/" + path + "/" + code + "/" + imageRe;

        //判断目标目录是否存在 如果存在 这删除
        if (FileUtil.isDirectory(disPath)) {
            FileUtil.del(disPath);
        }

        ZipUtils.unzip(new File(targetFilePath), disPath);

       // ZipUtil.unzip(targetFilePath, disPath);
        //更新
        distributeEntity.setDistributeFilePath(disPath);
        distributeEntity.setDistribute(YesNoEnum.YES.getValue());
        distributeEntity.setUpdateTime(new Date());

        this.update(distributeEntity, context);
    }


    public static void main(String[] args) {
        ZipUtil.unzip("F:\\1664176173348.zip", "F:\\tmp");

    }
}