package com.bizzan.bitrade.controller.system;

import com.bizzan.bitrade.annotation.AccessLog;
import com.bizzan.bitrade.constant.AdminModule;
import com.bizzan.bitrade.constant.PageModel;
import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.AndroidLog;
import com.bizzan.bitrade.entity.QAdmin;
import com.bizzan.bitrade.entity.QAdminAccessLog;
import com.bizzan.bitrade.entity.QAndroidLog;
import com.bizzan.bitrade.service.AndroidLogService;
import com.bizzan.bitrade.util.MessageResult;
import com.bizzan.bitrade.util.PredicateUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.util.Assert.notNull;

/**
 * @author
 * @description 日志管理
 * @date 2019/12/22 17:27
 */
@Slf4j
@RestController
@RequestMapping("/system/android-log")
@Transactional(readOnly = true)
public class AndroidLogController extends BaseAdminController {

    @Autowired
    private AndroidLogService androidLogService;


    @RequiresPermissions("system:access-log:detail")
    @GetMapping("/{id}")
    @AccessLog(module = AdminModule.SYSTEM, operation = "操作androidLog 详情")
    public MessageResult detail(@PathVariable("id") Long id) {
    	AndroidLog androidLog = androidLogService.queryById(id);
        notNull(androidLog, "validate id!");
        return success(androidLog);
    }

    @RequiresPermissions("system:access-log:page-query")
    @GetMapping("/page-query")
    @AccessLog(module = AdminModule.SYSTEM, operation = "分页查找androidLog")
    public MessageResult pageQuery(
            PageModel pageModel, String os, String error) {

        List<BooleanExpression> list = new ArrayList<>();
        if(os!=null && os.equals(""))
        	list.add(QAndroidLog.androidLog.os.contains(os));
        if(error!=null && error.equals(""))
        	list.add(QAndroidLog.androidLog.error.contains(error));
        Predicate predicate = PredicateUtils.getPredicate(list);
        
        Page<AndroidLog> all = androidLogService.findAll(predicate, pageModel.getPageable());
        return success(all);
    }


}
