package com.bizzan.bitrade.controller.system;

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

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

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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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.RiskControl;
import com.bizzan.bitrade.entity.QRiskControl;
import com.bizzan.bitrade.service.RiskControlService;
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;

@Slf4j
@RestController
@RequestMapping("/system/risk-control")
@Transactional(readOnly = true)
public class RiskControlController extends BaseAdminController {

	@Autowired
	private RiskControlService service;
	
	@RequiresPermissions("system:access-log:detail")
    @GetMapping("/{id}")
    @AccessLog(module = AdminModule.SYSTEM, operation = "操作androidLog 详情")
    public MessageResult detail(@PathVariable("id") Long id) {
		RiskControl androidLog = service.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, Long memberId, String content) {

        List<BooleanExpression> list = new ArrayList<>();
        if(memberId!=null)
        	list.add(QRiskControl.riskControl.memberId.eq(memberId));
        if(content!=null && !content.equals(""))
        	list.add(QRiskControl.riskControl.content.like(content));
        Predicate predicate = PredicateUtils.getPredicate(list);
        
        Page<RiskControl> all = service.findAll(predicate, pageModel.getPageable());
        return success(all);
    }
}
