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.Alertrule2Mapper;
import com.gyee.frame.model.auto.Alertrule2;
import com.gyee.frame.model.auto.Alertrule2Example;
import com.gyee.frame.model.custom.AlarmTotalVo;
import com.gyee.frame.model.custom.SimpleVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.model.custom.weather.Sys;
import com.gyee.frame.util.DateUtils;
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.Date;
import java.util.List;


@Service
public class Alertrule2ervice implements BaseService<Alertrule2, Alertrule2Example> {
    @Resource
    private Alertrule2Mapper alertrule2Mapper;


    @Override
    public int deleteByPrimaryKey(String ids) {

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


    }


    @Override
    public Alertrule2 selectByPrimaryKey(String id) {

        return alertrule2Mapper.selectByPrimaryKey(id);

    }


    @Override
    public int updateByPrimaryKeySelective(Alertrule2 record) {
        return alertrule2Mapper.updateByPrimaryKeySelective(record);
    }


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

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


        return alertrule2Mapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(Alertrule2 record, Alertrule2Example example) {

        return alertrule2Mapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(Alertrule2 record, Alertrule2Example example) {

        return alertrule2Mapper.updateByExample(record, example);
    }

    @Override
    public List<Alertrule2> selectByExample(Alertrule2Example example) {

        return alertrule2Mapper.selectByExample(example);
    }


    @Override
    public long countByExample(Alertrule2Example example) {

        return alertrule2Mapper.countByExample(example);
    }


    @Override
    public int deleteByExample(Alertrule2Example example) {

        return alertrule2Mapper.deleteByExample(example);
    }


    public  PageInfo<Alertrule2> getAlertruleList(Tablepar tablepar, String name, String category, String rank) {


        PageInfo<Alertrule2> pageInfo = new PageInfo<>();


        Alertrule2Example example = new Alertrule2Example();
        example.setOrderByClause("name DESC");

        if (StringUtils.isNotEmpty(tablepar.getOrderByColumn())) {
            StringBuilder sb = new StringBuilder();
            sb.append(" ").append(tablepar.getOrderByColumn());
            if (StringUtils.isNotEmpty(tablepar.getIsAsc())) {
                sb.append(" ").append(tablepar.getIsAsc());
            } else {
                sb.append(" asc ");
            }
            example.setOrderByClause(String.valueOf(sb));
        } else {
            example.setOrderByClause("name ASC");
        }

        Alertrule2Example.Criteria criteia = example.createCriteria();

        if (StringUtils.notEmp(name)) {

            StringBuilder sb = new StringBuilder();
            sb.append("%").append(name).append("%");
            criteia.andNameLike(String.valueOf(name));
        }

		if (StringUtils.notEmp(category)) {

			StringBuilder sb = new StringBuilder();
			sb.append("%").append(category).append("%");
			criteia.andCategoryLike(String.valueOf(category));
		}

		if (StringUtils.notEmp(rank)) {

			StringBuilder sb = new StringBuilder();
			sb.append("%").append(rank).append("%");
			criteia.andRankLike(String.valueOf(rank));
		}

        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
        List<Alertrule2> vos = alertrule2Mapper.selectByExample(example);
        pageInfo = new PageInfo<Alertrule2>(vos);

        return pageInfo;

    }


	public Alertrule2 findAlertrule2ById(String id) {


		Alertrule2  po = new Alertrule2();

		po = alertrule2Mapper.selectByPrimaryKey(id);

		return po;

	}

	public int updateAlertrule2(Alertrule2 po) {

		int result =-1;
		if(StringUtils.notEmp(po) && StringUtils.notEmp(po.getId()))
		{
			result =alertrule2Mapper.updateByPrimaryKeySelective(po);
		}
		return result;

	}

	public int insertAlertrule2(Alertrule2 po) {

		int result =-1;
		if(StringUtils.notEmp(po) )
		{
			result = alertrule2Mapper.insertSelective(po);
		}


		return result;

	}

	public int deleteAlertrule2(String id) {

		int result =-1;
		if(StringUtils.notEmp(id))
		{
			result =alertrule2Mapper.deleteByPrimaryKey(id);
		}
		return result;

	}


    public  List<Alertrule2> getAlertruleAllList() {

        Alertrule2Example example = new Alertrule2Example();

        List<Alertrule2> vos = alertrule2Mapper.selectByExample(example);


        return vos;

    }

    public List<AlarmTotalVo> getAlertruleAllList(String wtId,Date beginDate, Date endDate)
    {
        List<AlarmTotalVo> vos=new ArrayList<>();
        if(StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate))
        {


            PageHelper.startPage(1, 1);
            vos=alertrule2Mapper.getAlarmTotal(wtId, DateUtils.format(beginDate), DateUtils.format(endDate));
            PageInfo<AlarmTotalVo> pageInfo = new PageInfo<AlarmTotalVo>(vos);
            vos=pageInfo.getList();
        }
        return  vos;
    }


    public  List<SimpleVo> getAlarmTypenum()
    {
        List<SimpleVo> ls=new ArrayList<>();
        ls=alertrule2Mapper.getAlarmTypenum();
        return ls;
    }

    /**
     * 通过规则描述，场站，风机ID查询一条
     * 解析规则表达式，提取出所有uniformCode
     * @param name
     * @param station
     * @param modelid
     * @return
     */
    public List<String> getUniformCodeByNameAndStation(String name, String station, String modelid){
        Alertrule2Example example = new Alertrule2Example();
        example.createCriteria().andNameEqualTo(name)
               .andStationEqualTo(station)
        .andModelidEqualTo(modelid);
//               .andWindturbineLike("%" + wtId + "%");

        List<Alertrule2> rules = alertrule2Mapper.selectByExample(example);
        if (rules == null || rules.size() == 0)
            return null;

        Alertrule2 rule = rules.get(0);
        String expression = "[AI,DI](.{4})";
        List<String> list = StringUtils.pattern(rule.getExpression(), expression);

        return list;
    }

    public List<String> getUniformCodeByNameAndStations(){
        Alertrule2Example example = new Alertrule2Example();

        List<Alertrule2> rules = alertrule2Mapper.selectByExample(example);
        if (rules == null || rules.size() == 0)
            return null;

        List<String> list = new ArrayList<>();
        for (Alertrule2 rule : rules){
            String expression = "[AI,DI](.{4})";
            List<String> lists = StringUtils.pattern(rule.getExpression(), expression);
            for (String ls : lists){
                if (!list.contains(ls) && (ls.startsWith("AI") || ls.startsWith("DI")))
                    list.add(ls);
            }
        }

        return list;
    }

    public List<String> getModelID(){
        Alertrule2Example example = new Alertrule2Example();

        List<Alertrule2> rules = alertrule2Mapper.selectByExample(example);
        if (rules == null || rules.size() == 0)
            return null;

        List<String> list = new ArrayList<>();
        for (Alertrule2 rule : rules){
            String modelis = rule.getModelid();
            if (!list.contains(modelis))
                list.add(modelis);

        }

        return list;
    }
}
