package org.linlinjava.jz.admin.web;

import com.google.common.collect.Lists;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.*;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.util.SecurityHelper;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.db.domain.JzBuiltListCount;
import org.linlinjava.jz.db.domain.JzCampaign;
import org.linlinjava.jz.db.service.JzBuiltListService;
import org.linlinjava.jz.db.service.JzCampaignService;
import org.linlinjava.jz.service.TicketService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/index")
public class AdminIndexController {
    private final Log logger = LogFactory.getLog(AdminIndexController.class);
    @Resource
    private TicketService ticketService;
    @Resource
    private JzBuiltListService jzBuiltListService;
    @Resource
    private JzCampaignService jzCampaignService;

    @GetMapping("/index")
    public Object index() {
        Map<String, Object> map = new HashMap<>(16);
        long all = ticketService.selectCountByExample(1, null);
        long myTicket = ticketService.selectCountByExample(1, SecurityHelper.getAdminId());
        long moreThan24 = ticketService.selectCountByExample(2, SecurityHelper.getAdminId());
        long moreThan48 = ticketService.selectCountByExample(3, SecurityHelper.getAdminId());
        long completed = ticketService.selectCountByExample(4, SecurityHelper.getAdminId());
        long processing = ticketService.selectCountByExample(5, SecurityHelper.getAdminId());
        long myBuild = jzBuiltListService.queryCountByCampaignId(null, SecurityHelper.getAdminId());
        List<Map<String, Object>> maps = Lists.newArrayList();

        List<JzCampaign> jzCampaigns = jzCampaignService.queryAllNoDeleted();
        jzCampaigns.forEach(item -> {
            long count = jzBuiltListService.queryCountByCampaignId(item.getId(), SecurityHelper.getAdminId());
            Map<String, Object> map1 = new HashMap<>(16);
            map1.put("name", item.getName());
            map1.put("count", count);
            map1.put("desc", item.getRemark());
            maps.add(map1);
        });

        map.put("all", all);
        map.put("myTicket", myTicket);
        map.put("myBuild", myBuild);
        map.put("moreThan24", moreThan24);
        map.put("moreThan48", moreThan48);
        map.put("completed", completed);
        map.put("processing", processing);
        map.put("jzBuiltListCounts", maps);
        return ResponseUtil.ok(map);
    }

    @RequiresGuest
    @RequestMapping("/guest")
    public Object guest() {
        return ResponseUtil.ok("hello world, this is admin service");
    }

    @RequiresAuthentication
    @RequestMapping("/authn")
    public Object authn() {
        return ResponseUtil.ok("hello world, this is admin service");
    }

    @RequiresUser
    @RequestMapping("/user")
    public Object user() {
        return ResponseUtil.ok("hello world, this is admin service");
    }

    @RequiresRoles("admin")
    @RequestMapping("/admin")
    public Object admin() {
        return ResponseUtil.ok("hello world, this is admin service");
    }

    @RequiresRoles("admin2")
    @RequestMapping("/admin2")
    public Object admin2() {
        return ResponseUtil.ok("hello world, this is admin service");
    }

    @RequiresPermissions("index:permission:read")
    //@RequiresPermissionsDesc(menu = {"其他", "权限测试"}, button = "权限读")
    @GetMapping("/read")
    public Object read() {
        return ResponseUtil.ok("hello world, this is admin service");
    }

    @RequiresPermissions("index:permission:write")
    //@RequiresPermissionsDesc(menu = {"其他", "权限测试"}, button = "权限写")
    @PostMapping("/write")
    public Object write() {
        return ResponseUtil.ok("hello world, this is admin service");
    }


}
