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.WExcel;
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.${table.getCName()}CreateRequest;
import ${basePackage}.module.${module}.req.${table.getCName()}DeleteRequest;
import ${basePackage}.module.${module}.req.${table.getCName()}FindRequest;
import ${basePackage}.module.${module}.req.${table.getCName()}GetRequest;
import ${basePackage}.module.${module}.req.${table.getCName()}UpdateRequest;
import ${basePackage}.module.${module}.rsp.${table.getCName()}CreateResponse;
import ${basePackage}.module.${module}.rsp.${table.getCName()}DeleteResponse;
import ${basePackage}.module.${module}.rsp.${table.getCName()}FindResponse;
import ${basePackage}.module.${module}.rsp.${table.getCName()}GetResponse;
import ${basePackage}.module.${module}.rsp.${table.getCName()}UpdateResponse;

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(${table.getCName()}CreateRequest request) {
        return ${table.getFName()}Manager.create(request, LocalData.getToken());
    }
</#if>
<#if table.getDelete()>

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

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

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

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

    public ${table.getCName()}SearchResponse search(${table.getCName()}SearchRequest request) {
        return ${table.getFName()}Manager.search(request, LocalData.getToken());
    }
</#if>
<#if table.getHtml()>

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

    public Object exports(${table.getCName()}FindRequest request) {
        ${table.getCName()}FindResponse response = ${table.getFName()}Manager.find(request, LocalData.getToken());
        if (response.hasError()) {
            return response;
        } else if (response.getTotalCount() == 0) {
            response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
            return response;
        }
        return ResponseUtil.apply(new WExcel<>(${table.getCName()}.class).loadData(response.getResult()));
    }

    public Object imports(MultipartFile file) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            WExcel sheet = new WExcel<>(${table.getCName()}.class).loadData(file.getBytes(), new WExcel.Processor<${table.getCName()}>() {
                @Override
                public List<String> exec(${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>
}
