package com.sooka.system.gas.zenner_gas_purchase_center_infor.service.impl;

import com.sooka.common.core.text.Convert;
import com.sooka.common.utils.CacheUtils;
import com.sooka.common.utils.DateUtils;
import com.sooka.framework.util.ShiroUtils;
import com.sooka.system.gas.zenner_gas_purchase_center_infor.domain.TGuijiZennerGasPurchaseCenterInfor;
import com.sooka.system.gas.zenner_gas_purchase_center_infor.mapper.TGuijiZennerGasPurchaseCenterMapper;
import com.sooka.system.gas.zenner_gas_purchase_center_infor.service.ITGuijiZennerGasPurchaseCenterInforService;
import com.sooka.system.service.impl.Guiji_Base_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 真兰收费系统-中心计费购气信息Service业务层处理
 *
 * @author lei
 * @date 2022-08-22
 */
@Service
public class TGuijiZennerGasPurchaseCenterInforServiceImpl extends Guiji_Base_Service implements ITGuijiZennerGasPurchaseCenterInforService
{
    @Resource
    private TGuijiZennerGasPurchaseCenterMapper mapper;

    /**
     * 查询真兰收费系统-中心计费购气信息
     *
     * @param id 真兰收费系统-中心计费购气信息ID
     * @return 真兰收费系统-中心计费购气信息
     */
    @Override
    public TGuijiZennerGasPurchaseCenterInfor selectById(String id)
    {
        return mapper.selectById(id);
    }

    /**
     * 查询真兰收费系统-中心计费购气信息列表
     *
     * @param param 真兰收费系统-中心计费购气信息
     * @return 真兰收费系统-中心计费购气信息
     */
    @Override
    public List<TGuijiZennerGasPurchaseCenterInfor> selectList(TGuijiZennerGasPurchaseCenterInfor param)
    {
        List<TGuijiZennerGasPurchaseCenterInfor> list = mapper.selectList(param);
        param.setOperationStatus(1L);
        param.setParam(param.toString());
        param.setInterfacetype("platform_interfacetype_1");//1 共享 2归集
        param.setInterfaceinfoId("d5af6d05-7023-42bf-b28c-cd9fccfadbd2");
        param.setInterfaceinfoName("共享（市）-真兰收费系统-中心计费购气信息");
        Long deptId = ShiroUtils.getSysUser().getDept().getDeptId();
        String deptName = ShiroUtils.getSysUser().getDept().getDeptName();
        param.setDeptId(deptId);
        param.setDeptName(deptName);
        add_log(param);
        return list;
    }

    /**
     * 获取cache name
     *
     * @return 缓存名
     */
    public Long getTotal(String tableName, String paramStr)
    {
//        Long total = Convert.toLong(CacheUtils.get("guiji-data-count", tableName + "," + paramStr));//cacheName, cacheKey
        String cacheName = tableName + "," + paramStr;
//        Long total = mapper.getCache(cacheName);
        Map cache = mapper.getCache(cacheName);
        Long total;
        if(cache == null){
            String msg = "21:17:55.281 [http-nio-8080-exec-27] INFO  o.a.s.c.e.EhCacheManager - [getCache,158] - Cache with name 'guiji-data-count' does not yet exist.  Creating now.";
            Map map = new HashMap();
            map.put("tableName", tableName);
            total = mapper.getDataCount(map);
            map.put("updateTime", new Date());
            map.put("cacheName", cacheName);
            map.put("total", total);
            mapper.createCache(map);
            System.out.println("分页功能 缓存创建成功：" + tableName);
        }else {
            total = Long.valueOf(cache.get("total").toString());
//            Map map = new HashMap();
//            map.put("tableName", tableName);
//            total = mapper.getDataCount(map);
//            total = Long.valueOf(cache.get("total").toString());
//            final Long count = total;
            System.out.println("分页功能 根据缓存名称获取缓存：" + tableName);
            new Thread() {
                public void run() {
                    try{
                        String updateTime = cache.get("updateTime").toString();
                        System.out.println(updateTime.split("\\.")[0] + "=updateTime");//2022-12-01 15:26:16.0
                        boolean refresh = new Date().getTime() - new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(updateTime.split("\\.")[0]).getTime() > 10 * 60 * 1000;//最多10分钟刷新一次最新数据
                        if(refresh){
                            System.out.println("分页功能 缓存刷新中。。。：" + tableName);
    //                        String updateTime = cache.get("updateTime").toString();
    //                        System.out.println(updateTime + "=updateTime");
                            Map map = new HashMap();
                            map.put("updateTime", new Date());
                            map.put("cacheName", cacheName);
                            mapper.updateCache(map);//更新更新时间 防止多次操作
    //                        System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + interfaceinfo.getInterfaceName() + "更新更新时间 防止多次操作");
                            map.put("tableName", tableName);
                            Long total = mapper.getDataCount(map);//select count(id) from ${tableName} 此处最为耗时
                            map.put("total", total);
                            mapper.refreshCache(map);
                            System.out.println("分页功能 缓存刷新成功：" + tableName);
                        }else {
                            System.out.println("分页功能 缓存已是最新：" + tableName);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }.start();
        }
        return total;
    }

    /**
     * 获取cache name
     *
     * @return 缓存名
     */
    public Long getTotal_abandon(String tableName, String paramStr)
    {
        Long total = Convert.toLong(CacheUtils.get("guiji-data-count", tableName + "," + paramStr));//cacheName, cacheKey
        if(total == null){
            Map map = new HashMap();
            map.put("tableName", tableName);
            total = mapper.getDataCount(map);
            CacheUtils.put("guiji-data-count", tableName + "," + paramStr, total);//cacheName, cacheKey
            System.out.println("分页功能 缓存加入成功：" + tableName);
        }else {
            System.out.println("分页功能 根据缓存名称及键值获取缓存：" + tableName);
        }
        return total;
    }

    /**
     * 新增真兰收费系统-中心计费购气信息
     *
     * @param param 真兰收费系统-中心计费购气信息
     * @return 结果
     */
    @Override
    public int insert(TGuijiZennerGasPurchaseCenterInfor param)
    {
        param.setCreateTime(DateUtils.getNowDate());
        return mapper.insert(param);
    }

    /**
     * 修改真兰收费系统-中心计费购气信息
     *
     * @param param 真兰收费系统-中心计费购气信息
     * @return 结果
     */
    @Override
    public int update(TGuijiZennerGasPurchaseCenterInfor param)
    {
        return mapper.update(param);
    }

    /**
     * 删除真兰收费系统-中心计费购气信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteByIds(String ids)
    {
        return mapper.deleteByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除真兰收费系统-中心计费购气信息信息
     *
     * @param id 真兰收费系统-中心计费购气信息ID
     * @return 结果
     */
    @Override
    public int deleteById(String id)
    {
        return mapper.deleteById(id);
    }
}
