package com.ruoyi.appVersion.service.impl;

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

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wechat.VersionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.appVersion.mapper.TAppVersionMapper;
import com.ruoyi.appVersion.domain.TAppVersion;
import com.ruoyi.appVersion.service.ITAppVersionService;

/**
 * APP版本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-16
 */
@Service
public class TAppVersionServiceImpl implements ITAppVersionService 
{
    @Autowired
    private TAppVersionMapper tAppVersionMapper;

    /**
     * 查询APP版本信息
     * 
     * @param fId APP版本信息ID
     * @return APP版本信息
     */
    @Override
    public TAppVersion selectTAppVersionById(Long fId)
    {
        return tAppVersionMapper.selectTAppVersionById(fId);
    }

    /**
     * 查询APP版本信息列表
     * 
     * @param tAppVersion APP版本信息
     * @return APP版本信息
     */
    @Override
    public List<TAppVersion> selectTAppVersionList(TAppVersion tAppVersion)
    {
        return tAppVersionMapper.selectTAppVersionList(tAppVersion);
    }

    /**
     * 新增APP版本信息
     * 
     * @param tAppVersion APP版本信息
     * @return 结果
     */
    @Override
    public int insertTAppVersion(TAppVersion tAppVersion)
    {
        tAppVersion.setCreateTime(DateUtils.getNowDate());
        tAppVersion.setCreateBy(SecurityUtils.getUsername());
        int i = tAppVersionMapper.insertTAppVersion(tAppVersion);
        if (i > 0){
            VersionUtil.setDictCache(tAppVersion.getfParamkey(),tAppVersion.getfParamvalue());
        }
        return i;
    }

    /**
     * 修改APP版本信息
     * 
     * @param tAppVersion APP版本信息
     * @return 结果
     */
    @Override
    public int updateTAppVersion(TAppVersion tAppVersion)
    {
        tAppVersion.setUpdateTime(DateUtils.getNowDate());
        tAppVersion.setUpdateBy(SecurityUtils.getUsername());
        int i = tAppVersionMapper.updateTAppVersion(tAppVersion);
        if (i > 0){
            VersionUtil.clearDictCache(tAppVersion.getfParamkey());
            VersionUtil.setDictCache(tAppVersion.getfParamkey(),tAppVersion.getfParamvalue());
        }
        return i;
    }

    /**
     * 批量删除APP版本信息
     * 
     * @param fIds 需要删除的APP版本信息ID
     * @return 结果
     */
    @Override
    public int deleteTAppVersionByIds(Long[] fIds)
    {
        int i = tAppVersionMapper.updateTAppVersionFstatus(fIds);
        if (i > 0){
            for (Long fId : fIds) {
                TAppVersion tAppVersion = tAppVersionMapper.selectTAppVersionById(fId);
                VersionUtil.clearDictCache(tAppVersion.getfParamkey());
            }
        }
        return i;
    }

    /**
     * 删除APP版本信息信息
     * 
     * @param fId APP版本信息ID
     * @return 结果
     */
    @Override
    public int deleteTAppVersionById(Long fId)
    {
        TAppVersion tAppVersion = tAppVersionMapper.selectTAppVersionById(fId);
        int i = tAppVersionMapper.deleteTAppVersionById(fId);
        if (i > 0){
            VersionUtil.clearDictCache(tAppVersion.getfParamkey());
        }
        return i;
    }

    @Override
    public AjaxResult getAppVersion(Integer integer) {
        Map<String,Object> map = new HashMap<>();
        if (integer == 1){  //1 安卓
            map.put("version",VersionUtil.getDictCache("android_version"));
            map.put("url",VersionUtil.getDictCache("android_url"));
            map.put("plus_msg",VersionUtil.getDictCache("android_plus_msg"));
            map.put("minimum_Version",VersionUtil.getDictCache("android_minimum_Version"));
            return AjaxResult.success("success",map);
        }else if (integer == 2){ //2 ios
            map.put("version",VersionUtil.getDictCache("ios_version"));
            map.put("url",VersionUtil.getDictCache("ios_url"));
            map.put("plus_msg",VersionUtil.getDictCache("ios_plus_msg"));
            map.put("minimum_Version",VersionUtil.getDictCache("ios_minimum_Version"));
            return AjaxResult.success("success",map);
        }
        return AjaxResult.success();
    }
}
