package com.xjrsoft.module.organization.controller;


import com.xjrsoft.common.annotation.XjrLog;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.module.organization.dto.WeChatDepartPageDto;
import com.xjrsoft.module.organization.dto.WeChatPageDto;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.organization.service.DingtalkService;
import com.xjrsoft.module.organization.service.IDepartmentService;
import com.xjrsoft.module.organization.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping(value = GlobalConstant.ORGANIZATION_MODULE_PREFIX + "/ding-talk")
@Api(value = GlobalConstant.ORGANIZATION_MODULE_PREFIX + "/ding-talk", tags = "钉钉信息")
@AllArgsConstructor
public class DingtalkController {

    private final IUserService userService;

    private final IDepartmentService departmentService;


    private DingtalkService dingtalkService;

    private RedisUtil redisUtil;

    @PutMapping("/sync-user")
    @ApiOperation(value="钉钉信息更新")
    @XjrLog(value = "钉钉用户部门同步更新")
    public R updateInfo() throws Exception{
        dingtalkService.updateInfo();
        CompletableFuture.runAsync(() -> {
            List<Department> depList = departmentService.list();
            redisUtil.set(GlobalConstant.DEP_CACHE_KEY, depList);

            List<User> userList = userService.list();
            redisUtil.set(GlobalConstant.USER_CACHE_KEY, userList);
        });
        return  R.ok(true);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="获取员工分页信息")
    @XjrLog(value = "获取员工分页信息")
    public R page(WeChatPageDto dto){
        return  R.ok(userService.getPage(dto));
    }

    @GetMapping(value = "/departpage")
    @ApiOperation(value="获取部门分页信息")
    @XjrLog(value = "获取部门分页信息")
    public R page(WeChatDepartPageDto dto){
        return  R.ok(departmentService.Ipage(dto));
    }
}
