package com.hw.mes.controller;

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

import com.hw.common.core.constant.MesConstants;
import com.hw.mes.domain.MesProductOrder;
import com.hw.mes.service.IMesProductOrderService;
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.MesProductPlan;
import com.hw.mes.service.IMesProductPlanService;
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 xins
 * @date 2024-02-21
 */
@RestController
@RequestMapping("/productplan")
public class MesProductPlanController extends BaseController
{
    @Autowired
    private IMesProductPlanService mesProductPlanService;

    /**
     * 查询生产派工列表
     */
    @RequiresPermissions("mes:productplan:list")
    @GetMapping("/list")
    public TableDataInfo list(MesProductPlan mesProductPlan)
    {
        startPage();
        List<MesProductPlan> list = mesProductPlanService.selectMesProductPlanList(mesProductPlan);
        return getDataTable(list);
    }

    /**
     * 查询生产派工List
     * @param mesProductPlan
     * @return
     */
    @GetMapping("/getProductPlan")
    public AjaxResult getProductPlan(MesProductPlan mesProductPlan)
    {
        List<MesProductPlan> list = mesProductPlanService.selectMesProductPlanList(mesProductPlan);
        return success(list);
    }

    /**
     * 导出生产派工列表
     */
    @RequiresPermissions("mes:productplan:export")
    @Log(title = "生产派工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProductPlan mesProductPlan)
    {
        List<MesProductPlan> list = mesProductPlanService.selectMesProductPlanList(mesProductPlan);
        ExcelUtil<MesProductPlan> util = new ExcelUtil<MesProductPlan>(MesProductPlan.class);
        util.exportExcel(response, list, "生产派工数据");
    }

    /**
     * 获取生产派工详细信息
     */
    @RequiresPermissions("mes:productplan:query")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId)
    {
        return success(mesProductPlanService.selectMesProductPlanByPlanId(planId));
    }

    /**
     * 新增生产派工
     */
    @RequiresPermissions("mes:productplan:add")
    @Log(title = "生产派工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProductPlan mesProductPlan)
    {
        return toAjax(mesProductPlanService.insertMesProductPlan(mesProductPlan));
    }

    /**
     * 修改生产派工
     */
    @RequiresPermissions("mes:productplan:edit")
    @Log(title = "生产派工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProductPlan mesProductPlan)
    {
        return toAjax(mesProductPlanService.updateMesProductPlan(mesProductPlan));
    }

    /**
     * 删除生产派工
     */
    @RequiresPermissions("mes:productplan:remove")
    @Log(title = "生产派工", businessType = BusinessType.DELETE)
	@DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds)
    {
        return toAjax(mesProductPlanService.deleteMesProductPlanByPlanIds(planIds));
    }

    /**
     * 获取派工编号
     *
     * @return orderCode
     */
    @GetMapping(value = "/getDispatchCode")
    public AjaxResult getDispatchCode() {
        return success(mesProductPlanService.getDispatchCode());
    }

    /**
     * 生产工单新增生产派工List
     */
    @RequiresPermissions("mes:productplan:add")
    @Log(title = "生产派工", businessType = BusinessType.INSERT)
    @PostMapping("/orderAddMesProductPlanList")
    public AjaxResult orderAddMesProductPlanList(@RequestBody List<MesProductPlan> mesProductPlanList)
    {
        return toAjax(mesProductPlanService.orderAddMesProductPlanList(mesProductPlanList));
    }

    /**
     * 获取生产派工图纸List列表
     * @param planId
     * @return
     */
    @GetMapping(value = "/drawingList/{planId}")
    public AjaxResult getDispatchDrawingList(@PathVariable("planId") Long planId)
    {
        return success(mesProductPlanService.getDispatchAttachList(planId, MesConstants.MES_ATTACH_TYPE_DRAWING));
    }

    /**
     * 获取生产派工SOP附件List列表
     * @param planId
     * @return
     */
    @GetMapping(value = "/SOPAttachList/{planId}")
    public AjaxResult getDispatchSOPAttachList(@PathVariable("planId") Long planId)
    {
        return success(mesProductPlanService.getDispatchAttachList(planId, MesConstants.MES_ATTACH_TYPE_SOP));
    }

    /**
     * 工单撤回
     */
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PostMapping("/productOrderRecall")
    public AjaxResult productOrderRecall(@RequestBody MesProductOrder mesProductOrder) {
        return toAjax(mesProductPlanService.productOrderRecall(mesProductOrder));
    }

}
