package com.bizzan.bitrade.controller.system;

import static com.bizzan.bitrade.constant.SysConstant.SESSION_MEMBER;

import java.util.ArrayList;
import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.bizzan.bitrade.constant.PageModel;
import com.bizzan.bitrade.entity.MemberFeedback;
import com.bizzan.bitrade.entity.QMemberFeedback;
import com.bizzan.bitrade.entity.transform.AuthMember;
import com.bizzan.bitrade.service.MemberFeedbackService;
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;

@RestController
@Slf4j
@RequestMapping("feedback")
public class MemberFeedbackController {

	@Autowired
	private MemberFeedbackService feedbackService;
	
	@RequiresPermissions("member:page-query")
    @PostMapping("page-query")
    public MessageResult listAll(PageModel pageModel, MemberFeedback feedback) {
		Predicate predicate = getPredicate(feedback);
		MessageResult mr = new MessageResult(0, "success");
		Page<MemberFeedback> data = feedbackService.findAll(predicate, pageModel.getPageable());
		mr.setData(data);
        return mr;
	}
	
	public Predicate getPredicate(MemberFeedback feedback) {
		ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
		if(feedback.getMemberId()!=null)
			booleanExpressions.add(QMemberFeedback.memberFeedback.memberId.eq(feedback.getMemberId()));
		return PredicateUtils.getPredicate(booleanExpressions);
	}
	
	@PostMapping("/deletes")
	public MessageResult delete(@RequestParam(value = "ids") Long[] ids) {
		feedbackService.deleteBatch(ids);
		return MessageResult.success();
	}
}
