/**
 * Copyright &copy; 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.sys.controller.app;

import cn.hutool.extra.spring.SpringUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.sys.controller.UserController;
import com.jeeplus.sys.domain.Office;
import com.jeeplus.sys.domain.User;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.OfficeService;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.sys.service.dto.TenantDTO;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户Controller
 *
 * @author jeeplus
 * @version 2021-8-29
 */
@RestController
@RequestMapping("/app/sys/user")
public class AppUserController extends UserController {

    @Autowired
    OfficeService officeService;
    @Autowired
    UserService userService;

    /**
     * 获取机构JSON数据。
     *
     * @return
     */
    @GetMapping("treeData")
    public ResponseEntity treeData() {
        List <Office> list = officeService.list ( );
        // 通过用户名从数据库获取用户信息
        IUserApi userApi = SpringUtil.getBean(IUserApi.class);
        UserDTO userDTO = userApi.getByToken(TokenProvider.getCurrentToken());
        List rootTree = getRootTree ( list );
        TenantDTO tenantDTO = userDTO.getTenantDTO();
        if(tenantDTO != null){
            String tenantId = tenantDTO.getId();
            if(tenantId.equals("10009")){
                rootTree = (List) rootTree.stream()
                        .filter(item -> {
                            Map map = (Map) item;
                            Object label = map.get("label");
                            return label != null && label.toString().equals("景聚庭");
                        })
                        .collect(Collectors.toList());
            }
        }
        return ResponseEntity.ok ( rootTree );
    }

    /**
     * 移动端人员单选所需-获取机构JSON数据。
     *
     * @return
     */
    @GetMapping("treeDataRadio")
    public ResponseEntity treeDataRadio(String type) {
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        List<Office> list = new ArrayList<>();
        if (null != type && type.equals("ydd")) {
            list = officeService.list().stream()
                    .filter(office -> office.getTenantId().equals(userDTO.getTenantDTO().getId()))
                    .collect(Collectors.toList());
        } else {
            list = officeService.list ( );
        }

        List rootTree = getRootTreeRadio ( list, type, userDTO );
        return ResponseEntity.ok ( rootTree );
    }

    private List <Map> getRootTreeRadio(List <Office> list, String type, UserDTO userDTO) {
        List <Map> offices = Lists.newArrayList ( );

        List <Office> rootTrees = new ArrayList<>();
        if (null != type && type.equals("ydd")){
            rootTrees = officeService.treeData().stream()
                    .filter(office -> office.getTenantId().equals(userDTO.getTenantDTO().getId()))
                    .collect(Collectors.toList());
        } else {
            rootTrees = officeService.treeData ( );
        }

        for (Office root : rootTrees) {
            offices.add ( getChildOfTreeRadio ( root, list ) );
        }
        return offices;
    }

    private Map getChildOfTreeRadio(Office officeItem, List <Office> officeList) {
        Map oMap = new HashMap ( );
        oMap.put ( "id", officeItem.getId ( ) );
        oMap.put ( "type", "office" );
        oMap.put ( "label", officeItem.getName ( ) );
        List children = Lists.newArrayList ( );
        oMap.put ( "children", children );
        List <User> list = userService.lambdaQuery ( ).eq ( User::getOfficeId, officeItem.getId ( ) ).list ( );
        for (int i = 0; i < list.size ( ); i++) {
            User e = list.get ( i );
            Map <String, Object> map = Maps.newHashMap ( );
            map.put ( "id", e.getId ( ) );
            map.put ( "type", "user" );
            map.put ( "label", e.getName ( ) );
            children.add ( map );
        }
        for (Office child : officeList) {
            if ( child.getParentId ( ).equals ( officeItem.getId ( ) ) ) {
                children.add ( getChildOfTreeRadio ( child, officeList ) );
            }
        }
        return oMap;
    }

    private List <Map> getRootTree(List <Office> list) {
        List <Map> offices = Lists.newArrayList ( );
        List <Office> rootTrees = officeService.treeData ( );
        for (Office root : rootTrees) {
            offices.add ( getChildOfTree ( root, list ) );
        }
        return offices;
    }

    private Map getChildOfTree(Office officeItem, List <Office> officeList) {
        Map oMap = new HashMap ( );
        oMap.put ( "id", "o_" + officeItem.getId ( ) );
        oMap.put ( "type", "office" );
        oMap.put ( "label", officeItem.getName ( ) );
        List children = Lists.newArrayList ( );
        oMap.put ( "children", children );
        List <User> list = userService.lambdaQuery ( ).eq ( User::getOfficeId, officeItem.getId ( ) ).list ( );
        for (int i = 0; i < list.size ( ); i++) {
            User e = list.get ( i );
            Map <String, Object> map = Maps.newHashMap ( );
            map.put ( "id", e.getId ( ) );
            map.put ( "type", "user" );
            map.put ( "label", e.getName ( ) );
            children.add ( map );
        }
        for (Office child : officeList) {
            if ( child.getParentId ( ).equals ( officeItem.getId ( ) ) ) {
                children.add ( getChildOfTree ( child, officeList ) );
            }
        }
        return oMap;
    }


}
