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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.pdu.flow.FlowViewerPdu;
import com.mortals.xhx.module.yth.model.BusinessInfo;
import io.swagger.annotations.ApiParam;
import org.springframework.util.ObjectUtils;
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.yth.model.YthOnethingEntity;
import com.mortals.xhx.module.yth.service.YthOnethingService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * <p>Title: 一件事提交基础表</p>
 * <p>Description: YthOnethingController  </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 *
 * @author zxfei
 * @version 1.0.0
 */
@RestController
@RequestMapping("yth/onething")
public class YthOnethingController extends BaseCRUDJsonMappingController<YthOnethingService, YthOnethingForm, YthOnethingEntity, Long> {

    public YthOnethingController() {
        super.setFormClass(YthOnethingForm.class);
        super.setModuleDesc("一件事提交基础表");
    }

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

        form.getQuery().setOrderColList(new ArrayList<OrderCol>() {
            {
                add(new OrderCol("createTime", OrderCol.DESCENDING));
            }
        });
        super.doListBefore(request, response, form, model, context);
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, YthOnethingForm form, Map<String, Object> model, Context context) {
        this.addDict(model, "isRevoke", YesNoEnum.getEnumMap());
        this.addDict(model, "status", ApproveStateEnum.getEnumMap());
        this.addDict(model, "manner", MannerEnum.getEnumMap());
        this.addDict(model, "isAgent", AgentEnum.getEnumMap());

    }


    /**
     * 获取业务详细信息
     */
    @PostMapping(value = "getBusinessDetail")
    public String getBusinessById(@ApiParam(value = "流程Id") @RequestParam(value = "id") Long  id) {
        log.info("receive getBusinessDetail,id:" + id);
        JSONObject ret = new JSONObject();
        try {
            BusinessInfo businessDetail = this.service.getBusinessDetail(id);
            ret.put(KEY_RESULT_DATA, businessDetail);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
        } 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();
    }


}