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

import com.mortals.framework.common.Rest;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.AppTypeEnum;
import com.mortals.xhx.common.code.DistributeEnum;
import com.mortals.xhx.module.app.model.AppPublishEntity;
import com.mortals.xhx.module.app.service.AppPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 应用发布部署
 *
 * @author zxfei
 * @date 2023-02-01
 */
@RestController
@RequestMapping("app/publish")
public class AppPublishController extends BaseCRUDJsonBodyMappingController<AppPublishService, AppPublishEntity, Long> {

    @Autowired
    private ParamService paramService;

    public AppPublishController() {
        super.setModuleDesc("应用发布部署");
    }


    /**
     * 部署
     */
    @PostMapping(value = "active")
    public Rest<Void> deviceActive(@RequestBody AppPublishEntity appPublishEntity) {
        String busiDesc = this.getModuleDesc() + "应用部署";
        Rest<Void> rest = Rest.ok(busiDesc + " 【成功】");
        try {
            this.service.active(appPublishEntity, getContext());
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error("应用部署", e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "appType", AppTypeEnum.getEnumMap());
        this.addDict(model, "distribute", DistributeEnum.getEnumMap());
        this.addDict(model, "appPublish", paramService.getParamByFirstOrganize("appPublish"));
        super.init(model, context);
    }


}