package com.qmth.sop.business.templete.execute;

import cn.hutool.core.date.DateUtil;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.sop.business.bean.result.TBDingSubmitQueryResult;
import com.qmth.sop.business.entity.BasicAttachment;
import com.qmth.sop.business.entity.TBTask;
import com.qmth.sop.business.service.TBTaskService;
import com.qmth.sop.business.templete.export.AsyncExportTaskTemplete;
import com.qmth.sop.business.templete.service.TaskLogicService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.TaskResultEnum;
import com.qmth.sop.common.enums.TaskStatusEnum;
import com.qmth.sop.common.enums.TaskTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @Description: 考勤打卡统计导出
 * @Param:
 * @return:
 * @Author: dhshu
 * @Date: 2023/8/7
 */
@Service
public class AsyncDingCountExportService extends AsyncExportTaskTemplete {
    private final static Logger log = LoggerFactory.getLogger(AsyncDingCountExportService.class);
    public static final String OBJ_TITLE = "考勤打卡统计导出";

    @Resource
    TaskLogicService taskLogicService;

    @Resource
    TBTaskService tbTaskService;

    @Override
    public Result exportTask(Map<String, Object> map) throws Exception {
        TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
        StringJoiner stringJoinerSummary = new StringJoiner("\n")
                .add(MessageFormat.format("{0}{1}{2}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), BEGIN_TITLE, OBJ_TITLE));
        tbTask.setStatus(TaskStatusEnum.RUNNING);
        tbTask.setSummary(stringJoinerSummary.toString());
        tbTaskService.updateById(tbTask);
        try {
            Map<String, Object> result = taskLogicService.executeExportDingCountLogic(map);
            List<TBDingSubmitQueryResult> archivesImportDtoList = (List<TBDingSubmitQueryResult>) result.get(SystemConstant.EXCEL_DATA);
            BasicAttachment basicAttachment = (BasicAttachment) result.get(SystemConstant.EXCEL_ATTACHMENT);
            stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), FINISH_TITLE, !CollectionUtils.isEmpty(archivesImportDtoList) ? archivesImportDtoList.size() : 0, FINISH_SIZE));

            tbTask.setImportFileName(TaskTypeEnum.MESSAGE_EXPORT.getTitle());
            tbTask.setResultFilePath(basicAttachment.getPath());
            tbTask.setResult(TaskResultEnum.SUCCESS);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), EXCEPTION_TITLE, EXCEPTION_DATA, e.getMessage()));
            tbTask.setResult(TaskResultEnum.ERROR);
            if (e instanceof ApiException) {
                ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
            } else {
                ResultUtil.error(e.getMessage());
            }
        } finally {//生成txt文件
            tbTask.setSummary(stringJoinerSummary.toString());
            super.createTxt(tbTask);
        }
        return ResultUtil.ok();
    }
}
