package cn.estsh.i3plus.ext.mes.apiservice.controller.busi;

import cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.base.WorkCenterRunningStatusService;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesArea;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCenter;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 产线运行状态看板
 *
 * @author yumingxing
 * @version 1.0
 * @date 2021/6/8 14:18
 **/
@Api(tags = "看板")
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES + "/sx/border")
public class WorkCenterRunningStatusController {

    @Autowired
    private WorkCenterRunningStatusService workCenterRunningStatusService;


    @GetMapping(value = "query-data")
    @ApiOperation(value = "看板数据")
    public ResultBean getData(String organizeCode, String workCenterCode, String areaCode) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(workCenterRunningStatusService.getData(workCenterCode, areaCode, organizeCode));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/ep/query-data")
    @ApiOperation(value = "看板数据")
    public ResultBean getEpData(String organizeCode, String workCenterCode, String areaCode) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(workCenterRunningStatusService.getEpData(workCenterCode, areaCode, organizeCode));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "query-four-m")
    @ApiOperation(value = "4m看板")
    public ResultBean getFourMDetail(String organizeCode, String workCenterCode) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(workCenterRunningStatusService.getFourMDetail(workCenterCode, organizeCode));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "query-four-m-by-pager")
    @ApiOperation(value = "4m看板分页")
    public ResultBean getFourMDetailbyPager(String organizeCode, String workCenterCode, Pager pager) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(workCenterRunningStatusService.getFourMDetailByPager(workCenterCode, organizeCode, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "query-shift-code-record")
    @ApiOperation(value = "异常停机")
    public ResultBean getShiftProdCenterRecord(String organizeCode, String workCenterCode) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(workCenterRunningStatusService.abnormalDowntime(workCenterCode, organizeCode));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "query-shift-code-record-by-pager")
    @ApiOperation(value = "异常停机分页")
    public ResultBean getShiftProdCenterRecordByPager(String organizeCode, String workCenterCode, Pager pager) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(workCenterRunningStatusService.abnormalDowntimeByPager(workCenterCode, organizeCode, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "query-order-histogram")
    @ApiOperation(value = "查询柱状图")
    public ResultBean workOrderOHistogram(String organizeCode, String workCenterCode, String currentTime, Pager pager) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(workCenterRunningStatusService.workOrderOHistogram(organizeCode, workCenterCode, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/ep/query-order-histogram")
    @ApiOperation(value = "查询柱状图")
    public ResultBean epWorkOrderOHistogram(String organizeCode, String workCenterCode, Pager pager) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(workCenterRunningStatusService.epWorkOrderOHistogram(organizeCode, workCenterCode, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/mes-center/query")
    @ApiOperation(value = "查询生产线")
    public ResultBean queryMesWorkCenter(MesWorkCenter workCenter) {
        try {

            return ResultBean.success("操作成功").setResultList(workCenterRunningStatusService.queryWorkCenter(workCenter))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
    @GetMapping(value = "/mes-area/query")
    @ApiOperation(value = "查询区域")
    public ResultBean queryMesArea(MesArea mesArea) {
        try {

            return ResultBean.success("操作成功").setResultList(workCenterRunningStatusService.queryMesArea(mesArea))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
