package com.engine.salary.action;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.salaryarchive.param.SalaryArchiveImportHandleParam;
import com.engine.salary.enums.salaryarchive.SalaryArchiveImportTypeEnum;
import com.engine.salary.enums.salaryarchive.SalaryArchiveListTypeEnum;
import com.engine.salary.service.SalaryArchiveExcelService;
import com.engine.salary.service.impl.SalaryArchiveExcelServiceImpl;
import lombok.extern.slf4j.Slf4j;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class CheckBatEditSalaryExcelAction implements Action {

    private SalaryArchiveExcelService getSalaryArchiveExcelService(User user) {
        return ServiceUtil.getService(SalaryArchiveExcelServiceImpl.class, user);
    }

    private String batSalaryExcelField;

    public String getBatSalaryExcelField() {
        return batSalaryExcelField;
    }

    public void setBatSalaryExcelField(String batSalaryExcelField) {
        this.batSalaryExcelField = batSalaryExcelField;
    }

    private String operatorField;

    public String getOperatorField() {
        return operatorField;
    }

    public void setOperatorField(String operatorField) {
        this.operatorField = operatorField;
    }

    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            Property[] properties = requestInfo.getMainTableInfo().getProperty();
            Map<String, String> fieldMap = Arrays.stream(properties).collect(Collectors.toMap(Property::getName,
                    property -> Util.null2String(property.getValue())));

            String docId = fieldMap.get(batSalaryExcelField);

            RecordSet rs = new RecordSet();
            String queryImageId = "select imagefileid from docimagefile where docid = ?";
            rs.executeQuery(queryImageId, docId);
            String imagefileid = null;
            if (rs.next()) {
                imagefileid = rs.getString("imagefileid");
            }

            SalaryArchiveImportHandleParam param = SalaryArchiveImportHandleParam.builder().imageId(imagefileid)
                    .listType(SalaryArchiveListTypeEnum.FIXED.getValue())
                    .importType(SalaryArchiveImportTypeEnum.SALARYITEMADJUST.getValue())
                    .onlyCheck(true)
                    .isProcess(true)
                    .build();

            //操作人
            String uid = fieldMap.getOrDefault(operatorField, "1");
            Map<String, Object> map = getSalaryArchiveExcelService(new User(Integer.parseInt(uid))).batchImportEbatch(param);
            int errorCount = (int) map.get("errorCount");
            if (errorCount > 0) {
                log.error("批量调薪检查存在异常 requestId:{}  map:{}", requestInfo.getRequestid(), map);
                List<Map<String, String>> excelComments = (List<Map<String, String>>) map.get("errorNotice");
                StringBuilder message = new StringBuilder("excel中");
                for (Map<String, String> comments : excelComments) {
                    message.append(comments.get("message")).append("\n");
                }
                requestInfo.getRequestManager().setMessage(message.toString());
                return FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            log.error("批量调薪检查异常", e);
            requestInfo.getRequestManager().setMessage(e.getMessage());
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }
}
