package com.engine.salary.action;

import cn.hutool.core.util.NumberUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO;
import com.engine.salary.mapper.salaryacct.SalaryAcctRecordMapper;
import com.engine.salary.service.SalaryAcctRecordService;
import com.engine.salary.service.impl.SalaryAcctRecordServiceImpl;
import com.engine.salary.util.db.MapperProxyFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.Map;
import java.util.stream.Collectors;

/**
 * @ClassName updateSalaryApprovalStatusAction
 * @author Harryxzy
 * @date 2024/4/24 15:44
 * @description 跟新薪资核算审批状态
 */
@Slf4j
public class updateSalaryApprovalStatusAction implements Action {

    private SalaryAcctRecordService getSalaryAcctRecordService(User user) {
        return ServiceUtil.getService(SalaryAcctRecordServiceImpl.class, user);
    }

    private SalaryAcctRecordMapper getSalaryAcctRecordMapper() {
        return MapperProxyFactory.getProxy(SalaryAcctRecordMapper.class);
    }


    // 记录薪资核算记录id的字段
    private String salaryAcctRecordIdField;

    // 修改为对应的状态
    private String status;

    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            if (StringUtils.isEmpty(salaryAcctRecordIdField)) {
                requestInfo.getRequestManager().setMessage("薪资核算记录id字段不能为空");
                return FAILURE_AND_CONTINUE;
            }
            Property[] properties = requestInfo.getMainTableInfo().getProperty();
            Map<String, String> fieldMap = Arrays.stream(properties).collect(Collectors.toMap(Property::getName,
                    property -> Util.null2String(property.getValue())));
            String salaryAcctRecordId = fieldMap.get(salaryAcctRecordIdField);
            if (!NumberUtil.isNumber(salaryAcctRecordId)) {
                requestInfo.getRequestManager().setMessage("核算记录id不为数字");
                return FAILURE_AND_CONTINUE;
            }
            User user = new User();
            user.setUid(1);
            user.setLoginid("sysadmin");
            SalaryAcctRecordPO salaryAcctRecordPO = getSalaryAcctRecordService(user).getById(Long.valueOf(salaryAcctRecordId));
            if (salaryAcctRecordPO == null) {
                requestInfo.getRequestManager().setMessage("核算记录不存在或已被删除");
                return FAILURE_AND_CONTINUE;
            }
            salaryAcctRecordPO.setApprovalStatus(status);
            getSalaryAcctRecordMapper().updateIgnoreNull(salaryAcctRecordPO);
        } catch (Exception e) {
            log.error("审批状态更新失败", e);
            requestInfo.getRequestManager().setMessage(e.getMessage());
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }
}
