package com.gyee.frame.service;

import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.ProjectPlanMapper;
import com.gyee.frame.model.auto.Project;
import com.gyee.frame.model.auto.ProjectPlan;
import com.gyee.frame.model.auto.ProjectPlanExample;
import com.gyee.frame.model.custom.ProjectPlanVo;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


@Service
public class ProjectPlanService implements BaseService<ProjectPlan, ProjectPlanExample> {
    @Resource
    private ProjectPlanMapper projectPlanMapper;


    @Override
    public int deleteByPrimaryKey(String ids) {

        List<String> lista = Convert.toListStrArray(ids);
        ProjectPlanExample example = new ProjectPlanExample();
        example.createCriteria().andIdIn(lista);
        return projectPlanMapper.deleteByExample(example);


    }


    @Override
    public ProjectPlan selectByPrimaryKey(String id) {

        return projectPlanMapper.selectByPrimaryKey(id);

    }


    @Override
    public int updateByPrimaryKeySelective(ProjectPlan record) {
        return projectPlanMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 添加
     */
    @Override
    public int insertSelective(ProjectPlan record) {

        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());


        return projectPlanMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(ProjectPlan record, ProjectPlanExample example) {

        return projectPlanMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(ProjectPlan record, ProjectPlanExample example) {

        return projectPlanMapper.updateByExample(record, example);
    }

    @Override
    public List<ProjectPlan> selectByExample(ProjectPlanExample example) {

        return projectPlanMapper.selectByExample(example);
    }


    @Override
    public long countByExample(ProjectPlanExample example) {

        return projectPlanMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(ProjectPlanExample example) {

        return projectPlanMapper.deleteByExample(example);
    }

	public List<ProjectPlan> getProjectPlanList(String year) {


		List<ProjectPlan> list=new ArrayList<>();

		if (StringUtils.notEmp(year)  ) {

			ProjectPlanExample example=new ProjectPlanExample();
			example.setOrderByClause("projectid DESC");

			ProjectPlanExample.Criteria criteria =example.createCriteria();

			criteria.andYearEqualTo(year);
			criteria.andMonthIsNotNull();

			list= projectPlanMapper.selectByExample(example);

		}
		return list;

	}

	public List<ProjectPlan> getProjectPlanList(String wpId,String year,String month) {


		List<ProjectPlan> list=new ArrayList<>();

		if (StringUtils.notEmp(wpId)  && StringUtils.notEmp(year)  ) {

			ProjectPlanExample example=new ProjectPlanExample();
			example.setOrderByClause("projectid DESC");

			ProjectPlanExample.Criteria criteria =example.createCriteria();

			criteria.andWindpowerEqualTo(wpId);
			criteria.andYearEqualTo(year);
			criteria.andMonthEqualTo(month);
			criteria.andMonthIsNotNull();

			list= projectPlanMapper.selectByExample(example);

		}
		return list;

	}

	public List<ProjectPlan> getProjectPlanListByTypeId(String year,String id) {


		List<ProjectPlan> list=new ArrayList<>();

		if (StringUtils.notEmp(year) && StringUtils.notEmp(id)  ) {

			ProjectPlanExample example=new ProjectPlanExample();
			example.setOrderByClause("projectid DESC");

			ProjectPlanExample.Criteria criteria =example.createCriteria();

			criteria.andYearEqualTo(year);
			if(InitialRunner.wpmap.containsKey(id))
			{
				criteria.andWindpowerEqualTo(id);
			}else if(InitialRunner.pjmap.containsKey(id))
			{
				criteria.andProjectidEqualTo(id);
			}

			criteria.andMonthIsNotNull();

			list= projectPlanMapper.selectByExample(example);

		}
		return list;

	}
    public List<ProjectPlanVo> getProjectPlanVo(String wpId, String pjId, String year) {

		List<ProjectPlanVo> result=new ArrayList<>();
		List<ProjectPlanVo> list = new ArrayList<>();


		ProjectPlanExample example = new ProjectPlanExample();
		example.setOrderByClause("projectid asc");

		ProjectPlanExample.Criteria criteria = example.createCriteria();
		if (StringUtils.notEmp(wpId)) {
			criteria.andWindpowerEqualTo(wpId);
		}
		if (StringUtils.notEmp(pjId)) {
			criteria.andProjectidEqualTo(pjId);
		}
		if (StringUtils.notEmp(year)) {
			criteria.andYearEqualTo(year);
		}

		List<ProjectPlan> ls = projectPlanMapper.selectByExample(example);

		if(!ls.isEmpty())
		{
			for(ProjectPlan pp:ls)
			{
				ProjectPlanVo event=new ProjectPlanVo();
				event.setGeneratingcapacity(pp.getGeneratingcapacity());
				event.setId(pp.getId());
				event.setMonth(pp.getMonth());
				event.setOutagehours(pp.getOutagehours());
				event.setProjectid(pp.getProjectid());
				event.setWindpower(pp.getWindpower());
				event.setYear(pp.getYear());
				if(InitialRunner.pjmap.containsKey(pp.getProjectid()))
				{
					Project project=InitialRunner.pjmap.get(pp.getProjectid());
					event.setProjectName(project.getName());
				}

				if(InitialRunner.wpmap.containsKey(pp.getWindpower()))
				{
					event.setWindpowerName(InitialRunner.wpmap.get(pp.getWindpower()).getName());

				}
				list.add(event);
			}

			String currentWindpowerStation="",currentProject="";
			Map<String,ProjectPlanVo> retValue=new TreeMap<String,ProjectPlanVo>();
			double[] totalGc=new double[13];
			double[] windpowerGc=new double[13];
			for(int i=0;i<list.size();i++){
				ProjectPlanVo vo=list.get(i);
				if(StringUtils.empty(currentWindpowerStation)){
					currentWindpowerStation=vo.getWindpowerName();
				}
				if(!currentWindpowerStation.equals(vo.getWindpowerName())){
					ProjectPlanVo wpGc=new ProjectPlanVo();
//					wpGc.setGc01(String.valueOf(windpowerGc[0]));
//					wpGc.setGc02(String.valueOf(windpowerGc[1]));
//					wpGc.setGc03(String.valueOf(windpowerGc[2]));
//					wpGc.setGc04(String.valueOf(windpowerGc[3]));
//					wpGc.setGc05(String.valueOf(windpowerGc[4]));
//					wpGc.setGc06(String.valueOf(windpowerGc[5]));
//					wpGc.setGc07(String.valueOf(windpowerGc[6]));
//					wpGc.setGc08(String.valueOf(windpowerGc[7]));
//					wpGc.setGc09(String.valueOf(windpowerGc[8]));
//					wpGc.setGc10(String.valueOf(windpowerGc[9]));
//					wpGc.setGc11(String.valueOf(windpowerGc[10]));
//					wpGc.setGc12(String.valueOf(windpowerGc[11]));
//					wpGc.setGeneratingcapacity(String.valueOf(windpowerGc[12]));


					wpGc.setGc01(String.valueOf(StringUtils.round(windpowerGc[0],2)));
					wpGc.setGc02(String.valueOf(StringUtils.round(windpowerGc[1],2)));
					wpGc.setGc03(String.valueOf(StringUtils.round(windpowerGc[2],2)));
					wpGc.setGc04(String.valueOf(StringUtils.round(windpowerGc[3],2)));
					wpGc.setGc05(String.valueOf(StringUtils.round(windpowerGc[4],2)));
					wpGc.setGc06(String.valueOf(StringUtils.round(windpowerGc[5],2)));
					wpGc.setGc07(String.valueOf(StringUtils.round(windpowerGc[6],2)));
					wpGc.setGc08(String.valueOf(StringUtils.round(windpowerGc[7],2)));
					wpGc.setGc09(String.valueOf(StringUtils.round(windpowerGc[8],2)));
					wpGc.setGc10(String.valueOf(StringUtils.round(windpowerGc[9],2)));
					wpGc.setGc11(String.valueOf(StringUtils.round(windpowerGc[10],2)));
					wpGc.setGc12(String.valueOf(StringUtils.round(windpowerGc[11],2)));
					wpGc.setGeneratingcapacity(String.valueOf(StringUtils.round(windpowerGc[12],2)));
					wpGc.setProjectName(currentWindpowerStation);

					wpGc.setYear(year);
					retValue.put(currentWindpowerStation, wpGc);
					currentWindpowerStation=vo.getWindpowerName();
					for(int j=0;j<13;j++){
						totalGc[j]+=windpowerGc[j];
						windpowerGc[j]=0;
					}
				}{
					ProjectPlanVo item=null;
					if(retValue.containsKey(vo.getProjectName())){
						item=retValue.get(vo.getProjectName());
					}
					else{
						item=new ProjectPlanVo();
						item.setProjectName(vo.getProjectName());
						item.setProjectid(vo.getProjectid());
						item.setId(vo.getProjectid());
						item.setYear(vo.getYear());
						retValue.put(vo.getProjectName(), item);
					}
					if(StringUtils.empty(vo.getMonth())){
						windpowerGc[12]+=Double.parseDouble(vo.getGeneratingcapacity());
						item.setGeneratingcapacity(vo.getGeneratingcapacity());
					}
					else{
						int index=Integer.parseInt(vo.getMonth());
						if(!StringUtils.empty(vo.getGeneratingcapacity())){
							switch(index){
								case 1:
									item.setGc01(vo.getGeneratingcapacity());
									break;
								case 2:
									item.setGc02(vo.getGeneratingcapacity());
									break;
								case 3:
									item.setGc03(vo.getGeneratingcapacity());
									break;
								case 4:
									item.setGc04(vo.getGeneratingcapacity());
									break;
								case 5:
									item.setGc05(vo.getGeneratingcapacity());
									break;
								case 6:
									item.setGc06(vo.getGeneratingcapacity());
									break;
								case 7:
									item.setGc07(vo.getGeneratingcapacity());
									break;
								case 8:
									item.setGc08(vo.getGeneratingcapacity());
									break;
								case 9:
									item.setGc09(vo.getGeneratingcapacity());
									break;
								case 10:
									item.setGc10(vo.getGeneratingcapacity());
									break;
								case 11:
									item.setGc11(vo.getGeneratingcapacity());
									break;
								case 12:
									item.setGc12(vo.getGeneratingcapacity());
									break;
							}
							windpowerGc[index-1]+=Double.parseDouble(vo.getGeneratingcapacity());
						}

					}
				}
			}


			ProjectPlanVo wpGc=new ProjectPlanVo();
			wpGc.setGc01(String.valueOf(StringUtils.round(windpowerGc[0],2)));
			wpGc.setGc02(String.valueOf(StringUtils.round(windpowerGc[1],2)));
			wpGc.setGc03(String.valueOf(StringUtils.round(windpowerGc[2],2)));
			wpGc.setGc04(String.valueOf(StringUtils.round(windpowerGc[3],2)));
			wpGc.setGc05(String.valueOf(StringUtils.round(windpowerGc[4],2)));
			wpGc.setGc06(String.valueOf(StringUtils.round(windpowerGc[5],2)));
			wpGc.setGc07(String.valueOf(StringUtils.round(windpowerGc[6],2)));
			wpGc.setGc08(String.valueOf(StringUtils.round(windpowerGc[7],2)));
			wpGc.setGc09(String.valueOf(StringUtils.round(windpowerGc[8],2)));
			wpGc.setGc10(String.valueOf(StringUtils.round(windpowerGc[9],2)));
			wpGc.setGc11(String.valueOf(StringUtils.round(windpowerGc[10],2)));
			wpGc.setGc12(String.valueOf(StringUtils.round(windpowerGc[11],2)));
			wpGc.setGeneratingcapacity(String.valueOf(StringUtils.round(windpowerGc[12],2)));
			wpGc.setProjectName(currentWindpowerStation);

			wpGc.setYear(year);
			retValue.put(currentWindpowerStation, wpGc);
			for(int j=0;j<13;j++){
				totalGc[j]+=windpowerGc[j];
				windpowerGc[j]=0;
			}
			ProjectPlanVo totalVo=new ProjectPlanVo();
			totalVo.setProjectName("合计");
			totalVo.setYear(year);
			totalVo.setGc01(String.valueOf(StringUtils.round(totalGc[0],2)));
			totalVo.setGc02(String.valueOf(StringUtils.round(totalGc[1],2)));
			totalVo.setGc03(String.valueOf(StringUtils.round(totalGc[2],2)));
			totalVo.setGc04(String.valueOf(StringUtils.round(totalGc[3],2)));
			totalVo.setGc05(String.valueOf(StringUtils.round(totalGc[4],2)));
			totalVo.setGc06(String.valueOf(StringUtils.round(totalGc[5],2)));
			totalVo.setGc07(String.valueOf(StringUtils.round(totalGc[6],2)));
			totalVo.setGc08(String.valueOf(StringUtils.round(totalGc[7],2)));
			totalVo.setGc09(String.valueOf(StringUtils.round(totalGc[8],2)));
			totalVo.setGc10(String.valueOf(StringUtils.round(totalGc[9],2)));
			totalVo.setGc11(String.valueOf(StringUtils.round(totalGc[10],2)));
			totalVo.setGc12(String.valueOf(StringUtils.round(totalGc[11],2)));
			totalVo.setGeneratingcapacity(String.valueOf(StringUtils.round(totalGc[12],2)));

			result=new ArrayList<ProjectPlanVo>(retValue.values());
			result.add(totalVo);

		}



    	return result;
	}



	public ProjectPlanVo getSingleProjectPlan(String projectid, String year) {
		List<ProjectPlanVo> list = new ArrayList<>();


		ProjectPlanExample example = new ProjectPlanExample();
		example.setOrderByClause("month asc");

		ProjectPlanExample.Criteria criteria = example.createCriteria();

		if (StringUtils.notEmp(projectid)) {
			criteria.andProjectidEqualTo(projectid);
		}

		if (StringUtils.notEmp(year)) {
			criteria.andYearEqualTo(year);
		}

		List<ProjectPlan> ls = projectPlanMapper.selectByExample(example);

		if(!ls.isEmpty())
		{
			for(ProjectPlan pp:ls)
			{
				ProjectPlanVo event=new ProjectPlanVo();
				event.setGeneratingcapacity(pp.getGeneratingcapacity());
				event.setId(pp.getId());
				event.setMonth(pp.getMonth());
				event.setOutagehours(pp.getOutagehours());
				event.setProjectid(pp.getProjectid());
				event.setWindpower(pp.getWindpower());
				event.setYear(pp.getYear());
				event.setProjectName(InitialRunner.pjmap.get(pp.getProjectid()).getName());
				event.setWindpowerName(InitialRunner.wpmap.get(pp.getWindpower()).getName());
				list.add(event);
			}
		}

		ProjectPlanVo retValue=new ProjectPlanVo();
		retValue.setProjectid(projectid);
		for(int i=0;i<list.size();i++){
			ProjectPlanVo vo=list.get(i);
			retValue.setYear(vo.getYear());
			retValue.setWindpower(vo.getWindpower());
			retValue.setWindpowerName(vo.getWindpowerName());
			retValue.setProjectName(vo.getProjectName());
			if(StringUtils.empty(vo.getMonth())){
				retValue.setGeneratingcapacity(vo.getGeneratingcapacity());
			}
			else{
				int index=Integer.parseInt(vo.getMonth());
				switch(index){
					case 1:
						retValue.setGc01(vo.getGeneratingcapacity());
						break;
					case 2:
						retValue.setGc02(vo.getGeneratingcapacity());
						break;
					case 3:
						retValue.setGc03(vo.getGeneratingcapacity());
						break;
					case 4:
						retValue.setGc04(vo.getGeneratingcapacity());
						break;
					case 5:
						retValue.setGc05(vo.getGeneratingcapacity());
						break;
					case 6:
						retValue.setGc06(vo.getGeneratingcapacity());
						break;
					case 7:
						retValue.setGc07(vo.getGeneratingcapacity());
						break;
					case 8:
						retValue.setGc08(vo.getGeneratingcapacity());
						break;
					case 9:
						retValue.setGc09(vo.getGeneratingcapacity());
						break;
					case 10:
						retValue.setGc10(vo.getGeneratingcapacity());
						break;
					case 11:
						retValue.setGc11(vo.getGeneratingcapacity());
						break;
					case 12:
						retValue.setGc12(vo.getGeneratingcapacity());
						break;
				}
			}
		}
		return retValue;
	}


	public int saveData(ProjectPlanVo vo)  {

		List<ProjectPlan> list=new ArrayList<ProjectPlan>();
    	if(StringUtils.notEmp(vo.getProjectid()) && StringUtils.notEmp(vo.getYear()))
		{
			ProjectPlanExample example = new ProjectPlanExample();
			example.setOrderByClause("month asc");

			ProjectPlanExample.Criteria criteria = example.createCriteria();
			if (StringUtils.notEmp(vo.getProjectid())) {
				criteria.andProjectidEqualTo(vo.getProjectid());
			}

			if (StringUtils.notEmp(vo.getYear())) {
				criteria.andYearEqualTo(vo.getYear());
			}

			List<ProjectPlan> ls = projectPlanMapper.selectByExample(example);
			if(!ls.isEmpty())
			{
				for(ProjectPlan pp:ls)
				{
					deleteByPrimaryKey(pp.getId());
				}
			}



			for(int i=1;i<13;i++)
			{
				ProjectPlan po=new ProjectPlan();
				po.setProjectid(vo.getProjectid());
				po.setWindpower(vo.getWindpower());
				po.setYear(vo.getYear());
				po.setMonth(String.valueOf(i));
				list.add(po);
			}

			ProjectPlan po=new ProjectPlan();
			po.setProjectid(vo.getProjectid());
			po.setWindpower(vo.getWindpower());
			po.setYear(vo.getYear());
			list.add(po);

			for(ProjectPlan pp:list)
			{
				if(StringUtils.empty(pp.getMonth())){
					pp.setGeneratingcapacity(vo.getGeneratingcapacity());
				}
				else{
					int index=Integer.parseInt(pp.getMonth());
					switch(index){
						case 1:
							pp.setGeneratingcapacity(vo.getGc01());
							break;
						case 2:
							pp.setGeneratingcapacity(vo.getGc02());
							break;
						case 3:
							pp.setGeneratingcapacity(vo.getGc03());
							break;
						case 4:
							pp.setGeneratingcapacity(vo.getGc04());
							break;
						case 5:
							pp.setGeneratingcapacity(vo.getGc05());
							break;
						case 6:
							pp.setGeneratingcapacity(vo.getGc06());
							break;
						case 7:
							pp.setGeneratingcapacity(vo.getGc07());
							break;
						case 8:
							pp.setGeneratingcapacity(vo.getGc08());
							break;
						case 9:
							pp.setGeneratingcapacity(vo.getGc09());
							break;
						case 10:
							pp.setGeneratingcapacity(vo.getGc10());
							break;
						case 11:
							pp.setGeneratingcapacity(vo.getGc11());
							break;
						case 12:
							pp.setGeneratingcapacity(vo.getGc12());
							break;
					}
				}
			}
		}


		int value=0;
		for(ProjectPlan pp:list)
		{
			value=insertSelective(pp);
		}
		return value;
	}

}
