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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesTemplateService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesPartShippingGroupService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.print.IPrintTemplateStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.print.sortshipping.ISortShippingDispatchStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.api.iservice.base.IConfigService;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.*;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesLoadingList;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagement;
import cn.estsh.i3plus.pojo.mes.repository.IMesLoadingGroupRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesConfigRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesPartShippingGroupDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesPartShippingGroupRepository;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
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.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
public class MesPartShippingGroupService implements IMesPartShippingGroupService {

    @Autowired
    private IConfigService configService;

    @Autowired
    private IMesTemplateService templateService;

    @Autowired
    private MesPartShippingGroupRepository shippingGroupRepository;

    @Autowired
    private MesPartShippingGroupDetailRepository mesPartShippingGroupDetailRepository;

    @Autowired
    private MesConfigRepository mesConfigRao;

    @Autowired
    private IMesLoadingGroupRepository loadingGroupRDao;

    public List<MesPartShippingGroup> findAll(MesPartShippingGroup partShippingGroup) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(partShippingGroup.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(partShippingGroup.getShippingGroupCode(), MesPcnExtConstWords.SHIPPING_GROUP_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(partShippingGroup.getShippingGroupName(), MesPcnExtConstWords.SHIPPING_GROUP_NAME, packBean);
        return shippingGroupRepository.findByHqlWhere(packBean);
    }

    @Override
    public MesPartShippingGroup getMesPartShippingGroup(String organizeCode, String shippingGroupCode) {
        if(StringUtil.isEmpty(organizeCode) || StringUtil.isEmpty(shippingGroupCode)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(shippingGroupCode, MesPcnExtConstWords.SHIPPING_GROUP_CODE, packBean);
        return shippingGroupRepository.getByProperty(packBean);
    }

    @Override
    public ResultBean doMesMesShippingOrderManagementPrint(MesShippingOrderManagement bean) {

        List<String> chooseCodeList = Arrays.asList(bean.getShippingGroupCode().split(MesPcnExtConstWords.COMMA));
        List<MesConfig> configList = configService.getMesConfigListByCfgCodeKey(MesPcnExtConstWords.SORT_SHIPPING_PRINT_TEMPLATE, MesPcnExtConstWords.CUSTOM_CFG, bean.getOrganizeCode());
        Optional<MesConfig> optional = CollectionUtils.isEmpty(configList) ? null :
                configList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getCfgValue())
                        && !StringUtils.isEmpty(o.getCfgType()) && chooseCodeList.contains(o.getCfgType()))).findFirst();

        String templeteCodeCfg = (null == optional || !optional.isPresent()) ? MesPcnExtConstWords.SORT_SHIPPING_PRINT_TEMPLATE : optional.get().getCfgValue();
        MesConfig config = configService.getMesConfigByCfgCodeKeyOrg(templeteCodeCfg, MesPcnExtConstWords.SORT_SHIPPING_PRINT_TEMPLATE, bean.getOrganizeCode());
        if (null == config || StringUtils.isEmpty(config.getCfgValue())) MesPcnException.throwBusiException("未配置排序发运全局打印配置[%s]", MesPcnExtConstWords.SORT_SHIPPING_PRINT_TEMPLATE);

        MesLabelTemplate labelTemplate = templateService.getMesLabelTemplate(config.getCfgValue(), bean.getOrganizeCode());
        if (StringUtils.isEmpty(labelTemplate.getMethodCode())) MesPcnException.throwBusiException("排序发运打印模版[%s]未配置策略方法", config.getCfgValue());

        ISortShippingDispatchStrategyService dispatchStrategyService = (ISortShippingDispatchStrategyService) SpringContextsUtil.getBean(labelTemplate.getMethodCode());
        List<MesShippingOrderManagement> shippingOrderManagementList = dispatchStrategyService.getShippingOrderManagementList(bean);

        if (CollectionUtils.isEmpty(shippingOrderManagementList))
            return ResultBean.success("不存在需要打印的发运单").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());

        log.info("工厂{}排序发运打印 --- 获取到打印数据:{}条 --- bean:{} --- shippingOrderManagementList:{} --- {}",
                bean.getOrganizeCode(), shippingOrderManagementList.size(), JSONObject.toJSONString(bean), JSONObject.toJSONString(shippingOrderManagementList), Thread.currentThread().getName());

        IPrintTemplateStrategyService printStrategyService = (IPrintTemplateStrategyService) SpringContextsUtil.getBean(labelTemplate.getMethodCode());
        ResultBean resultBean = printStrategyService.execute(bean, shippingOrderManagementList);

        log.info("工厂{}排序发运打印 --- 封装打印数据完成 --- {}", bean.getOrganizeCode(), Thread.currentThread().getName());

        return resultBean;
    }

    @Override
    public MesPartShippingGroupDetail getMesPartShippingGroupDetail(String organizeCode, String shippingGroupCode, String partNo, String custPartNo) {
        if(StringUtil.isEmpty(organizeCode) || StringUtil.isEmpty(shippingGroupCode) || StringUtil.isEmpty(partNo) || StringUtil.isEmpty(custPartNo)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(shippingGroupCode, "shippingGroupCode", packBean);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", packBean);
        DdlPreparedPack.getStringEqualPack(custPartNo, "custPartNo", packBean);
        return mesPartShippingGroupDetailRepository.getByProperty(packBean);
    }

    @Override
    public MesPartShippingGroupDetail getMesPartShippingGroupDetailByPartNo(String organizeCode, String shippingGroupCode, String partNo) {
        if(StringUtil.isEmpty(organizeCode) || StringUtil.isEmpty(shippingGroupCode) || StringUtil.isEmpty(partNo)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(shippingGroupCode, "shippingGroupCode", packBean);
        DdlPreparedPack.getStringEqualPack(partNo, "partNo", packBean);
        return mesPartShippingGroupDetailRepository.getByProperty(packBean);
    }

    @Override
    public boolean isFirstInstall(MesPartShippingGroup shippingGroup, MesLoadingList loadingList) {
        if (loadingList != null) {
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(loadingList.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(loadingList.getLoadingGroupNo(), "loadingGroupNo", packBean);
            MesLoadingGroup loadingGroup = loadingGroupRDao.getByProperty(packBean);
            if (loadingGroup != null && loadingGroup.getIsFirstInstall() != null) {
                return Objects.equals(loadingGroup.getIsFirstInstall(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
            }
        }
        return Objects.equals(shippingGroup.getIsFirstInstall(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
    }

}
