package ${domain}.action.ajax.wsys;

import com.fasterxml.jackson.core.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import ${domain}.frame.excel.WExcel;
import ${domain}.frame.utils.LogUtil;
import ${domain}.frame.auth.LocalData;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.base.BaseResponse;
import ${domain}.frame.utils.ResponseUtil;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.frame.excel.exception.TemplateNotMatchException;
import ${domain}.module.wsys.ent.TaskSql;
import ${domain}.module.wsys.mgr.TaskSqlManager;
import ${domain}.module.wsys.req.TaskSqlCreateRequest;
import ${domain}.module.wsys.req.TaskSqlDeleteRequest;
import ${domain}.module.wsys.req.TaskSqlFindRequest;
import ${domain}.module.wsys.req.TaskSqlUpdateRequest;
import ${domain}.module.wsys.rsp.TaskSqlCreateResponse;
import ${domain}.module.wsys.rsp.TaskSqlDeleteResponse;
import ${domain}.module.wsys.rsp.TaskSqlFindResponse;
import ${domain}.module.wsys.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;
    }
}
