package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.Communal;
import com.lightinit.hsdatashow.entity.CommunalExample;
import com.lightinit.hsdatashow.entity.ObjCategory;
import com.lightinit.hsdatashow.entity.ObjCategoryExample;
import com.lightinit.hsdatashow.mapper.CommunalMapper;
import com.lightinit.hsdatashow.mapper.FacilityMapper;
import com.lightinit.hsdatashow.mapper.ObjCategoryMapper;
import com.lightinit.hsdatashow.mapper.OrganizationMapper;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.EducationOrganizationResultModel;
import com.lightinit.hsdatashow.model.admin.FacilitiesResultModel;
import com.lightinit.hsdatashow.service.admin.IFacilitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class FacilitiesServiceImpl implements IFacilitiesService {

    @Autowired
    private FacilityMapper facilityMapper ;

    @Autowired
    private ObjCategoryMapper objCategoryMapper ;
    @Autowired
    private CommunalMapper communalMapper ;
    @Autowired
    private OrganizationMapper organizationMapper ;

    @Override
    public List<FacilitiesResultModel> getFacility(String categoryName) {
        ObjCategoryExample example = new ObjCategoryExample() ;
        ObjCategoryExample.Criteria criteria = example.createCriteria() ;
        criteria.andCategoryNameEqualTo(categoryName) ;
        criteria.andParentCodeIsNull() ;
        List<ObjCategory> objCategories = objCategoryMapper.selectByExample(example) ;
        if (objCategories.size()>0){
            List<FacilitiesResultModel> facilitiesResultModels = facilityMapper.getFacility(objCategories.get(0).getCategoryCode()) ;
            return facilitiesResultModels;
        }else {
            return null ;
        }
    }

    @Override
    public ResultState getCommunal() {
        ResultState resultState = new ResultState();
        CommunalExample example =new CommunalExample();
        HashMap<String, Object> map = new HashMap<>();
        List<Communal> CommunalList = new ArrayList<>();
        List<Communal> list = communalMapper.selectByExample(example);
        map.put("array",list);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public List<EducationOrganizationResultModel> getMonitor(String categoryName, String name) {
        ObjCategoryExample example = new ObjCategoryExample() ;
        ObjCategoryExample.Criteria criteria = example.createCriteria() ;
        criteria.andCategoryNameEqualTo(categoryName) ;
        criteria.andParentCodeIsNull() ;
        List<ObjCategory> objCategories = objCategoryMapper.selectByExample(example) ;
        if (objCategories.size()>0){
            List<EducationOrganizationResultModel> educationOrganizationResultModels = organizationMapper.getEducationOrganization(objCategories.get(0).getCategoryCode(),name) ;
            return educationOrganizationResultModels;
        }else {
            return null ;
        }
    }
}
