package com.propertyManagement.web.controller.business.api;

import com.propertyManagement.business.domain.bo.WxLiveBillBo;
import com.propertyManagement.business.domain.vo.LoginWxUser;
import com.propertyManagement.business.domain.vo.WxLiveBillVo;
import com.propertyManagement.business.service.IWxLiveBillService;
import com.propertyManagement.business.support.util.AuthUtil;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.enums.LiveBillCreateType;
import com.propertyManagement.common.enums.LiveBillState;
import com.propertyManagement.common.enums.LiveBillType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 缴费账单
 *
 * @author liushuai
 * @date 2025-05-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/liveBill")
public class ApiWxLiveBillController extends BaseController {

    private final IWxLiveBillService iWxLiveBillService;

    /**
     * 查询待缴账单列表
     */
    @GetMapping("/listAll")
    public R<List<WxLiveBillVo>> listAll(@NotNull(message = "类型不能为空") Integer type, Long carportId) {
        LoginWxUser wxUser = AuthUtil.getWxUser();
        WxLiveBillBo wxLiveBillBo = new WxLiveBillBo();
        wxLiveBillBo.setProprietorId(wxUser.getProprietorId());
        wxLiveBillBo.setState(LiveBillState.NO.getCode());
        if (type.equals(LiveBillType.carport_manage.getCode())) {
            if (carportId == null) {
                throw new RuntimeException("车位ID不能为空");
            }else {
                wxLiveBillBo.setCarportId(carportId);
            }
        }
        wxLiveBillBo.setType(type);
        wxLiveBillBo.setCreateType(LiveBillCreateType.TWO.getCode());
        return R.ok(iWxLiveBillService.queryList(wxLiveBillBo));
    }

    /**
     * 创建缴费账单
     */
    @PostMapping("/add")
    public R<WxLiveBillBo> add(@Validated(AddGroup.class) @RequestBody WxLiveBillBo bo) {
        return R.ok(iWxLiveBillService.add(bo));
    }

}
