package com.mini.capi.api.biz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mini.capi.biz.domain.ErpSummaryAllView;
import com.mini.capi.biz.domain.ErpSummarySourceView;
import com.mini.capi.biz.service.ErpSummaryAllViewService;
import com.mini.capi.biz.service.ErpSummarySourceViewService;
import com.mini.capi.model.ApiResult;
import com.mini.capi.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@RestController
@RequestMapping("/appApi")
public class appController {


    @Resource
    private ErpSummaryAllViewService summaryAllViewService;


    @Resource
    private ErpSummarySourceViewService summarySourceViewService;


    /**
     * 实时汇总
     */
    @GetMapping("getSummaryAllChart")
    public ApiResult<?> getSummaryAllChart(String cycleType) {
        QueryWrapper<ErpSummaryAllView> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(cycleType)) {
            queryWrapper.eq("f_cycle", cycleType);
            String cDate = DateUtils.calculateStartCycleCode(cycleType);
            if (cDate != null) {
                queryWrapper.ge("c_date", cDate);
            }
        }
        List<ErpSummaryAllView> summaryViews = summaryAllViewService.list(queryWrapper);
        return ApiResult.success(summaryViews);
    }

    /**
     * 分类汇总
     */
    @GetMapping("getSummarySourceChart")
    public ApiResult<?> getSummarySourceChart(String cycleType, String source) {
        QueryWrapper<ErpSummarySourceView> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(source)) {
            queryWrapper.eq("f_source", source);
        }
        if (StringUtils.hasText(cycleType)) {
            queryWrapper.eq("f_cycle", cycleType);
            String cDate = DateUtils.calculateStartCycleCode(cycleType);
            if (cDate != null) {
                queryWrapper.ge("c_date", cDate);
            }
        }
        List<ErpSummarySourceView> summaryViews = summarySourceViewService.list(queryWrapper);
        return ApiResult.success(summaryViews);
    }
}
