package com.sw.mes.production.service.impl;


import com.alibaba.fastjson.JSON;
import com.sw.mes.base.factory.inspect.entity.DocumentPrint;
import com.sw.mes.base.factory.inspect.service.DocumentPrintService;
import com.sw.mes.base.system.service.impl.PrintService;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.dto.PrintInfoDto;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultCode;
import com.sw.mes.production.dao.TruncationReccutDao;
import com.sw.mes.production.entity.TruncationReccut;
import com.sw.mes.production.entity.TruncationReccutDetails;
import com.sw.mes.production.service.TruncationReccutDetailsService;
import com.sw.mes.production.service.TruncationReccutService;
import com.sw.mes.yb.production.entity.LongCrystal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class TruncationReccutServiceImpl extends BaseServiceImpl<TruncationReccut> implements TruncationReccutService {
    TruncationReccutDao dao;

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

    @Autowired
    private TruncationReccutService truncationReccutService;

    @Autowired
    private TruncationReccutDetailsService truncationReccutDetailsService;

    @Autowired
    private DocumentPrintService documentPrintService;

    @Autowired
    private PrintService printService;

    @Override
    public List<String> save(TruncationReccut truncationReccut, List<TruncationReccutDetails> truncationReccutDetailss, LongCrystal longCrystal) {
        truncationReccutService.saveOrUpdate(truncationReccut);
        List<Map<String, String>> mapList = new LinkedList<>();
        truncationReccutDetailss.forEach(truncationReccutDetails -> {
            truncationReccutDetails.setReccutId(truncationReccut.getId());
            Map<String, String> mapData = new HashMap<>();
            mapData.put("no", truncationReccutDetails.getSectionNo());
            if(longCrystal!=null) {
	            mapData.put("dopant", longCrystal.getDopant());
	            mapData.put("specification", longCrystal.getSpecs());
            }
            mapData.put("length", truncationReccutDetails.getLength().toString());
            mapData.put("remarks1", "回收料");
            mapData.put("remarks2", truncationReccut.getCause());
            mapList.add(mapData);
        });
        truncationReccutDetailsService.saveAll(truncationReccutDetailss);
        List<String> imgArr = new LinkedList<>();
        //查询短棒打印模版配置信息
        DocumentPrint documentPrintCrystal = documentPrintService.findByDocumentMould(DocumentPrint.CRYSTAL_DOCUMENT_MOULD_JD);
        if (documentPrintCrystal != null && mapList.size() > 0) {
            //打印短棒编码
            Result result2 = printCrystalNo(mapList, documentPrintCrystal, 1);
            if (result2.getCode() == ResultCode.SUCCESS.getCode() && result2.getData() != null
                    && result2.getData().toString().contains("[")) {
                imgArr.addAll(JSON.parseArray(result2.getData().toString(), String.class));
            }
        }
        return imgArr;
    }

    /**
     * 打印晶段编号
     *
     * @param keyvaluess
     * @param documentPrintCrystal
     */
    public Result printCrystalNo(List<Map<String, String>> keyvaluess, DocumentPrint documentPrintCrystal, int printNum) {
        PrintInfoDto printInfoDto = new PrintInfoDto();
        printInfoDto.setPrinterName(documentPrintCrystal.getPrintName());
        printInfoDto.setTemplateAddress(documentPrintCrystal.getDocumentPath());
        printInfoDto.setDocIp(documentPrintCrystal.getPrintIp());
        printInfoDto.setData(JSON.toJSONString(keyvaluess));
        printInfoDto.setPrintNum(printNum);
        return printService.printShow(printInfoDto);
    }

    @Override
    public TruncationReccut findByCrystalNo(String crystalNo) {
        return dao.findOneByCrystalNoOrderByIdDesc(crystalNo);
    }

    @Override
    public void deleted(Long id) {
        truncationReccutService.delete(id);
        truncationReccutDetailsService.deleteByReccutId(id);
    }
}
