package com.copy.trade.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.copy.trade.entity.form.TraderLevelForm;
import com.copy.trade.entity.po.TraderLevel;
import com.copy.trade.service.ITraderLevelService;
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.GetMapping;
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 java.util.List;

import javax.validation.Valid;

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

	ITraderLevelService traderLevelServiceImpl;

	/**
	 * 推荐使用构造器注入
	 * @param traderLevelServiceImpl
	 */	
	@Autowired
	TraderLevelController(ITraderLevelService traderLevelServiceImpl) {
		this.traderLevelServiceImpl = traderLevelServiceImpl;
	}
	
	@ApiOperation(value = "交易员等级列表")
	@GetMapping("/")
	public List<TraderLevel> list(){
		return traderLevelServiceImpl.list();
	}

	@ApiOperation(value = "添加交易员等级")
	@ApiImplicitParam(name = "traderLevelForm", value = "添加交易等级表单",dataType = "TraderLevelForm")
	@PostMapping("/add")
	public Boolean add(@RequestBody @Valid TraderLevelForm traderLevelForm){
		QueryWrapper<TraderLevel> traderLevelQueryWrapper = new QueryWrapper<>();
		traderLevelQueryWrapper.eq("name",traderLevelForm.getName());
		TraderLevel traderLevel = traderLevelServiceImpl.getOne(traderLevelQueryWrapper);
		WebAssert.isNull(traderLevel,"等级已存在");
		traderLevel = BeanCopior.map(traderLevelForm, TraderLevel.class);
		return traderLevelServiceImpl.save(traderLevel);
	}
	
	@ApiOperation(value = "修改交易员等级")
	@ApiImplicitParam(name = "traderLevelForm", value = "修改交易等级表单",dataType = "TraderLevelForm")
	@PostMapping("/update")
	public Boolean update(@RequestBody @Valid TraderLevelForm traderLevelForm){
		TraderLevel traderLevel = BeanCopior.map(traderLevelForm, TraderLevel.class);
		return traderLevelServiceImpl.updateById(traderLevel);
	}
	
	@ApiOperation(value = "删除交易员等级")
	@PutMapping("/delete/{id}")
	public Boolean delete(@PathVariable Long id){
		return traderLevelServiceImpl.removeById(id);
	}

}
