package com.huiju.estateagents.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.activity.service.ActivityServiceI;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.entity.TaRecommendCustomer;
import com.huiju.estateagents.service.ITaPersonService;
import com.huiju.estateagents.service.ITaRecommendCustomerService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author FXF
 * @date 2019-10-21
 */
@RestController
@RequestMapping("/api")
@Api(value = "所有活动", tags = "所有活动")
public class ActivityController extends BaseController {
	
	@Autowired
	private ActivityServiceI activityService;
	
	@Autowired
	private ITaPersonService taPersonService;
	
	@Autowired
	ITaRecommendCustomerService taRecommendCustomerService;
	
	@RequestMapping(value = "/wx/activity/list", method = RequestMethod.GET)
	public ResponseBean wxBuildingDynamiceList(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,
	                                           @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
	                                           @RequestParam(value = "cityId",required = false)Integer cityId,
	                                           HttpServletRequest request){
		String openid = getOpenId(request);
		Integer orgId = getOrgId(request);
		ResponseBean  responseBean = new ResponseBean();
		List<TaPerson> taPersons = taPersonService.getPersonsByOpenId(openid);
		if (null == taPersons || taPersons.size() != 1) {
			return responseBean.error("验证人员信息失败", ResponseBean.ERROR_UNAVAILABLE);
		}
		TaPerson person = taPersons.get(0);
		responseBean = activityService.getActivityList(pageNumber,pageSize,orgId,person,cityId);
		
		return responseBean;
	}
	
	@RequestMapping(value = "/wx/customer/activity/list", method = RequestMethod.GET)
	public ResponseBean wxActivityList(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,
	                                           @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
	                                           @RequestParam(value = "customerId") String customerId,
	                                           HttpServletRequest request){
		ResponseBean  responseBean = new ResponseBean();
		Integer orgId = getOrgId(request);
		//查找客户信息
		QueryWrapper<TaRecommendCustomer> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("customer_id",customerId);
		TaRecommendCustomer taRecommendCustomer = taRecommendCustomerService.getOne(queryWrapper);
		if (null == taRecommendCustomer){
			//验证人员信息失败小程序端希望是空数组对象
			return ResponseBean.success(new Page(pageNumber,pageSize));
		}
		if (null == taRecommendCustomer.getPersonId()){
			//验证人员信息失败小程序端希望是空数组对象
			return ResponseBean.success(new Page(pageNumber,pageSize));
		}
		
		QueryWrapper<TaPerson> taPersonQueryWrapper = new QueryWrapper<>();
		taPersonQueryWrapper.eq("person_id",taRecommendCustomer.getPersonId());
		TaPerson person = taPersonService.getOne(taPersonQueryWrapper);
		if (null == person){
			//验证人员信息失败小程序端希望是空数组对象
			return ResponseBean.success(new Page(pageNumber,pageSize));
		}
		responseBean = activityService.getCustomerActivityList(pageNumber,pageSize,orgId,person);
		return responseBean;
	}
}
