package com.iformall.controller.market;


import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxPrepayment;
import com.iformall.domain.po.WxPrepaymentHistory;
import com.iformall.enums.EnumUserType;
import com.iformall.service.WxPrepaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author gongbiao
 */
@RestController
@RequestMapping("prepayment")
@Api(description = "预付费")
public class WxPrepaymentController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxPrepaymentService wxPrepaymentService;

    @ApiOperation("列表查询")
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "预付费-列表查询")
    public ResultData list(@ModelAttribute WxPrepayment wxPrepayment, Integer pageNum, Integer pageSize) {
        String ipStr = getIpAddr();
        logger.info("prepayment/list: " + ipStr);
        if (wxPrepayment == null) {
            wxPrepayment = new WxPrepayment();
        }
        final PageInfo<WxPrepayment> page = wxPrepaymentService.listAsPage(wxPrepayment, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("列表查询历史")
    @GetMapping("listHistory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "预付费-列表查询历史")
    public ResultData listHistory(@ModelAttribute WxPrepaymentHistory wxPrepaymentHistory, Integer pageNum, Integer pageSize) {
        String ipStr = getIpAddr();
        logger.info("prepayment/listHistory: " + ipStr);
        if (wxPrepaymentHistory == null) {
            wxPrepaymentHistory = new WxPrepaymentHistory();
        }
        final PageInfo<WxPrepaymentHistory> page = wxPrepaymentService.listHistoryAsPage(wxPrepaymentHistory, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("首次充值")
    @PostMapping("payFirst")
    @SystemControllerLog(description = "预付费-交费")
    public ResultData payFirst(@RequestBody WxPrepayment wxPrepayment) {
        logger.debug("[" + getIpAddr() + "] prepayment::add");
        wxPrepayment.setTenantId(getTenantId());
        wxPrepayment.setOperationType(EnumUserType.MALLUSER.getCode());
        wxPrepayment.setOperator(getUserId());
        wxPrepaymentService.payFirst(wxPrepayment);
        return new ResultData();
    }

    @ApiOperation("再次充值")
    @PostMapping("payAgain")
    @SystemControllerLog(description = "预付费-交费或扣费")
    public ResultData payAgain(@RequestBody WxPrepayment wxPrepayment) {
        logger.debug("[" + getIpAddr() + "] prepayment::update");
        wxPrepayment.setTenantId(getTenantId());
        wxPrepayment.setOperationType(EnumUserType.MALLUSER.getCode());
        wxPrepayment.setOperator(getUserId());
        wxPrepaymentService.payAgain(wxPrepayment);
        return new ResultData();

    }

}
