package com.lightinit.hsdatashow.impl.ms;

import com.lightinit.hsdatashow.entity.BaseExample;
import com.lightinit.hsdatashow.entity.GarbageRegion;
import com.lightinit.hsdatashow.entity.GarbageRegionExample;
import com.lightinit.hsdatashow.entity.Region;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.ms.GarbageRegionModel;
import com.lightinit.hsdatashow.service.ms.IMSAtmosphereService;
import com.lightinit.hsdatashow.service.ms.IMSGarbageService;
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 MSGarbageServiceImpl implements IMSGarbageService {
    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(garbageTrendMapper);
        list.add(garbagedangerTrendMapper);
        list.add(garbagedangerRegionMapper);
        list.add(garbageTreatmentMapper);
        return list;
    }
    @Autowired
    private GarbageTrendMapper garbageTrendMapper;
    @Autowired
    private GarbagedangerTrendMapper garbagedangerTrendMapper;
    @Autowired
    private GarbagedangerRegionMapper garbagedangerRegionMapper;
    @Autowired
    private GarbageTreatmentMapper garbageTreatmentMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private GarbageRegionMapper garbageRegionMapper;
    @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<GarbageRegionModel> regionList(BaseExample.Page pager) {
        GarbageRegionExample example = new GarbageRegionExample();
        example.setPage(pager);
        List<GarbageRegionModel> list =  garbageRegionMapper.selectList(example);
        return list;
    }

    @Override
    public long regionCount(BaseExample.Page pager) {
        GarbageRegionExample example = new GarbageRegionExample();
        example.setPage(pager);
        return garbageRegionMapper.selectCount(example);
    }

    @Override
    public GarbageRegionModel findByRegionId(Long id) {
        return garbageRegionMapper.findByRegionId(id);
    }

    @Override
    public ResultState doAdd(GarbageRegionModel 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);
        }
        GarbageRegion garbageRegion = new GarbageRegion();
        BeanUtils.copyProperties(garbageRegionModel,garbageRegion);
        if(garbageRegionModel.getId()!=null) {
            resultSql = garbageRegionMapper.updateByPrimaryKeySelective(garbageRegion);
        }else{
            garbageRegion.setRegionId(region.getId());
            resultSql = garbageRegionMapper.insert(garbageRegion);
        }


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

    @Override
    public ResultState del(Long id,Long regionId) {
        ResultState resultState = new ResultState();
        int resultSql =0;  int resultSql1 =0;
        if(id!=null){
            resultSql = garbageRegionMapper.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;
    }
}
