package ${basePackage}.action.ajax.${module};

import com.fasterxml.jackson.core.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.utils.LogUtil;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.utils.MapperUtil;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.utils.ResponseUtil;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.module.${module}.ent.${table.getCName()};
import ${basePackage}.module.${module}.mgr.${table.getCName()}Manager;
import ${basePackage}.module.${module}.req.*;
import ${basePackage}.module.${module}.rsp.*;

import java.io.IOException;
import java.util.List;

public class ${table.getCName()}Ajax{

    @Autowired
    private ${table.getCName()}Manager ${table.getFName()}Manager;

<#if table.getCreate()>
    public ${table.getCName()}CreateResponse create(TreeNode jsonParam) {
        ${table.getCName()}CreateRequest request = MapperUtil.toJava(jsonParam, ${table.getCName()}CreateRequest.class);
        return ${table.getFName()}Manager.create(request, LocalData.getToken());
    }
</#if>
<#if table.getDelete()>

    public ${table.getCName()}DeleteResponse delete(TreeNode jsonParam) {
        ${table.getCName()}DeleteRequest request = MapperUtil.toJava(jsonParam, ${table.getCName()}DeleteRequest.class);
        return ${table.getFName()}Manager.delete(request, LocalData.getToken());
    }
</#if>
<#if table.getUpdate()>

    public ${table.getCName()}UpdateResponse update(TreeNode jsonParam) {
        ${table.getCName()}UpdateRequest request = MapperUtil.toJava(jsonParam, ${table.getCName()}UpdateRequest.class);
        return ${table.getFName()}Manager.update(request, LocalData.getToken());
    }
</#if>
<#if table.getFind()>

    public ${table.getCName()}FindResponse find(TreeNode jsonParam) {
        ${table.getCName()}FindRequest request = MapperUtil.toJava(jsonParam, ${table.getCName()}FindRequest.class);
        return ${table.getFName()}Manager.find(request, LocalData.getToken());
    }
</#if>
<#if table.getGet()>

    public ${table.getCName()}GetResponse get(TreeNode jsonParam) {
        ${table.getCName()}GetRequest request = MapperUtil.toJava(jsonParam, ${table.getCName()}GetRequest.class);
        return ${table.getFName()}Manager.get(request, LocalData.getToken());
    }
</#if>
<#if table.getSearch()>

    public ${table.getCName()}SearchResponse search(TreeNode jsonParam) {
        ${table.getCName()}SearchRequest request = MapperUtil.toJava(jsonParam, ${table.getCName()}SearchRequest.class);
        return ${table.getFName()}Manager.search(request, LocalData.getToken());
    }
</#if>
<#if table.getGetAll()>

    public ${table.getCName()}GetAllResponse getAll(TreeNode jsonParam) {
        ${table.getCName()}GetAllRequest request = MapperUtil.toJava(jsonParam, ${table.getCName()}GetAllRequest.class);
        return ${table.getFName()}Manager.getAll(request, LocalData.getToken());
    }
</#if>
<#if table.getHtml()>

    public Object template(){
        return ResponseUtil.apply(new WSheet<>(${table.getCName()}.class));
    }

    public Object exports(TreeNode jsonParam) {
        ${table.getCName()}FindRequest request = MapperUtil.toJava(jsonParam, ${table.getCName()}FindRequest.class);
        ${table.getCName()}FindResponse response = ${table.getFName()}Manager.find(request, LocalData.getToken());
        if (response.hasError()) {
            return response;
        }
        return ResponseUtil.apply(new WSheet<>(response.getResult()));
    }

    public Object imports(MultipartFile file) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            WSheet<${table.getCName()}> sheet = new WSheet<>(file.getBytes(), ${table.getCName()}.class, new WSheet.Validator<${table.getCName()}>() {
                @Override
                public List<String> validate(${table.getCName()} o) {
                    ${table.getCName()}CreateRequest request = MapperUtil.map(o, ${table.getCName()}CreateRequest.class);
                    return ValidationUtil.validate(request);
                }
            });

            if (sheet.hasError()) {
                return ResponseUtil.apply(sheet.getBytes(), sheet.getName() + "-检查.xlsx");
            } else {
                return baseResponse;
            }
        } catch (IOException e) {
            e.printStackTrace();
            LogUtil.dumpException(e);
            baseResponse.addError(ErrorType.BUSINESS_ERROR, "上传文件出错");
        } catch (TemplateNotMatchException | ReadErrorException e) {
            baseResponse.addError(ErrorType.BUSINESS_ERROR, e.getMessage());
        }
        return baseResponse;
    }
</#if>
}
