1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
import * as types from '../constants/ActionTypes';
import { importEmp,getEmployees,importAttend,getAttendances,getAttendanceOverview,listOrganizations,
         getSimpleEmployee,listPositions,editOrgMesUtil,addOrgMesUtil,turnInUtil,turnOutUtil,addPositionUtil,
         delPositionUtil,editPositionUtil,editPeoUtil,getWorkinfoById,
         getOrgs,getPositions,addOrg,addPosition,getPositionDetail,updateOrg,updatePosition,getTenants,
         getSubordinates, deleteOrgActionUtil ,getAppNameUtil 
       } from '../../utils/hrUtil';
import {getProvidentFunds,getSociaFunds,searchCompanyRecords,searchCompanySociaRecords,getHornorList,addHornorUtil,
        editHornorUtil,delHornorUtil
       } from '../../utils/workUtil';
import {SubmissionError, change} from 'redux-form';
import {message} from 'antd';




/*加载部门全局使用*/
export function loadOrganizations(id){
    return dispatch => {
        return getOrgs()
            .then(data => {
                const {items=[]}=data;
                dispatch(updateOrganizations(_handleOrganizations(items)));
                dispatch(updateOrganizationsForFind(_handleOrganizationsForFind(items)));
                items.map(item=>{
                    if(item.id==id)
                        dispatch(changeOrganizeForOrg(item))
                });
            })
            .catch(err => { throw err; });
    };
}

const _handleOrganizations=(items)=>{
    var temp=[];
    items.map((item,i)=>{
        temp.push({
            id:item.id,
            name:item.name,
            tenant_id:item.tenant_id,
            owner:item.owner,
            owner_id:item.owner_id,
            parent_org:item.parent_org,
            parent_org_id:item.parent_org_id
        });
    });
    return temp;
}
const _handleOrganizationsForFind=(items)=>{
    var temp={};
    items.map((item,i)=>{
        temp[item.id]={
            id:item.id,
            name:item.name,
            tenant_id:item.tenant_id,
            owner:item.owner,
            owner_id:item.owner_id,
            parent_org:item.parent_org,
            parent_org_id:item.parent_org_id
        };
    });
    return temp;
}

function updateOrganizationsForFind(organizationLookup){
    return {
        type: types.LOAD_LOOKUP_ORGANIZATIONS,
        organizationLookup
    };
}

function updateOrganizations(organizations){
    return {
        type: types.LOAD_ORGANIZATION,
        organizations
    };
}

/**/
export function changeOrganizeForOrg(orgData) {   //切换部门信息
    return dispatch => {
        dispatch({
            type: types.CHANGE_ORG_MES,
            orgMes:orgData
        })
    }
}
export function changePositionForOrg(orgID,str,values) { //切换岗位信息
    const query=orgID;//+'&reporting_position=true'
    return dispatch => {
        return getPositions({'org_id':orgID})
            .then(data => {
                const {items=[]}=data;
                dispatch({
                    type: types.CHANGE_POSITION_MES,
                    positionMes: items
                })
            })
            .catch(err => { throw err; });
    };
}
export function changeEmployeeForOrg(orgID) { //切换人员信息
    const query='&org_id='+orgID+'&limit=100&offset=0';
    return dispatch => {
        return getSimpleEmployee(query)
            .then(data => {
                const {items=[]}=data;
                dispatch({
                    type: types.CHANGE_EMP_MES,
                    empMes: items
                });
                dispatch(selectedEmp([]));
            })
            .catch(err => { throw err; });
    };
}
export function selectedEmp(emps){
    return dispatch => {
        dispatch({
            type: types.SELECTED_EMP,
            'selectedEmp':emps
        })
    }
}

export function needLoadEmployee(status){
    return dispatch => {
        dispatch({
            type: types.NEED_LOAD_EMPLOYEE_LIST,
            'loadEmpStatu':status
        })
    }
}

// 获取当前部门同级及上级部门
export function loadCurrentOrganizations(params){
    return dispatch => {
        return getOrgs(params)
            .then(data => {
                dispatch({
                    type: types.LOAD_CURRENT_ORGANIZATION,
                    currentOrganizations: data
                })
            })
            .catch(err => { throw err; });
    };
}

export function editOrgMes(values){ //编辑部门信息
    return dispatch => {
        return updateOrg(values)
            .then(data => {
                if(data.code&&data.code>=300){
                    message.error(data.message);
                    return
                }else{
                    message.success('编辑成功');
                    dispatch(loadOrganizations(values.id));
                    return data
                }
            })
            .catch(err => { throw err; });
    };
}

export function addOrgMes(values){ //添加子部门
    return dispatch => {
        return addOrg(values)
            .then(data => {
                if(data.code&&data.code>=300){
                    message.error('添加失败');
                }else{
                    dispatch({
                        type: types.ADD_ORG_MES,
                        addOrgMes: values
                    });
                    message.success('添加成功');
                }
                dispatch(loadOrganizations());
            })
            .catch(err => { throw err; });
    };
}

export function turnInAction(values){ //员工调入
    return dispatch => {
        return turnInUtil(values)
            .then(data => {
                dispatch(loadOrganizations());
            })
            .catch(err => { throw err; });
    };
}


export function turnOutAction(values){ //员工调出
    return dispatch => {
        return turnOutUtil(values)
            .then(data => {
                dispatch({
                    type: types.TURN_OUT_EMP,
                    turnOutEmp: values
                });
                dispatch(loadOrganizations());
            })
            .catch(err => { throw err; });
    };
}

export function saveSelectTurnOutEmp(selectData) {   //save选中的调出人员
    return dispatch => {
        dispatch({
            type      : types.SELECT_TURN_OUT_EMP,
            selectData: selectData
        })
    }
}


/*clean 职位*/
export function cleanPositions(){
    return  {
        type     : types.LOAD_POSITIONS,
        positions: []
    } 
}
/*
*加载职位
*/
export function loadPositions(org_id,all=false){
    return dispatch => {
        return getPositions({org_id,all})
            .then(data => {
                const {items=[]}=data;
                dispatch({
                    type     : types.LOAD_POSITIONS,
                    positions: items
                });
                if(items[0]){
                    dispatch(changePositionForOrg(items[0].org_id))
                }
                // items.map(item=>{
                //     if(item.org_id==org_id)
                // });
            })
            .catch(err => { throw err; });
    };
}

export function addPositionAction(values){  //添加岗位
    return dispatch => {
        return addPosition(values)
            .then(data => {
                if(data.code&&data.code>=300){
                    message.error(data.message);
                    throw new SubmissionError({_error:data.message});
                }else{
                    dispatch(loadPositions(data.org_id));
                    message.success('添加成功');
                }
            })
            .catch(err => { throw err; });
    };
}

export function delPositionAction(data){  //删除岗位
    return dispatch => {
        return delPositionUtil(data)
            .then(data => {
                dispatch(loadPositions());
            })
            .catch(err => { throw err; });
    };
}

export function eidtPositionAction(data){  //编辑岗位
    return dispatch => {
        return updatePosition(data)
            .then(data => {
                dispatch(loadPositions(data.org_id));
            })
            .catch(err => { throw err; });
    };
}

export function editPeoAction(data){  //编辑人员信息
    return dispatch => {
        return editPeoUtil(data)
            .then(data => {

            })
            .catch(err => { throw err; });
    };
}



export function loadWorkInfo(mep_id){
    return dispatch => {
        return getWorkinfoById(mep_id)
            .then(data => {
                dispatch({
                    type: types.LOAD_REPORTING,
                    workInfo: data
                });
            })
            .catch(err => { throw err; });
    };
}

/*获取tenants列表*/
export function loadTenants(){
    return dispatch => {
        return getTenants()
            .then(data => {
                const {items}=data;
                dispatch({
                    type: types.LOAD_TENANTS,
                    tenants: items
                });
                return data
            })
            .catch(err => { throw err; });
    };
}

/*获取所有下级列表*/
export function loadSubordinates(query){
    return dispatch => {
        return getSubordinates(query)
            .then(data => {
                const {items}=data;
                dispatch({
                    type: types.LOAD_SUBORDINATES,
                    subordinates: items
                });
            })
            .catch(err => { throw err; });
    };
}

// 组织管理获取部门负责人
export function getSimpleEmployeeActions(params){
    return dispatch => {
        return getSimpleEmployee(params)
            .then(data => {
                const { items } = data
                dispatch({
                    type: types.GET_SIMPLE_EMPLOYEE,
                    simpleEmployeeData: items
                })
                return data
            })
            .catch(err => { throw err })
    };
}

// 删除部门
export function deleteOrgAction(params) {
    return dispatch => {
        return deleteOrgActionUtil(params)
            .then(data => {
                if(data.code && data.code >= 300){
                    message.error(data.message)
                }else{
                    message.success('删除成功')
                    dispatch(change('frame', 'showDeleteOrg', false))
                    dispatch(loadOrganizations(params.root_id))
                    dispatch(changePositionForOrg(params.root_id));
                    dispatch(changeEmployeeForOrg(params.root_id));
                }
                return data
            })
    }
}
// 获取小程序名称
export function getAppNameAction() {
    return dispatch => {
        return getAppNameUtil()
            .then(data => {
                if(data.code && data.code >= 300){
                    message.error(data.message)
                }else{
                    dispatch({
                        type: types.GETAPPNAMEUTIL,
                        getAppNameData: data
                    })
                }
            })
    }
}

/*加载部门全局使用*/
export function loadOrganizationsAct(){
    return dispatch => {
        return getOrgs()
            .then(data => {
                return data
            })
            .catch(err => { throw err; });
    };
}