package com.fjhx.acc.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.acc.mapper.AccAccountMapper;
import com.fjhx.acc.service.AccAccountService;
import com.fjhx.entity.acc.AccAccount;
import com.fjhx.params.acc.AccAccountVo;
import com.fjhx.params.acc.AccountExcel;
import com.fjhx.utils.WrapperUtil;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 全局账号 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-08-15
 */
@Service
public class AccAccountServiceImpl extends ServiceImpl<AccAccountMapper, AccAccount> implements AccAccountService {

    @Autowired
    private BladeRedis bladeRedis;

    @Override
    public Page<AccAccount> getPage(Map<String, String> condition) {

        QueryWrapper<AccAccount> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .createTimeDesc();

        Page<AccAccount> page = page(condition, wrapper);
        return page;
    }

    @Override
    public void add(AccAccountVo accAccountVo) {
        save(accAccountVo);
    }

    @Override
    public void edit(AccAccountVo accAccountVo) {
        updateById(accAccountVo);
    }

    @Override
    public void delete(AccAccountVo accAccountVo) {
        removeById(accAccountVo.getId());
    }

    @Override
    public List<AccountExcel> refreshPassword() {

        List<Map<String, Object>> list = baseMapper.getList();

        List<AccountExcel> accountExcelList = new ArrayList<>();

        List<AccAccount> accAccountList = list.stream().map(item -> {
            AccountExcel accountExcel = BeanUtil.toBean(item, AccountExcel.class);
            String password = RandomUtil.randomString(8);
            String md5 = SecureUtil.md5(password);
            accountExcel.setPassword(password);
            accountExcelList.add(accountExcel);

            AccAccount accAccount = new AccAccount();
            accAccount.setId(item.get("id").toString());
            accAccount.setPassword(md5);
            return accAccount;
        }).collect(Collectors.toList());

        // 批量修改密码
        updateBatchById(accAccountList);

        Set<String> keys = bladeRedis.keys("*");
        bladeRedis.del(keys);

        return accountExcelList;
    }

    @Override
    public String updatePassword(String id) {

        String password = RandomUtil.randomString(8);
        String md5 = SecureUtil.md5(password);


        AccAccount accAccount = new AccAccount();
        accAccount.setId(id);
        accAccount.setPassword(md5);

        updateById(accAccount);

        Set<String> keys = bladeRedis.keys("list:*");
        bladeRedis.del(keys);

//        bladeRedis.del("list:account");
//        bladeRedis.del("list:user");
        return password;
    }


    @Override
    public Object test(String key) {

        Set<String> keys = bladeRedis.keys("*");

        Object o = bladeRedis.get(key);
        return o;
    }

}
