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

import cn.estsh.i3plus.ext.mes.api.busi.ISxProductDataReportFormService;
import cn.estsh.i3plus.ext.mes.pojo.model.SxProductDataReportAttributeModel;
import cn.estsh.i3plus.ext.mes.pojo.model.SxProductDataReportParamModel;
import cn.estsh.i3plus.ext.mes.pojo.model.SxProductDataReportResultModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesObjectCfg;
import cn.estsh.i3plus.pojo.mes.bean.MesPlc;
import cn.estsh.i3plus.pojo.mes.bean.MesProductData;
import cn.estsh.i3plus.pojo.mes.model.ProductDataModel;
import cn.estsh.i3plus.pojo.mes.repository.MesObjectCfgRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesPlcRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesProductDataRepository;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class SxProductDataReportFormService implements ISxProductDataReportFormService {

    @Autowired
    private MesPlcRepository plcRepository;

    @Autowired
    private MesObjectCfgRepository objectCfgRepository;

    @Autowired
    private MesProductDataRepository productDataRepository;

    @Override
    public SxProductDataReportResultModel queryProductDataReport(SxProductDataReportParamModel model, Pager pager) {
        SxProductDataReportResultModel result = new SxProductDataReportResultModel();

        List<SxProductDataReportAttributeModel> attributeModelList = packStandardAttribute();

        List<Map<String, String>> list = new ArrayList<>();
        Integer count = 0;

        List<MesPlc> plcList = plcRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.WORK_CENTER_CODE, MesExtConstWords.WORK_CELL_CODE},
                new Object[]{model.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), model.getWorkCenterCode(), model.getWorkCellCode()});
        List<String> plcCodeList = CollectionUtils.isEmpty(plcList) ? null :
                plcList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getPlcCode()))).map(MesPlc::getPlcCode).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(plcCodeList)) {
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
            DdlPreparedPack.getInPackList(plcCodeList, MesExtConstWords.PLC_CODE, packBean);
            DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), MesExtConstWords.IS_SHOW, packBean);
            DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesExtConstWords.SHOW_SEQ}, packBean);
            List<MesObjectCfg> objectCfgList = objectCfgRepository.findByHqlWhere(packBean);
            if (!CollectionUtils.isEmpty(objectCfgList)) {

                attributeModelList = packDynamicAttribute(attributeModelList, objectCfgList);

                DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
                DdlPreparedPack.getStringEqualPack(model.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, ddlPackBean);
                DdlPreparedPack.getStringEqualPack(model.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, ddlPackBean);
                DdlPreparedPack.getStringEqualPack(model.getWorkOrderNo(), MesExtConstWords.ORDER_NO, ddlPackBean);
                DdlPreparedPack.getStringEqualPack(model.getSerialNumber(), MesExtConstWords.SERIAL_NUMBER, ddlPackBean);
                DdlPreparedPack.getStringEqualPack(model.getPartNo(), MesExtConstWords.PART_NO, ddlPackBean);
                DdlPreparedPack.timeBuilder(model.getCreateDatetimeStart(), model.getCreateDatetimeEnd(), MesExtConstWords.CREATE_DATE_TIME, ddlPackBean, true);
                count = productDataRepository.findByHqlWhereCount(ddlPackBean);
                List<MesProductData> productDataList = productDataRepository.findByHqlPage(ddlPackBean, pager);
                if (!CollectionUtils.isEmpty(productDataList)) {

                    for (MesProductData productData : productDataList) {
                        if (null == productData || StringUtils.isEmpty(productData.getLineData())) {
                            continue;
                        }
                        Map<String, String> map = new HashMap<>();
                        map.put(MesExtConstWords.CREATE_DATE_TIME, productData.getCreateDatetime());
                        map.put(MesExtConstWords.WORK_CENTER_CODE, productData.getWorkCenterCode());
                        map.put(MesExtConstWords.WORK_CENTER_NAME, productData.getWorkCenterName());
                        map.put(MesExtConstWords.WORK_CELL_CODE, productData.getWorkCellCode());
                        map.put(MesExtConstWords.WORK_CELL_NAME, productData.getWorkCellName());
                        map.put(MesExtConstWords.ORDER_NO, productData.getOrderNo());
                        map.put(MesExtConstWords.SERIAL_NUMBER, productData.getSerialNumber());
                        map.put(MesExtConstWords.PART_NO, productData.getPartNo());
                        map.put(MesExtConstWords.PART_DESC, productData.getPartDesc());

                        List<ProductDataModel> productDataModels = JSONObject.parseArray(productData.getLineData(), ProductDataModel.class);
                        if (!CollectionUtils.isEmpty(productDataModels)) {
                            for (ProductDataModel productDataModel : productDataModels) {
                                map.put(productDataModel.getFieldCode(), productDataModel.getFieldValue());
                            }
                        }
                        list.add(map);
                    }
                }
            }
        }

        pager = PagerHelper.getPager(pager, count);
        result.setListPager(new ListPager(list, pager));
        result.setAttributeModelList(attributeModelList);
        return result;
    }

    private List<SxProductDataReportAttributeModel> packStandardAttribute() {
        List<SxProductDataReportAttributeModel> attributeModelList = new ArrayList<>();

        SxProductDataReportAttributeModel model = new SxProductDataReportAttributeModel();
        model.setFieldCode(MesExtConstWords.CREATE_DATE_TIME);
        model.setFieldName("创建时间");
        attributeModelList.add(model);

        SxProductDataReportAttributeModel model0 = new SxProductDataReportAttributeModel();
        model0.setFieldCode(MesExtConstWords.WORK_CENTER_CODE);
        model0.setFieldName("生产线代码");
        attributeModelList.add(model0);

        SxProductDataReportAttributeModel model1 = new SxProductDataReportAttributeModel();
        model1.setFieldCode(MesExtConstWords.WORK_CENTER_NAME);
        model1.setFieldName("生产线名称");
        attributeModelList.add(model1);

        SxProductDataReportAttributeModel model2 = new SxProductDataReportAttributeModel();
        model2.setFieldCode(MesExtConstWords.WORK_CELL_CODE);
        model2.setFieldName("工位代码");
        attributeModelList.add(model2);

        SxProductDataReportAttributeModel model3 = new SxProductDataReportAttributeModel();
        model3.setFieldCode(MesExtConstWords.WORK_CELL_NAME);
        model3.setFieldName("工位名称");
        attributeModelList.add(model3);

        SxProductDataReportAttributeModel model4 = new SxProductDataReportAttributeModel();
        model4.setFieldCode(MesExtConstWords.ORDER_NO);
        model4.setFieldName("生产工单号");
        attributeModelList.add(model4);

        SxProductDataReportAttributeModel model5 = new SxProductDataReportAttributeModel();
        model5.setFieldCode(MesExtConstWords.SERIAL_NUMBER);
        model5.setFieldName("产品条码");
        attributeModelList.add(model5);

        SxProductDataReportAttributeModel model6 = new SxProductDataReportAttributeModel();
        model6.setFieldCode(MesExtConstWords.PART_NO);
        model6.setFieldName("物料编码");
        attributeModelList.add(model6);

        SxProductDataReportAttributeModel model7 = new SxProductDataReportAttributeModel();
        model7.setFieldCode(MesExtConstWords.PART_DESC);
        model7.setFieldName("物料名称");
        attributeModelList.add(model7);

        return attributeModelList;
    }

    private List<SxProductDataReportAttributeModel> packDynamicAttribute(List<SxProductDataReportAttributeModel> attributeModelList, List<MesObjectCfg> objectCfgList) {
        for (MesObjectCfg objectCfg : objectCfgList) {
            if (null == objectCfg) {
                continue;
            }
            SxProductDataReportAttributeModel model = new SxProductDataReportAttributeModel();
            model.setFieldCode(objectCfg.getFieldCode());
            model.setFieldName(objectCfg.getFieldName());
            attributeModelList.add(model);
        }
        return attributeModelList;
    }
}
