package com.mvc.service.copy;

import com.github.pagehelper.PageHelper;
import com.mvc.constant.BaseConstant;
import com.mvc.dao.TExtendIdentMapper;
import com.mvc.entity.TExtendIdent;
import com.mvc.entity.TExtendIdentExample;
import com.mvc.model.TExtendIdentModel;
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 TExtendIdentService {
    @Resource
    private TExtendIdentMapper tExtendIdentMapper;

    public List<TExtendIdentModel> findTExtendIdentByAireadIdAndType(int aireadId,int identType){
        TExtendIdentExample example = new TExtendIdentExample();
        TExtendIdentExample.Criteria criteria = example.createCriteria();
        criteria.andAireadIdEqualTo(aireadId);
        criteria.andIdentTypeEqualTo(identType);
        return selectByExample(example);
    }

    /**
     * 根据站点编码和识别类型获取最新一条记录
     * @param stationCode
     * @param identType
     * @return
     */
    public TExtendIdentModel findLatestTExtendIdent(String stationCode,int identType){
        TExtendIdentExample example = new TExtendIdentExample();
        TExtendIdentExample.Criteria criteria = example.createCriteria();
        criteria.andStationCodeEqualTo(stationCode);
        criteria.andIdentTypeEqualTo(identType);
        example.setOrderByClause("ID DESC");
        PageHelper.startPage(1, 1);
        List<TExtendIdentModel> list = selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    private List<TExtendIdentModel> selectByExample(TExtendIdentExample example) {
        List<TExtendIdent> tExtendIdents = tExtendIdentMapper.selectByExample(example);
        List<TExtendIdentModel> resList = new ArrayList<>();
        for (TExtendIdent TExtendIdent : tExtendIdents) {
            TExtendIdentModel model = new TExtendIdentModel();
            BeanUtils.copyProperties(TExtendIdent, model);
            resList.add(model);
        }
        return resList;
    }

    public TExtendIdentModel saveTExtendIdent(TExtendIdentModel model) {
        TExtendIdent tExtendIdent = new TExtendIdent();
        BeanUtils.copyProperties(model, tExtendIdent);
        int insert = tExtendIdentMapper.insert(tExtendIdent);
        model.setId(tExtendIdent.getId());
        return model;
    }

    public TExtendIdentModel updateTExtendIdent(TExtendIdentModel model) {
        TExtendIdent tExtendIdent = tExtendIdentMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, tExtendIdent);
        int i = tExtendIdentMapper.updateByPrimaryKey(tExtendIdent);
        return model;
    }

    public int deleteTExtendIdent(int id) {
        int i = tExtendIdentMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据读数表ID和识别类型删除记录
     * @param aireadId
     * @param identType
     */
    public int deleteTExtendIdentByAireadIdAndIdentType(int aireadId,int identType){
        TExtendIdentExample example = new TExtendIdentExample();
        example.createCriteria().andAireadIdEqualTo(aireadId).andIdentTypeEqualTo(identType);
        return tExtendIdentMapper.deleteByExample(example);
    }

    /**
     * 根据设备SN获取可信的识别记录
     *
     * @param deviceSn
     * @return
     */
    public TExtendIdentModel getBelievableModelByDeviceSn(String deviceSn) {
        if (StringUtils.isEmpty(deviceSn)) {
            return null;
        }

        TExtendIdentExample example = new TExtendIdentExample();
        TExtendIdentExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceSnEqualTo(deviceSn);
        example.setOrderByClause("ID DESC");
        PageHelper.startPage(1, 10);

        List<TExtendIdentModel> list = selectByExample(example);
        for (TExtendIdentModel model : list) {
            if (BaseConstant.IDENT_GOOD_FLAG.contains(model.getResultCode().toString())) {
                return model;
            }
        }
        return null;
    }

    /**
     * 根据读数表相关信息获取数据列表
     *
     * @param deviceSn
     * @param stationCode
     * @param sampleTime
     * @return
     */
    public Map<Integer, TExtendIdentModel> findExtendIdentModelsByAiReadInfo(String deviceSn, String stationCode, Date sampleTime) {
        TExtendIdentExample example = new TExtendIdentExample();
        TExtendIdentExample.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.andIdentTimeGreaterThanOrEqualTo(sampleTime);
        }

        return getExtendIdentModelMapByExample(example);
    }

    /**
     * 根据TAiread表主键获取TExtendIdentModel Map
     *
     * @param aiReadKeys
     * @return
     */
    public Map<Integer, TExtendIdentModel> findExtendIdentMapByAiReadKeys(List<Integer> aiReadKeys) {
        if (aiReadKeys == null || aiReadKeys.size() == 0) return null;
        TExtendIdentExample example = new TExtendIdentExample();
        TExtendIdentExample.Criteria criteria = example.createCriteria();
        criteria.andAireadIdIn(aiReadKeys);

        return getExtendIdentModelMapByExample(example);
    }

    private Map<Integer, TExtendIdentModel> getExtendIdentModelMapByExample(TExtendIdentExample example) {
        List<TExtendIdent> tExtendIdents = tExtendIdentMapper.selectByExample(example);
        Map<Integer, TExtendIdentModel> map = new HashMap<>();

        for (TExtendIdent extendIdent : tExtendIdents) {
            TExtendIdentModel model = new TExtendIdentModel();
            BeanUtils.copyProperties(extendIdent, model);
            if (!map.containsKey(model.getAireadId())) {
                map.put(model.getAireadId(), model);
            }
        }
        return map;
    }
}
