package co.yixiang.yshop.module.express.controller.admin.electronicsorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.express.controller.admin.electronicsorder.vo.*;
import co.yixiang.yshop.module.express.dal.dataobject.electronicsorder.ElectronicsOrderDO;
import co.yixiang.yshop.module.express.convert.electronicsorder.ElectronicsOrderConvert;
import co.yixiang.yshop.module.express.service.electronicsorder.ElectronicsOrderService;

@Tag(name = "管理后台 - 电子面单")
@RestController
@RequestMapping("/express/electronics-order")
@Validated
public class ElectronicsOrderController {

    @Resource
    private ElectronicsOrderService electronicsOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建电子面单")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:create')")
    public CommonResult<Long> createElectronicsOrder(@Valid @RequestBody ElectronicsOrderCreateReqVO createReqVO) {
        return success(electronicsOrderService.createElectronicsOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新电子面单")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:update')")
    public CommonResult<Boolean> updateElectronicsOrder(@Valid @RequestBody ElectronicsOrderUpdateReqVO updateReqVO) {
        electronicsOrderService.updateElectronicsOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除电子面单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('express:electronics-order:delete')")
    public CommonResult<Boolean> deleteElectronicsOrder(@RequestParam("id") Long id) {
        electronicsOrderService.deleteElectronicsOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得电子面单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:query')")
    public CommonResult<ElectronicsOrderRespVO> getElectronicsOrder(@RequestParam("id") Long id) {
        ElectronicsOrderDO electronicsOrder = electronicsOrderService.getElectronicsOrder(id);
        return success(ElectronicsOrderConvert.INSTANCE.convert(electronicsOrder));
    }

    @GetMapping("/list")
    @Operation(summary = "获得电子面单列表")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:query')")
    public CommonResult<List<ElectronicsOrderRespVO>> getElectronicsOrderList() {
        List<ElectronicsOrderDO> list = electronicsOrderService.getElectronicsOrderList();
        return success(ElectronicsOrderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得电子面单分页")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:query')")
    public CommonResult<PageResult<ElectronicsOrderRespVO>> getElectronicsOrderPage(@Valid ElectronicsOrderPageReqVO pageVO) {
        PageResult<ElectronicsOrderDO> pageResult = electronicsOrderService.getElectronicsOrderPage(pageVO);
        return success(ElectronicsOrderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出电子面单 Excel")
    @PreAuthorize("@ss.hasPermission('express:electronics-order:export')")
    @OperateLog(type = EXPORT)
    public void exportElectronicsOrderExcel(@Valid ElectronicsOrderExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ElectronicsOrderDO> list = electronicsOrderService.getElectronicsOrderList(exportReqVO);
        // 导出 Excel
        List<ElectronicsOrderExcelVO> datas = ElectronicsOrderConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "电子面单.xls", "数据", ElectronicsOrderExcelVO.class, datas);
    }

}
