package com.mvc.service.copy;

import com.github.pagehelper.PageHelper;
import com.mvc.dao.TServerReadMapper;
import com.mvc.entity.TServerRead;
import com.mvc.entity.TServerReadExample;
import com.mvc.model.TAireadModel;
import com.mvc.model.TServerReadModel;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class TServerReadService {

    @Resource
    private TServerReadMapper tServerReadMapper;

//    @Autowired
//    private TAicbqCfgService tAicbqCfgService;

    public List<TServerReadModel> findTServerReadByAiReadid(String aiReadid) {
        TServerReadExample example = new TServerReadExample();
        TServerReadExample.Criteria criteria = example.createCriteria();
        criteria.andAireadIdEqualTo(aiReadid);

        List<TServerRead> TServerReads = tServerReadMapper.selectByExample(example);
        List<TServerReadModel> resList = new ArrayList<>();
        for (TServerRead TServerRead : TServerReads) {
            TServerReadModel model = new TServerReadModel();
            BeanUtils.copyProperties(TServerRead, model);
            resList.add(model);
        }
        return resList;
    }

    public TServerReadModel saveTServerRead(TServerReadModel model) {
        if (model != null) {
            //model=getModelByUnitType(model);
            model.setServerReadId(StringUtil.getUuid());
            model.setCreateTime(new Date());
            TServerRead TServerRead = new TServerRead();
            BeanUtils.copyProperties(model, TServerRead);
            int insert = tServerReadMapper.insert(TServerRead);
            model.setId(TServerRead.getId());
        }

        return model;
    }

    public TServerReadModel updateTServerRead(TServerReadModel model) {
        TServerRead TServerRead = tServerReadMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TServerRead);
        int i = tServerReadMapper.updateByPrimaryKey(TServerRead);
        return model;
    }

    public int deleteTServerRead(int id) {
        int i = tServerReadMapper.deleteByPrimaryKey(id);
        return i;
    }

//    /**
//     * 根据配置的尾数单位控制识别值的小数点个数
//     * @param model
//     * @return
//     */
/*    private TServerReadModel getModelByUnitType(TServerReadModel model){
        if(model==null||model.getIdentValue()==null) return model;
        try{
            List<TAicbqCfgModel> cfgs= tAicbqCfgService.findTAicbqCfg("",model.getStationCode(),"1");
            if(cfgs!=null&&cfgs.size()>0&&cfgs.get(0).getUintType()!=null){
                //此处暂时是固定的,后续可以从字典表读取(实际影响不大,小数位数的对应关系基本不会变)
                int num=cfgs.get(0).getUintType()<4?4-cfgs.get(0).getUintType():0;
                model.setIdentValue(model.getIdentValue().setScale(num, RoundingMode.DOWN));
            }
            else {
                model.setIdentValue(model.getIdentValue().setScale(1, RoundingMode.DOWN));      //默认1位小数
            }
        }
        catch (Exception e){

        }
        return model;
    }*/

    public TServerReadModel findTServerReadByAiServerReadid(String serverReadId) {
        TServerReadExample example = new TServerReadExample();
        TServerReadExample.Criteria criteria = example.createCriteria();
        criteria.andServerReadIdEqualTo(serverReadId);

        List<TServerRead> tServerReads = tServerReadMapper.selectByExample(example);
        if (tServerReads != null && tServerReads.size() > 0) {
            TServerReadModel model = new TServerReadModel();
            BeanUtils.copyProperties(tServerReads.get(0), model);
            return model;
        }
        return null;
    }


    /**
     * 根据当前读数对象获取最近一条有效数据
     *
     * @param curModel
     * @return
     */
    public TServerReadModel findPreGoodModelByDeviceSn(TAireadModel curModel) {
        if (curModel == null || StringUtils.isEmpty(curModel.getDeviceSn())) return null;

        TServerReadExample example = new TServerReadExample();
        TServerReadExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceSnEqualTo(curModel.getDeviceSn());
        criteria.andGoodFlagIn(Arrays.asList("1", "2", "7", "8"));
        example.setOrderByClause("ID desc");
        PageHelper.startPage(1, 1);

        List<TServerRead> list = tServerReadMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            TServerReadModel model = new TServerReadModel();
            BeanUtils.copyProperties(list.get(0), model);
            return model;
        }
        return null;
    }

    /**
     * 根据当前读数对象获取最近一条数据
     *
     * @param curModel
     * @return
     */
    public TServerReadModel findPreModelByDeviceSn(TAireadModel curModel) {
        if (curModel == null || StringUtils.isEmpty(curModel.getDeviceSn())) return null;

        TServerReadExample example = new TServerReadExample();
        TServerReadExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceSnEqualTo(curModel.getDeviceSn());
        example.setOrderByClause("ID desc");
        PageHelper.startPage(1, 1);

        List<TServerRead> list = tServerReadMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            TServerReadModel model = new TServerReadModel();
            BeanUtils.copyProperties(list.get(0), model);
            return model;
        }
        return null;
    }

    /**
     * 根据读数表相关信息获取数据列表
     *
     * @param deviceSn
     * @param stationCode
     * @param sampleTime
     * @return
     */
    public Map<String, TServerReadModel> findServerReadModelsByAireadInfo(String deviceSn, String stationCode, Date sampleTime) {
        TServerReadExample example = new TServerReadExample();
        TServerReadExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(deviceSn)) {
            criteria.andDeviceSnEqualTo(deviceSn);
        } else if (StringUtils.isNotEmpty(stationCode)) {
            criteria.andStationCodeEqualTo(stationCode);
        } else {
            return null;
        }
        if (sampleTime != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(sampleTime);
        }
        //使用map是为了不再检索
        return getStringTServerReadModelMapByExample(example);
    }

    /**
     * 根据TAiRead业务主键列表获取服务侧数据
     *
     * @param aiReadIds
     * @return
     */
    public Map<String, TServerReadModel> findServerReadModelsByAireadIds(List<String> aiReadIds) {
        if(aiReadIds==null||aiReadIds.size()==0){
            return null;
        }

        TServerReadExample example = new TServerReadExample();
        TServerReadExample.Criteria criteria = example.createCriteria();
        criteria.andAireadIdIn(aiReadIds);
        //使用map是为了不再检索
        return getStringTServerReadModelMapByExample(example);
    }

    /**
     * 根据设备编号和时间范围获取map
     *
     * @param deviceSn
     * @param startTime
     * @param endTime
     * @return
     */
    public Map<String, TServerReadModel> queryTServerReadModelsByDeviceSnAndTime(String deviceSn,Date startTime,Date endTime){
        TServerReadExample example = new TServerReadExample();
        TServerReadExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceSnEqualTo(deviceSn);
        criteria.andCreateTimeBetween(startTime,endTime);

        return getStringTServerReadModelMapByExample(example);
    }

    /**
     * 根据example获取Map<String, TServerReadModel>
     *
     * @param example
     * @return
     */
    private Map<String, TServerReadModel> getStringTServerReadModelMapByExample(TServerReadExample example) {
        Map<String, TServerReadModel> map = new HashMap<>();

        List<TServerRead> list = tServerReadMapper.selectByExample(example);
        for (TServerRead serverRead : list) {
            TServerReadModel model = new TServerReadModel();
            BeanUtils.copyProperties(serverRead, model);
            if (!map.containsKey(serverRead.getAireadId())) {
                map.put(serverRead.getAireadId(), model);
            }
        }
        return map;
    }
}
