package cn.gov.customs.demo.controller;

import cn.gov.customs.cacp.sdks.core.reflection.TypeDescriptor;
import cn.gov.customs.cacp.sdks.core.reflection.TypeDescriptorHelper;
import cn.gov.customs.cacp.sdks.core.result.Result;
import cn.gov.customs.cacp.sdks.core.user.annotation.LogonUser;
import cn.gov.customs.cacp.sdks.core.user.pojo.CacpLogonUser;
import cn.gov.customs.demo.constants.OrderLevel;
import cn.gov.customs.demo.pojo.OrderCategoryInfo;
import cn.gov.customs.demo.pojo.OrderInfo;
import cn.gov.customs.demo.pojo.OrderQueryInfo;
import cn.gov.customs.demo.service.OrderCategoryService;
import cn.gov.customs.demo.service.OrderService;
import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.web.bind.annotation.*;

/**
 * TODO
 *
 * @author sunxuewen
 * @since 2024/8/21 17:16
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

  private final OrderCategoryService categoryService;
  private final OrderService service;


  @GetMapping("/get-category-list")
  public Result<List<OrderCategoryInfo>> getCategoryList() {
    List<OrderCategoryInfo> list = this.categoryService.getList();
    return Result.success(list);
  }

  @PostMapping("/insert-category")
  public Result<Integer> insertCategory(@RequestBody OrderCategoryInfo info) {
    int result = this.categoryService.insert(info);
    return Result.success(result);
  }

  @PostMapping("/update-category")
  public Result<Integer> updateCategory(@RequestBody OrderCategoryInfo info) {
    int result = this.categoryService.update(info);
    return Result.success(result);
  }

  @PostMapping("/delete-category")
  public Result<Integer> deleteCategory(@RequestParam String categoryCode) {
    int result = this.categoryService.delete(categoryCode);
    return Result.success(result);
  }

  @GetMapping("/get-order")
  public Result<OrderInfo> getOrder(@RequestParam String orderId) {
    OrderInfo info = this.service.get(orderId);
    return Result.success(info);
  }

  @PostMapping("/get-order-list")
  public Result<PageInfo<OrderInfo>> getOrderList(@RequestBody OrderQueryInfo query) {
    PageInfo<OrderInfo> list = this.service.getPageList(query);
    return Result.success(list);
  }

  @PostMapping("/insert-order")
  public Result<Integer> insertOrder(@LogonUser CacpLogonUser user, @RequestBody OrderInfo info) {
    info.setParentId(user.getParentId());
    info.setUserId(user.getUserId());
    info.setUserName(user.getUserName());

    int result = this.service.insert(info);
    return Result.success(result);
  }

  @PostMapping("/update-order")
  public Result<Integer> updateOrder(@RequestBody OrderInfo info) {
    int result = this.service.update(info);
    return Result.success(result);
  }

  @PostMapping("/delete-order")
  public Result<Integer> deleteOrder(@RequestParam String orderId) {
    int result = this.service.delete(orderId);
    return Result.success(result);
  }

  @GetMapping("/get-level-list")
  public Result<List<TypeDescriptor>> getLevelList() {
    List<TypeDescriptor> list = TypeDescriptorHelper.getEnumMap(OrderLevel.class);
    return Result.success(list);
  }
}
