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.module.system.api.user.dto.*;
import cn.ey.biz.module.system.convert.user.UserConvert;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import cn.ey.biz.module.system.service.user.AdminUserService;
import cn.ey.biz.module.system.service.user.AdminUserSuperService;
import cn.ey.biz.module.system.service.user.UserSwitchRecordService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.module.system.enums.ApiConstants.VERSION;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
@Slf4j
public class AdminUserApiImpl implements AdminUserApi {

    @Resource
    private AdminUserService userService;

    @Resource
    private AdminUserSuperService adminUserSuperService;

    @Resource
    private UserSwitchRecordService userSwitchRecordService;

    @Override
    public R<AdminUserRespDTO> getUser(Long id) {
        AdminUserDO user = userService.getUser(id);
        return success(UserConvert.INSTANCE.convert4(user));
    }

    @Override
    public R<AdminLoginUserRespDTO> getByUserCode(String userCode) {
        return success(UserConvert.INSTANCE.convertAdminLoginUserRespDTO(userService.selectByUserCode(userCode)));
    }

    @Override
    public R<AdminUserRespDTO> getUserForGpnOrEmail(String gpn, String businessEmail) {

        return success(UserConvert.INSTANCE.convert4(userService.getUserForGpnOrEmail(gpn,businessEmail)));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUserForGpns(Collection<String> gpns) {
        return success(UserConvert.INSTANCE.convertList4( userService.getUserForGpns(gpns)));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUserForGui(Collection<String> gui) {
        return success(UserConvert.INSTANCE.convertList4(userService.getUserForGui(gui)));
    }

    @Override
    public  R<PageResult<AdminUserRespDTO>> getUserForKeywords(UsersForKeywordsReqDTO usersForKeywordsReqDTO) {
        return success(adminUserSuperService.getUserForKeywords(usersForKeywordsReqDTO));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUsersForLastDateTime(UsersForLastDateTimeReqDTO usersForLastDateTimeReqDTO) {
        log.info("获取增量用户数据->参数: {}", JSON.toJSONString(usersForLastDateTimeReqDTO,JSONWriter.Feature.WriteMapNullValue));
        R<List<AdminUserRespDTO>> success = success(UserConvert.INSTANCE.convertList4(userService.getUsersForLastDateTime(usersForLastDateTimeReqDTO)));
        log.info("获取增量用户数据->返回结果result: {}", JSON.toJSONString(success,JSONWriter.Feature.WriteMapNullValue));
        return success;
    }

    @Override
    public R<PageResult<AdminUserRespDTO>> getUsersForConditions(UsersForConditionsReqDTO usersForConditionsReqDTO) {
        return success(adminUserSuperService.getUsersForConditions(usersForConditionsReqDTO));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUsers(Collection<Long> ids) {
        List<AdminUserDO> users = userService.getUsers(ids);
        return success(UserConvert.INSTANCE.convertList4(users));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUsersByDeptIds(Collection<Long> deptIds) {
        List<AdminUserDO> users = userService.getUsersByDeptIds(deptIds);
        return success(UserConvert.INSTANCE.convertList4(users));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUsersByPostIds(Collection<Long> postIds) {
        List<AdminUserDO> users = userService.getUsersByPostIds(postIds);
        return success(UserConvert.INSTANCE.convertList4(users));
    }

    @Override
    public R<Boolean> updateUserByGPN(UpdateUserReqDTO updateUserReqDTO) {
        LambdaUpdateWrapper<AdminUserDO> updateUser = Wrappers.lambdaUpdate(AdminUserDO.class)
                .eq(AdminUserDO::getGpn, updateUserReqDTO.getGpn())
                .set(StringUtils.isNotBlank(updateUserReqDTO.getSupervisor()), AdminUserDO::getSupervisor, updateUserReqDTO.getSupervisor());
        boolean update = userService.update(updateUser);
        return R.success(update);
    }

    @Override
    public R<Boolean> validUsers(Set<Long> ids) {
        userService.validUsers(ids);
        return success(true);
    }

    @Override
    public R<Boolean> pushHrUsers(UserSyncFullDataReqDTO userSyncFullDataReqDTO) {
        log.info("上游用户同步数据至临时表 pushHrUsers：{}", JSON.toJSONString(userSyncFullDataReqDTO,JSONWriter.Feature.WriteMapNullValue));
        userService.userSyncToTemp(userSyncFullDataReqDTO);
        return  success(true);
    }



    @Override
    public R<Boolean> saveSwitchUser(UserSwitchRecordReqDTO userSwitchRecordReqDTO) {
        userSwitchRecordService.saveSwitchUser(userSwitchRecordReqDTO);
        return  success(true);
    }

    @Override
    public R<Long> querySwitchUser(UserSwitchRecordReqDTO userSwitchRecordReqDTO) {
        Long toUserId = userSwitchRecordService.querySwitchUser(userSwitchRecordReqDTO);
        return success(toUserId);
    }

    @Override
    public R<Boolean> updateSwitchUser(@Valid UserSwitchRecordReqDTO userSwitchRecordReqDTO) {
        userSwitchRecordService.updateSwitchUser(userSwitchRecordReqDTO);
        return  success(true);
    }

}
