package com.ruoyi.web.controller.system;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.GtgsywMapper;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.impl.QyInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 首页 业务处理
 *
 * @author ruoyi
 */
@Controller
public class SysIndexController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private QyInfoServiceImpl qyInfoService;
    @Autowired
    private GtgsywMapper gtgsywService;
    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
       if(user==null){
           return "login";
       }
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        List<String> role= roleService.selRole(user.getUserId());
        for (String ro:role) {
            System.err.println(ro);
        }
        mmap.put("role",role);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("copyrightYear", Global.getCopyrightYear());
        mmap.put("fwip", Global.getConfig("ruoyi.fwip"));
        mmap.put("demoEnabled", Global.isDemoEnabled());
        return "index";
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap, Model model) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String end = (formatter.format(new Date()));
        String nian = end.substring(0, 5);
        String start = nian + "01-01 00:00:00";
        String yue = end.substring(0, 7);
        String yuestar = yue + "-01 00:00:00";
        String taday = end.substring(0, 10);
        String tadaystar = taday + " 00:00:00";
        Map a = qyInfoService.selectQyInfoByDate(start, end);
        Map b = gtgsywService.selectByDate(start, end);
        Map byqy = qyInfoService.selectQyInfoByDate(yuestar, end);
        Map bygt = gtgsywService.selectByDate(yuestar, end);
        Map jtqy = qyInfoService.selectQyInfoByDate(tadaystar, end);
        Map jtgt = gtgsywService.selectByDate(tadaystar, end);
        System.err.println(jtqy.get("qc1"));
        if (a.get("qc1").toString().equals("0")){
            a.put("qs1", 0);
        }
        if (b.get("gc1").toString().equals("0")) {
            b.put("gs1", 0);
        }

        if (byqy.get("qs1") == null) {
            byqy.put("qc1", "0");
        }
        if (bygt.get("gc1") == null) {
            jtgt.put("gc1", "0");
        }
        if (bygt.get("gs1") == null) {
            jtqy.put("qc1", "0");
        }
        model.addAttribute("qy", a);
        model.addAttribute("gt", b);
        model.addAttribute("byqy", byqy);
        model.addAttribute("bygt", bygt);
        model.addAttribute("jtqy", jtqy);
        model.addAttribute("jtgt", jtgt);

        mmap.put("version", Global.getVersion());
        return "main";
    }
}
