package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.store.LifeLog;
import shop.alien.mapper.LifeLogMapper;

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

/**
 * 日志
 */
@Api(tags = {"一期-日志"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/log/")
@RequiredArgsConstructor
public class LifeLogController {

    private final LifeLogMapper lifeLogMapper;

    @GetMapping("/getLogPage")
    public Map<String, Object> getStoresPage(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "10") int size,
                                             @RequestParam(required = false) String createUser,
                                             @RequestParam(required = false) Date startTime,
                                             @RequestParam(required = false) Date endTime) {
        log.info("LifeLogController.getLogPage?page={},size={},createUser={},startTime={},endTime={}", page, size, createUser, startTime, endTime);
        QueryWrapper<LifeLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!createUser.isEmpty(), "created_user_id", createUser);
        queryWrapper.ge(startTime != null, "created_time", startTime);
        queryWrapper.le(endTime != null, "created_time", endTime);
        queryWrapper.orderByDesc("created_time");
        IPage<LifeLog> storePage = new Page<>(page, size);
        IPage<LifeLog> stores = lifeLogMapper.selectPage(storePage, queryWrapper);
        Map<String, Object> response = new HashMap<>();
        response.put("data", stores.getRecords());
        response.put("total", stores.getRecords().size());
        response.put("current", stores.getCurrent());
        response.put("size", stores.getSize());
        return response;
    }
}
