package cn.cslg.pas.service;

import cn.cslg.pas.common.model.vo.UserVO;
import cn.cslg.pas.common.utils.CacheUtils;
import cn.cslg.pas.common.utils.DateUtils;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.domain.User;
import cn.cslg.pas.mapper.UserMapper;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 王岩
 * @since 2021-12-17
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class UserService extends ServiceImpl<UserMapper, User> {

    private final CacheUtils cacheUtils;
    private final RequestService requestService;

    public User getByUsername(String username) {
        return this.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
    }

    public List<User> getUserByIds(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(User::getId, ids);
        return this.list(queryWrapper);
    }

    public List<User> getUserByNames(List<String> names) {
        if (names == null || names.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(User::getName, names);
        return this.list(queryWrapper);
    }

    public String getPageList(UserVO params) throws IOException {
        return requestService.getTenantMessageFromPCS(params);
    }

    public String getPersonnelList(UserVO params) throws IOException {
        return requestService.getPersonnelMessageFromPCS(params);
    }


    public String add(User user) {
        User temp = this.getByUsername(user.getUsername());
        if (temp != null) {
            return Response.error("用户名重复");
        }
        user.setPassword(SecureUtil.md5("123456"));
        user.setCreateTime(DateUtils.getDateTime());
        user.insert();
        return Response.success(user.getId());
    }

    public String edit(User user) {
        User temp = this.getByUsername(user.getUsername());
        if (temp != null && !temp.getId().equals(user.getId())) {
            return Response.error("用户名重复");
        }
        user.updateById();
        cacheUtils.setLoginUser(user);
        return Response.success();
    }

    public String delete(Integer id) {
        this.removeById(id);
        return Response.success();
    }

    public void resetPassword(Integer id) {
        User user = this.getById(id);
        user.setPassword(SecureUtil.md5("123456"));
        user.updateById();
    }
}
