package cn.iocoder.yudao.module.mes.controller.admin.saleorder;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.module.mes.controller.admin.saleorder.vo.SaleOrderItemsRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.saleorder.vo.SaleOrderPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.saleorder.vo.SaleOrderRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.saleorder.vo.SaleOrderSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.saleorder.SaleOrderDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.saleorder.SaleOrderItemsDO;
import cn.iocoder.yudao.module.mes.service.saleorder.SaleOrderService2;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 销售订单")
@RestController
@RequestMapping("/mes/sale-order")
@Validated
public class SaleOrderController {

    @Resource
    private SaleOrderService2 saleOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建销售订单")
    public CommonResult<Long> createSaleOrder(@Valid @RequestBody SaleOrderSaveReqVO createReqVO) {
        return success(saleOrderService.createSaleOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新销售订单")
    public CommonResult<Boolean> updateSaleOrder(@Valid @RequestBody SaleOrderSaveReqVO updateReqVO) {
        saleOrderService.updateSaleOrder(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得销售订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SaleOrderRespVO> getSaleOrder(@RequestParam("id") Long id) {
        SaleOrderDO saleOrder = saleOrderService.getSaleOrder(id);
        return success(BeanUtils.toBean(saleOrder, SaleOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得销售订单分页")
    public CommonResult<PageResult<SaleOrderRespVO>> getSaleOrderPage(@Valid SaleOrderPageReqVO pageReqVO) {
        PageResult<SaleOrderRespVO> pageResult = saleOrderService.getSaleOrderPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出销售订单 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSaleOrderExcel(@Valid SaleOrderPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SaleOrderRespVO> list = saleOrderService.getSaleOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售订单.xls", "数据", SaleOrderRespVO.class,
                list);
    }

    // ==================== 子表（销售订单项） ====================

    @GetMapping("/sale-order-items/page")
    @Operation(summary = "获得销售订单项分页")
    @Parameter(name = "orderId", description = "销售订单编号")
    public CommonResult<PageResult<SaleOrderItemsRespVO>> getSaleOrderItemsPage(PageParam pageReqVO,
                                                                                @RequestParam("orderId") Long orderId) {
        return success(saleOrderService.getSaleOrderItemsPage(pageReqVO, orderId));
    }

    @PostMapping("/sale-order-items/create")
    @Operation(summary = "创建销售订单项")
    public CommonResult<Long> createSaleOrderItems(@Valid @RequestBody SaleOrderItemsDO saleOrderItems) {
        return success(saleOrderService.createSaleOrderItems(saleOrderItems));
    }

    @PutMapping("/sale-order-items/update")
    @Operation(summary = "更新销售订单项")
    public CommonResult<Boolean> updateSaleOrderItems(@Valid @RequestBody SaleOrderItemsDO saleOrderItems) {
        saleOrderService.updateSaleOrderItems(saleOrderItems);
        return success(true);
    }

    @DeleteMapping("/sale-order-items/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除销售订单项")
    public CommonResult<Boolean> deleteSaleOrderItems(@RequestParam("id") Long id) {
        saleOrderService.deleteSaleOrderItems(id);
        return success(true);
    }

    @GetMapping("/sale-order-items/get")
    @Operation(summary = "获得销售订单项")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<SaleOrderItemsDO> getSaleOrderItems(@RequestParam("id") Long id) {
        return success(saleOrderService.getSaleOrderItems(id));
    }

    @GetMapping("/sale-order-items/getByOrderId")
    @Operation(summary = "获得销售订单项")
    @Parameter(name = "orderId", description = "订单编号", required = true)
    public CommonResult<List<SaleOrderItemsRespVO>> getByOrderId(@RequestParam("orderId") Long orderId) {
        List<SaleOrderItemsRespVO> list = saleOrderService.getSaleOrderItemsByOrderId(orderId);
        return success(list);
    }
}