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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.PayOrderDivisionRefundRecord;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.PayOrderDivisionRefundRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分账回退记录
 *
 * @author terrfly
 *
 * @date 2022-06-10 17:50
 */
@RestController
@RequestMapping("api/division/refunds/records")
public class PayOrderDivisionRefundRecordController extends CommonCtrl {

	@Autowired private PayOrderDivisionRefundRecordService payOrderDivisionRefundRecordService;

	/** list */
	@PreAuthorize("hasAnyAuthority( 'ENT_DIVISION_REFUND_RECORD_LIST' )")
	@RequestMapping(value="", method = RequestMethod.GET)
	public ApiRes list() {

		PayOrderDivisionRefundRecord queryObject = getObject(PayOrderDivisionRefundRecord.class);
		queryObject.setMchNo(getCurrentMchNo());
		IPage<PayOrderDivisionRefundRecord> pages = payOrderDivisionRefundRecordService.listByPage(getIPage(), queryObject);
		return ApiRes.page(pages);
	}

	/** detail */
	@PreAuthorize("hasAuthority( 'ENT_DIVISION_REFUND_RECORD_VIEW' )")
	@RequestMapping(value="/{recordId}", method = RequestMethod.GET)
	public ApiRes detail(@PathVariable("recordId") String recordId) {
		PayOrderDivisionRefundRecord record = payOrderDivisionRefundRecordService
				.getOne(PayOrderDivisionRefundRecord.gw()
						.eq(PayOrderDivisionRefundRecord::getMchNo, getCurrentMchNo())
						.eq(PayOrderDivisionRefundRecord::getDivisionRefundId, recordId));
		if (record == null) {
            throw new BizException(ApiCodeEnum.SYS_OPERATION_FAIL_SEARCH);
        }
		return ApiRes.ok(record);
	}

}
