package com.fjhx.controller.order;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.entity.order.OrderInfo;
import com.fjhx.enums.stock.OutTypeEnum;
import com.fjhx.params.order.IssueVo;
import com.fjhx.params.order.OrderInfoEx;
import com.fjhx.params.order.OrderInfoVo;
import com.fjhx.service.order.OrderDetailsService;
import com.fjhx.service.order.OrderInfoService;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 销售订单 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-12-02
 */
@RestController
@RequestMapping("/orderSales")
public class OrderSalesController {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private OrderDetailsService orderDetailsService;

    @PostMapping("/page")
    public R page(@RequestBody Map<String, Object> condition) {
        Page<OrderInfoEx> result = orderInfoService.getPage(condition);
        return R.success(result);
    }

    @PostMapping("/issuePage")
    public R issuePage(@RequestBody Map<String, Object> condition) {
        condition.put("deliverable", true);
        Page<OrderInfoEx> result = orderInfoService.getPage(condition);
        return R.success(result);
    }

    @PostMapping("/add")
    public R add(@RequestBody OrderInfoVo orderVo) {
        orderInfoService.add(orderVo);
        return R.success();
    }

    @PostMapping("/edit")
    public R edit(@RequestBody OrderInfoVo orderVo) {
        orderInfoService.edit(orderVo);
        return R.success();
    }

    /**
     * 销售出库
     */
    @PostMapping("/issue")
    public R issue(@RequestBody IssueVo issueVo) {
        issueVo.setOutTypeEnum(OutTypeEnum.PURCHASE);
        orderDetailsService.issue(issueVo);
        return R.success();
    }

    /**
     * 根据客户id查询订单
     */
    @PostMapping("/getOrderByCustomerId")
    public R getOrderByCustomerId(@RequestBody OrderInfo orderInfo) {
        List<OrderInfo> list = orderInfoService.getOrderByCustomerId(orderInfo.getCustomerInfoId());
        return R.success(list);
    }

    /**
     * 订单明细
     */
    @PostMapping("/getDetails")
    public R getDetails(@RequestBody OrderInfo orderInfo) {
        OrderInfoEx orderInfoEx = orderInfoService.getDetails(orderInfo.getId());
        return R.success(orderInfoEx);
    }


}

