package com.jeeplus.sys.feign;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.SecurityUtils;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.common.utils.RequestUtils;
import com.jeeplus.sys.domain.User;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.sys.service.dto.CertDTO;
import com.jeeplus.sys.service.dto.DataRuleDTO;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.service.mapstruct.UserWrapper;
import com.jeeplus.sys.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
public class UserApiImpl implements IUserApi {

    @Autowired
    private UserService userService;

    @Override
    public List<UserDTO> getAllUserInfos() {
        return userService.getAllUserInfoByTentId();
    }

    @Override
    public Integer selectCountByMobile(String mobile) {
        Integer num = userService.selectCountByMobile(mobile);
        return num;
    }

    @Override
    public Map<String, Object> sendRandomCodes(String mobile, String randomCode) throws IOException {
        return UserUtils.sendRandomCodes(mobile, randomCode);
    }

    @Override
    public UserDTO getByLoginName(String loginName, String tenantId) {
        if ( StrUtil.isBlank ( loginName ) || StrUtil.isBlank ( tenantId ) ) {
            return null;
        }
        UserDTO userDTO = userService.getUserByLoginName ( loginName, tenantId );
        return userDTO;
    }

    @Override
    public UserDTO getByLoginNameNotTenantId(String loginName) {
        if ( StrUtil.isBlank ( loginName )) {
            return null;
        }
        UserDTO userDTO = userService.getByLoginNameNotTenantId ( loginName);
        return userDTO;
    }

    @Override
    public void updateUser(UserDTO userDTO) {
        userService.updateById ( UserWrapper.INSTANCE.toEntity ( userDTO ) );
    }

    @Override
    public void deleteById(String id) {
        userService.updateDelFlagById(id);
    }


    @Override
    public void clearCache(UserDTO userDTO) {
        UserUtils.deleteCache ( userDTO );
    }

    @Override
    public List <DataRuleDTO> getDataRuleList(UserDTO userDTO) {
        return UserUtils.getDataRuleListByUserId ( userDTO.getId ( ) );
    }

    @Override
    public Set <String> getPermissions(String loginName) {
        return UserUtils.getPermissionsByLoginName ( loginName, TenantUtils.getTenantId ( ) );
    }

    @Override
    public UserDTO getByToken(String token) {
        String loginName = TokenProvider.getLoginName ( token );
        String tenantId = TenantUtils.getTenantId ( );
        return UserUtils.getByLoginName ( loginName, tenantId );
    }

    @Override
    public UserDTO getById(String id) {
        return UserUtils.get ( id );
    }

    @Override
    public String getByIdForXXL(String id) {
        UserDTO userDTO = UserUtils.get(id);
        String s = JSON.toJSONString(userDTO);
        return s;
    }

    @Override
    public UserDTO getFlowAbleById(String id) {
        return userService.getFlowAbleById ( id );
    }

    @Override
    public List <UserDTO> findListByPostId(String postId) {
        return userService.findListByPostId ( postId );
    }

    @Override
    public List<UserDTO> getAllUserInfo() {
        return userService.getAllUserInfo();
    }

    @Override
    public List<UserDTO> findListFlowAbleByPostId(String postId) {
        return userService.findListFlowAbleByPostId ( postId );
    }

    @Override
    public List<String> findIdListFlowAbleByPostId(String postId) {
        List<UserDTO> listFlowAbleByPostId = userService.findListFlowAbleByPostId(postId);
        List<String> ids = new ArrayList<>();
        listFlowAbleByPostId.forEach(li->{
            ids.add(li.getId());
        });
        return ids;
    }

    @Override
    public List <UserDTO> findListByRoleId(String roleId) {
        return userService.findListByRoleId ( roleId );
    }

    @Override
    public List <UserDTO> findListByCompanyId(String companyId) {
        return userService.lambdaQuery ( ).eq ( User::getCompanyId, companyId ).list ( ).stream ( ).map ( UserWrapper.INSTANCE::toDTO ).collect ( Collectors.toList ( ) );
    }

    @Override
    public List <UserDTO> findListByOfficeId(String officeId) {
        return userService.lambdaQuery ( ).eq ( User::getOfficeId, officeId ).list ( ).stream ( ).map ( UserWrapper.INSTANCE::toDTO ).collect ( Collectors.toList ( ) );
    }

    @Override
    public boolean isEnableLogin(String tenantId, String loginName) {
        return userService.isEnableLogin ( tenantId, loginName );
    }

    @Override
    public UserDTO getUserDTOByName(String name) {
        return UserWrapper.INSTANCE.toDTO ( userService.lambdaQuery ( ).eq ( User::getTenantId, TenantUtils.getTenantId ( ) ).eq ( User::getName, name ).one ( ) );
    }

    /**
     * 修改用户更新密码状态信息
     * @param userDTO
     */
    @Override
    public void updateUserUpPassword(UserDTO userDTO) {
        userService.updateUserUpPassword (userDTO);
    }

    @Override
    public String getSysParam(String parameter,String token) {
        return UserUtils.getSysParamByToken(parameter,token);
    }

    @Override
    public List<CertDTO> getCertListByUserId() {
        return userService.getCertListByUserId();
    }

    @Override
    public String getFileDir(String url) {
        return FileKit.getFileDir(url);
    }

    @Override
    public boolean delFile(String id) {
        return FileUtils.delFile(id);
    }

    @Override
    public List<UserDTO> getUserInfoByEnName(String enName) {
        return userService.getUserInfoByEnName(enName);
    }

    @Override
    public List<User> selectListByName(String name) {
        return userService.selectListByName(name);
    }

    @Override
    public List<User> getUserByOfficeAll(String operatorOffice) {
        return userService.getUserByOfficeAll(operatorOffice);
    }

    @Override
    public IPage<UserDTO> findPage() {
        Page<UserDTO> page = new Page<>();
        page.setCurrent(1);
        page.setSize(-1);
        return userService.findPage(page, new QueryWrapper<UserDTO>());
    }

    @Override
    public void saveOrUpdate(UserDTO userDTO) {
        userService.saveOrUpdate(userDTO);
    }

    @Override
    public String enterpriseSearchByName(String keyword) {
        return TicketQueryUtils.enterpriseSearchByName(keyword);
    }

    @Override
    public String enterpriseTicketInfoQueryById(String id) {
        return TicketQueryUtils.enterpriseTicketInfoQueryById(id);
    }

    @Override
    public String getUserIdByName(String name) {
        return userService.getUserIdByName(name) ;
    }

    @Override
    public Integer getUserPostCountById(String id) {
        return userService.getUserPostCountById(id) ;
    }

}
