package ${packageName}.web;
<#if table.isShowControl==1 >
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSONObject;
    <#assign Controller = "BaseCRUDJsonController">
<#elseif table.isShowControl==2 >
    <#assign Controller = "BaseCRUDJsonBodyMappingController">
<#else>
    <#assign Controller = "BaseCRUDJsonMappingController">
</#if>
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mortals.framework.web.${Controller};
import ${packageName}.model.${ClassName}Entity;
import ${packageName}.model.${ClassName}Query;
import ${packageName}.service.${ClassName}Service;
<#if table.tplCategory=="tree">
import ${packageName}.model.${ClassName}TreeSelect;
</#if>
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
import java.util.Arrays;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import static com.mortals.framework.ap.SysConstains.*;
/**
*
* ${functionName}
*
* @author ${author}
* @date ${datetime}
*/
@RestController
@RequestMapping("${RequestMapping}")
public class ${ClassName}Controller extends ${Controller}<${ClassName}Service,${ClassName}Form,${ClassName}Entity,${pkColumn.javaType}> {

    @Autowired
    private ParamService paramService;

    public ${ClassName}Controller(){
        super.setFormClass(${ClassName}Form.class);
        super.setModuleDesc( "${functionName}");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, ${ClassName}Form form, Map<String, Object> model, Context context) {
        <#list columns as column>
            <#if column.columnType?contains("tinyint") ||column.htmlType==3||column.htmlType==5>
        this.addDict(model, "${column.javaField}", paramService.getParamBySecondOrganize("${ClassName}","${column.javaField}"));
            </#if>
        </#list>
        super.init(request, response, form, model, context);
    }

<#if table.tplCategory=="tree">
    @PostMapping("list/exclude")
    public String excludeList(${ClassName}Form form) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询" + this.getModuleDesc();
        Long id = form.getId()[0];
        int code=VALUE_RESULT_SUCCESS;
        try {
            List<${ClassName}Entity> collect = this.service.find(new ${ClassName}Query()).stream().map(item -> {
                if (item.getId().intValue() == id || ArrayUtils.contains(StringUtils.split(item.getAncestors(), ","), id + "")) {
                    return null;
                }
                return item;
            }).filter(f -> f != null).collect(Collectors.toList());
            model.put("result", collect);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        ret.put(KEY_RESULT_DATA, model);
        ret.put(KEY_RESULT_CODE, code);
        return ret.toJSONString();
    }


    /**
     * 获取站点下拉树列表
     */
    @PostMapping("treeselect")
    public String treeselect(${ClassName}Form form) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询" + this.getModuleDesc();
        int code=VALUE_RESULT_SUCCESS;
        try {
            List<${ClassName}Entity> list = this.service.find(new ${ClassName}Query());
            List<${ClassName}TreeSelect> treeSelects = this.service.build${ClassName}TreeSelect(list);
             model.put("result", treeSelects);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        ret.put(KEY_RESULT_DATA, model);
        ret.put(KEY_RESULT_CODE, code);
        return ret.toJSONString();
    }


    /**
    * 根据parentId查询子信息
    */
    @GetMapping(value = "getListByParentId")
    public String getListByParentId(String parentId) {
        JSONObject ret = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = "查询" + this.getModuleDesc()+"子节点";
        try {
            List<${ClassName}TreeSelect> treeList = this.service.getListByParentId(parentId, getContext());
            model.put(RESULT_KEY,treeList);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_DATA, model);
            recordSysLog(request, busiDesc+"【成功】");
        } catch (Exception e) {
            log.error("根据parentId查询子信息错误", e);
            this.doException(request, busiDesc, model, e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, e.getMessage());
        }
        return ret.toJSONString();
    }

</#if>
<#if table.isShowControl==1 >
    /**
    * 查询${functionName}
    */
    @PostMapping(value = "list")
    public String list(${ClassName}Form form) {
        try {
            return super.list(form);
        } catch (Exception e) {
            log.error("查询${functionName}列表错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
    * 编辑${functionName}
    */
    @PostMapping(value = "edit")
    public String edit(${ClassName}Form form) {
        try {
            return super.edit(form);
        } catch (Exception e) {
            log.error("编辑${functionName}错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
    * 保存${functionName}
    */
    @PostMapping(value = "save")
    public String save(${ClassName}Form form) {
        try {
            return super.save(form);
        } catch (Exception e) {
            log.error("保存${functionName}错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
    * 删除${functionName}
    */
    @PostMapping(value = "delete")
    public String delete(${ClassName}Form form) {
        try {
            return super.delete(form);
        } catch (Exception e) {
            log.error("删除${functionName}错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
    * 查看${functionName}
    */
    @PostMapping(value = "view")
    public String view(${ClassName}Form form) {
        try {
            return super.view(form);
        } catch (Exception e) {
            log.error("查看${functionName}错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
    * 逻辑删除${functionName}
    */
    @PostMapping(value = "logicDelete")
    public String logicDelete(${ClassName}Form form) {
        try {
            return super.logicDelete(form);
        } catch (Exception e) {
            log.error("逻辑删除${functionName}错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

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

    /**
    * ${functionName}导入
    */
    @PostMapping(value = "importData")
    public String importData(MultipartFile file, boolean updateSupport) {
        try {
            return super.importData(file,updateSupport);
        } catch (Exception e) {
            log.error("${functionName}导入错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }
    <#elseif table.isShowControl==2 >
    /**
     * 查询${functionName}
     */
    @PostMapping(value = "list")
    public String list(@RequestBody ${ClassName}Query query) {
        try {
            ${ClassName}Form form = new ${ClassName}Form();
            form.setQuery(query);
            return super.list(form);
        } catch (Exception e) {
            log.error("查询${functionName}列表错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
    * 新增${functionName}
    */
    @PostMapping(value = "add")
    public String add() {
        try {
        ${ClassName}Form form = new ${ClassName}Form();
            return super.add(form);
        } catch (Exception e) {
            log.error("新增${functionName}错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

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

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

    /**
     * 删除${functionName}
     */
    @GetMapping(value = "delete")
    public String delete(String id) {
        try {
            ${ClassName}Form form = new ${ClassName}Form();
            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("删除${functionName}错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

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


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

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

    /**
    * ${functionName}导出
    */
    @PostMapping("exportExcel")
    public void exportExcel(@RequestBody ${ClassName}Query query) {
        try {
        ${ClassName}Form form = new ${ClassName}Form();
            form.setQuery(query);
            super.exportExcel(form);
        } catch (Exception e) {
            log.error("${functionName}导出错误", e);
        }
    }
</#if>
}