package com.jeequan.jeepay.mch.ctrl.red;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.RedPacketAccountDetail;
import com.jeequan.jeepay.db.entity.RedPacketInfo;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.RedPacketAccountDetailService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * TODO
 * @author crystal
 * @date 2023/12/6 15:11
 */
@RestController
@RequestMapping("/api/redPacketChange")
public class RedPacketAccountDetailController extends CommonCtrl {

    @Autowired private RedPacketAccountDetailService redPacketAccountDetailService;

    /**
     *
     * @return
     */
    @PreAuthorize("hasAuthority('ENT_REDPACKET_CHANGE_LIST')")
    @GetMapping
    public ApiRes list() {
        RedPacketAccountDetail info = getObject(RedPacketAccountDetail.class);
        LambdaQueryWrapper<RedPacketAccountDetail> wrapper = RedPacketAccountDetail.gw(info);
        wrapper.eq(RedPacketAccountDetail::getMchNo, getCurrentMchNo());
        Date[] dateRange = info.buildQueryDateRange();
        if (dateRange[0] != null) {
            wrapper.ge(RedPacketAccountDetail::getUpdatedAt, dateRange[0]);
        }
        if (dateRange[1] != null) {
            wrapper.le(RedPacketAccountDetail::getUpdatedAt, dateRange[1]);
        }
        wrapper.orderByDesc(RedPacketAccountDetail::getUpdatedAt);
        Page<RedPacketAccountDetail> pages = redPacketAccountDetailService.page(getIPage(), wrapper);
        return ApiRes.page(pages);
    }
}
