package com.sw.mes.yb.plan.service.impl;


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.NumberUtil;
import com.sw.mes.core.utils.WbServiceUtils;
import com.sw.mes.third.entity.Teco;
import com.sw.mes.third.entity.WebServiceApiName;
import com.sw.mes.third.entity.WebServiceUri;
import com.sw.mes.third.service.SendWebService;
import com.sw.mes.yb.plan.dao.YbProductionOrderDao;
import com.sw.mes.yb.plan.entity.YbProductionOrder;
import com.sw.mes.yb.plan.service.YbProductionOrderService;
import com.sw.mes.yb.production.entity.LongCrystal;
import com.sw.mes.yb.production.service.LongCrystalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.xml.namespace.QName;
import java.util.*;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
@Slf4j
public class YbProductionOrderServiceImpl extends BaseServiceImpl<YbProductionOrder> implements YbProductionOrderService {

    @Autowired
    private LongCrystalService longCrystalService;

    YbProductionOrderDao dao;
	@Autowired
    public void setDao(YbProductionOrderDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Autowired
    private SendWebService sendWebService;

    @Override
    public void close(YbProductionOrder sapProductionOrder) {
        dao.save(sapProductionOrder);
        //向SAP发送订单关闭信息
//        try {
//            List<Teco> list = new ArrayList<>();
//            Teco teco = new Teco();
//            teco.setZMESID(UUID.randomUUID().toString());
//            teco.setWERKS("1011");
//            teco.setSTATUS("1");
//            teco.setCOMMENT("");
//            teco.setAUART(sapProductionOrder.getAuart());
//            teco.setAUFNR(sapProductionOrder.getAufnr());
//            teco.setMATNR(sapProductionOrder.getMatnr());
//            teco.setISSUEDATE(WbServiceUtils.getSapDate(new Date()));//日期 yyyymmdd
//            teco.setISSUETIME(WbServiceUtils.getSapTime(new Date()));//时间 HHmmss
//            list.add(teco);
//            Map map = sendWebService.mesToSap(list, WebServiceUri.WSDL_URL, WebServiceApiName.TECO, 4000);
//            String sapResult = map.get(new QName("ZTYPE")).toString();
//            if (!"S".equals(sapResult)) {
//                String sapError = map.get(new QName("ZMESSAGE")).toString();
//                throw new RuntimeException("向SAP发送订单关闭信息异常:" + sapError);
//            }
//        } catch (Exception e) {
//            log.error("向SAP发送订单关闭信息异常:" + e.getMessage());
//            throw new RuntimeException("向SAP发送订单关闭信息异常:" + e.getMessage());
//        }
    }

    @Override
    public YbProductionOrder findByAufnr(String aufnr) {
        List<YbProductionOrder> list = dao.findByAufnr(aufnr);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    @Override
    public List<YbProductionOrder> getYbProductionOrder(String productType, String dopant, String conductiveType, String specs, Date gmtCreate) {
        return dao.getYbProductionOrder(productType,dopant,conductiveType,specs,gmtCreate);
    }

    @Override
    public YbProductionOrder getYbProductionOrderByMk(Map<String, Object> inspectMap,String productModelCode) {
        /**
         *
         * @param productType 产品类别
         * @param conductiveType 导电型号
         * @param specs 产品模型规格  有问题
         * @param dopant 掺杂剂
         * @param matnr 物料编码
         * @return
         */
        String productType = NumberUtil.object2String(inspectMap.get("product_type"), null);

        String conductiveType = NumberUtil.object2String(inspectMap.get("conductive_type"), null);

        String dopant = NumberUtil.object2String(inspectMap.get("dopant"), null);

        String specs = NumberUtil.object2String(inspectMap.get("product_type_second"), null);

        String matnr = NumberUtil.object2String(productModelCode, null);

        String crystalNo = NumberUtil.object2String(inspectMap.get("crystal_no"), null);
        LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(crystalNo);

        String area = null;
        if(null != byCrystalNo){
            area = byCrystalNo.getArea();
        }

        if(productType!=null&&conductiveType!=null&&dopant!=null&&specs!=null&&matnr!=null&&area!=null){
            List<YbProductionOrder> ybProductionOrderByMk = dao.getYbProductionOrderByMk(productType, dopant, conductiveType, specs, matnr, area);
            if(!CollectionUtils.isEmpty(ybProductionOrderByMk)){
                return ybProductionOrderByMk.get(0);
            }
            return null;
        }else{
            return null;
        }
    }

    @Override
    public List<YbProductionOrder> findByProductCodeAndTime(String matnr, Date gstrp, Date gltrp) {
        return dao.findByStatusAndProductCodeAndTime(true, matnr, gstrp, gltrp);
    }

}
