package com.ym.mec.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.service.OaUserService;
import com.ym.mec.oa.OaFeignService;
import com.ym.mec.oa.entity.OAUser;
import com.ym.mec.oa.entity.PostDept;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class OaServiceImpl implements OaUserService {
    @Autowired
    private OaFeignService oaFeignService;

    @Override
    @Async
    public Object addOaUser(Employee employee) {
        OAUser oaUser = new OAUser();
        oaUser.setMecUserId(employee.getUserId());
        oaUser.setUserName(employee.getRealName());
        oaUser.setNickName(employee.getRealName());
        oaUser.setPhone(employee.getPhone());
        if(employee.getGender() != null){
            oaUser.setSex(employee.getGender().toString());
        }
        oaUser.setDeptId(employee.getDeptId());
        oaUser.setDeptIds(JSON.parseArray(employee.getDeptIds(), Integer.class));
        oaUser.setAvatar(employee.getAvatar());
        oaUser.setPostDeptIds(JSON.parseArray(employee.getPostDeptIds(), PostDept.class));
        if (oaUser.getPostDeptIds() != null) {
            List<Integer> postIds = oaUser.getPostDeptIds().stream().map(PostDept::getPostId).collect(Collectors.toList());
            oaUser.setPostIds(postIds);
        }
        return oaFeignService.register(oaUser);
    }

    @Override
    public Object updateOaUser(Employee employee) {
        OAUser oaUser = new OAUser();
        oaUser.setMecUserId(employee.getUserId());
        oaUser.setUserName(employee.getRealName());
        oaUser.setNickName(employee.getRealName());
        oaUser.setPhone(employee.getPhone());
        oaUser.setSex(employee.getGender().toString());
        oaUser.setAvatar(employee.getAvatar());
        if(Objects.nonNull(employee.getDeptId())){
            oaUser.setDeptId(employee.getDeptId());
        }
        if(Objects.nonNull(employee.getDeptIds())){
            oaUser.setDeptIds(JSON.parseArray(employee.getDeptIds(), Integer.class));
        }
        if(Objects.nonNull(employee.getPostDeptIds())){
            oaUser.setPostDeptIds(JSON.parseArray(employee.getPostDeptIds(), PostDept.class));
            List<Integer> postIds = oaUser.getPostDeptIds().stream().map(PostDept::getPostId).collect(Collectors.toList());
            oaUser.setPostIds(postIds);
        }
        return oaFeignService.updateUser(oaUser);
    }

    @Override
    @Async
    public Object delOaUser(Employee employee) {
        OAUser oaUser = new OAUser();
        oaUser.setMecUserId(employee.getUserId());
        return oaFeignService.delUser(oaUser);
    }

    @Override
    @Async
    public Object reInsertOaUser(Employee employee) {
        OAUser oaUser = new OAUser();
        oaUser.setMecUserId(employee.getUserId());
        return oaFeignService.reInsertUser(oaUser);
    }
}
