package com.yunzhupaas.base.service.impl;


import com.yunzhupaas.base.model.MailAccount;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunzhupaas.base.entity.EmailConfigEntity;
import com.yunzhupaas.base.entity.SysConfigEntity;
import com.yunzhupaas.base.mapper.SysconfigMapper;
import com.yunzhupaas.base.service.SysconfigService;
import com.yunzhupaas.model.BaseSystemInfo;
import com.yunzhupaas.base.util.*;
import com.yunzhupaas.util.CacheKeyUtil;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.RedisUtil;
import com.yunzhupaas.util.context.SpringContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;

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

/**
 * 系统配置
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date 2023/09/27
 */
@Service
public class SysconfigServiceImpl extends SuperServiceImpl<SysconfigMapper, SysConfigEntity> implements SysconfigService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CacheKeyUtil cacheKeyUtil;
    @Autowired
    private Pop3Util pop3Util;

    @Override
    public List<SysConfigEntity> getList(String type) {
        List<SysConfigEntity> list =new ArrayList<>();
        if("WeChat".equals(type)){
            QueryWrapper<SysConfigEntity> queryWrapper = new QueryWrapper<>();
            list = this.list(queryWrapper).stream().filter(t->"QYHConfig".equals(t.getCategory())||"MPConfig".equals(t.getCategory())).collect(Collectors.toList());
        }
        if("SysConfig".equals(type)){
            QueryWrapper<SysConfigEntity> queryWrapper = new QueryWrapper<>();
            list = this.list(queryWrapper).stream().filter(t->!"QYHConfig".equals(t.getCategory())&&!"MPConfig".equals(t.getCategory())).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public BaseSystemInfo getWeChatInfo() {
        Map<String, String> objModel = new HashMap<>(16);
        List<SysConfigEntity> list = this.getList("WeChat");
        for (SysConfigEntity entity : list) {
            objModel.put(entity.getFkey(), entity.getValue());
        }
        BaseSystemInfo baseSystemInfo = JsonUtil.getJsonToBean(objModel, BaseSystemInfo.class);
        return baseSystemInfo;
    }
    @Override
    public BaseSystemInfo getSysInfo() {
        Map<String, String> objModel = new HashMap<>(16);
        List<SysConfigEntity> list = this.getList("SysConfig");
        for (SysConfigEntity entity : list) {
            objModel.put(entity.getFkey(), entity.getValue());
        }
        BaseSystemInfo baseSystemInfo = JsonUtil.getJsonToBean(objModel, BaseSystemInfo.class);
        return baseSystemInfo;
    }

    @Override
    @DSTransactional
    public void save(List<SysConfigEntity> entitys) {
        String cacheKey = cacheKeyUtil.getSystemInfo();
        redisUtil.remove(cacheKey);
        this.baseMapper.deleteFig();
        for (SysConfigEntity entity: entitys) {
            entity.setCategory("SysConfig");
            this.baseMapper.insert(entity);
        }
    }
    @Override
    @DSTransactional
    public boolean saveMp(List<SysConfigEntity> entitys){
        String cacheKey = cacheKeyUtil.getWechatConfig();
        int flag=0;
        redisUtil.remove(cacheKey);
        this.baseMapper.deleteMpFig();
        for (SysConfigEntity entity: entitys) {
            entity.setCategory("MPConfig");
           if(this.baseMapper.insert(entity)>0){
               flag++;
           }
        }
        if(entitys.size()==flag){
            return true;
        }
        return false;
    }
    @Override
    @DSTransactional
    public void saveQyh(List<SysConfigEntity> entitys){
        String cacheKey = cacheKeyUtil.getWechatConfig();
        redisUtil.remove(cacheKey);
        this.baseMapper.deleteQyhFig();
        for (SysConfigEntity entity: entitys) {
            entity.setCategory("QYHConfig");
            this.baseMapper.insert(entity);
        }
    }

    @Override
    public String checkLogin(EmailConfigEntity configEntity) {
        MailAccount mailAccount = new MailAccount();
        mailAccount.setAccount(configEntity.getAccount());
        mailAccount.setPassword(configEntity.getPassword());
        mailAccount.setPop3Host(configEntity.getPop3Host());
        mailAccount.setPop3Port(configEntity.getPop3Port());
        mailAccount.setSmtpHost(configEntity.getSmtpHost());
        mailAccount.setSmtpPort(configEntity.getSmtpPort());
        if ("1".equals(String.valueOf(configEntity.getEmailSsl()))) {
            mailAccount.setSsl(true);
        } else {
            mailAccount.setSsl(false);
        }
        if (mailAccount.getSmtpHost() != null) {
            return SmtpUtil.checkConnected(mailAccount);
        }
        if (mailAccount.getPop3Host() != null) {
            return pop3Util.checkConnected(mailAccount);
        }
        return "false";
    }

    @Override
    public String getValueByKey(String keyStr) {
        QueryWrapper<SysConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysConfigEntity::getFkey,keyStr);
        SysConfigEntity sysConfigEntity = getOne(queryWrapper);
        return sysConfigEntity.getValue();
    }

}

