package com.copy.trade.controller;

import com.bizzan.bitrade.entity.transform.AuthMember;
import com.copy.trade.constant.DogeApi;
import com.copy.trade.entity.po.Customer;
import com.copy.trade.entity.po.Tag;
import com.copy.trade.entity.vo.TagVo;
import com.copy.trade.enums.TraderEnum;
import com.copy.trade.service.ICustomerService;
import com.copy.trade.service.ITraderTagService;
import com.copy.trade.util.BeanCopior;
import com.copy.trade.util.WebAssert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 交易员标签表 前端控制器
 * </p>
 *
 * @author wz
 * @since 2022-11-12
 */
@Api(value = "交易员标签表", tags = "交易员标签表")
@RestController
@RequestMapping("/trader-tag")
public class TraderTagController {

	private final int MAX_TAG = 4;

	ITraderTagService traderTagServiceImpl;

	@Autowired
	private ICustomerService customerServiceImpl;

	/**
	 * 推荐使用构造器注入
	 * @param traderTagServiceImpl
	 */	
	@Autowired
	TraderTagController(ITraderTagService traderTagServiceImpl) {
		this.traderTagServiceImpl = traderTagServiceImpl;
	}


	@ApiOperation(value = "交易员标签列表")
	@GetMapping("/")
	public List<TagVo> list(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember){
		Customer trader = customerServiceImpl.getByMemberId(authMember.getId());
		List<Tag> tagList = traderTagServiceImpl.list(trader.getId());
		return BeanCopior.mapList(tagList, TagVo.class);
	}

	@ApiOperation(value = "更新交易员标签")
	@ApiImplicitParam(name = "tagIdList", value = "标签id数组",required = true,allowMultiple=true,dataType = "long")
	@PostMapping("/update")
	public Boolean update(@SessionAttribute(DogeApi.SESSION_MEMBER)AuthMember authMember,@RequestBody List<Long> tagIdList){
		WebAssert.isTrue(tagIdList.size() <= MAX_TAG,"chk.tag.max");
		Customer trader = customerServiceImpl.getByMemberId(authMember.getId());
		WebAssert.isTrue(TraderEnum.TRADER.getStatus().equals(trader.getTrader()),"chk.not.trader");
		return traderTagServiceImpl.update(trader.getId(),tagIdList);
	}
}
