package com.ruoyi.ibs.dashboard.controller;

import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.dashboard.domain.dto.DashboardQuickAccessDTO;
import com.ruoyi.ibs.dashboard.domain.entity.DashboardQuickAccess;
import com.ruoyi.ibs.dashboard.domain.entity.LoadIndDmPaItemHome;
import com.ruoyi.ibs.dashboard.domain.vo.*;
import com.ruoyi.ibs.dashboard.service.DashboardCampaignService;
import com.ruoyi.ibs.dashboard.service.DashboardService;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author 吴凯程
 * @Date 2025/3/3
 **/
@Api(tags = "首页数据接口")
@RestController
@RequestMapping("/dashboard")
public class DashboardController extends BaseController {

    @Resource
    private DashboardService dashboardService;

    @Resource
    private DashboardCampaignService dashboardCampaignService;

    @Autowired
    private ISysNoticeService noticeService;

    @Resource
    private RedisCache redisCache;

    @ApiOperation("获取贷款预警信息列表")
    @Log(title = "首页提醒-获取贷款预警信息列表")
    @GetMapping("/loan/warn/list")
    public TableDataPageInfo<LoanContractDueWarnVO> getWarnList() {
        if(SecurityUtils.isAdmin(getUserId())){
            return null;
        }
        Page<Object> page = startPage();
        List<LoanContractDueWarnVO> warnList = dashboardService.getLoanWarnList();
        return getDataTable(warnList, page);
    }

    /**
     * 测试用接口
     * @return
     */
    @ApiOperation("获取预警信息列表")
    @Log(title = "首页提醒-获取预警信息列表")
    @PostMapping("/warn/test")
    public R<List<WarnInfoVO>> getWarnInfoTest() {
        if(SecurityUtils.isAdmin(getUserId())){
            return null;
        }
        List<WarnInfoVO> warnInfoVOList = new ArrayList<>(Arrays.asList(
                new WarnInfoVO("贷款到期提醒", null),
                new WarnInfoVO("提前还贷预警", null),
                new WarnInfoVO("存款指标提醒", null),
                new WarnInfoVO("贷款指标提醒", null),
                new WarnInfoVO("定期到期提醒", null),
                new WarnInfoVO("扣息不足提醒", null),
                new WarnInfoVO("大额动账提醒", null)
        ));
        return R.ok(warnInfoVOList);
    }

    /**
     * 获取预警信息列表--青田
     * @return
     */
    @ApiOperation("获取预警信息列表")
    @Log(title = "首页提醒-获取预警信息列表")
    @GetMapping("/warnResp/list")
    public TableDataPageInfo<Map<String, Object>> getWarnRespList(String type, Long pageNum, Long pageSize){
        if(SecurityUtils.isAdmin(getUserId())){
            return null;
        }
        CommonRespVO commonRespVO = dashboardService.getWarnRespList(type, pageNum, pageSize);
        return getDataTableByQt(commonRespVO.getData(), commonRespVO.getTotal());
    }

    @ApiOperation("获取预警信息列表")
    @Log(title = "首页提醒-获取预警信息列表")
    @GetMapping("/warn/list")
    public R<List<WarnInfoVO>> getWarnInfoList() {
        if(SecurityUtils.isAdmin(getUserId())){
            return null;
        }
        return R.ok(dashboardService.getWarnInfoList());
    }

    @ApiOperation("获取快速访问下拉框")
    @Log(title = "首页提醒-获取快速访问下拉框")
    @GetMapping("/quick/access/select")
    public R<List<String>> getRouterList() {
        return R.ok(dashboardService.getRouterList());
    }

    @ApiOperation("更新快速访问下拉框")
    @Log(title = "首页提醒-更新快速访问下拉框")
    @PostMapping("/quick/access/update")
    public R<String> updateQuickAccess(@Valid @RequestBody @Size(max = 4) List<String> routeNames) {
        return R.ok(dashboardService.updateQuickAccess(routeNames));
    }

    @ApiOperation("获取快速访问列表")
    @Log(title = "首页提醒-获取快速访问列表")
    @GetMapping("/quick/access/list")
    public R<List<DashboardQuickAccess>> getQuickAccessList() {
        return R.ok(dashboardService.getQuickAccessList());
    }

    @ApiOperation("支行管理员获取任务清单")
    @Log(title = "首页提醒-支行管理员获取任务清单")
    @GetMapping("/campaign/branch")
    public TableDataPageInfo<CampaignVO> getListByBranchAdmin(@RequestParam Boolean isFinish) {
        Page<Object> page = startPage();
        List<CampaignVO> listByBranchAdmin = dashboardCampaignService.getListByBranchAdmin(isFinish);
        return getDataTable(listByBranchAdmin, page);
    }

    @ApiOperation("总行获取代办")
    @Log(title = "首页提醒-总行获取代办")
    @GetMapping("/campaign/head")
    public TableDataPageInfo<CampaignVO> getListByheadAdmin(@RequestParam Boolean isFinish) {
        Page<Object> page = startPage();
        List<CampaignVO> listByBranchAdmin = dashboardCampaignService.getListByHeadAdmin(isFinish);
        return getDataTable(listByBranchAdmin, page);
    }

    @ApiOperation("客户经理获取任务清单")
    @Log(title = "首页提醒-客户经理获取任务清单")
    @GetMapping("/campaign/manager")
    public TableDataPageInfo<CampaignVO> getListByManager() {
        Page<Object> page = startPage();
        List<CampaignVO> listByManager = dashboardCampaignService.getListByManager();
        return getDataTable(listByManager, page);
    }

    @ApiOperation("获取指标数据")
    @Log(title = "首页提醒-获取指标数据")
    @GetMapping("/index")
    public R<List<LoadIndDmPaItemHome>> getIndDmPaItemHomeList() {
        if(SecurityUtils.isAdmin(getUserId()) || SecurityUtils.getHeadId().equals("932")){
            return null;
        }
        return R.ok(dashboardService.getIndDmPaItemHomeList());
    }

    @GetMapping("/notice")
    @Log(title = "首页提醒-获取通知公告")
    @ApiOperation("获取通知公告")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        notice.setCurrentHeadDeptId(SecurityUtils.getHeadId() + "000");
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    @GetMapping("/externalUrl")
    @Log(title = "首页提醒-外部链接")
    @ApiOperation("外部链接")
    public R<List<ExternalUrlVo>> externalUrl()
    {

        List<ExternalUrlVo> ans = new ArrayList<>();
        ArrayList<SysDictData> sysDictTypeArrayList = new ArrayList<SysDictData>(redisCache.getCacheObject("sys_dict:external_url"));
        if(sysDictTypeArrayList !=null &&  sysDictTypeArrayList.size()>0){
            ans = sysDictTypeArrayList.stream()
                    .filter(e -> e.getDictLabel().startsWith(SecurityUtils.getUsername().substring(0, 3)))
                    .map(e -> ExternalUrlVo.builder().url(e.getDictValue()).name(e.getDictLabel().split("-")[1]).flag(e.getDictSort().toString()).build())
                    .collect(Collectors.toList());
        }
        return R.ok(ans);

    }


}
