package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.station.function;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesMediaFileCfgService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesProdShiftRecordService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionCustomContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionProcessContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProdShiftContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseSwsService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.function.IFsmModuleFunctionService;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCell;
import cn.estsh.i3plus.pojo.mes.model.ButtonDynamicModel;
import cn.estsh.i3plus.pojo.mes.model.StationKvBean;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description : 清除加工数【按钮】
 **/
@Service
public class MesFunctionProductionStatisticsService extends BaseSwsService implements IFsmModuleFunctionService {

    @Autowired
    private IMesProductionCustomContextStepService productionCustomContextStepService;

    @Autowired
    private IMesMediaFileCfgService mediaFileCfgService;

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProdShiftRecordService prodShiftRecordService;

    @Override
    public Boolean doFunction(StationRequestBean reqBean, StationResultBean resultBean, ButtonDynamicModel buttonDynamicModel) {

        productionCustomContextStepService.removeProductionStatisticsContext(reqBean);

        this.sendMessage(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PICK.getValue()).scanInfo(buttonDynamicModel.getFunctionValue()),
                String.format("生产线[%s]工位[%s]%s成功!", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), buttonDynamicModel.getButtonName()), MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

        //重置页面的加工数
        resetStationData(reqBean, productionProcessContextStepService.getProductionProcessContext(reqBean, false).getWorkCell());

        return true;

    }

    //重置页面的加工数
    private void resetStationData(StationRequestBean reqBean, MesWorkCell workCell) {
        if (null == workCell) return;

        //如果工位当前配置的是展示柱状图则无须重置加工数
        if (!StringUtils.isEmpty(workCell.getIsShowMsg()) && workCell.getIsShowMsg().compareTo(MesExtEnumUtil.WORK_FILE_TYPE.HISTOGRAM.getValue()) == 0) return;

        //判断是否配置的是展示文件
        String url = null;
        if (!StringUtils.isEmpty(workCell.getIsShowMsg()) && !StringUtils.isEmpty(MesExtEnumUtil.WORK_FILE_TYPE.valueOfDescription(workCell.getIsShowMsg()))) {
            url = mediaFileCfgService.getMediaFileUrl(reqBean.getOrganizeCode(), workCell.getFileId());
        }

        //如果不存在文件, 且当前工位配置需要统计加工数的情况下才进行重置加工数
        if (StringUtils.isEmpty(url) && !StringUtils.isEmpty(workCell.getIsCountFinish()) && workCell.getIsCountFinish().compareTo(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()) == 0) {
            //获取生产线的当前班组班次信息
            MesProdShiftContext mesProdShiftKvBean = prodShiftRecordService.getMesProdShiftKvBean(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode());
            List<StationKvBean> productionStatisticsContext = productionCustomContextStepService.getProductionStatisticsContext(mesProdShiftKvBean, MesPcnExtConstWords.ZERO_STR);

            StationResultBean resultBean = new StationResultBean();
            resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.MODULE_CUSTOM_CONTENT.getValue());
            resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.TEXT.getValue());
            resultBean.setCustomPageName(MesPcnExtConstWords.CUSTOM_PAGE_NAME_WORK_CELL);
            resultBean.setResultList(productionStatisticsContext);
            this.sendMessage(reqBean, resultBean);
        }

    }

}
