package com.aidex.web.controller.system;

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

import cn.hutool.core.date.DateUtil;
import com.aidex.common.annotation.Log;
import com.aidex.common.constant.Constants;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.framework.cache.DictUtils;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.system.domain.SysMiniappDailyVisit;
import com.aidex.system.service.SysMiniappDailyVisitService;

/**
 * 小程序日数据统计Controller
 * @author ChenSir
 * @email 914769835
 * @date 2024-03-18
 */
@RestController
@RequestMapping("/system/sysMiniappDailyVisit")
public class SysMiniappDailyVisitController extends BaseController {

    @Autowired
    private SysMiniappDailyVisitService sysMiniappDailyVisitService;

    /**
     * 查询小程序日数据统计列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysMiniappDailyVisit:list')")
    @GetMapping("/list")
    public R<PageInfo> list(SysMiniappDailyVisit sysMiniappDailyVisit, HttpServletRequest request, HttpServletResponse response) {
        sysMiniappDailyVisit.setPage(new PageDomain(request, response));
        return R.data(sysMiniappDailyVisitService.findPage(sysMiniappDailyVisit));
    }

    /**
     * 获取小程序日数据统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysMiniappDailyVisit:query')")
    @GetMapping(value = "/{id}")
    public R<SysMiniappDailyVisit> detail(@PathVariable("id") String id) {
        return R.data(sysMiniappDailyVisitService.get(id));
    }

    /**
     * 新增小程序日数据统计
     */
    @PreAuthorize("@ss.hasPermi('system:sysMiniappDailyVisit:add')")
    @Log(title = "小程序日数据统计", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  SysMiniappDailyVisit sysMiniappDailyVisit) {
        return R.status(sysMiniappDailyVisitService.save(sysMiniappDailyVisit));
    }

    /**
     * 修改小程序日数据统计
     */
    @PreAuthorize("@ss.hasPermi('system:sysMiniappDailyVisit:edit')")
    @Log(title = "小程序日数据统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated SysMiniappDailyVisit sysMiniappDailyVisit) {
        return R.status(sysMiniappDailyVisitService.save(sysMiniappDailyVisit));
    }

    /**
     * 删除小程序日数据统计
     */
    @PreAuthorize("@ss.hasPermi('system:sysMiniappDailyVisit:remove')")
    @Log(title = "小程序日数据统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(sysMiniappDailyVisitService.deleteSysMiniappDailyVisitByIds(ids));
    }


    /**
     * 导出小程序日数据统计列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysMiniappDailyVisit:export')")
    @Log(title = "小程序日数据统计", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(SysMiniappDailyVisit sysMiniappDailyVisit) {
        List<SysMiniappDailyVisit> list = sysMiniappDailyVisitService.findList(sysMiniappDailyVisit);
        ExcelUtil<SysMiniappDailyVisit> util = new ExcelUtil<SysMiniappDailyVisit>(SysMiniappDailyVisit.class);
        return util.exportExcel(list, "小程序日数据统计数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    /**
     * 首页折线图小程序统计数据
     */
    @GetMapping(value = "/getSysMiniappDailyVisit/{statisticsType}")
    public R getSysMiniappDailyVisit(@PathVariable String statisticsType) {
        if (Constants.STATISTICS_TYPE_WEEK.equals(statisticsType)){
            return R.data(sysMiniappDailyVisitService.getSysMiniappDailyVisit(statisticsType, DateUtil.beginOfWeek(new Date()), DateUtil.endOfWeek(new Date())));
        } else if (Constants.STATISTICS_TYPE_MONTH.equals(statisticsType)){
            return R.data(sysMiniappDailyVisitService.getSysMiniappDailyVisit(statisticsType, DateUtil.beginOfMonth(new Date()), DateUtil.endOfMonth(new Date())));
        } else if (Constants.STATISTICS_TYPE_YEAR.equals(statisticsType)){
            return R.data(sysMiniappDailyVisitService.getSysMiniappDailyVisit(statisticsType, DateUtil.beginOfYear(new Date()), DateUtil.endOfYear(new Date())));
        }else {
            return R.data(null,"统计类型错误！");
        }
    }

}
