package com.engine.salary.action;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.salaryarchive.param.SalaryArchiveImportActionParam;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.wrapper.SalaryArchiveWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class EditSalaryAction implements Action {


    private SalaryArchiveWrapper getSalaryArchiveWrapper(User user) {
        return ServiceUtil.getService(SalaryArchiveWrapper.class, user);
    }

    private String tableName;


    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    @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())));


            RecordSet rs = new RecordSet();

            String queryImageId = "select salaryname,processfield from " + tableName + " where workflowid = ?";
            rs.executeQuery(queryImageId, requestInfo.getWorkflowid());

            List<SalaryField> list = new ArrayList<>();
            while (rs.next()) {
                String processField = rs.getString("processfield");
                String salaryName = rs.getString("salaryname");
                String value = fieldMap.get(processField);
                list.add(new SalaryField(processField, salaryName, value));
            }
            List<Map<String, Object>> importData = new ArrayList<>();
            importData.add(SalaryEntityUtil.convert2Map(list, SalaryField::getSalaryName, SalaryField::getValue));

            SalaryArchiveImportActionParam build = SalaryArchiveImportActionParam.builder()
                    .importDatas(importData)
                    .build();

            //操作人
            String uid = list.stream().filter(f -> f.salaryName.equals("操作人")).findFirst().map(SalaryField::getValue).orElse("1");
            Map<String, Object> map = getSalaryArchiveWrapper(new User(Integer.parseInt(uid))).adjustmentSalaryArchive(build);

            List errorNotice = (List) map.get("errorNotice");
            if (CollectionUtils.isNotEmpty(errorNotice)) {
                log.error("调薪存在异常 requestId:{}  map:{}", requestInfo.getRequestid(), map);
//                List<Map<String, String>> excelComments = (List<Map<String, String>>) map.get("errorNotice");
//                StringBuilder message = new StringBuilder("");
//                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;
    }


    class SalaryField {

        private String processField;

        private String salaryName;

        private String value;

        public String getProcessField() {
            return processField;
        }

        public void setProcessField(String processField) {
            this.processField = processField;
        }

        public String getSalaryName() {
            return salaryName;
        }

        public void setSalaryName(String salaryName) {
            this.salaryName = salaryName;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public SalaryField(String processField, String salaryName, String value) {
            this.processField = processField;
            this.salaryName = salaryName;
            this.value = value;
        }
    }
}
