package com.yuchen.saas.park.service.controller.property.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.property.PropertyAppListPrepaymentDto;
import com.yuchen.saas.park.api.dto.property.PropertyPrepaymentFeeItemBalanceDto;
import com.yuchen.saas.park.api.dto.property.PropertyPrepaymentQryDto;
import com.yuchen.saas.park.api.dto.property.PropertyPrepaymentSaveDto;
import com.yuchen.saas.park.api.vo.property.PropertyPrepaymentBalanceVo;
import com.yuchen.saas.park.api.vo.property.PropertyPrepaymentFeeItemListVo;
import com.yuchen.saas.park.service.service.property.PropertyAppCustomerService;
import com.yuchen.saas.park.service.service.property.PropertyPrepaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "小程序端预存款管理")
@RestController
@RequestMapping("/app/propertyPrepayment")
public class PropertyAppPrepaymentController {
    @Resource
    private PropertyPrepaymentService propertyPrepaymentService;
    @Resource
    private PropertyAppCustomerService propertyAppCustomerService;

    @PostMapping("/recharge")
    @ApiOperation(value = "预存充值")
    public R recharge(@Valid @RequestBody PropertyPrepaymentSaveDto dto) {
        if (Func.isEmpty(dto.getIsAppPerson())) {
            throw new ServiceException("预存充值参数异常");
        }
        dto.setCustomerId(propertyAppCustomerService.getAppPropertyCustomerArchivesId(dto.getIsAppPerson()));
        return R.data(propertyPrepaymentService.savePrepayment(dto));
    }

    @PostMapping("/listPrepayment")
    @ApiOperation(value = "获取预缴账户列表")
    public R<List<PropertyPrepaymentFeeItemListVo>> listPrepayment(@Valid @RequestBody PropertyAppListPrepaymentDto dto) {
        PropertyPrepaymentFeeItemBalanceDto qryDto = new PropertyPrepaymentFeeItemBalanceDto();
        Long customerId = propertyAppCustomerService.getAppPropertyCustomerArchivesId(dto.getIsAppPerson());
        qryDto.setParkId(dto.getParkId());
        qryDto.setCustomerId(customerId);
        qryDto.setHouseId(dto.getHouseId());
        return R.data(propertyPrepaymentService.listPrepaymentAccount(qryDto));
    }

    @PostMapping("/pagePrepayment")
    @ApiOperation(value = "预存账户列表")
    public R<Page<PropertyPrepaymentBalanceVo>> list(@RequestBody PropertyPrepaymentQryDto qryDto) {
        Long customerId = propertyAppCustomerService.getAppPropertyCustomerArchivesId(qryDto.getIsAppPerson());
        qryDto.setCustomerId(customerId);
        return R.data(propertyPrepaymentService.selectPage(qryDto));
    }

}
