package com.lightinit.hsdatashow.impl.ms;

import com.lightinit.hsdatashow.entity.BaseExample;
import com.lightinit.hsdatashow.entity.Greening;
import com.lightinit.hsdatashow.entity.GreeningExample;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.ResultStateCode;
import com.lightinit.hsdatashow.service.ms.IMSGreeningService;
import com.lightinit.hsdatashow.service.ms.IMSLivelihoodImproveService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
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.Date;
import java.util.List;


@Service
public class MSLivelihoodImproveServiceImpl implements IMSLivelihoodImproveService {
    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(unemploymentMapper);
        list.add(coefficientMapper);
        list.add(urbanRuralGdpMapper);
        list.add(urbanRuralContrastMapper);
        list.add(qualityMapper);
        return list;
    }
    @Autowired
    private UnemploymentMapper unemploymentMapper;
    @Autowired
    private CoefficientMapper coefficientMapper;
    @Autowired
    private UrbanRuralGdpMapper urbanRuralGdpMapper;
    @Autowired
    private UrbanRuralContrastMapper urbanRuralContrastMapper;
    @Autowired
    private QualityMapper qualityMapper;
    @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;
    }
}
