package com.sooka.web.controller.system;

import java.util.List;

import com.sooka.apply.service.IIntRecordService;
import com.sooka.common.utils.StringUtils;
import com.sooka.system.domain.SysDept;
import com.sooka.system.domain.TUInterfaceinfo;
import com.sooka.system.service.*;
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 com.sooka.common.config.Global;
import com.sooka.common.core.controller.BaseController;
import com.sooka.framework.util.ShiroUtils;
import com.sooka.system.domain.SysMenu;
import com.sooka.system.domain.SysUser;

/**
 * 首页 业务处理
 *
 * @author lei_wang
 */
@Controller
public class SysIndexController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysIndexService sysIndexService;

    @Autowired
    private ITUInterfaceinfoService interfaceinfoService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IIntRecordService iIntRecordService;

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
        mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
        mmap.put("copyrightYear", Global.getCopyrightYear());
        mmap.put("demoEnabled", Global.isDemoEnabled());
        return "index";
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin(ModelMap mmap)
    {
        return "skin";
    }

    /**
     * 系统首页
     * @param mmap
     * @return
     */
    @GetMapping("/system/main")
    public String sysMain(ModelMap mmap)
    {
        //系统版本
        mmap.put("version", Global.getVersion());

        //接口数量
        mmap.put("count", sysIndexService.getInterfaceCountAll());
        //调用频次
        mmap.put("callFrequency", NumberUtils.amountConversion(sysIndexService.callFrequency()));
        //归集频次
        mmap.put("pushFrequency", NumberUtils.amountConversion(sysIndexService.pushFrequency()));
        //共享频次
        mmap.put("pullFrequency", NumberUtils.amountConversion(sysIndexService.pullFrequency()));
        //接入部门
        SysDept sysDept = new SysDept();
        sysDept.setParentId(214L);
        mmap.put("depts", sysIndexService.joinUpDepts(sysDept));

        //接口故障
        mmap.put("faultCount", sysIndexService.getInterfaceCountError());

        //省接口申请频次
        String[] arr = {"share_type_3","share_type_4"};
        mmap.put("province", "[" + StringUtils.join(iIntRecordService.applyFrequency(arr), ",") + "]");
        //市接口申请频次
        String[] arr1 = {"share_type_1","share_type_2"};
        mmap.put("city", "[" + StringUtils.join(iIntRecordService.applyFrequency(arr1), ",") + "]");
        return "main";
    }

    // 系统介绍
    public String main(ModelMap mmap)
    {
        //接口数量
        List<TUInterfaceinfo> list_count = interfaceinfoService.getCount();
        mmap.put("version", Global.getVersion());
        //接口数量
        mmap.put("count", interfaceinfoService.getInterfaceCount("0"));
        //故障接口数量
        mmap.put("faultCount", interfaceinfoService.getInterfaceCount("1"));
        //调用频次
        mmap.put("callFrequency", NumberUtils.amountConversion(interfaceinfoService.callFrequency()));
        //归集频次
        mmap.put("pushFrequency", NumberUtils.amountConversion(interfaceinfoService.pushFrequency()));
        //共享频次
        mmap.put("pullFrequency", NumberUtils.amountConversion(interfaceinfoService.pullFrequency()));
        //接入部门
        SysDept dept = new SysDept();
        dept.setParentId(214L);
        List<SysDept> depts = sysDeptService.selectDeptList(dept);
        mmap.put("depts", depts.size());
        //省接口申请频次
        String[] arr = {"share_type_3","share_type_4"};
        mmap.put("province", "[" + StringUtils.join(iIntRecordService.applyFrequency(arr), ",") + "]");
        //市接口申请频次
        String[] arr1 = {"share_type_1","share_type_2"};
        mmap.put("city", "[" + StringUtils.join(iIntRecordService.applyFrequency(arr1), ",") + "]");
        return "main";
    }

}
