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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.xhx.common.code.PlatformStatus;
import com.mortals.xhx.common.pdu.flow.FlowSaveXmlPdu;
import com.mortals.xhx.module.platform.model.PlatformEntity;
import com.mortals.xhx.module.platform.service.PlatformService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.module.platform.model.PlatformModelinfoEntity;
import com.mortals.xhx.module.platform.service.PlatformModelinfoService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
* <p>Title: 平台用户模型关系表</p>
* <p>Description: PlatformModelinfoController  </p>
* <p>Copyright: Copyright &reg;  </p>
* <p>Company: </p>
* @author zxfei
* @version 1.0.0
*/
@RestController
@RequestMapping("platform/modelinfo")
public class PlatformModelinfoController extends BaseCRUDJsonMappingController<PlatformModelinfoService,PlatformModelinfoForm,PlatformModelinfoEntity,Long> {

    @Autowired
    private PlatformService platformService;


    @Override
    protected void doListBefore(HttpServletRequest request, HttpServletResponse response, PlatformModelinfoForm form, Map<String, Object> model, Context context) throws AppException {

        form.getQuery().setOrderColList(Arrays.asList(new OrderCol("createTime",OrderCol.DESCENDING)));
        super.doListBefore(request, response, form, model, context);
    }

    public PlatformModelinfoController(){
        super.setFormClass(PlatformModelinfoForm.class);
        super.setModuleDesc("平台用户模型关系表");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, PlatformModelinfoForm form, Map<String, Object> model, Context context) {
        this.addDict(model, "status", IBaseEnum.getEnumMap(PlatformStatus.class));

        super.init(request, response, form, model, context);
    }


    /**
     * 流程部署XML
     */
    @PostMapping(value = "deployFlowByXml")
    public String deployFlowByXml(@ApiParam(value = "流程定义类") @RequestParam Long id) {
        JSONObject ret = new JSONObject();
        try {
            String definId = this.getService().deployFlowByXml(id, getContext());


            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_MSG, "模型部署成功，definitionId:" + definId);
        } catch (Exception e) {
            log.error("模型部署异常", e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return ret.toJSONString();
    }


    /**
     * 导入附件
     */
    @PostMapping(value = "upload")
    public String upload(@RequestParam(required = false) Long id,
                         MultipartFile file) {

        JSONObject ret = new JSONObject();
        try {
            String xml = IoUtil.readUtf8(file.getInputStream());
            PlatformEntity entity = platformService.get(id, getContext());
            PlatformModelinfoEntity modelinfoEntity = new PlatformModelinfoEntity();
            modelinfoEntity.setBpmnXml(xml);
            modelinfoEntity.setPlatformSn(entity.getPlatformSn());
            modelinfoEntity.setCreateUserId(getContext().getUser().getId());
            modelinfoEntity.setCreateUser(getContext().getUser().getLoginName());
            modelinfoEntity.setCreateTime(new Date());

            this.service.save(modelinfoEntity);
        } catch (Exception e) {
            log.error("导入文件异常", e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return ret.toJSONString();
    }

}