package co.yixiang.yshop.module.distributor.controller.admin.distributororder;

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.distributor.controller.admin.distributororder.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributororder.DistributorOrderDO;
import co.yixiang.yshop.module.distributor.convert.distributororder.DistributorOrderConvert;
import co.yixiang.yshop.module.distributor.service.distributororder.DistributorOrderService;

@Tag(name = "管理后台 - 分销商订单")
@RestController
@RequestMapping("/distributor/order")
@Validated
public class DistributorOrderController {

    @Resource
    private DistributorOrderService orderService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商订单")
    @PreAuthorize("@ss.hasPermission('distributor:order:create')")
    public CommonResult<Long> createOrder(@Valid @RequestBody DistributorOrderCreateReqVO createReqVO) {
        return success(orderService.createOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商订单")
    @PreAuthorize("@ss.hasPermission('distributor:order:update')")
    public CommonResult<Boolean> updateOrder(@Valid @RequestBody DistributorOrderUpdateReqVO updateReqVO) {
        orderService.updateOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distributor:order:delete')")
    public CommonResult<Boolean> deleteOrder(@RequestParam("id") Long id) {
        orderService.deleteOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distributor:order:query')")
    public CommonResult<DistributorOrderRespVO> getOrder(@RequestParam("id") Long id) {
        DistributorOrderDO order = orderService.getOrder(id);
        return success(DistributorOrderConvert.INSTANCE.convert(order));
    }

    @GetMapping("/list")
    @Operation(summary = "获得分销商订单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('distributor:order:query')")
    public CommonResult<List<DistributorOrderRespVO>> getOrderList(@RequestParam("ids") Collection<Long> ids) {
        List<DistributorOrderDO> list = orderService.getOrderList(ids);
        return success(DistributorOrderConvert.INSTANCE.convertList(list));
    }


    @GetMapping("/page")
    @Operation(summary = "获得分销商订单分页")
    @PreAuthorize("@ss.hasPermission('distributor:order:query')")
    public CommonResult<PageResult<DistributorOrderRespVO>> getOrderPage(@Valid DistributorOrderPageReqVO pageVO) {
        PageResult<DistributorOrderDO> pageResult = orderService.getOrderPage(pageVO);
        return success(DistributorOrderConvert.INSTANCE.convertPage(pageResult));
    }


}
