package shop.alien.store.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeClassManage;
import shop.alien.store.service.StoreClassManageService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户端经营课堂
 */
@Api(tags = {"商户端经营课堂"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/storeClasses")
@RequiredArgsConstructor
public class StoreClassManageController {

    private final StoreClassManageService storeClassManageService;

    @ApiOperation("课程列表")
    @GetMapping("/getClasses")
    public R<List<Map<String, Object>>> getClasses(
            @RequestParam(required = false) String className,
            @RequestParam(required = false) String classTeacher,
            @RequestParam(required = false) String classBigType,
            @RequestParam(required = false) String classSmallType) {
        log.info("StoreClassManageController.getClasses?className={},classTeacher={},classBigType={},classSmallType={}", className, classTeacher, classBigType, classSmallType);
        Map<String, Map<String, List<LifeClassManage>>> stores = storeClassManageService.getAllList(className, classTeacher, classBigType, classSmallType);
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Map.Entry<String, Map<String, List<LifeClassManage>>> bigEntry : stores.entrySet()) {
            String bigType = bigEntry.getKey();
            Map<String, List<LifeClassManage>> smallMap = bigEntry.getValue();
            Map<String, Object> bigTypeMap = new HashMap<>();
            bigTypeMap.put("bigType", bigType);
            List<Map<String, Object>> smallTypeList = new ArrayList<>();
            for (Map.Entry<String, List<LifeClassManage>> smallEntry : smallMap.entrySet()) {
                String smallType = smallEntry.getKey();
                List<LifeClassManage> records = smallEntry.getValue();

                Map<String, Object> smallTypeMap = new HashMap<>();
                smallTypeMap.put("smallType", smallType);
                smallTypeMap.put("smallTypes", records);
                smallTypeList.add(smallTypeMap);
            }
            bigTypeMap.put("bigTypes", smallTypeList);
            mapList.add(bigTypeMap);
        }
        return R.data(mapList);
    }

    @ApiOperation("经营课堂搜索")
    @GetMapping("/classesSelect")
    public R<List<LifeClassManage>> classesSelect(
            @RequestParam(required = false) String keyword) {
        Map<String, List<LifeClassManage>> stores = storeClassManageService.classesSelect(keyword);
        List<LifeClassManage> allRecords = new ArrayList<>();
        for (Map.Entry<String, List<LifeClassManage>> entry : stores.entrySet()) {
            allRecords.addAll(entry.getValue());
        }
        return R.data(allRecords);
    }
}
