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.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.busiz.req.DefinitionReq;
import com.mortals.xhx.common.code.PlatformStatus;
import com.mortals.xhx.common.pdu.api.ApiRespPdu;
import com.mortals.xhx.common.pdu.flow.FlowSaveXmlPdu;
import com.mortals.xhx.feign.flowable.IApiFlowDefinitionFeign;
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.*;
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 BaseCRUDJsonBodyMappingController<PlatformModelinfoService,PlatformModelinfoEntity,Long> {

    @Autowired
    private PlatformService platformService;

    @Autowired
    private IApiFlowDefinitionFeign apiFlowDefinitionFeign;



    @Override
    protected void doListBefore(PlatformModelinfoEntity query, Map<String, Object> model, Context context) throws AppException {

        query.setOrderColList(Arrays.asList(new OrderCol("createTime",OrderCol.DESCENDING)));

        super.doListBefore(query, model, context);
    }

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

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

    @Override
    protected int doListAfter(PlatformModelinfoEntity query, Map<String, Object> model, Context context) throws AppException {
        model.put("result",model.get("data"));
        return super.doListAfter(query, 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("deploy/delete")
    public ApiRespPdu delete(@RequestParam String deployId) {

        ApiRespPdu<String> respPdu = new ApiRespPdu<>();
        try {
            respPdu = apiFlowDefinitionFeign.delete(deployId);
        } catch (Exception e) {
            log.error("error", e);
        }
        return respPdu;
    }



    /**
     * 导入附件
     */
    @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();
    }

}