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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.orderdetail.vo.OrderDetailRespVO;
import cn.iocoder.yudao.module.member.controller.app.orderdetail.vo.AppOrderDetailPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.orderdetail.vo.AppOrderDetailSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.orderdetail.OrderDetailDO;
import cn.iocoder.yudao.module.member.service.orderdetail.OrderDetailService;
import cn.iocoder.yudao.module.system.api.deviceInfo.DeviceInfoApi;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "用户 App - 订单明细")
@RestController
@RequestMapping("/member/order-detail")
@Validated
public class AppOrderDetailController {

    @Resource
    private OrderDetailService orderDetailService;

    @Resource
    private HttpServletRequest httpServletRequest;

    @Resource
    private DeviceInfoApi deviceInfoApi;

    @PostMapping("/create")
    @Operation(summary = "创建订单明细")
    public CommonResult<Long> createOrderDetail(@Valid @RequestBody AppOrderDetailSaveReqVO createReqVO) {
        if (createReqVO.getStoreId()==null){
            createReqVO.setStoreId(getCarteen());
        }
        return success(orderDetailService.createOrderDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单明细")
    public CommonResult<Boolean> updateOrderDetail(@Valid @RequestBody AppOrderDetailSaveReqVO updateReqVO) {
        orderDetailService.updateOrderDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:order-detail:delete')")
    public CommonResult<Boolean> deleteOrderDetail(@RequestParam("id") Long id) {
        orderDetailService.deleteOrderDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<OrderDetailRespVO> getOrderDetail(@RequestParam("id") Long id) {
        OrderDetailDO orderDetail = orderDetailService.getOrderDetail(id);
        return success(BeanUtils.toBean(orderDetail, OrderDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单明细分页")
    @PreAuthorize("@ss.hasPermission('member:order-detail:query')")
    public CommonResult<PageResult<OrderDetailRespVO>> getOrderDetailPage(@Valid AppOrderDetailPageReqVO pageReqVO) {
        PageResult<OrderDetailDO> pageResult = orderDetailService.getOrderDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单明细 Excel")
    @PreAuthorize("@ss.hasPermission('member:order-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderDetailExcel(@Valid AppOrderDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderDetailDO> list = orderDetailService.getOrderDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单明细.xls", "数据", OrderDetailRespVO.class,
                        BeanUtils.toBean(list, OrderDetailRespVO.class));
    }

    public Long getCarteen() {
        try {
            String authorization = httpServletRequest.getHeader("Authorization");
            Long carteen = deviceInfoApi.getCarteen(authorization);

            return carteen;
        } catch (Exception e) {
            throw exception(2000_10_001, "无法获取设备编码");
        }
    }
}