package com.lightinit.hsdatashow.impl.ms;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.ms.GarbageRegionModel;
import com.lightinit.hsdatashow.model.ms.GreenModel;
import com.lightinit.hsdatashow.service.ms.IMSAtmosphereService;
import com.lightinit.hsdatashow.service.ms.IMSGreenService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


@Service
public class  MSGreenServiceImpl implements IMSGreenService {
    private  List<Class> getMappers(){
        ArrayList<Class> arrayList = new ArrayList();
        List clazzz = getClazzz();
        for (Object o : clazzz) {
            arrayList.add(o.getClass().getInterfaces()[0]);
        }
        return arrayList;
    };
    private  List getClazzz(){
        ArrayList list = new ArrayList();
        list.add(greenCityMapper);
        list.add(greenForestMapper);
        return list;
    }
    @Autowired
    private GreenCityMapper greenCityMapper;
    @Autowired
    private GreenForestMapper greenForestMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private GreenBuildMapper greenBuildMapper;
    @Autowired
    private GreenDistributionMapper greenDistributionMapper;
    @Autowired
    private GreenForestDistributionMapper greenForestDistributionMapper;
    @Override
    public List list(String tableType, BaseExample.Page pager) {
        for(int i=0;i< getMappers().size();i++){
            Class name = getMappers().get(i);
            if(name.getSimpleName().equalsIgnoreCase(tableType+"Mapper")){
                try{
                    Class<?> aClass = Class.forName("com.lightinit.hsdatashow.entity."+tableType+ "Example");
                    Object obj=aClass.newInstance();
                    Method orderByClause = obj.getClass().getDeclaredMethod("setOrderByClause",String.class);
                    orderByClause.invoke(obj,"id desc limit "+pager.getBeginIndex()+","+pager.getPageSize());

                    Object  o= getClazzz().get(i);
                    Method selectByExample = o.getClass().getDeclaredMethod("selectByExample", aClass);
                    return  (List)selectByExample.invoke(o,obj);
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            }
        }
        return null;
    }

    @Override
    public <V> ResultState add(V v,boolean isEdit) {
        ResultState resultState = new ResultState();
        int i=0;
        try{
            List<Class> names = getMappers();
            for(int j=0;j< names.size();j++){
                if(names.get(j).getSimpleName().equalsIgnoreCase(v.getClass().getSimpleName()+"Mapper")){
                    Object  o= getClazzz().get(j);
                    if(isEdit){
                        Method selectByExample = o.getClass().getDeclaredMethod("updateByPrimaryKeySelective", v.getClass());
                        i=(Integer) selectByExample.invoke(o,v);
                    }else{
                        Method selectByExample = o.getClass().getDeclaredMethod("insert", v.getClass());
                        i=(Integer) selectByExample.invoke(o,v);
                    }
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i<1){
            resultState.setStateCode(400);
            resultState.setMsg("操作失败");
            return resultState;
        }
        resultState.setMsg("操作成功");
        return resultState;
    }

    @Override
    public Object findById(String tableType, Long id) {
        for(int i=0;i< getMappers().size();i++){
            Class name = getMappers().get(i);
            if(name.getSimpleName().equalsIgnoreCase(tableType+"Mapper")){
                try {
                    Object  o= getClazzz().get(i);
                    Method  selectByPrimaryKey=o.getClass().getDeclaredMethod("selectByPrimaryKey",Long.class);
                    return selectByPrimaryKey.invoke(o,id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }
        return null;
    }

    @Override
    public long queryCount(String tableType) {
        for(int i=0;i< getMappers().size();i++){
            Class name = getMappers().get(i);
            if(name.getSimpleName().equalsIgnoreCase(tableType+"Mapper")){
                try{
                    Class<?> aClass = Class.forName("com.lightinit.hsdatashow.entity."+tableType+ "Example");
                    Object obj=aClass.newInstance();

                    Object  o= getClazzz().get(i);
                    Method selectByExample = o.getClass().getDeclaredMethod("countByExample", aClass);
                    return  (long)selectByExample.invoke(o,obj);
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            }
        }
        return  0;
    }

    @Override
    public ResultState delete(String tableType, Long id) {
        ResultState resultState = new ResultState();
        int j=0;
        for(int i=0;i< getMappers().size();i++){
            Class name = getMappers().get(i);
            if(name.getSimpleName().equalsIgnoreCase(tableType+"Mapper")){
                try {
                    Object  o= getClazzz().get(i);
                    Method  deleteByPrimaryKey=o.getClass().getDeclaredMethod("deleteByPrimaryKey",Long.class);
                    j=(Integer) deleteByPrimaryKey.invoke(o,id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }
        if(j<1){
            resultState.setStateCode(400);
            resultState.setMsg("删除失败");
            return resultState;
        }
        resultState.setMsg("删除成功");
        return resultState;
    }

    @Override
    public List<GreenModel> buildList(String type,BaseExample.Page pager) {
        GarbageRegionExample example = new GarbageRegionExample();
        example.setPage(pager);
        List<GreenModel> list = null;
        if(type.equals("城市建成区绿化率排名")) {
             list =  greenBuildMapper.selectList(example);
        }else if(type.equals("绿化分布情况")){
            list = greenDistributionMapper.selectList(example);
        }else if(type.equals("森林覆盖情况")){
            list = greenForestDistributionMapper.selectList(example);

        }
        return list;
    }

    @Override
    public long buildCount(String type,BaseExample.Page pager) {
        GarbageRegionExample example = new GarbageRegionExample();
        example.setPage(pager);
       long count = 0;
        if(type.equals("城市建成区绿化率排名")) {
            count =  greenBuildMapper.selectCount(example);
        }else if(type.equals("绿化分布情况")){
            count =  greenDistributionMapper.selectCount(example);
        }else if(type.equals("森林覆盖情况")){
            count = greenForestDistributionMapper.selectCount(example);
        }
        return count;
    }

    @Override
    public GreenModel findByBuildId(Long id,String type) {
        if(type.equals("城市建成区绿化率排名")) {
            return greenBuildMapper.findByBuildId(id);
        }else if(type.equals("绿化分布情况")){
            return  greenDistributionMapper.findByDistributionId(id);
        }else if(type.equals("森林覆盖情况")){
            return  greenForestDistributionMapper.findByForestId(id);
        }
        return null;
    }

    @Override
    public ResultState doAdd(GreenModel garbageRegionModel, boolean isEdit) {
        ResultState resultState = new ResultState();
        int resultSql =0;  int resultSql1 =0;
        Region region = new Region();
        BeanUtils.copyProperties(garbageRegionModel,region);
        if(garbageRegionModel.getRegionId()!=null) {
            region.setId(garbageRegionModel.getRegionId());
            resultSql1 = regionMapper.updateByPrimaryKeySelective(region);
        }else{
            resultSql1 = regionMapper.insert(region);
        }
        if(garbageRegionModel.getType().equals("城市建成区绿化率排名")){
            GreenBuild greenBuild = new GreenBuild();
            BeanUtils.copyProperties(garbageRegionModel,greenBuild);
            if(garbageRegionModel.getId()!=null) {
                resultSql = greenBuildMapper.updateByPrimaryKeySelective(greenBuild);
            }else{
                greenBuild.setRegionId(region.getId());
                resultSql = greenBuildMapper.insert(greenBuild);
            }
        }else if(garbageRegionModel.getType().equals("绿化分布情况")){
            GreenDistribution greenDistribution = new GreenDistribution();
            BeanUtils.copyProperties(garbageRegionModel,greenDistribution);
            if(garbageRegionModel.getId()!=null) {
                resultSql = greenDistributionMapper.updateByPrimaryKeySelective(greenDistribution);
            }else{
                greenDistribution.setRegionId(region.getId());
                resultSql = greenDistributionMapper.insert(greenDistribution);
            }
        }else if(garbageRegionModel.getType().equals("森林覆盖情况")){
            GreenForestDistribution greenForestDistribution = new GreenForestDistribution();
            BeanUtils.copyProperties(garbageRegionModel,greenForestDistribution);
            if(greenForestDistribution.getId()!=null) {
                resultSql = greenForestDistributionMapper.updateByPrimaryKeySelective(greenForestDistribution);
            }else{
                greenForestDistribution.setRegionId(region.getId());
                resultSql = greenForestDistributionMapper.insert(greenForestDistribution);
            }
        }


        if(!(resultSql>0&&resultSql1>0)) {
            resultState.setStateCode(400);
            resultState.setMsg("操作失败");
        }
        resultState.setStateCode(200);
        resultState.setMsg("操作成功");
        return resultState;
    }

    @Override
    public ResultState delBuild(Long id,Long regionId,String type) {
        ResultState resultState = new ResultState();
        int resultSql =0;  int resultSql1 =0;
        if(type.equals("城市建成区绿化率排名")) {
            if(id!=null){
                resultSql = greenBuildMapper.deleteByPrimaryKey(id);
            }
            if(regionId!=null){
                resultSql1 = regionMapper.deleteByPrimaryKey(regionId);
            }
        }else if(type.equals("绿化分布情况")){
            if(id!=null){
                resultSql = greenDistributionMapper.deleteByPrimaryKey(id);
            }
            if(regionId!=null){
                resultSql1 = regionMapper.deleteByPrimaryKey(regionId);
            }

        }else if(type.equals("森林覆盖情况")){
            if(id!=null){
                resultSql = greenForestDistributionMapper.deleteByPrimaryKey(id);
            }
            if(regionId!=null){
                resultSql1 = regionMapper.deleteByPrimaryKey(regionId);
            }
        }
        if(!(resultSql>0&&resultSql1>0)) {
            resultState.setStateCode(400);
            resultState.setMsg("操作失败");
        }
        resultState.setStateCode(200);
        resultState.setMsg("操作成功");
        return resultState;
    }
}
