package com.tourism.webadmin.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.*;
import com.tourism.webadmin.back.dao.SysUserWebMapper;
import com.tourism.webadmin.back.model.SysUserWeb;
import com.tourism.webadmin.back.service.SysUserWebService;
import com.tourism.webadmin.upms.model.constant.SysUserStatus;
import com.tourism.common.ext.base.BizWidgetDatasource;
import com.tourism.common.core.base.dao.BaseDaoMapper;
import com.tourism.common.core.constant.GlobalDeletedFlag;
import com.tourism.common.core.object.*;
import com.tourism.common.core.base.service.BaseService;
import com.tourism.common.sequence.wrapper.IdGeneratorWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 用户管理数据操作服务类。
 *
 * @author 吃饭睡觉
 * @date 2024-09-06
 */
@Slf4j
@Service("sysUserWebService")
public class SysUserWebServiceImpl extends BaseService<SysUserWeb, Long> implements SysUserWebService, BizWidgetDatasource {

    @Autowired
    private SysUserWebMapper sysUserWebMapper;
    @Autowired
    private IdGeneratorWrapper idGenerator;
    @Autowired
    private PasswordEncoder passwordEncoder;
    /**
     * 获取指定登录名的用户对象。
     *
     * @param loginName 指定登录用户名。
     * @return 用户对象。
     */
    @Override
    public SysUserWeb getSysUserByLoginName(String loginName) {
        SysUserWeb filter = new SysUserWeb();
        filter.setLoginName(loginName);
        return sysUserWebMapper.selectOne(new QueryWrapper<>(filter));
    }

    @Override
    public SysUserWeb getSysUserByMobile(String mobile) {
        SysUserWeb filter = new SysUserWeb();
        filter.setMobile(mobile);
        return sysUserWebMapper.selectOne(new QueryWrapper<>(filter));
    }

    @Override
    public SysUserWeb saveNew(SysUserWeb user) {
        user.setUserId(idGenerator.nextLongId());
        user.setPassword(user.getPassword());
        user.setUserStatus(SysUserStatus.STATUS_NORMAL);
        user.setDeletedFlag(GlobalDeletedFlag.NORMAL);
        user.setCreateUserId(user.getUserId());
        user.setCreateTime(new Date());
        user.setUpdateUserId(user.getUserId());
        user.setUpdateTime(new Date());
        sysUserWebMapper.insert(user);
        return user;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean changePassword(Long userId, String newPass) {
        SysUserWeb updatedUser = new SysUserWeb();
        updatedUser.setUserId(userId);
        updatedUser.setPassword(passwordEncoder.encode(newPass));
        return sysUserWebMapper.updateById(updatedUser) == 1;
    }
    @Override
    protected BaseDaoMapper<SysUserWeb> mapper() {
        return null;
    }

    @Override
    public MyPageData<Map<String, Object>> getDataList(String widgetType, Map<String, Object> filter, MyOrderParam orderParam, MyPageParam pageParam) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getDataListWithInList(String widgetType, String fieldName, List<String> fieldValues) {
        return null;
    }
}
