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

import com.alibaba.fastjson.JSONObject;
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.PayInterfaceDefine;
import com.jeequan.jeepay.db.entity.PayUser;
import com.jeequan.jeepay.db.entity.RedPacketAccount;
import com.jeequan.jeepay.db.entity.SettleInfo;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.PayInterfaceDefineService;
import com.jeequan.jeepay.service.impl.RedPacketAccountService;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

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

    @Autowired private RedPacketAccountService redPacketAccountService;

    @PreAuthorize("hasAuthority('ENT_REDPACKET_USER_LIST')")
    @GetMapping
    public ApiRes list() {
        LambdaQueryWrapper<RedPacketAccount> wrapper = initQuery();
        wrapper.orderByDesc(RedPacketAccount::getUpdatedAt);
        Page<RedPacketAccount> pages = redPacketAccountService.page(getIPage(), wrapper);
        return ApiRes.page(pages);
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public ApiRes count() {
        LambdaQueryWrapper<RedPacketAccount> wrapper = initQuery();
        Map<String,Object> resultMap = redPacketAccountService.stsCount(wrapper);
        return ApiRes.ok(resultMap);
    }

    private LambdaQueryWrapper<RedPacketAccount> initQuery() {
        RedPacketAccount info = getObject(RedPacketAccount.class);
        LambdaQueryWrapper<RedPacketAccount> wrapper = RedPacketAccount.gw(info);
        wrapper.eq(RedPacketAccount::getMchNo, getCurrentMchNo());
        Date[] dateRange = info.buildQueryDateRange();
        if (dateRange[0] != null) {
            wrapper.ge(RedPacketAccount::getUpdatedAt, dateRange[0]);
        }
        if (dateRange[1] != null) {
            wrapper.le(RedPacketAccount::getUpdatedAt, dateRange[1]);
        }
        return wrapper;
    }
}
