package com.jkcredit.invoice.hub.controller.web.recharge;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.hub.controller.base.WebResource;
import com.jkcredit.invoice.hub.controller.web.recharge.fb.RechargeFB;
import com.jkcredit.invoice.hub.controller.web.recharge.fb.RechargeQueryFB;
import com.jkcredit.invoice.hub.model.dto.recharge.RechargeDto;
import com.jkcredit.invoice.hub.model.vo.recharge.RechargeVo;
import com.jkcredit.invoice.hub.service.recharge.RechargeService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.spi.web.data.ResponseData;
import lombok.extern.slf4j.Slf4j;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/12 20:31
 * @version: V1.0
 **/
@Slf4j
@RestController
public class RechargeResource extends WebResource {
    @Autowired
    RechargeService rechargeService;

    @PreAuthorize("hasPermission('recharge','edit')")
    @PostMapping("/recharge")
    public ResponseData recharge(@RequestBody RechargeFB rechargeFB) {
        try {
            // todo 充值操作人未加
            validate(rechargeFB);
            RechargeDto rechargeDto = mapper.map(rechargeFB, RechargeDto.class);
            rechargeService.recharge(rechargeDto);
            return ResponseData.success("充值成功");
        } catch (ServiceException e) {
            log.error(e.getMessage());
            return ResponseData.failed("充值失败，失败原因" + e.getMessage());
        }
    }

    @PreAuthorize("hasPermission('recharge','read')")
    @GetMapping("/page/rechargeRecords")
    public ResponseData getRechargeRecords(Page page, RechargeQueryFB rechargeQueryFB) {
        try {
            validate(rechargeQueryFB);
            RechargeDto rechargeDto = mapper.map(rechargeQueryFB, RechargeDto.class);

            IPage<RechargeVo> rechargeVoIPage = rechargeService.getRechargeRecords(page, rechargeDto);
            return ResponseData.success(rechargeVoIPage);
        } catch (ServiceException e) {
            return ResponseData.failed("获取充值列表失败");
        }
    }
}
