package com.ruoyi.applet.contoller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyl.h5.config.SecurityUtil;
import com.cyl.manager.oms.domain.OrderItem;
import com.cyl.manager.oms.mapper.OrderItemMapper;
import com.cyl.manager.ums.domain.Member;
import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.model.domain.AppletOrderDateFrequency;
import com.ruoyi.model.domain.AppletOrderItemDateLog;
import com.ruoyi.model.service.IAppletOrderDateFrequencyService;
import com.ruoyi.model.service.IAppletOrderItemDateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 新版本小程序日订单Controller
 * 
 * @author daixiande
 * @date 2025-05-28
 */
@RestController
@RequestMapping("/applet/appletOrderDateFrequency")
public class AppletOrderDateFrequencyAppletController extends BaseController {

    @Autowired
    private IAppletOrderDateFrequencyService appletOrderDateFrequencyService;

    @Autowired
    private IAppletOrderItemDateLogService appletOrderItemDateLogService;

    /**
     * 查询日订单列表
     */
    @GetMapping("/list")
    public AjaxResult list(AppletOrderDateFrequency frequency) {

        List<AppletOrderDateFrequency> list = appletOrderDateFrequencyService.selectAppletOrderDateFrequencyList(frequency);

        // 按日期分组
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, List<AppletOrderDateFrequency>> groupedByDate = list.stream()
            .collect(Collectors.groupingBy(item -> formatter.format(item.getServiceDate())));

        // 转换为目标格式，按日期排序
        List<Map<String, Object>> result = new ArrayList<>();
        groupedByDate.entrySet().stream()
            .sorted(Map.Entry.comparingByKey())
            .forEach(entry -> {
                Map<String, Object> group = new HashMap<>();
                group.put("date", entry.getKey());
                group.put("list", entry.getValue());
                result.add(group);
            });

        return success(result);
    }

    /**
     * 获取日订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        AppletOrderDateFrequency frequency = appletOrderDateFrequencyService.selectAppletOrderDateFrequencyById(id);
        AppletOrderItemDateLog check = appletOrderItemDateLogService.selectAppletOrderItemDateLogByItemDateId(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("frequency", frequency);
        map.put("check", check);
        return AjaxResult.success(map);
    }

    /**
     * 打卡
     */
    @PostMapping(value = "/check")
    @RepeatSubmit
    public AjaxResult check(@RequestBody AppletOrderItemDateLog log) {
        return AjaxResult.success(appletOrderDateFrequencyService.check(log));
    }

}
