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

import cn.hutool.core.date.DatePattern;
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.orderspacecapsule.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.orderspacecapsule.OrderSpaceCapsuleDO;
import cn.iocoder.yudao.module.member.service.orderspacecapsule.OrderSpaceCapsuleService;
import cn.iocoder.yudao.module.member.service.statisticsspacecapsuleorder.StatisticsSpaceCapsuleOrderService;
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.util.Date;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "小程序 - 太空舱订单")
@RestController
@RequestMapping("/order-space-capsule")
@Validated
public class AppOrderSpaceCapsuleController {

    @Resource
    private OrderSpaceCapsuleService orderSpaceCapsuleService;
    @Resource
    private StatisticsSpaceCapsuleOrderService capsuleOrderService;
    @PostMapping("/create")
    @Operation(summary = "创建太空舱订单")
    public CommonResult<Long> createOrderSpaceCapsule(@Valid @RequestBody OrderSpaceCapsuleSaveReqVO createReqVO) {
        return success(orderSpaceCapsuleService.createOrderSpaceCapsule(createReqVO),"下单成功");
    }

    @PutMapping("/update")
    @Operation(summary = "更新太空舱订单")
    public CommonResult<Boolean> updateOrderSpaceCapsule(@Valid @RequestBody OrderSpaceCapsuleSaveReqVO updateReqVO) {
        orderSpaceCapsuleService.updateOrderSpaceCapsule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除太空舱订单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteOrderSpaceCapsule(@RequestParam("id") Long id) {
        orderSpaceCapsuleService.deleteOrderSpaceCapsule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得太空舱订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<OrderSpaceCapsuleRespVO> getOrderSpaceCapsule(@RequestParam("id") Long id) {
        OrderSpaceCapsuleDO orderSpaceCapsule = orderSpaceCapsuleService.getOrderSpaceCapsule(id);
        return success(BeanUtils.toBean(orderSpaceCapsule, OrderSpaceCapsuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得太空舱订单分页")
    public CommonResult<PageResult<OrderSpaceCapsuleRespVO>> getOrderSpaceCapsulePage(@Valid OrderSpaceCapsulePageReqVO pageReqVO) {
        PageResult<OrderSpaceCapsuleDO> pageResult = orderSpaceCapsuleService.getOrderSpaceCapsulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderSpaceCapsuleRespVO.class));
    }
    @PostMapping("/open/cmd")
    @Operation(summary = "生成太空舱cmd")
    public CommonResult<OrderSpaceCapsuleRespVO> getOpenCmd(@Valid @RequestBody OrderSpaceCapsuleOperateVo operateVo) {
        OrderSpaceCapsuleDO orderSpaceCapsuleDO = orderSpaceCapsuleService.openDoor(operateVo);
        return success(BeanUtils.toBean(orderSpaceCapsuleDO, OrderSpaceCapsuleRespVO.class));
    }
    @PostMapping("/open/cmd/admin")
    @Operation(summary = "超级管理员跳过订单验证.打开太空舱")
    public CommonResult<OrderSpaceCapsuleRespVO> getCmdAdmin(@Valid @RequestBody OrderSpaceCapsuleAdminVo adminVo) {
        OrderSpaceCapsuleDO orderSpaceCapsuleDO = orderSpaceCapsuleService.openCmdAdmin(adminVo);
        return success(BeanUtils.toBean(orderSpaceCapsuleDO, OrderSpaceCapsuleRespVO.class));
    }
/*    @GetMapping("/statistics")
    @Operation(summary = "日报，月报")
    public CommonResult<OrderSpaceCapsuleStatisticsVo> getStatistics(@RequestParam("carteenId") Long carTeenId,
                                                                     @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                                                     @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        return success(orderSpaceCapsuleService.getStatistics(carTeenId, startTime, endTime));
    }*/
    @GetMapping("/statistics")
    @Operation(summary = "日报，月报")
    public CommonResult<OrderSpaceCapsuleStatisticsVo> getStatistics1(@RequestParam("carteenId") Long carTeenId,
                                                                      @RequestParam("type")String type,
                                                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam Date date) {
        return success(capsuleOrderService.compDayMon(carTeenId, type,date));
    }
}