package com.mvc.service;

import com.mvc.dao.TUsingunitGroupMapper;
import com.mvc.entity.TUsingunitGroup;
import com.mvc.entity.TUsingunitGroupExample;
import com.mvc.model.TUsingunitGroupModel;
import com.mvc.utils.StringUtil;
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.*;

@Service
@Transactional
public class TUsingunitGroupService {
    @Autowired
    private TUsingunitGroupMapper tUsingunitGroupMapper;

    public List<TUsingunitGroupModel> findAllTUsingunitGroup(TUsingunitGroupModel queryModel) {
        TUsingunitGroupExample example=new TUsingunitGroupExample();
        TUsingunitGroupExample.Criteria criteria=example.createCriteria();
        if(StringUtils.isNotEmpty(queryModel.getUnitCode())){
            criteria.andUnitCodeEqualTo(queryModel.getUnitCode());
        }
        if(StringUtils.isNotEmpty(queryModel.getUseFlag())){
            criteria.andUseFlagEqualTo(queryModel.getUseFlag());
        }

        List<TUsingunitGroup> TUsingunitGroups = tUsingunitGroupMapper.selectByExample(example);
        List<TUsingunitGroupModel> resList = new ArrayList<>();
        for (TUsingunitGroup TUsingunitGroup : TUsingunitGroups) {
            TUsingunitGroupModel model = new TUsingunitGroupModel();
            BeanUtils.copyProperties(TUsingunitGroup, model);
            resList.add(model);
        }

        return resList;
    }

    public TUsingunitGroupModel saveTUsingunitGroup(TUsingunitGroupModel model) {
        model.setGroupCode(StringUtil.getUuid());
        model.setCreateTime(new Date());
        TUsingunitGroup TUsingunitGroup = new TUsingunitGroup();
        BeanUtils.copyProperties(model, TUsingunitGroup);
        int insert = tUsingunitGroupMapper.insert(TUsingunitGroup);
        model.setId(TUsingunitGroup.getId());
        return model;
    }

    public TUsingunitGroupModel updateTUsingunitGroup(TUsingunitGroupModel model) {
        TUsingunitGroup TUsingunitGroup = tUsingunitGroupMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TUsingunitGroup);
        int i = tUsingunitGroupMapper.updateByPrimaryKey(TUsingunitGroup);
        return model;
    }

    public int deleteTUsingunitGroup(int id) {
        int i = tUsingunitGroupMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据单位编码获取下属的单位分组
     * @param unitCode
     * @return
     */
    public List<TUsingunitGroupModel> findTUsingunitGroupByUnitCode(String unitCode){
        List<TUsingunitGroupModel> resList=new ArrayList<>();

        TUsingunitGroupExample example=new TUsingunitGroupExample();
        TUsingunitGroupExample.Criteria criteria=example.createCriteria();
        criteria.andUnitCodeEqualTo(unitCode);
        criteria.andUseFlagEqualTo("1");
        List<TUsingunitGroup> tUsingunitGroups=tUsingunitGroupMapper.selectByExample(example);
        for (TUsingunitGroup TUsingunitGroup : tUsingunitGroups) {
            TUsingunitGroupModel model = new TUsingunitGroupModel();
            BeanUtils.copyProperties(TUsingunitGroup, model);
            resList.add(model);
        }
        return resList;
    }

    public String getStationCodesByGroupCode(String groupCode,int type){
        String stationCodes="";

        TUsingunitGroupExample example=new TUsingunitGroupExample();
        TUsingunitGroupExample.Criteria criteria=example.createCriteria();

        if(type==1){
            criteria.andGroupCodeEqualTo(groupCode);
        }else {
            criteria.andUnitCodeEqualTo(groupCode);
        }

        List<TUsingunitGroup> tUsingunitGroups=tUsingunitGroupMapper.selectByExample(example);
        if(tUsingunitGroups!=null&&tUsingunitGroups.size()>0){
            TUsingunitGroup unit=tUsingunitGroups.get(0);
            if(StringUtils.isNotEmpty(unit.getStationCodes())){
                stationCodes+=unit.getStationCodes()+",";
            }
            stationCodes+=getStationCodesByGroupCode(tUsingunitGroups.get(0).getGroupCode(),2)+",";
        }

        return stationCodes;
    }
}
