package com.ygtx.gxt.controller;

import com.github.pagehelper.util.StringUtil;
import com.ygtx.common.annotation.Log;
import com.ygtx.common.constant.UserConstants;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.core.domain.entity.SysDept;
import com.ygtx.common.core.domain.entity.SysUser;
import com.ygtx.common.core.page.TableDataInfo;
import com.ygtx.common.enums.BusinessType;
import com.ygtx.common.utils.SecurityUtils;
import com.ygtx.common.utils.poi.ExcelUtil;
import com.ygtx.gxt.domain.*;
import com.ygtx.gxt.service.*;
import com.ygtx.system.service.ISysConfigService;
import com.ygtx.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 工作负责人
 *
 * @author ruoyi
 * @date 2025-11-03
 */
@RestController
@RequestMapping("/gxt/leader")
@Api(tags = "工作负责人")
public class GxtUserLeaderController extends BaseController
{

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IGxtUserOperatorHistoryService gxtUserOperatorHistoryService;

    @GetMapping("/listLeader")
    public AjaxResult listLeader(SysUser user)
    {
        GxtUserOperatorHistory gxtUserOperatorHistory = new GxtUserOperatorHistory();
        gxtUserOperatorHistory.setOpType(UserConstants.OP_TYPE_LEADER);
        gxtUserOperatorHistory.setOpUserid(getUserId());
        List<GxtUserOperatorHistory> gxtUserOperatorHistoryList = gxtUserOperatorHistoryService.selectGxtUserOperatorHistoryList(gxtUserOperatorHistory);
        user.setSortDeptId(SecurityUtils.getDeptId());
        List<SysUser> list = userService.selectUserListNoPermi(user);
        sortSysUser(list, gxtUserOperatorHistoryList);
        return AjaxResult.success(list);
    }

    private void sortSysUser(List<SysUser> list, List<GxtUserOperatorHistory> gxtUserOperatorHistoryList){
        Map<Long, GxtUserOperatorHistory> historyMap = gxtUserOperatorHistoryList.stream()
                .collect(Collectors.toMap(GxtUserOperatorHistory::getOpUserid, item -> item, (existing, replacement) -> existing, LinkedHashMap::new));
        List<SysUser> result1 = new ArrayList<>();
        for(GxtUserOperatorHistory history:gxtUserOperatorHistoryList){
            for (SysUser sysUser : list) {
                if (history.getOpLeaderId().equals(sysUser.getUserId())) {
                    result1.add(sysUser);
                }
            }
        }
        for (SysUser sysUser : result1) {
            list.remove(sysUser);
        }
//        result1.addAll(list);
//        list.clear();
        list.addAll(0, result1);
    }
}
