package com.yunzhi.marketing.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.marketing.activity.service.ActivityServiceI;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.entity.TaPerson;
import com.yunzhi.marketing.entity.TaRecommendCustomer;
import com.yunzhi.marketing.service.ITaPersonService;
import com.yunzhi.marketing.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;
	}
}
