package com.mvc.service.taizhang;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TAicnnMapper;
import com.mvc.entity.TAicnn;
import com.mvc.entity.TAicnnExample;
import com.mvc.model.TAicnnModel;
import com.mvc.service.PackLogMana;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

//CNN网络输出数据Service
@Service
@Transactional
public class TAicnnService {

    @Autowired(required = false)
    private TAicnnMapper tAicnnMapper;

    public Map<String, Object> findAllTAicnn(TAicnnModel queryModel) {
        TAicnnExample example = new TAicnnExample();
        TAicnnExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(queryModel.getDeviceSn())){
            criteria.andDeviceSnEqualTo(queryModel.getDeviceSn());
        }
        if(StringUtils.isNotEmpty(queryModel.getStationCode())){
            criteria.andStationCodeEqualTo(queryModel.getStationCode());
        }
        example.setOrderByClause(" REV_TIME desc");
        Map<String, Object> resMap = new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPage(), queryModel.getRows());
        List<TAicnn> TAicnns = tAicnnMapper.selectByExample(example);
        List<TAicnnModel> resList = new ArrayList<>();
        for (TAicnn TAicnn : TAicnns) {
            TAicnnModel model = new TAicnnModel();
            BeanUtils.copyProperties(TAicnn, model);
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public TAicnnModel saveTAicnn(TAicnnModel model) {
        TAicnn tAicnn = new TAicnn();
        BeanUtils.copyProperties(model, tAicnn);
        int insert = tAicnnMapper.insert(tAicnn);
        model.setId(tAicnn.getId());
        return model;
    }

    /**
     * 保存CNN数据,并存储文件
     * @param model
     * @param logger
     * @return
     */
    public TAicnnModel saveTAicnn(TAicnnModel model,StringBuffer logger) {
        TAicnn tAicnn = new TAicnn();
        String fileName= PackLogMana.saveCnnDataByRawMsg(model,logger);
        model.setFileName(fileName);
        BeanUtils.copyProperties(model, tAicnn);
        int insert = tAicnnMapper.insert(tAicnn);
        model.setId(tAicnn.getId());
        return model;
    }

    public TAicnnModel updateTAicnn(TAicnnModel model) {
        TAicnn TAicnn = tAicnnMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TAicnn);
        int i = tAicnnMapper.updateByPrimaryKey(TAicnn);
        return model;
    }

    public int deleteTAicnn(int id) {
        int i = tAicnnMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据读数表中的信息获取对应的CNN数据包(含原始数据文本和二维数组)
     * @param randomCode
     * @param deviceSn
     * @return
     */
    public TAicnnModel findTAicnnDataByTAireadInfo(String randomCode,String deviceSn){
        TAicnnExample example = new TAicnnExample();
        TAicnnExample.Criteria criteria= example.createCriteria();
        criteria.andRandomCodeEqualTo(randomCode);
        criteria.andDeviceSnEqualTo(deviceSn);

        List<TAicnn> tAicnns = tAicnnMapper.selectByExample(example);
        if(tAicnns!=null&&tAicnns.size()>0){
            TAicnnModel model=new TAicnnModel();
            BeanUtils.copyProperties(tAicnns.get(0),model);
            setCnnData(model);
            return model;
        }
        return null;
    }

    /**
     * 根据ID对应的CNN数据(包含原始数据文本和二维数组)
     * @param id
     * @return
     */
    public TAicnnModel findAicnnDataById(Integer id){
        TAicnn tAicnns = tAicnnMapper.selectByPrimaryKey(id);
        if(tAicnns!=null){
            TAicnnModel model=new TAicnnModel();
            BeanUtils.copyProperties(tAicnns,model);
            setCnnData(model);
            return model;
        }
        return null;
    }

    /**
     * 从文件中读取CNN数据并填充到Model中
     * @param model
     */
    private void setCnnData(TAicnnModel model){
        //int[][] cnnData = PackLogMana.getCnnRawData2(model.getStationCode(),model.getFileName());
        int[][] cnnData = PackLogMana.getCnnRawData_Simple(model);
        model.setCnnData(cnnData);
        model.setCnnMsg(getCnnMsgByArray(cnnData));
    }

    /**
     * 获取文本矩阵,供前台展示
     * @param cnnData
     * @return
     */
    private String getCnnMsgByArray(int[][] cnnData){
        StringBuilder sb=new StringBuilder();
        int num=1;
        if(cnnData!=null){
            sb.append(" -------------------------------------------------------\r\n");
            sb.append("|");
            sb.append("  S/N\t|");
            sb.append("class\t|");
            sb.append(" prob\t|");
            sb.append(" xmin\t|");
            sb.append(" ymin\t|");
            sb.append(" xmax\t|");
            sb.append(" ymax\t|\r\n");
            sb.append(" --------------------------------------------------------\r\n");
            for (int[] ints : cnnData) {
                sb.append("|");
                sb.append(StringUtils.leftPad(num+"",5," "));
                sb.append("\t|");
                for (int anInt : ints) {
                    sb.append(StringUtils.leftPad(anInt+"",5," "));
                    sb.append("\t|");
                }
                sb.append("\r\n -------------------------------------------------------");
                sb.append("\r\n");
                num++;
            }
        }
        return sb.toString();
    }
}
