package com.gyee.wisdom.alarm.sharding.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.wisdom.alarm.sharding.entity.Alertrule2;
import com.gyee.wisdom.alarm.sharding.entity.Scadabj;
import com.gyee.wisdom.alarm.sharding.logtool.LogService;
import com.gyee.wisdom.alarm.sharding.mapper.Alertrule2Mapper;
import com.gyee.wisdom.alarm.sharding.mapper.ScadabjMapper;
import com.gyee.wisdom.alarm.sharding.util.SnowflakeGenerator;
import com.gyee.wisdom.alarm.sharding.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class ScadabjService extends ServiceImpl<ScadabjMapper, Scadabj> {
    @Autowired
    private ScadabjMapper scadabjMapper;

    @Autowired
    private LogService logService;

    @Autowired
    private RuleUpdateEventService eventService;

    public int saveAndUpdateScadabj(Scadabj scadabj,String useName) {
        Scadabj oldData=null;
        int result = 0;
        if (scadabj.getId() == null || scadabj.getId() <= 0) {
            //如果id为空，则通过雪花算法 生成一个id
            scadabj.setId(SnowflakeGenerator.generateId());
            result = scadabjMapper.insert(scadabj);

        } else {
             oldData = scadabjMapper.selectById(scadabj.getId());
            if (oldData != null) {
                result = scadabjMapper.updateById(scadabj);
            }
            else {
                result = scadabjMapper.insert(scadabj);
            }
        }

        if(result>0)
            eventService.saveEventScadaBj(scadabj,oldData,useName);
        return result;

    }

    public int deleteScadabj(Scadabj scadabj) {
        return scadabjMapper.deleteById(scadabj.getId());
    }

    public IPage<Scadabj> pageQueryAll(Page page, String pointKey, String stationId, String enable, String description) {


        return scadabjMapper.pageQueryAll(page, pointKey, stationId, enable, description);
    }

    public List<Scadabj> test() {

        //region 条件查询lambda 方式查询 类似于jpa


        //setSqlSelect	设置 SELECT 查询字段
        //where	    WHERE 语句，拼接 + WHERE 条件
        //and	    AND 语句，拼接 + AND 字段=值
        //andNew	    AND 语句，拼接 + AND (字段=值)
        //or	OR      语句，拼接 + OR 字段=值
        //orNew	    OR 语句，拼接 + OR (字段=值)
        //eq	        等于=
        //allEq	    基于 map 内容等于=
        //ne	        不等于<>
        //gt	        大于>
        //ge	        大于等于>=
        //lt	        小于<
        //le	        小于等于<=
        //like	    模糊查询 LIKE
        //notLike	模糊查询 NOT LIKE
        //in	IN      查询
        //notIn	    NOT IN 查询
        //isNull	    NULL 值查询
        //isNotNull	IS NOT NULL
        //groupBy	分组 GROUP BY
        //having	    HAVING 关键词
        //orderBy	排序 ORDER BY
        //orderAsc	ASC 排序 ORDER BY
        //orderDesc	DESC 排序 ORDER BY
        //exists	    EXISTS 条件语句
        //notExists	NOT EXISTS 条件语句
        //between	BETWEEN 条件语句
        //notBetween	NOT BETWEEN 条件语句
        //addFilter	自由拼接 SQL
        //last	    拼接在最后，例如：last("LIMIT 1")
        QueryWrapper<Scadabj> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .and(obj ->
                        obj.eq(Scadabj::getPointKey, "PLSJ1.NX_GD_PLG_DQ_P1_L1_001_DI0888")
                                .eq(Scadabj::getCategory1, "SYZ"));
        List<Scadabj> lst = list(queryWrapper);
        //endregion
        return scadabjMapper.getAll();
    }
}
