package jnpf.personnels.controller.web.org;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.attendance.service.AttendanceGroupService;
import jnpf.authority.utils.PermissionsEnums;
import jnpf.authority.utils.PermissionsUtils;
import jnpf.base.ActionResult;
import jnpf.model.attendance.vo.AttendanceGroupVo;
import jnpf.model.personnels.dto.oa.RequestForOA;
import jnpf.model.personnels.dto.staff.roster.WorkerGroupDataDto;
import jnpf.model.personnels.po.FtbPersonnelsStaffEmploymentApply;
import jnpf.model.personnels.po.FtbPersonnelsStaffRoster;
import jnpf.permission.PersonnelsAuthorityApi;
import jnpf.permission.UserApi;
import jnpf.permission.V2GradesApi;
import jnpf.permission.V2OrganizeApi;
import jnpf.permission.V2PositionApi;
import jnpf.permission.V2UserApi;
import jnpf.permission.dto.QueryUserListDTO;
import jnpf.permission.dto.v2.grades.QueryGradeListDTO;
import jnpf.permission.dto.v2.organzie.QueryOrganizeListTargetTypesDTO;
import jnpf.permission.dto.v2.position.QueryPositionListDetailGradesDTO;
import jnpf.permission.eum.v2.OrganizeCategoryEnums;
import jnpf.permission.model.user.UserBaseVO;
import jnpf.permission.vo.position.ListGradesByPositionVO;
import jnpf.permission.vo.position.ListUsersByGradesVO;
import jnpf.permission.vo.v2.grades.GradeVO;
import jnpf.permission.vo.v2.organzie.OrganizeGeneralDetailVO;
import jnpf.permission.vo.v2.position.PositionAndGradesVO;
import jnpf.permission.vo.v2.position.PositionBoundOrganizeVO;
import jnpf.permission.vo.v2.user.UserBoundInfoVO;
import jnpf.permission.vo.v2.user.UserPageListVO;
import jnpf.personnels.service.FtbPersonnelsStaffEmploymentApplyService;
import jnpf.personnels.service.FtbPersonnelsStaffRosterService;
import jnpf.personnels.utils.PersonnelOrgUtils;
import jnpf.personnels.utils.PersonnelPerUtils;
import jnpf.salary.QuerySalaryApi;
import jnpf.util.NoDataSourceBind;
import jnpf.util.UserProvider;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * web组织信息
 */
@RestController
@RequestMapping("/personnel-org")
public class FtbPersonnelOrgController {

    @Autowired
    private UserApi userApi;

    @Autowired
    private FtbPersonnelsStaffRosterService staffRosterService;

    @Autowired
    private PersonnelOrgUtils personnelOrgUtils;

    @Autowired
    private PersonnelPerUtils personnelPerUtils;

    @Autowired
    private PersonnelsAuthorityApi personnelsAuthorityApi;

    @Autowired
    V2OrganizeApi v2OrganizeApi;

    @Autowired
    V2PositionApi v2PositionApi;

    @Autowired
    V2GradesApi v2GradesApi;

    @Autowired
    private V2UserApi v2UserApi;

    @Resource
    private AttendanceGroupService attendanceGroupService;

    @Autowired
    PermissionsUtils permissionsUtils;

    @Autowired
    FtbPersonnelsStaffEmploymentApplyService staffEmploymentApplyService;

    @Autowired
    QuerySalaryApi querySalaryApi;

    /**
     * 查询组织下用户信息过滤已离职
     * @param organizeId
     * @return
     */
    @GetMapping("/query-org-info-user/{organizeId}")
    public ActionResult<List<ListGradesByPositionVO>> queryAllEmployeesRemoveResignedEmployees(@PathVariable("organizeId") String organizeId){
        // ftb_personnels_staff_roster
        // 304、待离职  305 离职
        List<String> list = new ArrayList<>();
        list.add("304");
        list.add("305");
        List<FtbPersonnelsStaffRoster> rosters = staffRosterService.lambdaQuery().in(FtbPersonnelsStaffRoster::getWorkerStatus, list)
                .eq(FtbPersonnelsStaffRoster::getEnabledMark, 0).list();
        List<String> collect = rosters.stream().map(FtbPersonnelsStaffRoster::getUserId).collect(Collectors.toList());
        ActionResult<List<ListGradesByPositionVO>> actionResult = userApi.treePositionGradesUser(organizeId, "");
        if (actionResult == null){
            return ActionResult.fail("获取组织信息接口调用失败!");
        }
        if (CollUtil.isEmpty(collect)){
            return ActionResult.success(actionResult.getData());
        }
        List<ListGradesByPositionVO> data = actionResult.getData();
        List<ListGradesByPositionVO> result = new ArrayList<>();
        if (CollUtil.isEmpty(data)){
            return ActionResult.success();
        }
        for (ListGradesByPositionVO vo : data){
            List<ListUsersByGradesVO> gradesList = vo.getGradesList();
            List<ListUsersByGradesVO> newGradesList =new ArrayList<>();
            for (ListUsersByGradesVO vo1 : gradesList){
                List<UserBaseVO> userList = vo1.getUserList();
                List<UserBaseVO> newUserList =new ArrayList<>();
                for (UserBaseVO vo2 : userList){
                    if (!collect.contains(vo2.getId())){
                        newUserList.add(vo2);
                    }
                }
                vo1.setUserList(newUserList);
                newGradesList.add(vo1);
            }
            vo.setGradesList(newGradesList);
            result.add(vo);
        }

        return ActionResult.success(result);
    }

    /**
     * 获取百度地图数据
     *
     * @return {@link ActionResult}
     */
    @PostMapping("/get-map")
    @NoDataSourceBind
    public ActionResult<String> getMap(@Valid @RequestBody GetBaiduMap getBaiduMap) {
        String string = HttpUtil.get(getBaiduMap.getUrl());
        return ActionResult.success(string);
    }

    @Data
    public class GetBaiduMap {
        /**
         * url地址
         */
        @NotBlank(message = "url地址不能为空")
        private String url;

    }

    /**
     * 更具用户获取组织信息
     * @param
     * @return
     */
    @PostMapping("/get-org-list")
    public ActionResult<List<WorkerGroupDataDto>> getOrgList(@RequestBody RequestForOA request) {
        List<WorkerGroupDataDto> list = personnelOrgUtils.getUserOrgBoundInfo(request.getUserIds().get(0));
        // 过滤权限
        if ("0".equals(request.getWhetherToQueryPermissions())) {
            List<String> orgIds = personnelPerUtils.obtainPersonnelOrganizationIdDataPermissions();
            list = list.stream().filter(a -> {
                // 超级管理员
                if (CollUtil.isEmpty(orgIds)) {
                    return true;
                } else {
                    return orgIds.contains(a.getAffiliatedOrg());
                }
            }).collect(Collectors.toList());
        }
        if (CollUtil.isNotEmpty(list)){
            String affiliatedOrg = request.getAffiliatedOrg();
            String affiliatedPosition = request.getAffiliatedPosition();
            if (StringUtils.isNotEmpty(affiliatedPosition) && StringUtils.isNotEmpty(affiliatedOrg)){
                list = list.stream().filter(item -> affiliatedPosition.equals(item.getAffiliatedPosition()) && isEquals(item, affiliatedOrg)).collect(Collectors.toList());
                return ActionResult.success(list );
            }
            if (StringUtils.isNotEmpty(affiliatedOrg)){
                list = list.stream().filter(item -> isEquals(item, affiliatedOrg) && StringUtils.isNotEmpty(item.getAffiliatedPosition()) ).collect(Collectors.toList());
                return ActionResult.success(list );
            }
            if ("1".equals(request.getFlag())){
                list = list.stream().filter(WorkerGroupDataDto::getIsDefault).map(vo->{
                    WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
                    workerGroupDataDto.setAffiliatedOrg(vo.getAffiliatedOrg());
                    workerGroupDataDto.setAffiliatedOrgName(vo.getAffiliatedOrgName());
                    return workerGroupDataDto;
                }).distinct().collect(Collectors.toList());
                return ActionResult.success(list);
            }
        }
        // 所有组织去重
        list = list.stream().map(vo->{
            WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
            workerGroupDataDto.setAffiliatedOrg(vo.getAffiliatedOrg());
            workerGroupDataDto.setAffiliatedOrgName(vo.getAffiliatedOrgName());
            return workerGroupDataDto;
        }).distinct().collect(Collectors.toList());
        return ActionResult.success(list);
    }
    /**
     * 更具用户获取组织信息
     * @param
     * @return
     */
    @PostMapping("/get-org-list-gd")
    public ActionResult<List<WorkerGroupDataDto>> getOrgListForGUDi(@RequestBody RequestForOA request){
        List<WorkerGroupDataDto> list = personnelOrgUtils.getUserOrgBoundInfo(request.getUserIds().get(0));
        return ActionResult.success(list);
    }

    /**
     * OA控件通用(组织)
     * 1.可以根据用户获取组织
     * 2.无参数查询所有组织信息
     * @param
     * @return
     */
    @PostMapping("/getOrganization")
    public ActionResult<List<WorkerGroupDataDto>> getOrganization(@RequestBody RequestForOA request) {
        List<String> userIds = request.getUserIds();
        // 过滤权限
        if (CollUtil.isEmpty(userIds) && "0".equals(request.getWhetherToQueryPermissions())) {
            List<String> orgIds = new ArrayList<>();
            if (!UserProvider.getUser().getIsAdministrator()){
                orgIds = permissionsUtils.
                        obtainPersonnelOrganizationIdDataPermissions(UserProvider.getUser().getUserId(), PermissionsEnums.PERSONNEL_MANAGEMENT_WEB.getValue(), "web");
                if (orgIds != null && orgIds.isEmpty()) return ActionResult.success();
            }
            QueryOrganizeListTargetTypesDTO queryOrganizeListTargetTypesDTO = new QueryOrganizeListTargetTypesDTO();
            queryOrganizeListTargetTypesDTO.setOrganizeCategoryEnums(List.of(OrganizeCategoryEnums.COMPANY, OrganizeCategoryEnums.DEPARTMENT, OrganizeCategoryEnums.STORE));
            ActionResult<List<OrganizeGeneralDetailVO>> actionResult = v2OrganizeApi.listOrganizeByTargetTypes(queryOrganizeListTargetTypesDTO);
            List<OrganizeGeneralDetailVO> lists = actionResult.getData();
            List<String> finalOrgIds = orgIds;
            List<WorkerGroupDataDto> collected = lists.stream().filter(v -> {
                if (v.getOrganizeCategoryEnums().equals(OrganizeCategoryEnums.STORE) && !v.getEnabled()) return false;
                return true;
            }).filter(v->{
                if (UserProvider.getUser().getIsAdministrator()) return true;
                if (finalOrgIds == null) return true;
                // 超级管理员
                if (CollUtil.isEmpty(finalOrgIds)) {
                    return false;
                }
                return finalOrgIds.contains(v.getId());
            }).map(vo -> {
                WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
                workerGroupDataDto.setAffiliatedOrg(vo.getId());
                workerGroupDataDto.setAffiliatedOrgName(vo.getName());
                return workerGroupDataDto;
            }).filter(Objects::nonNull).collect(Collectors.toList());
            return ActionResult.success(collected);
        }
        if (CollUtil.isEmpty(userIds) && StringUtils.isEmpty(request.getWhetherToQueryPermissions())){
            QueryOrganizeListTargetTypesDTO queryOrganizeListTargetTypesDTO = new QueryOrganizeListTargetTypesDTO();
            queryOrganizeListTargetTypesDTO.setOrganizeCategoryEnums(List.of(OrganizeCategoryEnums.COMPANY, OrganizeCategoryEnums.DEPARTMENT, OrganizeCategoryEnums.STORE));
            ActionResult<List<OrganizeGeneralDetailVO>> actionResult = v2OrganizeApi.listOrganizeByTargetTypes(queryOrganizeListTargetTypesDTO);
            List<OrganizeGeneralDetailVO> lists = actionResult.getData();
            // 所有组织去重
            List<WorkerGroupDataDto> dataDtos = lists.stream().map(vo -> {
                WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
                workerGroupDataDto.setAffiliatedOrg(vo.getId());
                workerGroupDataDto.setAffiliatedOrgName(vo.getName());
                return workerGroupDataDto;
            }).distinct().collect(Collectors.toList());
            return ActionResult.success(dataDtos);
        }
        String userId = userIds.get(0);
        List<WorkerGroupDataDto> list = personnelOrgUtils.getUserOrgBoundInfo(userId);
        // 查询当前人的数据过滤权限
        if ("0".equals(request.getWhetherToQueryPermissions()) && !UserProvider.getUser().getIsAdministrator()) {
            List<String> orgIds = permissionsUtils.
                    obtainPersonnelOrganizationIdDataPermissions(UserProvider.getUser().getUserId(), PermissionsEnums.PERSONNEL_MANAGEMENT_WEB.getValue(), "web");
            list = list.stream().filter(a -> {
                // 超级管理员
                if (CollUtil.isEmpty(orgIds)) {
                    return true;
                } else {
                    return orgIds.contains(a.getAffiliatedOrg());
                }
            }).collect(Collectors.toList());
        }
        if (CollUtil.isNotEmpty(list)){
            String affiliatedOrg = request.getAffiliatedOrg();
            String affiliatedPosition = request.getAffiliatedPosition();
            if (StringUtils.isNotEmpty(affiliatedPosition) && StringUtils.isNotEmpty(affiliatedOrg)){
                list = list.stream().filter(item -> affiliatedPosition.equals(item.getAffiliatedPosition()) && isEquals(item, affiliatedOrg)).collect(Collectors.toList());
                return ActionResult.success(list );
            }
            if (StringUtils.isNotEmpty(affiliatedOrg)){
                list = list.stream().filter(item -> isEquals(item, affiliatedOrg) && StringUtils.isNotEmpty(item.getAffiliatedPosition()) ).collect(Collectors.toList());
                return ActionResult.success(list );
            }
            // 主岗
            if ("0".equals(request.getWhetherToQueryMainPost())){
                list = list.stream().filter(WorkerGroupDataDto::getIsPrimaryPosition).map(vo->{
                    WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
                    workerGroupDataDto.setAffiliatedOrg(vo.getAffiliatedOrg());
                    workerGroupDataDto.setAffiliatedOrgName(vo.getAffiliatedOrgName());
                    workerGroupDataDto.setIsPrimaryPosition(vo.getIsPrimaryPosition());
                    return workerGroupDataDto;
                }).distinct().collect(Collectors.toList());
                return ActionResult.success(list);
            }
            // 默认组织
            if ("1".equals(request.getFlag())){
                list = list.stream().filter(WorkerGroupDataDto::getIsDefault).map(vo->{
                    WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
                    workerGroupDataDto.setAffiliatedOrg(vo.getAffiliatedOrg());
                    workerGroupDataDto.setAffiliatedOrgName(vo.getAffiliatedOrgName());
                    return workerGroupDataDto;
                }).distinct().collect(Collectors.toList());
                return ActionResult.success(list);
            }
        }
        // 所有组织去重
        list = list.stream().map(vo->{
            WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
            workerGroupDataDto.setAffiliatedOrg(vo.getAffiliatedOrg());
            workerGroupDataDto.setAffiliatedOrgName(vo.getAffiliatedOrgName());
            return workerGroupDataDto;
        }).distinct().collect(Collectors.toList());
        return ActionResult.success(list);
    }

    /**
     * OA控件通用(岗位)
     * 1.可以根据用户获取岗位
     * 2.无参数查询所有组织信息
     * @param
     * @return
     */
    @PostMapping("/getPosts")
    public ActionResult<List<WorkerGroupDataDto>> getPosts(@RequestBody RequestForOA request){
        List<String> userIds = request.getUserIds();
        String affiliatedOrg = request.getAffiliatedOrg();
        // 查询默认的所有岗位信息
        if (CollUtil.isEmpty(userIds) && StringUtils.isEmpty(affiliatedOrg)){
            QueryPositionListDetailGradesDTO gradesDTO = new QueryPositionListDetailGradesDTO();
            gradesDTO.setIsAll(true);
            ActionResult<List<PositionAndGradesVO>> actionResult = v2PositionApi.listPositionDetailGradesConcreteInfo(gradesDTO);
            List<PositionAndGradesVO> data = actionResult.getData();
            if (data == null || actionResult.getCode() != 200 ) return ActionResult.fail(actionResult.getMsg());
            List<WorkerGroupDataDto> dataDtos = data.stream().map(vo -> {
                WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
                workerGroupDataDto.setAffiliatedPosition(vo.getId());
                workerGroupDataDto.setAffiliatedPositionName(vo.getFullName());
                return workerGroupDataDto;
            }).distinct().collect(Collectors.toList());
            return ActionResult.success(dataDtos);
        }
        if  (CollUtil.isEmpty(userIds) && StringUtils.isNotEmpty(affiliatedOrg)){
            ActionResult<List<PositionBoundOrganizeVO>> actionResult = v2PositionApi.listPositionBoundInfoOrganizeIds(Arrays.asList(affiliatedOrg),  null);
            List<PositionBoundOrganizeVO> data = actionResult.getData();
            if (data == null || actionResult.getCode() != 200 ) return ActionResult.fail(actionResult.getMsg());
            List<WorkerGroupDataDto> dataDtos = data.stream().map(vo -> {
                WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
                workerGroupDataDto.setAffiliatedPosition(vo.getId());
                workerGroupDataDto.setAffiliatedPositionName(vo.getFullName());
                return workerGroupDataDto;
            }).distinct().collect(Collectors.toList());
            return ActionResult.success(dataDtos);
        }
        String userId = userIds.get(0);
        List<WorkerGroupDataDto> list = personnelOrgUtils.getUserOrgBoundInfo(userId);
        if (CollUtil.isNotEmpty(list)) {
            if (StringUtils.isNotEmpty(affiliatedOrg)){
                list = list.stream().filter(item -> isEquals(item, affiliatedOrg) && StringUtils.isNotEmpty(item.getAffiliatedPosition())).collect(Collectors.toList());
                // 过滤主岗查询
                if ("0".equals(request.getWhetherToQueryMainPost())) list = list.stream().filter(WorkerGroupDataDto::getIsPrimaryPosition).collect(Collectors.toList());
                return ActionResult.success(list);
            }
        }
        return ActionResult.success();
    }

    private static boolean isEquals(WorkerGroupDataDto item, String affiliatedOrg) {
        return affiliatedOrg.equals(item.getAffiliatedOrg());
    }

    /**
     * OA控件通用(职等)
     * 1.可以根据用户获取职等
     * 2.无参数查询所有组织信息
     * @param
     * @return
     */
    @PostMapping("/getGrade")
    public ActionResult<List<WorkerGroupDataDto>> getGrade(@RequestBody RequestForOA request){
        String affiliatedOrg = request.getAffiliatedOrg();
        String affiliatedPosition = request.getAffiliatedPosition();
        List<String> userIds = request.getUserIds();
        if (CollUtil.isEmpty(userIds) &&
                StringUtils.isEmpty(affiliatedOrg) &&
                StringUtils.isEmpty(affiliatedPosition)){
            QueryGradeListDTO gradeListDTO = new QueryGradeListDTO();
            ActionResult<List<GradeVO>> result = v2GradesApi.listGrades(gradeListDTO);
            List<GradeVO> data = result.getData();
            if (data == null || result.getCode() != 200 ) return ActionResult.fail(result.getMsg());
            List<WorkerGroupDataDto> groupDataDtos = result.getData().stream().map(v -> {
                WorkerGroupDataDto dataDto = new WorkerGroupDataDto();
                dataDto.setAffiliatedRank(v.getId());
                dataDto.setAffiliatedRankName(v.getFullName());
                return dataDto;
            }).collect(Collectors.toList());
            return ActionResult.success(groupDataDtos);
        }
        if (CollUtil.isEmpty(userIds)  && StringUtils.isNotEmpty(affiliatedPosition)){
            QueryGradeListDTO gradeListDTO = new QueryGradeListDTO();
            gradeListDTO.setPositionId(affiliatedPosition);
            ActionResult<List<GradeVO>> result = v2GradesApi.listGrades(gradeListDTO);
            List<GradeVO> data = result.getData();
            if (data == null || result.getCode() != 200 ) return ActionResult.fail(result.getMsg());
            List<WorkerGroupDataDto> groupDataDtos = result.getData().stream().map(v -> {
                WorkerGroupDataDto dataDto = new WorkerGroupDataDto();
                dataDto.setAffiliatedRank(v.getId());
                dataDto.setAffiliatedRankName(v.getFullName());
                return dataDto;
            }).collect(Collectors.toList());
            return ActionResult.success(groupDataDtos);
        }
        String userId = userIds.get(0);
        List<WorkerGroupDataDto> list = personnelOrgUtils.getUserOrgBoundInfo(userId);
        if (CollUtil.isNotEmpty(list)) {
            if (StringUtils.isNotEmpty(affiliatedPosition) && StringUtils.isNotEmpty(affiliatedOrg)) {
                list = list.stream().filter(item -> affiliatedPosition.equals(item.getAffiliatedPosition()) && isEquals(item, affiliatedOrg) &&  StringUtils.isNotEmpty(item.getAffiliatedRank()))
                        .collect(Collectors.toList());
                return ActionResult.success(list);
            }
        }
        return ActionResult.success();
    }
    /**
     *  OA控件通用(考情组)
     *
     * @param orgId 组织Id
     */
    @GetMapping("/groupListByOrgId")
    public ActionResult<List<AttendanceGroupVo>> groupListByOrgIds(String orgId) {
        List<AttendanceGroupVo> groupVoList = attendanceGroupService.groupListByOrgId(orgId);
        return ActionResult.success(groupVoList);
    }

    /**
     * OA控件通用(直属主管)
     *
     * @param dto
     * @return
     */
    @GetMapping("/queryAllUserForOrgAndStatus")
    public ActionResult<List<UserPageListVO>> getAllUser(QueryUserListDTO dto) {
        List<UserPageListVO> list = staffRosterService.queryAllUserForOrgAndStatus(dto);
        return ActionResult.success("成功", list);
    }

    /**
     * OA控件通用(班组)
     *
     * @param request
     * @return
     */
    @PostMapping("/queryCrews")
    public ActionResult<List<WorkerGroupDataDto>> queryCrews(@RequestBody RequestForOA request) {
        List<String> userIds = request.getUserIds();
        String userId = CollUtil.isNotEmpty(userIds) ? userIds.get(0) : request.getUserId();
        if (CollUtil.isNotEmpty(userIds) || StringUtils.isNotEmpty(userId)){
            ActionResult<UserBoundInfoVO> usersBound = v2UserApi.getUsersBound(userId, UserProvider.getUser().getTenantId());
            if  (usersBound == null || usersBound.getData() == null) return ActionResult.success();
            if  (usersBound.getData() != null) {
                UserBoundInfoVO data = usersBound.getData();
                WorkerGroupDataDto dataDto = new WorkerGroupDataDto();
                dataDto.setStoreTeamId(data.getStoreTeamId());
                dataDto.setStoreTeamName(data.getStoreTeamName());
                return ActionResult.success(List.of(dataDto));
            }
        }

        ActionResult<List<OrganizeGeneralDetailVO>> actionResult;
        if(StringUtils.isNotEmpty(request.getAffiliatedOrg())){

            List<String> affiliatedOrg = List.of(request.getAffiliatedOrg());
            ActionResult<List<OrganizeGeneralDetailVO>> result = v2OrganizeApi.organizesByOrganizeIds(affiliatedOrg);
            if (result.getCode() != 200 )  return ActionResult.success(new ArrayList<>());
            if (result.getData().stream().filter(v->v.getOrganizeCategoryEnums().equals(OrganizeCategoryEnums.STORE)).count() == 0 ) return ActionResult.success(new ArrayList<>());
            actionResult  = v2OrganizeApi.organizesOrHaveChildByOrganizeIds(affiliatedOrg, true,UserProvider.getUser().getTenantId());
        } else {
            QueryOrganizeListTargetTypesDTO queryOrganizeListTargetTypesDTO = new QueryOrganizeListTargetTypesDTO();
            queryOrganizeListTargetTypesDTO.setOrganizeCategoryEnums(List.of(OrganizeCategoryEnums.TEAM));
            actionResult = v2OrganizeApi.listOrganizeByTargetTypes(queryOrganizeListTargetTypesDTO);
        }
        if  (actionResult == null || actionResult.getData() == null) return ActionResult.success( );
        List<OrganizeGeneralDetailVO> data = actionResult.getData();
        List<WorkerGroupDataDto> list = data.stream().filter(item ->
                item.getOrganizeCategoryEnums().equals(OrganizeCategoryEnums.TEAM)).map(v -> {
            if (v.getId() == null) return null;
            WorkerGroupDataDto dataDto = new WorkerGroupDataDto();
            dataDto.setStoreTeamId(v.getId());
            dataDto.setStoreTeamName(v.getName());
            return dataDto;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        return ActionResult.success("成功", list);
    }
    /**
     * oa通用前置校验(校验用户是否未入职)
     */
    @PostMapping("/check-user-is-not-entry")
    public void checkUserIsNotEntry(@RequestBody RequestForOA request) {
        List<String> userIds = request.getUserIds();
        String userId = CollUtil.isNotEmpty(userIds) ? userIds.get(0) : request.getUserId();
        LambdaQueryWrapper<FtbPersonnelsStaffRoster> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(FtbPersonnelsStaffRoster::getUserId, request.getUserId());
        queryWrapper1.eq(FtbPersonnelsStaffRoster::getEnabledMark, 0);
        long count = staffRosterService.count(queryWrapper1);
        LambdaQueryWrapper<FtbPersonnelsStaffEmploymentApply> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsStaffEmploymentApply::getUserId, userId);
        queryWrapper.eq(FtbPersonnelsStaffEmploymentApply::getEnabledMark, 0);
        queryWrapper.in(FtbPersonnelsStaffEmploymentApply::getCheckStatus, 0);
        queryWrapper.last("limit 1");
        FtbPersonnelsStaffEmploymentApply one = staffEmploymentApplyService.getOne(queryWrapper);
        if (Objects.nonNull(one) && count == 0) {
            throw new RuntimeException("当前选择人员还未办理入职,无法提交!");
        }
    }
    /**
     * oa查询当前人员薪酬
     */
    @GetMapping("/query-salary-by-userId")
    public ActionResult<Map<String, Object>> querySalaryByUserId(@RequestParam("userId") String userId) {
//        LambdaQueryWrapper<FtbPersonnelsStaffRoster> queryWrapper1 = Wrappers.lambdaQuery();
//        queryWrapper1.eq(FtbPersonnelsStaffRoster::getUserId, userId);
//        queryWrapper1.eq(FtbPersonnelsStaffRoster::getEnabledMark, 0);
//        queryWrapper1.last("limit 1");
//        FtbPersonnelsStaffRoster serviceOne = staffRosterService.getOne(queryWrapper1);
//        LambdaQueryWrapper<FtbPersonnelsStaffEmploymentApply> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(FtbPersonnelsStaffEmploymentApply::getUserId, userId);
//        queryWrapper.eq(FtbPersonnelsStaffEmploymentApply::getEnabledMark, 0);
//        queryWrapper.in(FtbPersonnelsStaffEmploymentApply::getCheckStatus, 0,1);
//        queryWrapper.last("limit 1");
//        FtbPersonnelsStaffEmploymentApply one = staffEmploymentApplyService.getOne(queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
//        if (Objects.nonNull(one) && Objects.isNull(serviceOne)) {
//            map.put("entryMoney", one.getEntryMoney() != null ? one.getEntryMoney() : "");
//            return ActionResult.success( map);
//        }
        ActionResult<BigDecimal> totalByUserId = querySalaryApi.getSalaryTotalByUserId(userId);
        map.put("totalSalary", totalByUserId.getData() != null ? totalByUserId.getData() : "");
        return ActionResult.success(map);
    }
}
