package com.hys.app.controller.erp.use;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.BaseQueryParam;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.security.model.Admin;
import com.hys.app.model.erp.dos.OrderSendDO;
import com.hys.app.model.erp.dto.OrderDTO;
import com.hys.app.model.erp.dto.OrderQueryParams;
import com.hys.app.model.erp.enums.ScanFormCodeTypeEnum;
import com.hys.app.model.erp.vo.OrderVO;
import com.hys.app.model.erp.vo.ProductVO;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.service.erp.OrderManager;
import com.hys.app.service.kuaidiniao.entity.AddressDTO;
import com.hys.app.service.kuaidiniao.entity.PreOrderModel;
import com.hys.app.service.kuaidiniao.entity.PreOrderModelDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 订单 API
 *
 * @author 张崧
 * 2024-01-24 15:58:31
 */
@RestController
@RequestMapping("/admin/erp/order")
@Api(tags = "订单API")
@Validated
public class OrderController {

    @Autowired
    private OrderManager orderManager;

    @ApiOperation(value = "分页列表")
    @GetMapping
    public WebPage<OrderVO> list(OrderQueryParams queryParams) {
        Admin admin = AdminUserContext.getAdmin();
        return orderManager.list(queryParams);
    }

    @ApiOperation(value = "添加")
    @PostMapping
    @Log(client = LogClient.admin, detail = "创建订单")
    public Long add(@RequestBody @Valid OrderDTO orderDTO) {
        return orderManager.add(orderDTO);
    }

    @ApiOperation(value = "修改")
    @PutMapping("/{id}")
    @Log(client = LogClient.admin, detail = "修改id为[${id}]的订单")
    public void edit(@PathVariable Long id, @RequestBody @Valid OrderDTO orderDTO) {
        orderDTO.setId(id);
        orderManager.edit(orderDTO);
    }

    @ApiOperation(value = "查询")
    @GetMapping("/{id}")
    public OrderVO getDetail(@PathVariable Long id) {
        return orderManager.getDetail(id);
    }

    @ApiOperation(value = "确认")
    @PostMapping("/{id}/submit")
    @Log(client = LogClient.admin, detail = "提交ID为[${id}]的订单")
    public void submit(@PathVariable Long id) {
        orderManager.submit(id);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @Log(client = LogClient.admin, detail = "删除id为[${id}]的订单")
    public void delete(@PathVariable Long id) {
        orderManager.delete(id);
    }


    @ApiOperation(value = "发货")
    @PostMapping("/orderSend/{orderSendId}")
    @Log(client = LogClient.admin, detail = "销售列表发货")
    public void orderSend(@PathVariable(value = "orderSendId") Long orderSendId,@RequestBody PreOrderModelDTO orderSendDO) {
         orderManager.orderSend(orderSendId,orderSendDO);
    }

    @ApiOperation(value = "保存发货单")
    @PostMapping("/saveOrderSend/{orderSendId}")
    @Log(client = LogClient.admin, detail = "销售列表发货")
    public void saveOrderSend(@PathVariable(value = "orderSendId") Long orderSendId,@RequestBody PreOrderModelDTO orderSendDO) {
        orderManager.saveOrderSend(orderSendId,orderSendDO);
    }


    @ApiOperation(value = "已发货分页列表")
    @GetMapping("saleOrder/list")
    public IPage<OrderSendDO> saleOrder(OrderQueryParams queryParams) {
        return orderManager.hasSendOrder(queryParams);
    }

    @ApiOperation(value = "获取发货详情")
    @GetMapping("getSendOrderDetail/{id}")
    public OrderSendDO getSendOrderDetail( @PathVariable(value = "id") String id) {
        return orderManager.getSendOrderDetail(id);
    }


    @ApiOperation(value = "getOneBySn")
    @GetMapping("getOneBySn/{sn}")
    public OrderVO getOneBySn(@PathVariable String sn) {
        return orderManager.getOneBySn(sn);
    }


    @ApiOperation(value = "延期")
    @PostMapping("/extension/{orderId}")
    @Log(client = LogClient.admin, detail = "延期")
    public void extension(@PathVariable(value = "orderId") Long orderId ,@RequestBody @Valid OrderDTO orderDTO) {
        orderManager.extension(orderId,orderDTO);
    }

}

