package cn.ey.biz.module.system.api.user;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.common.util.collection.CollectionUtils;
import cn.ey.biz.module.system.api.user.dto.*;
import cn.ey.biz.module.system.enums.ApiConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@FeignClient(name = ApiConstants.NAME)
@Api(tags = "RPC 服务 - 用户中心")
public interface AdminUserApi {

    String PREFIX = ApiConstants.PREFIX + "/user";

    /**
     * 获取用户的url地址，主要是提供给 Gateway 使用
     */
    String GET_USER_URL = "http://" + ApiConstants.NAME + PREFIX +"/get-by-user-code";

    @GetMapping(PREFIX + "/get")
    @ApiOperation("通过用户 ID 查询用户")
    @ApiImplicitParam(name = "id", value = "用户编号", example = "1", required = true, dataTypeClass = Long.class)
    R<AdminUserRespDTO> getUser(@RequestParam("id") Long id);

    @RequestMapping(value = PREFIX + "/get-by-user-code",method = RequestMethod.GET)
    @ApiOperation("通过用户用户编号 查询用户")
    @ApiImplicitParam(name = "userCode", value = "用户编号", example = "1", required = true, dataTypeClass = String.class)
    R<AdminLoginUserRespDTO> getByUserCode(@RequestParam("userCode") String userCode);

    @GetMapping(value = PREFIX + "/get-user-for-gpn-or-email")
    @ApiOperation("精确查询-通过GPN或邮箱查询用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gpn", value = "用户编码-GPN", example = "1", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "businessEmail", value = "公司邮箱", example = "xxxx@cn.ey.com", required = false, dataTypeClass = String.class)
    })
    R<AdminUserRespDTO> getUserForGpnOrEmail(@RequestParam(value = "gpn",required=false) String gpn,
                                             @RequestParam(value = "businessEmail",required=false) String businessEmail);

    @GetMapping(value = PREFIX + "/get-user-for-gpns")
    @ApiOperation("精确查询-通过GPN批量查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gpns", value = "用户编码-GPN", example = "1", required =true, dataTypeClass = Collection.class)
    })
    R<List<AdminUserRespDTO>> getUserForGpns(@RequestParam(value = "gpns") Collection<String> gpns);

    @GetMapping(value = PREFIX + "/get-user-for-gui")
    @ApiOperation("精确查询-通过gui查询用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gui", value = "用户编码-gui", example = "1", required = true, dataTypeClass = Collection.class),
    })
    R<List<AdminUserRespDTO>>  getUserForGui(@RequestParam(value = "guis") Collection<String> guis);

    @PostMapping(value = PREFIX + "/get-user-for-keywords")
    @ApiOperation("模糊查询-通过GPN|email|userName，及职位级别获取用户列表")
    R<PageResult<AdminUserRespDTO>> getUserForKeywords(@RequestBody UsersForKeywordsReqDTO usersForKeywordsReqDTO);

    @PostMapping(value = PREFIX + "/get-users-for-last-date-time")
    @ApiOperation("分页获取时间点之后的用户数据")
    R<List<AdminUserRespDTO>> getUsersForLastDateTime( @Valid @RequestBody UsersForLastDateTimeReqDTO usersForLastDateTimeReqDTO);

    @PostMapping(value = PREFIX + "/get-users-for-conditions")
    @ApiOperation("通过任意组合条件分页查询用户,条件之间为and关系")
    R<PageResult<AdminUserRespDTO>> getUsersForConditions(@RequestBody UsersForConditionsReqDTO usersForConditionsReqDTO);

    @GetMapping(PREFIX + "/list")
    @ApiOperation("通过用户 ID 查询用户们")
    @ApiImplicitParam(name = "ids", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    R<List<AdminUserRespDTO>> getUsers(@RequestParam("ids") Collection<Long> ids);

    @GetMapping(PREFIX + "/list-by-dept-id")
    @ApiOperation("获得指定部门的用户数组")
    @ApiImplicitParam(name = "deptIds", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    R<List<AdminUserRespDTO>> getUsersByDeptIds(@RequestParam("deptIds") Collection<Long> deptIds);

    @GetMapping(PREFIX + "/list-by-post-id")
    @ApiOperation("获得指定岗位的用户数组")
    @ApiImplicitParam(name = "postIds", value = "岗位编号数组", example = "2,3", required = true, allowMultiple = true)
    R<List<AdminUserRespDTO>> getUsersByPostIds(@RequestParam("postIds") Collection<Long> postIds);

    /**
     *修改用户
     * @param updateUserReqDTO 要修改的对象
     * @return true or false
     */
    @PostMapping(PREFIX + "/update-user-by-gpn")
    @ApiOperation("根据gpn修改用户")
    R<Boolean>  updateUserByGPN(@Valid @RequestBody UpdateUserReqDTO updateUserReqDTO);

    /**
     * 获得用户 Map
     *
     * @param ids 用户编号数组
     * @return 用户 Map
     */
    default Map<Long, AdminUserRespDTO> getUserMap(Collection<Long> ids) {
        return CollectionUtils.convertMap(getUsers(ids).getCheckedData(), AdminUserRespDTO::getId);
    }

    @GetMapping(PREFIX + "/valid")
    @ApiOperation("校验用户们是否有效")
    @ApiImplicitParam(name = "ids", value = "用户编号数组", example = "3,5", required = true)
    R<Boolean> validUsers(@RequestParam("ids") Set<Long> ids);

    @PostMapping(PREFIX + "/sync-hr-users")
    @ApiOperation("HR 增量同步至临时表")
    R<Boolean> pushHrUsers(@Valid @RequestBody UserSyncFullDataReqDTO userSyncFullDataReqDTO);

    @PostMapping(PREFIX + "/save-switch-user")
    @ApiOperation("切换用户")
    R<Boolean> saveSwitchUser(@Valid @RequestBody UserSwitchRecordReqDTO userSwitchRecordReqDTO);

    @PostMapping(PREFIX + "/query-switch-user")
    //@ApiOperation("获取切换用户记录")
    R<Long> querySwitchUser(@Valid @RequestBody UserSwitchRecordReqDTO userSwitchRecordReqDTO);

    @PostMapping(PREFIX + "/update-switch-user")
    @ApiOperation("切换用户更新时间")
    R<Boolean> updateSwitchUser(@Valid @RequestBody UserSwitchRecordReqDTO userSwitchRecordReqDTO);
}

