package com.hw.mes.controller;

import com.hw.common.core.constant.MesConstants;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.mes.domain.MesMaterialBom;
import com.hw.mes.domain.MesProductPlan;
import com.hw.mes.domain.MesProductPlanDetail;
import com.hw.mes.domain.vo.*;
import com.hw.mes.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * MES对外提供接口Controller
 *
 * @author xins
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/api")
public class MesApiController extends BaseController {
    @Autowired
    private IMesMaterialBomService mesMaterialBomService;

    @Autowired
    private IMesProductPlanService productPlanService;

    @Autowired
    private IMesProductPlanDetailService productPlanDetailService;

    @Autowired
    private IMesBaseBarcodeInfoService mesBaseBarcodeInfoService;

    /**
     * 查询物料BOM信息列表
     */
//    @RequiresPermissions("mes:materialBom:list")
    @GetMapping("/getMaterialBoms")
    public AjaxResult getMaterialBoms(MesMaterialBom mesMaterialBom) {
        String ancestors = mesMaterialBom.getAncestors();
        ancestors = "0," + ancestors;
        mesMaterialBom.setAncestors(ancestors);
        List<MesMaterialBom> list = mesMaterialBomService.selectMesMaterialBomJoinList(mesMaterialBom);
        return success(list);
    }


    /**
     * 查询车间派工信息列表表（适用于各楼层各工序）
     */
//    @RequiresPermissions("mes:materialBom:list")
    @GetMapping("/getProductPlans")
    public TableDataInfo getProductPlans(MesProductPlan mesProductPlan) {
        startPage();
        List<MesProductPlan> list = productPlanService.selectMesProductPlanJoinList(mesProductPlan);
        return getDataTable(list);
    }


    /**
     * 生成生产计划明细（适用于1楼、四楼装配工位）
     */
    @Log(title = "生产计划明细", businessType = BusinessType.INSERT)
    @PostMapping(("/insertProductPlanDetails"))
    public AjaxResult insertProductPlanDetails(@Validated @RequestBody MesProductPlanDetailVo mesProductPlanDetailVo) {
        return success(productPlanDetailService.insertMesProductPlanDetails(mesProductPlanDetailVo));
    }


    /**
     * 根据生产计划ID查询车间生产计划明细列表（适用于1楼、四楼装配工位）
     */
//    @RequiresPermissions("mes:materialBom:list")
    @GetMapping("/getProductPlanDetails")
    public TableDataInfo getProductPlanDetails(MesProductPlanDetail mesProductPlanDetail) {
        startPage();
        List<MesProductPlanDetail> list = productPlanDetailService.selectMesProductPlanDetailList(mesProductPlanDetail);
        return getDataTable(list);
    }


    /**
     * 开始生产计划明细（适用于1楼、四楼装配工位），在生成计划明细时一条一条的开始
     */
    @Log(title = "生产计划明细", businessType = BusinessType.START)
    @PostMapping(("/startProductPlanDetail"))
    public AjaxResult startProductPlanDetail(@RequestBody MesProductPlanDetail mesProductPlanDetail) {
        return success(productPlanDetailService.startMesProductPlanDetail(mesProductPlanDetail));
    }


    /**
     * 完成生产计划明细（适用于各工序）
     * 传planDetailId
     */
    @Log(title = "生产计划明细", businessType = BusinessType.COMPLETE)
    @PostMapping(("/completeProductPlanDetail"))
    public AjaxResult completeProductPlanDetail(@RequestBody MesProductPlanDetail mesProductPlanDetail) {
        return success(productPlanDetailService.completeMesProductPlanDetail(mesProductPlanDetail));
    }


    /**
     * 获取最新的生产计划明细，在刚登录页面打开时获取根据最新一条派工获取，如果点击生产计划则根据生产计划ID获取(适用于四楼折弯焊接和五楼)
     */
//    @RequiresPermissions("mes:baseAttachInfo:query")
    @GetMapping(value = "/getNewestProductPlanDetail")
    public AjaxResult getNewestProductPlanDetail(MesProductPlanDetail queryProductPlanDetail) {
//        if (queryProductPlanDetail.getPlanId() == null) {
//            Long stationId = SecurityUtils.getStationId();
//            queryProductPlanDetail.setStationId(stationId);
//        }
        return success(productPlanDetailService.selectNewestMesProductPlanDetail(queryProductPlanDetail));
    }

    /**
     * 获取最新的生产计划明细Join图纸信息，在刚登录页面打开时获取根据最新一条派工获取，如果点击生产计划则根据生产计划ID获取(适用于四楼激光切割)
     */
//    @RequiresPermissions("mes:baseAttachInfo:query")
    @GetMapping(value = "/getNewestProductPlanDetailJoinAttach")
    public AjaxResult getNewestProductPlanDetailJoinAttach(MesProductPlanDetail queryProductPlanDetail) {
//        if (queryProductPlanDetail.getPlanId() == null) {
//            Long stationId = SecurityUtils.getStationId();
//            queryProductPlanDetail.setStationId(stationId);
//        }
        return success(productPlanDetailService.selectNewestMesProductPlanDetailJoinAttach(queryProductPlanDetail));
    }


    /**
     * 开始下一生产计划明细（适用于四楼折弯焊接和五楼），开始时生成一条计划明细
     * 传planId
     */
    @Log(title = "生产计划明细", businessType = BusinessType.START)
    @PostMapping(("/startNextProductPlanDetail"))
    public AjaxResult startNextProductPlanDetail(@RequestBody MesProductPlanDetail mesProductPlanDetail) {
        return success(productPlanDetailService.startNextMesProductPlanDetail(mesProductPlanDetail,""));
    }

    /**
     * 开始下一生产计划明细（适用于五楼装配区域），扫描或输入物料条码后开始时生成一条计划明细
     * 传planId
     */
    @Log(title = "生产计划明细", businessType = BusinessType.START)
    @PostMapping(("/scan2StartNextProductPlanDetail"))
    public AjaxResult scan2StartNextProductPlanDetail(@Validated @RequestBody MesMaterialBarcodeScan2StartVo mesMaterialBarcodeScan2StartVo) {
        MesProductPlanDetail mesProductPlanDetail = new MesProductPlanDetail();
        mesProductPlanDetail.setPlanId(mesMaterialBarcodeScan2StartVo.getPlanId());
        mesProductPlanDetail.setMaterialBarcode(mesMaterialBarcodeScan2StartVo.getMaterialBarcode());
        return success(productPlanDetailService.startNextMesProductPlanDetail(mesProductPlanDetail, MesConstants.MES_START_TYPE_FIFTH_FLOOR_ASSEMBLE));
    }

    /**
     * 开始下一生产计划明细（适用于四楼激光切割），开始时生成一条计划明细
     * 四楼激光切割机传planId和attachId
     */
    @Log(title = "生产计划明细", businessType = BusinessType.START)
    @PostMapping(("/startNextProductPlanDetailAttach"))
    public AjaxResult startNextProductPlanDetailAttach(@RequestBody MesProductPlanDetailAttachVo mesProductPlanDetailAttachVo) {
        MesProductPlanDetail mesProductPlanDetail = new MesProductPlanDetail();
        mesProductPlanDetail.setPlanId(mesProductPlanDetailAttachVo.getPlanId());
        mesProductPlanDetail.setAttachId(mesProductPlanDetailAttachVo.getAttachId());
        return success(productPlanDetailService.startNextMesProductPlanDetail(mesProductPlanDetail,""));
    }

    /**
     * 获取生产派工图纸列表(适用于四楼激光切割)
     */
//    @RequiresPermissions("mes:baseAttachInfo:query")
    @GetMapping("/getProductPlanDrawings")
    public AjaxResult getProductPlanDrawings(MesProductPlan queryProductPlan) {

        return success(productPlanService.getDrawings(queryProductPlan));
    }

    /**
     * 扫描确认物料使用（适用于一楼和五楼扫描确认）
     */
    @Log(title = "生产计划明细", businessType = BusinessType.CONFIRM)
    @PostMapping(("/scanMaterial2Confirm"))
    public AjaxResult scanMaterial2Confirm(@RequestBody MesMaterialScanVo mesMaterialScanVo) {
        return success(productPlanService.scanMaterial2Confirm(mesMaterialScanVo));
    }


    /**
     * 在五楼柜体拆分后绑定背板条码（成品条码）和柜体条码
     */
    @Log(title = "条码信息", businessType = BusinessType.BIND)
    @PostMapping(("/bindBarcode"))
    public AjaxResult bindBarcode(@RequestBody MesBindBarcodeVo mesBindBarcodeVo) {
        return success(mesBaseBarcodeInfoService.bindBarcode(mesBindBarcodeVo));
    }

    /**
     * 在五楼柜体拆分后将门板下发给相应工位组装(分配任务)
     */
    @Log(title = "柜体分配任务", businessType = BusinessType.ALLOCATE)
    @PostMapping(("/assignTask"))
    public AjaxResult assignTask(@RequestBody @Validated MesAssignTaskVo mesAssignTaskVo) {
        return success(productPlanService.assignTask(mesAssignTaskVo));
    }

    /**
     * 查询物料安装情况
     * @param hashMap (installType=1已安装,installType=2未安装)
     * @return
     */
    @GetMapping(("/selectMaterialInstallationCircumstance"))
    public TableDataInfo selectMaterialInstallationCircumstance(@RequestParam(required = false) Map hashMap) {
        startPage();
        return getDataTable(productPlanService.selectMaterialInstallationCircumstance(hashMap));
    }

}
