package com.sifang.modules.customer.Service;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sifang.modules.config.SfCacheService;
import com.sifang.modules.customer.dao.CrmAcceptingReportDao;
import com.sifang.modules.customer.entity.CrmAcceptingReport;
import com.sifang.modules.platform.utils.Base64Url;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CrmAcceptingReportService extends BaseService {


    @Autowired
    CrmAcceptingReportDao reportDao;

    @Autowired
    SfCacheService cacheService;
    private Logger loger = LoggerFactory.getLogger(getClass());


    //TODO 暂时屏蔽 验收报告需要重新调整 待处理
    public synchronized Map<String,String> dealWithQueue(String reportId) {
        Map<String,String> buymap = Maps.newHashMap();
        try {
            String key = "redisAccReportImgs"+reportId;
            //为了尽量确保数据的一致性,处理之前先从redis中获取验收报告
            String residue = cacheService.get(key);
            if (StringUtil.isNotBlank(residue)) {//如果有,就直接返回
                 buymap = new Gson().fromJson(residue, new TypeToken<Map<String,String>>() {
                }.getType());
                return buymap;
            }
            //取出验收报告的数据
            CrmAcceptingReport report = reportDao.get(reportId);
            buymap.put("componentSpec",report.getComponentSpec().toString());
            buymap.put("componentNum",report.getComponentSpec().toString());
            buymap.put("capacity",report.getCapacity().toString());
            buymap.put("nbqPlateImg", Base64Url.ImageToBase64ByOnline(report.getNbqPlateImg()));
//            buymap.put("txPlateImg",Base64Url.ImageToBase64ByOnline(report.getTxPlateImg()));
            buymap.put("zjPlateImg",Base64Url.ImageToBase64ByOnline(report.getZjPlateImg()));
            /*buymap.put("cxImg",Base64Url.ImageToBase64ByOnline(report.getCxImg()));
            if(StringUtil.isNotBlank(report.getPzImgs())){
                findForIgs(buymap,report.getPzImgs(),"pzImgs");
            }
            if(StringUtil.isNotBlank(report.getZjImgs())){
                findForIgs(buymap,report.getZjImgs(),"zjImgs");
            }
            if(StringUtil.isNotBlank(report.getInstallImgs())){
                findForIgs(buymap,report.getInstallImgs(),"installImgs");
            }
            if(StringUtil.isNotBlank(report.getTestImgs())){
                findForIgs(buymap,report.getTestImgs(),"testImgs");
            }
            if(StringUtil.isNotBlank(report.getJdImgs())){
                findForIgs(buymap,report.getJdImgs(),"jdImgs");
            }
            if(StringUtil.isNotBlank(report.getTotalImgs())){
                findForIgs(buymap,report.getTotalImgs(),"totalImgs");
            }
            if(StringUtil.isNotBlank(report.getOtherImgs())){
                findForIgs(buymap,report.getOtherImgs(),"otherImgs");
            }
            if(StringUtil.isNotBlank(report.getPurchaseSaleImg())){
                findForIgs(buymap,report.getPurchaseSaleImg(),"purchaseSaleImg");
            }
            buymap.put("conclusion",report.getConclusion());
            buymap.put("acceptTime", report.getAcceptTime().toString());//验收时间*/
            cacheService.setex(key,172800,new Gson().toJson(buymap));
        } catch (Exception e) {
            loger.error("Exception :{}",e);
        }
        return buymap;
    }


    public void findForIgs(Map<String,String> map,String imgs,String name){
        String img[] = imgs.split(",");
        for(int i=0;i<img.length;i++){
            if(i==0){
                map.put(name,Base64Url.ImageToBase64ByOnline(img[i]));
            }else{
                map.put(name+i,Base64Url.ImageToBase64ByOnline(img[i]));
            }
        }
    }

}
