package com.xjrsoft.module.organization.controller;


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.IDepartmentService;
import com.xjrsoft.module.organization.service.IUserService;
import com.xjrsoft.module.organization.service.IWeChatService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping(value = GlobalConstant.ORGANIZATION_MODULE_PREFIX + "/wechat")
@Api(value = GlobalConstant.ORGANIZATION_MODULE_PREFIX + "/wechat", tags = "微信")
@AllArgsConstructor
public class WechatController {


    private final IWeChatService WeChatService;

    private final IUserService userService;

    private final IDepartmentService departmentService;

    private final RedisUtil redisUtil;

    @PutMapping("/sync-user")
    @ApiOperation(value="微信信息更新")
    public R updateInfo(){

        WeChatService.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();
    }

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

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

    @GetMapping(value = "/js-config")
    @ApiOperation(value="前端jssdk页面配置需要用到的配置参数")
    public  R getWeixinJs (@RequestParam(value = "url",required = true) String url) throws Exception {
        return R.ok(WeChatService.getJsConfig(url));
    }


}
