package com.hw.mes.controller;

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

import com.hw.common.security.utils.SecurityUtils;
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.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.mes.domain.MesProductOrder;
import com.hw.mes.service.IMesProductOrderService;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.page.TableDataInfo;

/**
 * 生产工单Controller
 *
 * @author Yinq
 * @date 2024-02-20
 */
@RestController
@RequestMapping("/productOrder")
public class MesProductOrderController extends BaseController {
    @Autowired
    private IMesProductOrderService mesProductOrderService;

    /**
     * 查询生产工单列表
     */
    @RequiresPermissions("mes:productOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(MesProductOrder mesProductOrder) {
        startPage();
        List<MesProductOrder> list = mesProductOrderService.selectMesProductOrderList(mesProductOrder);
        return getDataTable(list);
    }

    /**
     * 导出生产工单列表
     */
    @RequiresPermissions("mes:productOrder:export")
    @Log(title = "生产工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProductOrder mesProductOrder) {
        List<MesProductOrder> list = mesProductOrderService.selectMesProductOrderList(mesProductOrder);
        ExcelUtil<MesProductOrder> util = new ExcelUtil<MesProductOrder>(MesProductOrder.class);
        util.exportExcel(response, list, "生产工单数据");
    }

    /**
     * 获取生产工单详细信息
     */
    @RequiresPermissions("mes:productOrder:query")
    @GetMapping(value = "/{productOrderId}")
    public AjaxResult getInfo(@PathVariable("productOrderId") Long productOrderId) {
        return success(mesProductOrderService.selectMesProductOrderByProductOrderId(productOrderId));
    }

    /**
     * 新增生产工单
     */
    @RequiresPermissions("mes:productOrder:add")
    @Log(title = "生产工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProductOrder mesProductOrder) {
        mesProductOrder.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        return toAjax(mesProductOrderService.insertMesProductOrder(mesProductOrder));
    }

    /**
     * 修改生产工单
     */
    @RequiresPermissions("mes:productOrder:edit")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProductOrder mesProductOrder) {
        mesProductOrder.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return toAjax(mesProductOrderService.updateMesProductOrder(mesProductOrder));
    }

    /**
     * 删除生产工单
     */
    @RequiresPermissions("mes:productOrder:remove")
    @Log(title = "生产工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productOrderIds}")
    public AjaxResult remove(@PathVariable Long[] productOrderIds) {
        return toAjax(mesProductOrderService.deleteMesProductOrderByProductOrderIds(productOrderIds));
    }

    /**
     * 锁库存
     */
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PostMapping("/productOrderLockInventory")
    public AjaxResult productOrderLockInventory(@RequestBody MesProductOrder mesProductOrder) {
        return toAjax(mesProductOrderService.productOrderLockInventory(mesProductOrder));
    }

    /**
     * 工单发布
     */
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PostMapping("/productOrderPublish")
    public AjaxResult productOrderPublish(@RequestBody MesProductOrder mesProductOrder) {
        return toAjax(mesProductOrderService.productOrderPublish(mesProductOrder));
    }

    /**
     * 获取工单编号
     *
     * @return orderCode
     */
    @GetMapping(value = "/getOrderCode")
    public AjaxResult getOrderCode() {
        return success(mesProductOrderService.getOrderCode());
    }

    /**
     * 校验BOM是否被生产工单选择
     *
     * @return materialBomId
     */
    @GetMapping(value = "/verifyBOMIsProduction/{materialBomId}")
    public AjaxResult verifyBOMIsProduction(@PathVariable Long materialBomId) {
        return success(mesProductOrderService.verifyBOMIsProduction(materialBomId));
    }

}
