package com.mvc.service.taizhang;

import com.mvc.dao.StationGroupMapper;
import com.mvc.entity.StationGroup;
import com.mvc.entity.StationGroupExample;
import com.mvc.model.StationAccModel;
import com.mvc.model.StationGroupModel;
import com.mvc.othermodel.GroupAndStationTree;
import com.mvc.utils.StringUtil;
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 StationGroupService {

    @Autowired
    private StationGroupMapper stationGroupMapper;

    @Autowired
    private StationAccService stationAccService;

    @Autowired
    private CustomUserService  customUserService;


    public StationGroupModel saveStationGroup(StationGroupModel model) {
        model.setGroupCode(StringUtil.getUuid());
        model.setCreateTime(new Date());
        StationGroup StationGroup = new StationGroup();
        model.setGroupCode(StringUtil.getUuid());
        BeanUtils.copyProperties(model,StationGroup);
        int insert = stationGroupMapper.insert(StationGroup);
        model.setId(StationGroup.getId());
        return model;
    }

    public StationGroupModel updateStationGroup(StationGroupModel model) {
        StationGroup StationGroup = stationGroupMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model,StationGroup);
        int i = stationGroupMapper.updateByPrimaryKey(StationGroup);
        return model;
    }

    public int deleteStationGroup(int id) {
        int i = stationGroupMapper.deleteByPrimaryKey(id);
        return i;
    }


    public List<StationGroupModel> findStationGroup(String  customerId) {
        List<StationGroupModel> resList=new ArrayList<>();
        if(customerId!=null){
            StationGroupExample example=new StationGroupExample();
            StationGroupExample.Criteria criteria = example.createCriteria();
            criteria.andCustomerIdEqualTo(customerId);
            example.setOrderByClause("GROUP_LEVEL ASC");
            List<StationGroup> list = stationGroupMapper.selectByExample(example);
            for(int i=0;i<list.size();i++){
                StationGroupModel model = new StationGroupModel();
                BeanUtils.copyProperties(list.get(i),model);
                //设置父节点名称
                if(model.getParentCode()!=null){
                    StationGroupModel stationGroupParent = findStationGroupBygroupCode(model.getParentCode());
                    if(stationGroupParent!=null){
                        model.setParentName(stationGroupParent.getGroupName());
                    }
                }
                resList.add(model);
            }
        }
        return resList;
    }

    public StationGroupModel findStationGroupBygroupCode(String  groupCode) {
        StationGroupExample example=new StationGroupExample();
        StationGroupExample.Criteria criteria = example.createCriteria();
        criteria.andGroupCodeEqualTo(groupCode);
        List<StationGroup> stationGroups = stationGroupMapper.selectByExample(example);
        if(stationGroups.size()>0){
            StationGroupModel model = new StationGroupModel();
            BeanUtils.copyProperties(stationGroups.get(0),model);
            return model;
        }else{
            return null;
        }

    }

    public List<GroupAndStationTree> fingStationGroupAndStation(String customerId) {
        List<GroupAndStationTree> resList=new ArrayList<>();
        List<StationGroupModel> groupList = findStationGroup(customerId);
        for(int i=0;i<groupList.size();i++){
            StationGroupModel group = groupList.get(i);
            GroupAndStationTree tree = new GroupAndStationTree();
            tree.setId(StringUtil.getUuid());
            tree.setLabel(group.getGroupName());
            tree.setCode(group.getGroupCode());
            tree.setParentcode(group.getParentCode());
            tree.setType("0");
            tree.setData(group);
            resList.add(tree);
            List<StationAccModel> accList = stationAccService.findStationListByGroupcode(group.getGroupCode());
            for(int j=0;j<accList.size();j++){
                StationAccModel acc = accList.get(j);
                GroupAndStationTree tree2 = new GroupAndStationTree();
                tree2.setId(StringUtil.getUuid());
                tree2.setLabel(acc.getStationName());
                tree2.setCode(acc.getStationCode());
                tree2.setParentcode(acc.getGroupCode());
                tree2.setType("1");
                tree2.setData(acc);
                resList.add(tree2);
            }
        }
        return resList;
    }
}
