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.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesConfig;
import cn.estsh.i3plus.pojo.mes.bean.MesLabelTemplate;
import cn.estsh.i3plus.pojo.mes.bean.MesPartShippingGroup;
import cn.estsh.i3plus.pojo.mes.bean.MesPartShippingGroupDetail;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagement;
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.List;

@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;

    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) {

        MesConfig config = configService.getMesConfigByCfgCode(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);
    }

}
