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

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.TemplateNotMatchException;
import ${basePackage}.module.system.ent.TaskSql;
import ${basePackage}.module.system.mgr.TaskSqlManager;
import ${basePackage}.module.system.req.TaskSqlCreateRequest;
import ${basePackage}.module.system.req.TaskSqlDeleteRequest;
import ${basePackage}.module.system.req.TaskSqlFindRequest;
import ${basePackage}.module.system.req.TaskSqlUpdateRequest;
import ${basePackage}.module.system.rsp.TaskSqlCreateResponse;
import ${basePackage}.module.system.rsp.TaskSqlDeleteResponse;
import ${basePackage}.module.system.rsp.TaskSqlFindResponse;
import ${basePackage}.module.system.rsp.TaskSqlUpdateResponse;

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

public class TaskSqlAjax{

    @Autowired
    private TaskSqlManager taskSqlManager;

    public TaskSqlCreateResponse create(TaskSqlCreateRequest request) {
        return taskSqlManager.create(request, LocalData.getToken());
    }

    public TaskSqlDeleteResponse delete(TaskSqlDeleteRequest request) {
        return taskSqlManager.delete(request, LocalData.getToken());
    }

    public TaskSqlUpdateResponse update(TaskSqlUpdateRequest request) {
        return taskSqlManager.update(request, LocalData.getToken());
    }

    public TaskSqlFindResponse find(TaskSqlFindRequest request) {
        return taskSqlManager.find(request, LocalData.getToken());
    }

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

    public Object exports(TaskSqlFindRequest request) {
        TaskSqlFindResponse response = taskSqlManager.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<>(TaskSql.class).addDatas(response.getResult()));
    }

    public Object imports(MultipartFile file) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            WExcel sheet = new WExcel<>(TaskSql.class).read(file.getBytes(), new WExcel.Processor<TaskSql>() {
                @Override
                public List<String> exec(TaskSql o, int index) {
                    TaskSqlCreateRequest request = MapperUtil.map(o, TaskSqlCreateRequest.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 e) {
            baseResponse.addError(ErrorType.BUSINESS_ERROR, e.getMessage());
        }
        return baseResponse;
    }
}
