package com.fs.stats;

import com.fs.common.core.domain.AjaxResult;
import com.fs.statis.service.IFsStatsMemberDailyService;
import com.fs.statis.vo.FsStatsMemberDailyVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stats/member")
@AllArgsConstructor
public class FsStatsMemberController {

    private final IFsStatsMemberDailyService statsMemberDailyService;

    @GetMapping("/dailyData")
    public AjaxResult dailyData(@RequestParam String startDate,
                                @RequestParam String endDate,
                                @RequestParam Integer type,
                                @RequestParam(required = false) Long companyId,
                                @RequestParam(required = false) Long companyUserId,
                                @RequestParam(required = false) Long userId,
                                @RequestParam(required = false) String phone,
                                @RequestParam(required = false) Long trainCampId,
                                @RequestParam(required = false) Long periodId,
                                @RequestParam(required = false) Long courseId,
                                @RequestParam(required = false) Long videoId,
                                @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("companyId", companyId);
        params.put("companyUserId", companyUserId);
        params.put("userId", userId);
        params.put("phone", phone);
        params.put("trainCampId", trainCampId);
        params.put("periodId", periodId);
        params.put("courseId", courseId);
        params.put("videoId", videoId);

        if (type == 1) {
            params.put("startDate", LocalDate.parse(startDate));
            params.put("endDate", LocalDate.parse(endDate).plusDays(1));
        } else {
            params.put("startDate", LocalDate.parse(startDate).withDayOfMonth(1));
            params.put("endDate", LocalDate.parse(endDate).withDayOfMonth(1).plusMonths(1));
        }

        PageHelper.startPage(pageNum, pageSize);
        List<FsStatsMemberDailyVO> list = statsMemberDailyService.selectDailyData(params);
        return AjaxResult.success(new PageInfo<>(list));
    }
}
