package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.demo.entity.App;
import com.ruoyi.demo.entity.vo.AppVo;
import com.ruoyi.demo.mapper.AppMapper;
import com.ruoyi.demo.service.AppService;
import com.ruoyi.demo.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class AppServiceImpl implements AppService {
    @Autowired
    AppMapper appMapper;
    @Override
    public void addApp(AppVo appVo) {
        App app = new App(appVo);
        //生成appkey
        app.setAppkey(UuidUtil.getUUID());
        //生成密钥
        app.setAppSecret(UuidUtil.getUUID());
        //时间
        app.setCreate_time(LocalDateTime.now());
        appMapper.insert(app);
    }

    @Override
    public void deleteApp(String[] appKeys) {
        appMapper.deleteBatchIds(Arrays.asList(appKeys));
    }

    @Override
    public PageInfo<App> getAppList(AppVo appVo) {
        QueryWrapper<App> queryWrapper = new QueryWrapper<>();
        if (appVo.getSearchText() != null && !appVo.getSearchText().equals("")){
            queryWrapper.likeRight("app_key",appVo.getSearchText());
            queryWrapper.or().like("app_name",appVo.getSearchText());
            queryWrapper.or().likeRight("commercial_name",appVo.getSearchText());
        }

        PageHelper.startPage(appVo.getPageNum(),appVo.getPageSize());
        List<App> apps = appMapper.selectList(queryWrapper);
        PageInfo<App> pageInfo = new PageInfo<>(apps);
        return pageInfo;
    }

    @Override
    public App getApp(AppVo appVo) {
        App app = appMapper.selectById(appVo.getAppkey());
        return app;
    }

    @Override
    public void updateApp(AppVo appVo) {
        App app = new App(appVo);
        app.setAppkey(appVo.getAppkey());
        appMapper.updateApp(app);
    }

    @Override
    public void testCallBack(String callBack) {
        //TODO 发送请求
    }
}
