package com.gyee.frame.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.ProjectplanMapper;
import com.gyee.frame.model.auto.Projectplan;
import com.gyee.frame.model.auto.ProjectplanExample;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 项目 ProjectService
 * @Title: ProjectService.java 
 * @Package com.gyee.frame.service 
 * @author gyee_自动生成
 * @email 1@qq.com
 * @date 2019-12-31 14:58:09  
 **/
@Service
public class ProjectplanService implements BaseService<Projectplan, ProjectplanExample>{
    @Resource
    private ProjectplanMapper projectplanMapper;


    /**
     * 分页查询
     * @param tablepar
     * @param name
     * @return
     */
    public PageInfo<Projectplan> list(Tablepar tablepar,String name){
        ProjectplanExample testExample=new ProjectplanExample();
        testExample.setOrderByClause("id ASC");
        if(name!=null&&!"".equals(name)){
            testExample.createCriteria().andIdLike("%"+name+"%");
        }

        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
        List<Projectplan> list= projectplanMapper.selectByExample(testExample);
        PageInfo<Projectplan> pageInfo = new PageInfo<Projectplan>(list);
        return  pageInfo;
    }

    @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);
    }

    /**
     * 检查name
     * @param project
     * @return
     */
    public int checkNameUnique(Projectplan project){
        ProjectplanExample example=new ProjectplanExample();
        example.createCriteria().andIdEqualTo(project.getId());
        List<Projectplan> list=projectplanMapper.selectByExample(example);
        return list.size();
    }

    public double planMonthPower(String name){


        ProjectplanExample example=new ProjectplanExample();
        Calendar c = Calendar.getInstance();

        int year= c.get(Calendar.YEAR);
        int month= c.get(Calendar.MONTH)+1;


        if(name.equals("0"))
        {
            example.createCriteria().andWindpowerNotLike("%_GDC").andYearEqualTo(String.valueOf(year)).andMonthEqualTo(String.valueOf(month));

        }else
        {
            example.createCriteria().andWindpowerEqualTo(name).andYearEqualTo(String.valueOf(year)).andMonthEqualTo(String.valueOf(month));


        }
        List<Projectplan> wtls = selectByExample(example);
        double tmp = 0;
        for(Projectplan wt:wtls)
        {
            tmp = tmp + Double.valueOf(wt.getGeneratingcapacity());
        }
        return tmp;
    }

    public double planYearPower(String name){


        ProjectplanExample example=new ProjectplanExample();
        Calendar c = Calendar.getInstance();

        int year= c.get(Calendar.YEAR);
        int month= c.get(Calendar.MONTH)+1;


        if(name.equals("0"))
        {
            example.createCriteria().andWindpowerNotLike("%_GDC").andYearEqualTo(String.valueOf(year)).andMonthIsNull();

        }else
        {
            example.createCriteria().andWindpowerEqualTo(name).andYearEqualTo(String.valueOf(year)).andMonthIsNull();


        }
        List<Projectplan> wtls = selectByExample(example);
        double tmp = 0;
        for(Projectplan wt:wtls)
        {
            tmp = tmp + Double.valueOf(wt.getGeneratingcapacity());
        }
        return tmp;
    }
}
