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.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;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/06/07
 * @version: 1.0
 */
public class StaffChangeAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        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 (int i = 0; i < property.length; i++) {
            String name = property[i].getName();
            String value = Util.null2String(property[i].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;
                }
            }

        }

        // 根据分部、部门、岗位 定位具体编制信息
        StaffPO staffPO = MapperProxyFactory.getProxy(StaffMapper.class).getStaffByFilter(companyId, departmentId, jobId);
        if (null != staffPO) {

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

            // 更新编制表
            staffPO.setStaffNum(staffPO.getStaffNum() + changeNum);

            StaffBO.buildStaffDesc(staffPO);

            MapperProxyFactory.getProxy(StaffMapper.class).updateStaff(staffPO);
        }

        return null;
    }

}
