package cn.iocoder.yudao.module.member.controller.app.billing;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.billing.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.billing.BillingDO;
import cn.iocoder.yudao.module.member.service.billing.BillingService;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;


@Tag(name = "小程序 - 开票记录")
@RestController
@RequestMapping("/t/billing")
@Validated
public class AppBillingController {

    @Resource
    private BillingService billingService;

    @PostMapping("/create")
    @Operation(summary = "创建开票记录")
    public CommonResult<String> createBilling(@Valid @RequestBody BillingSaveReqVO createReqVO) {
        return success(billingService.createBillingUnit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新开票记录")
    public CommonResult<Boolean> updateBilling(@Valid @RequestBody BillingSaveReqVO updateReqVO) {
        billingService.updateBillingUnit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除开票记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBilling(@RequestParam("id") String id) {
        billingService.deleteBilling(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得开票记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BillingRespVO> getBilling(@RequestParam("id") String id) {
        BillingDO billing = billingService.getBilling(id);
        return success(BeanUtils.toBean(billing, BillingRespVO.class));
    }
    @GetMapping("/moeny")
    @Operation(summary = "获得开票金额与未开票金额")
    public CommonResult<BillingRespMoneyVO> getBillingPage(@RequestParam("userId")Long userId,@RequestParam(value = "carteenId",required = false) Long carteenId) {
        return success(billingService.getUserHaveWithoutMoney(userId,carteenId));
    }
    @GetMapping("/page")
    @Operation(summary = "获得开票记录分页")
    public CommonResult<PageResult<BillingRespVO>> getBillingPage(@Valid BillingPageReqVO pageReqVO) {
        Long userId = pageReqVO.getUserId();
//        pageReqVO.setUserPhone(billingService.getMemberId(userId));
        pageReqVO.setUserPhone(String.valueOf(userId));
        pageReqVO.setUserId(null);
        PageResult<BillingDO> pageResult = billingService.getBillingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillingRespVO.class));
    }
    @GetMapping("/totalMoney")
    @Operation(summary = "获取月时间段的金额")
    public CommonResult<BigDecimal> getBillingPage(BillingPageDataVo vo) {
        BigDecimal totalMoney = billingService.getUserTotalMoney(vo);
        return success(totalMoney);
    }
    @GetMapping("/data")
    @Operation(summary = "获取月时间段的订单详情")
    public CommonResult<Object> getData(BillingPageDataVo vo) {
        Object data = billingService.getData(vo);
        return success(data);
    }
    @GetMapping("/TypeTotal")
    @Operation(summary = "获取开票订单数量")
    public CommonResult<Map<Integer, Integer>> getBillingTypeTotal(@RequestParam(value = "carteenId",required = false) Long carteenId,@RequestParam("userId") Long userId) {
        Map<Integer, Integer> billingTypeTotal = billingService.getBillingTypeTotal(carteenId, userId);
        return success(billingTypeTotal);
    }
}