package com.hys.app.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hys.app.framework.util.JsonUtil;
import com.hys.app.mapper.system.SmsPlatformMapper;
import com.hys.app.model.base.CachePrefix;
import com.hys.app.model.base.vo.ConfigItem;
import com.hys.app.service.base.plugin.sms.SmsPlatform;
import com.hys.app.service.base.plugin.sms.SmsPlatformConverter;
import com.hys.app.service.system.SmsPlatformManager;
import com.hys.app.model.errorcode.SystemErrorCode;
import com.hys.app.model.system.dos.SmsPlatformDO;
import com.hys.app.model.system.vo.SmsPlatformVO;
import com.hys.app.framework.cache.Cache;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.exception.ResourceNotFoundException;
import com.hys.app.framework.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 短信网关表业务类
 *
 * @author zh
 * @version v7.0.0
 * @since v7.0.0
 * 2018-03-23 11:31:05
 */
@Service
public class SmsPlatformManagerImpl implements SmsPlatformManager {

    @Autowired
    private List<SmsPlatform> smsPlatforms;
    @Autowired
    private Cache cache;
    @Autowired
    private SmsPlatformMapper smsPlatformMapper;

    /**
     * 查询短信网关表列表
     *
     * @param pageNo     页码
     * @param pageSize 每页数量
     * @return WebPage
     */
    @Override
    public WebPage list(long pageNo, long pageSize) {
        List<SmsPlatformVO> resultList = this.getPlatform();
        for (SmsPlatformVO vo : resultList) {
            this.add(vo);
        }
        return new WebPage(pageNo, Integer.valueOf(resultList.size()).longValue(), pageSize, resultList);
    }

    /**
     * 添加短信网关
     *
     * @param smsPlatform 短信网关参数
     * @return 短信网关对象
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SmsPlatformVO add(SmsPlatformVO smsPlatform) {
        SmsPlatformDO smsPlatformDO = new SmsPlatformDO(smsPlatform);
        if (smsPlatformDO.getId() == null || smsPlatformDO.getId() == 0) {
            SmsPlatformDO platformDO = this.getSmsPlateform(smsPlatformDO.getBean());
            if (platformDO != null) {
                throw new ServiceException(SystemErrorCode.E900.code(), "该短信方案已经存在");
            }
            smsPlatformMapper.insert(smsPlatformDO);
        }
        // 更新缓存
        cache.remove(CachePrefix.SPlATFORM.getPrefix());
        return smsPlatform;
    }

    /**
     * 获取短信网关表
     *
     * @param id 短信网关表主键
     * @return Platform  短信网关表
     */
    @Override
    public SmsPlatformDO getModel(Long id) {
        return smsPlatformMapper.selectById(id);
    }

    /**
     * 启用网关
     *
     * @param bean 短信网关beanid
     */
    @Override
    public void openPlatform(String bean) {
        List<SmsPlatformVO> vos = this.getPlatform();
        for (SmsPlatformVO vo : vos) {
            this.add(vo);
        }
        SmsPlatformDO smsPlatformDO = this.getSmsPlateform(bean);
        if (smsPlatformDO == null) {
            throw new ResourceNotFoundException("该短信方案不存在");
        }

        SmsPlatformDO smsPlatformDo = new SmsPlatformDO();
        UpdateWrapper<SmsPlatformDO> wrapper = new UpdateWrapper<>();
        smsPlatformDo.setOpen(0);
        smsPlatformMapper.update(smsPlatformDo, wrapper);
        wrapper.eq("bean", bean);
        smsPlatformDo.setOpen(1);
        smsPlatformMapper.update(smsPlatformDo, wrapper);


        // 更新缓存
        cache.remove(CachePrefix.SPlATFORM.getPrefix());

    }


    /**
     * 获取所有的短信方案
     *
     * @return 所有的短信方案
     */
    private List<SmsPlatformVO> getPlatform() {
        List<SmsPlatformVO> resultList = new ArrayList<>();

        QueryWrapper<SmsPlatformDO> wrapper = new QueryWrapper<>();
        List<SmsPlatformDO> list = smsPlatformMapper.selectList(wrapper);

        Map<String, SmsPlatformDO> map = new HashMap<>(16);

        for (SmsPlatformDO smsPlatformDO : list) {
            map.put(smsPlatformDO.getBean(), smsPlatformDO);
        }
        for (SmsPlatform plugin : smsPlatforms) {
            SmsPlatformDO smsPlatformDO = map.get(plugin.getPluginId());
            SmsPlatformVO result = null;

            if (smsPlatformDO != null) {
                result = new SmsPlatformVO(smsPlatformDO);
            } else {
                result = SmsPlatformConverter.toSmsPlatformVO(plugin);
            }

            resultList.add(result);
        }
        return resultList;
    }

    /**
     * 根据短信网关的beanid 获取短信网关的配置项
     *
     * @param bean 短信网关 beanid
     * @return 短信网关VO
     */
    @Override
    public SmsPlatformVO getConfig(String bean) {
        List<SmsPlatformVO> vos = this.getPlatform();
        for (SmsPlatformVO vo : vos) {
            this.add(vo);
        }
        SmsPlatformDO smsPlatformDO = this.getSmsPlateform(bean);
        if (smsPlatformDO == null) {
            throw new ResourceNotFoundException("该短信网关方案不存在");
        }
        return new SmsPlatformVO(smsPlatformDO);
    }

    /**
     * 获取开启的短信网关
     *
     * @return 短信网关VO
     */
    @Override
    public SmsPlatformVO getOpen() {
        SmsPlatformVO smsPlatformVO = (SmsPlatformVO) this.cache.get(getSmsPlatformKey());
        if (smsPlatformVO == null) {

            QueryWrapper<SmsPlatformDO> wrapper = createQueryWrapper();
            wrapper.eq("open",1);
            SmsPlatformDO smsPlatformDO = smsPlatformMapper.selectOne(wrapper);

            if (smsPlatformDO == null) {
                throw new ResourceNotFoundException("未找到可用的短信网关");
            }
            smsPlatformVO = new SmsPlatformVO();
            smsPlatformVO.setConfig(smsPlatformDO.getConfig());
            smsPlatformVO.setBean(smsPlatformDO.getBean());
            cache.put(getSmsPlatformKey(), smsPlatformVO);
        }
        return smsPlatformVO;
    }

    /**
     * 根据beanid获取短信网关
     *
     * @param bean beanid
     * @return
     */
    @Override
    public SmsPlatformDO getSmsPlateform(String bean) {
        QueryWrapper<SmsPlatformDO> wrapper = new QueryWrapper<>();
        wrapper.eq("bean",bean);
        return smsPlatformMapper.selectOne(wrapper);
    }

    /**
     * 添加短信网关表
     *
     * @param smsPlatform 短信网关vo
     * @return
     */
    @Override
    public SmsPlatformVO edit(SmsPlatformVO smsPlatform) {
        List<SmsPlatformVO> vos = this.getPlatform();
        for (SmsPlatformVO vo : vos) {
            this.add(vo);
        }
        SmsPlatformDO up = this.getSmsPlateform(smsPlatform.getBean());
        if (up == null) {
            throw new ResourceNotFoundException("该短信方案不存在");
        }
        smsPlatform.setId(up.getId());
        smsPlatformMapper.updateById(new SmsPlatformDO(smsPlatform));
        //this.systemDaoSupport.update(new SmsPlatformDO(smsPlatform), up.getId());
        return smsPlatform;
    }

    protected QueryWrapper<SmsPlatformDO> createQueryWrapper() {
        return new QueryWrapper<>();
    }

    protected String getSmsPlatformKey() {
        return CachePrefix.SPlATFORM.getPrefix();
    }

    @Override
    public Map<String, String> getConfigMap(String beanId) {
        Map<String, String> configMap = new HashMap<>();
        SmsPlatformDO aliSmsPlatform = this.getSmsPlateform(beanId);

        String config = aliSmsPlatform.getConfig();
        List<ConfigItem> configItems = JsonUtil.jsonToList(config, ConfigItem.class);

        for (ConfigItem configItem : configItems) {
            configMap.put(configItem.getName(), String.valueOf(configItem.getValue()));
        }
        return configMap;
    }

}
