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

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.pdu.flow.FlowSaveXmlPdu;
import com.mortals.xhx.module.basics.web.BasicsForm;
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.datum.model.DatumEntity;
import com.mortals.xhx.module.datum.service.DatumService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * <p>Title: 一件事申请材料</p>
 * <p>Description: DatumController  </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 *
 * @author zxfei
 * @version 1.0.0
 */
@RestController
@RequestMapping("datum")
public class DatumController extends BaseCRUDJsonMappingController<DatumService, DatumForm, DatumEntity, Long> {


    @Autowired
    private ParamService paramService;
    @Autowired
    private UploadService uploadService;

    public DatumController() {
        super.setFormClass(DatumForm.class);
        super.setModuleDesc("一件事申请材料");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, DatumForm form, Map<String, Object> model, Context context) {
        this.addDict(model, "isMust", YesNoEnum.getEnumMap());
        this.addDict(model, "iszzsb", YesNoEnum.getEnumMap());
        this.addDict(model, "isjianm", YesNoEnum.getEnumMap());
        this.addDict(model, "isLack", YesNoEnum.getEnumMap());
        this.addDict(model, "materialProperty", paramService.getParamByFirstOrganize(Constant.Param_materialProperty));
        this.addDict(model, "materialType", paramService.getParamByFirstOrganize(Constant.Param_materialType));
        this.addDict(model, "electronicgs", paramService.getParamByFirstOrganize(Constant.Param_electronicgs));
        this.addDict(model, "materialSource", paramService.getParamByFirstOrganize(Constant.Param_materialSourceSm));
        this.addDict(model, "paperGg", paramService.getParamByFirstOrganize(Constant.Param_paperGg));
        this.addDict(model, "jianmMs", paramService.getParamByFirstOrganize(Constant.Param_jianmMs));
        this.addDict(model, "sealWay", paramService.getParamByFirstOrganize(Constant.Param_sealWay));

    }

    /**
     * 上传附件
     */
    @PostMapping(value = "upload")
    public String upload(@RequestParam(required = false) String prePath,
                         MultipartFile file) {

        JSONObject ret = new JSONObject();
        try {

            String url = uploadService.saveFileUpload(file, prePath, getCurUser());
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_DATA,url);
            ret.put(KEY_RESULT_MSG, "上传附件成功!");
        } 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();
    }

}