package com.sooka.system.controller;

import java.io.Serializable;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sooka.common.annotation.Log;
import com.sooka.common.constant.ShiroConstants;
import com.sooka.common.core.controller.BaseController;
import com.sooka.common.core.domain.AjaxResult;
import com.sooka.common.core.page.PageDomain;
import com.sooka.common.core.page.TableDataInfo;
import com.sooka.common.core.page.TableSupport;
import com.sooka.common.core.text.Convert;
import com.sooka.common.enums.BusinessType;
import com.sooka.common.utils.CacheUtils;
import com.sooka.common.utils.poi.ExcelUtil;
import com.sooka.system.domain.TUInterfaceinfo;
import com.sooka.system.mapper.TUInterfaceinfoMapper;
import com.sooka.system.mapper.TULogMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCache;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.xmlbeans.impl.common.SystemCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sooka.system.domain.TULog;
import com.sooka.system.service.ITULogService;

import javax.annotation.Resource;


/**
 * 接口调用日志Controller
 *
 * @author lei
 * @date 2021-07-06
 */
@Controller
@RequestMapping("/system/log")
public class TULogController extends BaseController
{
    private String prefix = "system/log";

    @Autowired
    private ITULogService tULogService;

    @Resource
    private TULogMapper tuLogMapper;

    @Resource
    private TUInterfaceinfoMapper tuInterfaceinfoMapper;


    @RequiresPermissions("system:log:view")
    @GetMapping("/{shareType}")
    public String log(@PathVariable("shareType") String shareType, ModelMap modelMap)
    {
        modelMap.put("shareType",shareType);
        return prefix + "/log";
    }

    /**
     * 跳转日志列表
     */
    @GetMapping("/logList/{id}/{shareType}")
    public String logList(@PathVariable("id") String id, @PathVariable("shareType") String shareType, ModelMap modelMap)
    {
        modelMap.put("intId",id);
        modelMap.put("shareType",shareType);
        return prefix + "/logList";
    }

    /**
     * 查询接口调用日志列表
     */
    @RequiresPermissions("system:log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(String id,String loginName, String platformInterfacetype,String deptName)
    {
        TULog tULog = new TULog();
        tULog.setInterfaceinfoId(id);
        tULog.setLoginName(loginName);
        //update t_u_log set platform_interfacetype = (select share_type from interfaceinfo where id = interfaceinfo_id)
        //Affected rows: 1124113
        //时间: 265.674s
//        tULog.setPlatformInterfacetype(platformInterfacetype);//按照接口id就可以确定唯一的共享类型，也就不需要按照共享类型查询了。
        tULog.setDeptName(deptName);
        startPage();
        List<TULog> list = tULogService.selectTULogList(tULog);
        return getDataTable(list);
        /*PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String limit = "limit " + ((pageNum - 1) * pageSize) + ", " + pageSize;
        Map params = new HashMap();
        params.put("limit", limit);
        tULog.setParams(params);
        List<TULog> idList = tuLogMapper.selectIds(tULog);
        String ids = "";
        for (TULog log : idList) {
            ids += "'" + log.getId() + "',";
        }
        ids = ids.length() == 0 ? "0" : ids.substring(0, ids.length() - 1);
        Map map = new HashMap();
        map.put("ids", ids);
        List<TULog> list = tuLogMapper.selectListbyIds(map);
        TUInterfaceinfo tuInterfaceinfo = tuInterfaceinfoMapper.selectTUInterfaceinfoById(id);
        //update t_u_interfaceinfo set log_count = (select count(id) from t_u_log where interfaceinfo_id = t_u_interfaceinfo.id)
        //-- update t_u_interfaceinfo set callfailnum = log_count where callfailnum = 0
        return getDataTable(list, tuInterfaceinfo.getLogCount());*/
    }

    /**
     * 获取cache name
     *
     * @return 缓存名
     */
    public Long getTotal(TULog tULog)
    {
        Long total = Convert.toLong(CacheUtils.get(tULog.getCacheName(),tULog.getCacheKey()));
        if(total == null){
            total = tULogService.getLogCount();
            CacheUtils.put(tULog.getCacheName(), tULog.getCacheKey(), total);
        }
        return total;
    }

    /**
     * 导出接口调用日志列表
     */
    @RequiresPermissions("system:log:export")
    @Log(title = "接口调用日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TULog tULog)
    {
        List<TULog> list = tULogService.selectTULogList(tULog);
        ExcelUtil<TULog> util = new ExcelUtil<TULog>(TULog.class);
        return util.exportExcel(list, "log");
    }

    /**
     * 新增接口调用日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存接口调用日志
     */
    @RequiresPermissions("system:log:add")
    @Log(title = "接口调用日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TULog tULog)
    {
        return toAjax(tULogService.insertTULog(tULog));
    }

    /**
     * 修改接口调用日志
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        TULog tULog = tULogService.selectTULogById(id);
        mmap.put("tULog", tULog);
        return prefix + "/edit";
    }

    /**
     * 查看接口调用日志详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap) {
        TULog tULog = tULogService.selectTULogById(id);
        mmap.put("tULog", tULog);
        return prefix + "/detail";
    }

        /**
         * 修改保存接口调用日志
         */
    @RequiresPermissions("system:log:edit")
    @Log(title = "接口调用日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TULog tULog)
    {
        return toAjax(tULogService.updateTULog(tULog));
    }

    /**
     * 删除接口调用日志
     */
    @RequiresPermissions("system:log:remove")
    @Log(title = "接口调用日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tULogService.deleteTULogByIds(ids));
    }
}
