package com.copy.trade.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bizzan.bitrade.entity.transform.AuthMember;
import com.copy.trade.constant.DogeApi;
import com.copy.trade.entity.base.BaseQueryForm;
import com.copy.trade.entity.po.Customer;
import com.copy.trade.entity.vo.TraderVo;
import com.copy.trade.service.ICustomerService;
import com.copy.trade.service.IFavoriteService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 关注表 前端控制器
 * </p>
 *
 * @author wz
 * @since 2022-11-08
 */
@Api(value = "关注表", tags = "关注表")
@RestController
@RequestMapping("/favorite")
public class FavoriteController {

	IFavoriteService favoriteServiceImpl;

	@Autowired
	private ICustomerService customerServiceImpl;

	/**
	 * 推荐使用构造器注入
	 * @param favoriteServiceImpl
	 */	
	@Autowired
	FavoriteController(IFavoriteService favoriteServiceImpl) {
		this.favoriteServiceImpl = favoriteServiceImpl;
	}

	@ApiOperation(value = "关注交易员")
	@PutMapping("/{traderId}")
	public Boolean favorite(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember, @PathVariable Long traderId){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		return favoriteServiceImpl.favorite(customer.getId(), traderId);
	}


	@ApiOperation(value = "取消关注")
	@PutMapping("/unfavorite/{traderId}")
	public Boolean unfavorite(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember, @PathVariable Long traderId){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		return favoriteServiceImpl.unfavorite(customer.getId(), traderId);
	}

	@ApiOperation(value = "我的关注")
	@ApiImplicitParam(name = "baseQueryForm",value = "分页条件",dataType = "BaseQueryForm")
	@PostMapping("/list")
	public Page<TraderVo> list(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember, @RequestBody BaseQueryForm baseQueryForm){
		Customer customer = customerServiceImpl.getByMemberId(authMember.getId());
		return favoriteServiceImpl.list(customer.getId(),baseQueryForm.newFormPage());
	}
}
