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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.CheckChannelBill;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.CheckChannelBillService;
import org.apache.commons.lang3.StringUtils;
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;

import java.util.Date;

/**
 * 渠道账单管理类
 *
 * @author zx
 *
 * @date 2021-06-07 07:15
 */
@RestController
@RequestMapping("/api/channel/bill")
public class ChannelBillController extends CommonCtrl {

    @Autowired private CheckChannelBillService checkChannelBillService;

    /**
     * @author: zx
     * @describe: 列表
     */
    @PreAuthorize("hasAuthority('ENT_CHECK_CHANNEL_BILL_LIST')")
    @GetMapping("")
    public ApiRes list() {
        CheckChannelBill channelBill = getObject(CheckChannelBill.class);

        // 时间范围条件
        Date[] dateRange = channelBill.buildQueryDateRange();

        IPage<CheckChannelBill> pages = checkChannelBillService.page(getIPage(true), CheckChannelBill.gw()
                .eq(StringUtils.isNotBlank(channelBill.getBatchNo()), CheckChannelBill::getBatchNo, channelBill.getBatchNo())
                .eq(StringUtils.isNotBlank(channelBill.getIfCode()), CheckChannelBill::getIfCode, channelBill.getIfCode())
                .eq(StringUtils.isNotBlank(channelBill.getBillType()), CheckChannelBill::getBillType, channelBill.getBillType())
                .ge(dateRange[0] != null, CheckChannelBill::getBillDate, dateRange[0])
                .le(dateRange[1] != null, CheckChannelBill::getBillDate, dateRange[1])
                .orderByDesc(CheckChannelBill::getCreatedAt)
        );

        setIfName(pages.getRecords());

        return ApiRes.page(pages);
    }

    /**
     * @author: zx
     * @describe: 统计
     */
    @PreAuthorize("hasAuthority('ENT_CHECK_CHANNEL_BILL_COUNT')")
    @GetMapping("/count")
    public ApiRes count() {

        CheckChannelBill channelBill = getObject(CheckChannelBill.class);
        JSONObject paramJSON = getReqParamJSON();
        JSONObject resJson = checkChannelBillService.billCount(channelBill, paramJSON);
        return ApiRes.ok(resJson);
    }

    /**
     * @author: zx
     * @describe: 详情
     */
    @PreAuthorize("hasAuthority('ENT_CHECK_CHANNEL_BILL_VIEW')")
    @GetMapping("/{id}")
    public ApiRes detail(@PathVariable("id") Long id) {
        CheckChannelBill channelBill = checkChannelBillService.getById(id);
        if (channelBill == null) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_SEARCH);
        }
        return ApiRes.ok(channelBill);
    }
}
