package com.foreverwin.mesnac.production.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.meapi.dto.SfcDto;
import com.foreverwin.mesnac.meapi.dto.WorkCenterDto;
import com.foreverwin.mesnac.meapi.model.Sfc;
import com.foreverwin.mesnac.production.service.PodTemplateService;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

@Controller
@RequestMapping("/sfcDataMains")
public class PodTemplateController {

    @Autowired
    public PodTemplateService podTemplateService;

    /**
     * 资源回车
     * @param workCenterDto
     * @return
     */
    @ResponseBody
    @GetMapping("/resrceEnter")
    public R resrceEnter(WorkCenterDto workCenterDto) {
        return R.ok(podTemplateService.resrceEnter(workCenterDto));
    }

    /**
     * sfc回车
     * @param sfcDto
     * @return
     */
    @ResponseBody
    @GetMapping("/sfcEnter")
    public R sfcEnter(SfcDto sfcDto) {
        return R.ok(podTemplateService.sfcEnter(sfcDto));
    }

    /**
     * sfc开始
     * @param map
     * @return
     */
    @ResponseBody
    @PostMapping("/sfcStart")
    public R sfcStart(@RequestBody Map<String,Object> map) {
        podTemplateService.sfcStart(map);
        return R.ok();
    }

    /**
     * sfc完成
     * @param map
     * @return
     */
    @ResponseBody
    @PostMapping("/sfcComplete")
    public R sfcComplete(@RequestBody Map<String,Object> map) {
        List<SfcDto> sfcDtoList = (List<SfcDto>) map.get("sfcDtoList");
        ObjectMapper mapper = new ObjectMapper();
        sfcDtoList = mapper.convertValue(sfcDtoList, new TypeReference<List<SfcDto>>() {
        });
        String resrce = (String) map.get("resrce");
        if (sfcDtoList == null || sfcDtoList.size() < 1) {
            throw new BaseException("作业列表不能为空");
        }
        if (StringUtil.isBlank(resrce)) {
            throw new BaseException("资源不能为空");
        }
        ArrayList<String> sfcList = new ArrayList<>();
        try {
            for (SfcDto sfcDto : sfcDtoList) {
                String completeSfc = podTemplateService.sfcComplete(sfcDto, resrce);
                sfcList.add(completeSfc);
            }
        }catch (Exception e){
            return R.failed(sfcList,e.getMessage());
        }

        return R.ok();
    }

    /**
     * 生产操作员面板工作列表
     * @param sfcDto
     * @return
     */
    @ResponseBody
    @GetMapping("/getSfcInfo")
    public R getSfcInfo(SfcDto sfcDto) {
        return R.ok(podTemplateService.getSfcInfo(sfcDto));
    }

    /**
     * 生产操作员工作界面
     */
    @ResponseBody
    @GetMapping("/getInfoBySfc")
    public R getSfcInfo(Sfc sfc) {
        return R.ok(podTemplateService.getInfoBySfc(sfc));
    }
}
