package com.jeequan.jeepay.mgr.ctrl.statistics;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.StatsTaskRecord;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.StatsTaskRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 统计任务记录接口
 *
 * @author zx
 *
 * @date 2022/5/23 16:52
 */
@Slf4j
@RestController
@RequestMapping("/api/stats/record")
public class StatsTaskRecordController extends CommonCtrl {

    @Autowired private StatsTaskRecordService statsTaskRecordService;

    /**
     * @author: zx
     * @date: 2022/5/23 17:15
     * @describe: 列表
     */
    @PreAuthorize("hasAnyAuthority('ENT_STATISTIC_RECORD_LIST')")
    @GetMapping
    public ApiRes list() {
        StatsTaskRecord record = getObject(StatsTaskRecord.class);

        IPage<StatsTaskRecord> page = statsTaskRecordService.page(getIPage(), StatsTaskRecord.gw()
                .eq(record.getState() != null, StatsTaskRecord::getState, record.getState())
                .eq(record.getTaskType() != null, StatsTaskRecord::getTaskType, record.getTaskType())
                .ge(record.getBeginTime() != null, StatsTaskRecord::getBeginTime, record.getBeginTime())
                .le(record.getEndTime() != null, StatsTaskRecord::getEndTime, record.getEndTime())
                .orderByDesc(StatsTaskRecord::getCreatedAt)
        );
        return ApiRes.page(page);
    }

    /**
     * @author: zx
     * @date: 2022/5/23 17:15
     * @describe: 详情
     */
    @PreAuthorize("hasAuthority('ENT_STATISTIC_RECORD_VIEW')")
    @GetMapping("/{id}")
    public ApiRes repair(@PathVariable("id") Long id) {
        return ApiRes.ok(statsTaskRecordService.getById(id));
    }

}
