package weaver.interfaces.organization.action;

import com.engine.organization.entity.staff.bo.StaffBO;
import com.engine.organization.entity.staff.po.StaffPO;
import com.engine.organization.entity.staff.po.StaffsPO;
import com.engine.organization.mapper.staff.StaffMapper;
import com.engine.organization.mapper.staff.StaffsMapper;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/06/07
 * @version: 1.0
 */
public class StaffChangeAction implements Action {

    /**
     * 1:编制
     * 2:变更
     * 3:冻结
     * 4:冻结释放
     * 5:扣减
     * 6:减员释放
     */
    private String changeType;


    @Override
    public String execute(RequestInfo requestInfo) {
        if (StringUtils.isBlank(changeType)) {
            requestInfo.getRequestManager().setMessagecontent("编制调整失败，未配置对应调整方式!");
            return FAILURE_AND_CONTINUE;
        }

        Integer requestId = Integer.parseInt(requestInfo.getRequestid());
        Long companyId = null;
        Long departmentId = null;
        Long jobId = null;
        Integer changeNum = null;
        RequestManager requestManager = requestInfo.getRequestManager();
        int creator = requestManager.getCreater();
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();
        // 取表单数据赋值
        for (Property item : property) {
            String name = item.getName();
            String value = Util.null2String(item.getValue());
            if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) {
                switch (name) {
                    case "fb": // 分部
                        companyId = Long.parseLong(value);
                        break;
                    case "bm": // 部门
                        departmentId = Long.parseLong(value);
                        break;
                    case "gw": // 岗位
                        jobId = Long.parseLong(value);
                        break;
                    case "bzbds": // 编制变动数
                        changeNum = Integer.parseInt(value);
                        break;
                    default:
                        break;
                }
            }

        }
        if (null == changeNum) {
            requestInfo.getRequestManager().setMessagecontent("编制变动数必填!");
            return FAILURE_AND_CONTINUE;
        }
        if (changeNum < 0) {
            requestInfo.getRequestManager().setMessagecontent("编制变动数需大于0!");
            return FAILURE_AND_CONTINUE;
        }

        // 根据分部、部门、岗位 定位具体编制信息，按照更新时间倒序，取最新的一条数据
        List<StaffPO> staffPOs = MapperProxyFactory.getProxy(StaffMapper.class).getStaffByFilter(companyId, departmentId, jobId);

        if (CollectionUtils.isNotEmpty(staffPOs)) {
            StaffPO staffPO = staffPOs.get(0);
            // 编制数
            Integer staffNum = staffPO.getStaffNum();
            switch (changeType) {
                case "3":// 冻结，比如招聘中，面试中，入职办理中等
                    if (staffPO.getFreezeNum() + changeNum > staffNum) {
                        requestInfo.getRequestManager().setMessagecontent("冻结数不能大于编制数!");
                        return FAILURE_AND_CONTINUE;
                    }
                    // 设置冻结数
                    staffPO.setFreezeNum(staffPO.getFreezeNum() + changeNum);
                    break;
                case "4":// 冻结释放，比如面试不通过，offer不接受等
                    if (changeNum > staffPO.getFreezeNum()) {
                        requestInfo.getRequestManager().setMessagecontent("冻结释放数不能大于冻结数!");
                        return FAILURE_AND_CONTINUE;
                    }
                    staffPO.setFreezeNum(staffPO.getFreezeNum() - changeNum);
                    break;
                case "5":// 扣减，比如正式入职，调入等
                    staffPO.setPermanentNum(staffPO.getPermanentNum() + changeNum);
                    if (staffPO.getPermanentNum() > staffPO.getStaffNum()) {
                        requestInfo.getRequestManager().setMessagecontent("在编数不能大于编制数!");
                        return FAILURE_AND_CONTINUE;
                    }
                    break;
                case "6":// 减员释放，比如离职、调出等
                    staffPO.setPermanentNum(staffPO.getPermanentNum() - changeNum);
                    if (staffPO.getPermanentNum() < 0) {
                        requestInfo.getRequestManager().setMessagecontent("调整数量不可大于在编数!");
                        return FAILURE_AND_CONTINUE;
                    }
                    break;
                case "1":// 编制
                case "2":// 变更
                default:
                    requestInfo.getRequestManager().setMessagecontent("流程操作事件changeType配置错误,请联系管理员!");
                    return FAILURE_AND_CONTINUE;
            }

            // 插入明细表
            StaffsPO staffsPO = StaffsPO.builder()
                    .staffId(staffPO.getId())
                    .businessType(Integer.parseInt(changeType))
                    .changeNum(changeNum)
                    .businessSource(2)// 流程
                    .requestId(requestId)
                    .deleteType(0)
                    .creator((long) creator)
                    .createTime(new Date())
                    .build();
            MapperProxyFactory.getProxy(StaffsMapper.class).insertIgnoreNull(staffsPO);

            // 编制描述
            StaffBO.buildStaffDesc(staffPO);
            MapperProxyFactory.getProxy(StaffMapper.class).updateStaff(staffPO);
            // 同步组织架构图编制信息
            // new Thread(new StaffTriggerRunnable(staffPO)).start();
            return SUCCESS;
        }
        requestInfo.getRequestManager().setMessagecontent("未找到对应维度的编制信息!");
        return FAILURE_AND_CONTINUE;
    }

    public String getChangeType() {
        return changeType;
    }

    public void setChangeType(String changeType) {
        this.changeType = changeType;
    }
}
