package com.mvc.service.ysfx;

import com.mvc.dao.TStationStructureMapper;
import com.mvc.entity.TAiread;
import com.mvc.entity.TStationStructure;
import com.mvc.entity.TStationStructureExample;
import com.mvc.model.StationAccModel;
import com.mvc.model.TAireadModel;
import com.mvc.model.ysfx.StationStruViewModel;
import com.mvc.model.ysfx.StructuresTreeModel;
import com.mvc.model.ysfx.TStationStructureModel;
import com.mvc.service.copy.TAireadService;
import com.mvc.service.taizhang.StationAccService;
import com.mvc.utils.DateUtil;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class TStationStructureService {

    @Resource
    private TStationStructureMapper tStationStructureMapper;

    @Resource
    private StationAccService stationAccService;

    @Resource
    private TAireadService tAireadService;

    public List<TStationStructureModel> findAllTStationStructure() {
        List<TStationStructure> TStationStructures = tStationStructureMapper.selectByExample(null);
        List<TStationStructureModel> resList = new ArrayList<>();
        for (TStationStructure TStationStructure : TStationStructures) {
            TStationStructureModel model = new TStationStructureModel();
            BeanUtils.copyProperties(TStationStructure, model);
            resList.add(model);
        }
        return resList;
    }

    public TStationStructureModel saveTStationStructure(TStationStructureModel model) {
        TStationStructure TStationStructure = new TStationStructure();
        BeanUtils.copyProperties(model, TStationStructure);
        int insert = tStationStructureMapper.insert(TStationStructure);
        model.setId(TStationStructure.getId());
        return model;
    }

    public TStationStructureModel updateTStationStructure(TStationStructureModel model) {
        TStationStructure TStationStructure = tStationStructureMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TStationStructure);
        int i = tStationStructureMapper.updateByPrimaryKey(TStationStructure);
        return model;
    }

    public int deleteTStationStructure(int id) {
        int i = tStationStructureMapper.deleteByPrimaryKey(id);
        return i;
    }

    public TStationStructureModel findTStationStructureModelByType(Integer type) {
        TStationStructureExample example = new TStationStructureExample();
        TStationStructureExample.Criteria criteria = example.createCriteria();
        criteria.andWaterTypeEqualTo(type);
        List<TStationStructure> tStationStructures = tStationStructureMapper.selectByExample(example);
        if (tStationStructures.size() > 0) {
            TStationStructureModel resModel = new TStationStructureModel();
            BeanUtils.copyProperties(tStationStructures.get(0), resModel);
            return resModel;
        } else {
            return null;
        }
    }

    /**
     * 根据单位代码返回前台需要的列表(List<StationStruViewModel>)
     *
     * @param unitCode
     * @return
     */
    public List<StationStruViewModel> findStationStruViewModelsByUnitCode(String unitCode) {
        List<StationStruViewModel> res = new ArrayList<>();
        TStationStructureExample example = new TStationStructureExample();
        TStationStructureExample.Criteria criteria = example.createCriteria();
        criteria.andUnitCodeEqualTo(unitCode);
        List<TStationStructure> tStationStructures = tStationStructureMapper.selectByExample(example);
        if (tStationStructures.size() > 0) {
            res.addAll(findChildren("0", tStationStructures));
        }
        return res;
    }

    /**
     * 根据单位代码和等级获取对象
     *
     * @param unitCode
     * @return
     */
    public TStationStructureModel findStationStruViewModelsByUnitCodeAndLevel(String unitCode, Integer level) {
        TStationStructureModel res = null;
        TStationStructureExample example = new TStationStructureExample();
        TStationStructureExample.Criteria criteria = example.createCriteria();
        criteria.andUnitCodeEqualTo(unitCode);
        criteria.andWaterMeterLevelEqualTo(level);
        List<TStationStructure> tStationStructures = tStationStructureMapper.selectByExample(example);
        if (tStationStructures.size() > 0) {
            TStationStructureModel model = new TStationStructureModel();
            BeanUtils.copyProperties(tStationStructures.get(0), model);
            res = model;
        }
        return res;
    }

    private List<StationStruViewModel> findChildren(String stationCode, List<TStationStructure> list) {
        List<StationStruViewModel> res = new ArrayList<>();
        List<TStationStructure> tmp = list.stream().filter(s -> s.getParentCode().equals(stationCode)).collect(Collectors.toList());
        if (tmp != null && tmp.size() > 0) {
            for (TStationStructure item : tmp) {
                StationAccModel acc = stationAccService.findStationAccByStationCode(item.getStationCode());
                StationStruViewModel viewModel = new StationStruViewModel();
                if (acc != null) {
                    viewModel.setName(acc.getStationName());
                    TAiread tAiread = tAireadService.findlastTAireadBydatatime(item.getStationCode(), new Date());
                    if (tAiread != null) {
                        String sampleTime = DateUtil.formateDate(tAiread.getSampleTime(), "yyyy-MM-dd HH:mm");
                        viewModel.setSampleTime(sampleTime);
                        viewModel.setValue(tAiread.getResultValue());
                        viewModel.setPicUrl(tAiread.getSmallJpgUrl());
                    }
                }
                viewModel.setChildren(findChildren(item.getStationCode(), list));
                res.add(viewModel);
            }
        }
        return res;
    }

    /**
     * 根据单位代码查询所有记录
     *
     * @param unitCode
     * @return
     */
    public List<TStationStructureModel> findTStationStructureModelsByUnitCode(String unitCode) {
        List<TStationStructureModel> res = new ArrayList<>();
        TStationStructureExample example = new TStationStructureExample();
        TStationStructureExample.Criteria criteria = example.createCriteria();
        criteria.andUnitCodeEqualTo(unitCode);
        List<TStationStructure> tStationStructures = tStationStructureMapper.selectByExample(example);
        if (tStationStructures.size() > 0) {
            for (TStationStructure tStationStructure : tStationStructures) {
                TStationStructureModel model = new TStationStructureModel();
                BeanUtils.copyProperties(tStationStructure, model);
                res.add(model);
            }
        }
        return res;
    }

    /**
     * 根据站代码获取树形结构数据(节水项目)
     * @param unitCode
     * @return
     */
    public List<StructuresTreeModel> getStructuresTreeDataByUnitCode(String unitCode) {
        List<TStationStructureModel> list = findTStationStructureModelsByUnitCode(unitCode);
        List<StructuresTreeModel> treeModels = new ArrayList<>();

        for (TStationStructureModel item : list) {
            if (item.getWaterMeterLevel() == 1) {
                StructuresTreeModel root = new StructuresTreeModel();
                root.setName(item.getNodeLabel());
                List<StructuresTreeModel> children = getChildren(list, item.getNodeKey());
                root.setChildren(children);
                treeModels.add(root);
                break;//理论只有一个一级表
            }
        }
        return treeModels;
    }

    private List<StructuresTreeModel> getChildren(List<TStationStructureModel> list, String parentKey) {
        List<StructuresTreeModel> res = new ArrayList<>();
        for (TStationStructureModel item : list) {
            if (item.getParentKey().equals(parentKey)) {
                StructuresTreeModel model = new StructuresTreeModel();
                model.setName(item.getNodeLabel());//节点名称(可与台账中stationName不同,灵活一点)
                model.setValue("");
                if (item.getNodeType() == 1 && StringUtils.isNotEmpty(item.getStationCode())) {//节点对应实际站点
                    TAireadModel airead = tAireadService.findLatesTAireadByStationCode(item.getStationCode());
                    if (airead != null) {
                        model.setValue(airead.getBigJpgUrl());//节点图片
                    }
                }
                List<StructuresTreeModel> children =getChildren(list, item.getNodeKey());//递归获取子节点
                model.setChildren(children);
                res.add(model);
            }
        }

        return res;
    }
}
