package com.mortals.xhx.module.device.web;
import cn.hutool.core.net.url.UrlBuilder;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.ImageReEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.device.model.DeviceEntity;
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.util.ObjectUtils;
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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.device.model.DeviceModuleDistributeEntity;
import com.mortals.xhx.module.device.service.DeviceModuleDistributeService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;
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 static com.mortals.xhx.common.key.Constant.PARAM_SERVER_HTTP_URL;

/**
 *
 * 设备前端模块部署
 *
 * @author zxfei
 * @date 2022-08-02
 */
@RestController
@RequestMapping("device/module/distribute")
public class DeviceModuleDistributeController extends BaseCRUDJsonBodyMappingController<DeviceModuleDistributeService,DeviceModuleDistributeEntity,Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private ProductService productService;

    public DeviceModuleDistributeController(){
        super.setModuleDesc( "设备前端模块部署");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "distribute", YesNoEnum.getEnumMap());
        this.addDict(model, "productId", productService.find(new ProductEntity()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getProductName())));
        this.addDict(model, "imageResolution", ImageReEnum.getEnumMap());
        super.init(model, context);
    }


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


    @Override
    protected int viewAfter(Long id, Map<String, Object> model, DeviceModuleDistributeEntity entity, Context context) throws AppException {
        String homeUrl="";
        ProductEntity productEntity = productService.get(entity.getProductId(), context);
        if(!ObjectUtils.isEmpty(productEntity.getHomeUrl())){
            homeUrl= productEntity.getHomeUrl();
        }

        String domain = GlobalSysInfo.getParamValue(PARAM_SERVER_HTTP_URL, "http://192.168.0.98:11091");
        entity.setFilePath(UrlBuilder.of(domain).addPath(homeUrl).toString());
        return super.viewAfter(id, model, entity, context);
    }
}