package com.mortals.xhx.module.license.web;

import cn.hutool.core.util.ZipUtil;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.utils.LicenseUtil;
import com.mortals.xhx.module.license.model.LicenseQuery;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.license.model.LicenseEntity;
import com.mortals.xhx.module.license.service.LicenseService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.io.File;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

import com.mortals.xhx.common.code.*;

/**
 * 许可证
 *
 * @author zxfei
 * @date 2024-07-05
 */
@RestController
@RequestMapping("license")
public class LicenseController extends BaseCRUDJsonBodyMappingController<LicenseService, LicenseEntity, Long> {




    @Resource
    private UploadService uploadService;

    public LicenseController() {
        super.setModuleDesc("许可证");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "licenseStatus", LicenseStatusEnum.getEnumMap());
        super.init(model, context);
    }


    @PostMapping("/download")
    public void licenseDownload(@RequestBody LicenseQuery query, HttpServletResponse response) {

        LicenseEntity licenseEntity = this.service.get(query.getId());
        LicenseUtil.setLicense(licenseEntity.getLicensePriPath(), licenseEntity.getLicenseKeyPath()
                , licenseEntity.getLicensePubPath(), licenseEntity.getStartTime().getTime()
                , licenseEntity.getEndTime().getTime(), licenseEntity.getLicenseCode());

        //将pub与key文件打包下载\

        File appendKeyFile = new File(licenseEntity.getLicenseKeyPath());
        File appendPubFile = new File(licenseEntity.getLicensePubPath());

        String downloadFilePath = "license.zip";
        File zipFile = new File(uploadService.getFilePath(downloadFilePath));
        //CopyOption copyOption = new CopyOption();
        ZipUtil.append(zipFile.toPath(), appendKeyFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        ZipUtil.append(zipFile.toPath(), appendPubFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

        uploadService.uploadDownload(downloadFilePath, response);

    }


}