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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonController;
import com.mortals.framework.web.BaseCRUDJsonPhpController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.model.model.ModelEntity;
import com.mortals.xhx.module.model.model.ModelQuery;
import com.mortals.xhx.module.model.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 模块
 *
 * @author zxfei
 * @date 2022-01-17
 */
@RestController
@RequestMapping("model")
public class ModelController extends BaseCRUDJsonPhpController<ModelService, ModelForm, ModelEntity, Long> {

    @Autowired
    private ParamService paramService;

    public ModelController() {
        super.setFormClass(ModelForm.class);
        super.setModuleDesc("模块");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, ModelForm form, Map<String, Object> model, Context context) {
        super.init(request, response, form, model, context);
    }

    /**
     * 查询模块
     */
    @PostMapping(value = "list")
    public String list(@RequestBody ModelQuery query) {
        try {
            ModelForm form = new ModelForm();
            form.setQuery(query);
            return super.list(form);
        } catch (Exception e) {
            log.error("查询模块列表错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
     * 编辑模块
     */
    @GetMapping(value = "edit")
    public String edit(Long id) {
        try {
            ModelForm form = new ModelForm();
            form.setId(new Long[]{id});
            return super.edit(form);
        } catch (Exception e) {
            log.error("编辑模块错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
     * 保存模块
     */
    @PostMapping(value = "save")
    public String save(@RequestBody ModelEntity entity) {
        try {
            ModelForm form = new ModelForm();
            form.setModel(entity);
            return super.save(form);
        } catch (Exception e) {
            log.error("保存模块错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
     * 删除模块
     */
    @GetMapping(value = "delete")
    public String delete(String id) {
        try {
            ModelForm form = new ModelForm();
            String[] split = id.split(",");
            Long[] ids = Arrays.asList(split).stream().map(Long::parseLong).toArray(Long[]::new);
            form.setId(ids);
            return super.delete(form);
        } catch (Exception e) {
            log.error("删除模块错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
     * 查看模块
     */
    @GetMapping(value = "info")
    public String view(Long id) {
        try {
            ModelForm form = new ModelForm();
            form.setId(new Long[]{id});
            return super.view(form);
        } catch (Exception e) {
            log.error("查看模块错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }


    /**
     * 模块导入模板下载
     */
    @PostMapping(value = "downloadTemplate")
    public void downloadTemplate() {
        try {
            super.downloadTemplate();
        } catch (Exception e) {
            log.error("模块导入模板下载错误", e);
        }
    }

    /**
     * 模块导入
     */
    @PostMapping(value = "importData")
    public String importData(MultipartFile file, boolean updateSupport) {
        try {
            return super.importData(file, updateSupport);
        } catch (Exception e) {
            log.error("模块导入错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }
}