package com.op.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.op.mes.domain.dto.SplitOrderDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.op.common.log.annotation.Log;
import com.op.common.log.enums.BusinessType;
import com.op.common.security.annotation.RequiresPermissions;
import com.op.mes.domain.ProOrder;
import com.op.mes.service.IProOrderService;
import com.op.common.core.web.controller.BaseController;
import com.op.common.core.web.domain.AjaxResult;
import com.op.common.core.utils.poi.ExcelUtil;

/**
 * 订单Controller
 *
 * @author Open Platform
 * @date 2023-07-13
 */
@RestController
@RequestMapping("/order")
public class ProOrderController extends BaseController {
    @Autowired
    private IProOrderService proOrderService;

    /**
     * 获取工单信息列表
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getWorkOrderList/{id}")
    public AjaxResult getWorkOrderList(@PathVariable("id") String id) {
        return proOrderService.getWorkOrderList(id);
    }


    /**
     * 获取当前订单信息列表
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getOrderList/{id}")
    public AjaxResult getOrderList(@PathVariable("id") String id) {
        return proOrderService.getOrderList(id);
    }

    /**
     * 提交拆分订单接口
     *
     * @param splitOrderDTO
     * @return
     */
    @PostMapping("/subSplitOrder")
    public AjaxResult subSplitOrder(@RequestBody SplitOrderDTO splitOrderDTO) {
        return proOrderService.subSplitOrder(splitOrderDTO);
    }

    /**
     * 获取换算值
     *
     * @return
     */
    @GetMapping("/getConvert")
    public AjaxResult getConvert() {
        return success(proOrderService.getConvert());
    }

    /**
     * 获取成型机
     *
     * @return
     */
    @GetMapping("/getProdLineShift")
    public AjaxResult getProdLineShift(String type) {
        return success(proOrderService.getProdLineShift(type));
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("mes:pro:order:list")
    @GetMapping("/list")
    public AjaxResult list(ProOrder proOrder) {
        List<ProOrder> list = proOrderService.selectProOrderList(proOrder);
        return success(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("mes:pro:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProOrder proOrder) {
        List<ProOrder> list = proOrderService.selectProOrderList(proOrder);
        ExcelUtil<ProOrder> util = new ExcelUtil<ProOrder>(ProOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @RequiresPermissions("mes:pro:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(proOrderService.selectProOrderById(id));
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("mes:pro:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProOrder proOrder) {
        return toAjax(proOrderService.insertProOrder(proOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("mes:pro:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProOrder proOrder) {
        return toAjax(proOrderService.updateProOrder(proOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("mes:pro:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(proOrderService.deleteProOrderByIds(ids));
    }
}
