package com.engine.salary.action;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.biz.SalarySendInfoBiz;
import com.engine.salary.entity.salaryBill.param.SalarySendGrantParam;
import com.engine.salary.entity.salaryBill.po.SalarySendPO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO;
import com.engine.salary.service.SalaryAcctRecordService;
import com.engine.salary.service.SalaryBillService;
import com.engine.salary.service.SalarySendService;
import com.engine.salary.service.SalaryTemplateService;
import com.engine.salary.service.impl.SalaryAcctRecordServiceImpl;
import com.engine.salary.service.impl.SalaryBillServiceImpl;
import com.engine.salary.service.impl.SalarySendServiceImpl;
import com.engine.salary.service.impl.SalaryTemplateServiceImpl;
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.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Harryxzy
 * @ClassName FileSalaryAcctRecordAction
 * @date 2023/12/13 9:17
 * @description 工资单发放action
 */
@Slf4j
public class SendSalaryAction implements Action {


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

    private SalarySendService getSalarySendService(User user) {
        return ServiceUtil.getService(SalarySendServiceImpl.class, user);
    }

    private SalaryTemplateService getSalaryTemplateService(User user) {
        return ServiceUtil.getService(SalaryTemplateServiceImpl.class, user);
    }

    private SalarySendInfoBiz salarySendInfoMapper = new SalarySendInfoBiz();

    private SalaryBillService getSalaryBillService(User user) {
        return ServiceUtil.getService(SalaryBillServiceImpl.class, user);
    }


    /**
     * 发放id（核算记录id，工资单id）流程字段名
     */
    private String idFieldName;

    /**
     * 根据什么id（核算记录id，工资单id）发工资单
     */
    private String sendBy;


    @Override
    public String execute(RequestInfo requestInfo) {
        Property[] properties = requestInfo.getMainTableInfo().getProperty();
        Map<String, String> fieldMap = Arrays.stream(properties).collect(Collectors.toMap(Property::getName,
                property -> Util.null2String(property.getValue())));
        String idStr = fieldMap.get(idFieldName);
        if (StringUtils.isBlank(idStr)) {
            requestInfo.getRequestManager().setMessage("核算记录id或工资单id不能为空");
            return FAILURE_AND_CONTINUE;
        }
        User user = new User();
        user.setUid(1);
        Long id = Long.valueOf(idStr);
        SalarySendPO salarySendPO;
        if(!org.h2.util.StringUtils.isNullOrEmpty(sendBy) && sendBy.equals("salaryAcctRecordId")) {
            // 根据核算记录id发
            SalaryAcctRecordPO salaryAcctRecordPO = getSalaryAcctRecordService(user).getById(id);
            if (salaryAcctRecordPO == null) {
                requestInfo.getRequestManager().setMessage("薪资核算记录不存在,或已被删除");
                return FAILURE_AND_CONTINUE;
            }
            if (salaryAcctRecordPO.getStatus() == 1) {
                requestInfo.getRequestManager().setMessage("核算记录还未归档,请先归档");
                return FAILURE_AND_CONTINUE;
            }
            // 获取工资单id
            List<SalarySendPO> salarySendPOList = getSalarySendService(user).listSome(SalarySendPO.builder().salaryAccountingId(id).sendStatus(0).build());
            salarySendPO = salarySendPOList.get(0);
        } else if (!org.h2.util.StringUtils.isNullOrEmpty(sendBy) && sendBy.equals("salarySendId")) {
            // 根据工资单发
            salarySendPO = getSalarySendService(user).getById(id);
        } else {
            requestInfo.getRequestManager().setMessage("请先维护根据什么id发放工资单的sendBy参数");
            return FAILURE_AND_CONTINUE;
        }

        if (salarySendPO == null || salarySendPO.getId() == null) {
            requestInfo.getRequestManager().setMessage("工资单不存在或已被删除！");
            return FAILURE_AND_CONTINUE;
        }
        try {
            // 全部发放
            getSalaryBillService(user).grant(SalarySendGrantParam.builder().salarySendId(salarySendPO.getId()).build());
        } catch (Exception e) {
            requestInfo.getRequestManager().setMessage(e.getMessage());
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }
}
