package org.springblade.channel.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.channel.dto.query.ChannelPageQuery;
import org.springblade.channel.entity.BladeChannel;
import org.springblade.channel.service.IBladeChannelService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zbc
 * @since 2021-03-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("channel/bladeChannel")
@Api(value = "渠道管理", tags = "渠道管理接口")
public class BladeChannelController{

	private final IBladeChannelService bladeChannelService;


	@GetMapping("/list")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "列表", notes = "传入bladePrize")
	public R<IPage<BladeChannel>> list(BladeChannel bladeChannel, Query query) {
		IPage<BladeChannel> pages = bladeChannelService.page(Condition.getPage(query), Condition.getQueryWrapper(bladeChannel));
		return R.data(pages);
	}


	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入pageQuery")
	public R<IPage<BladeChannel>> list(ChannelPageQuery pageQuery, Query query) {
		IPage<BladeChannel> page = Condition.getPage(query);
		IPage<BladeChannel> pages = bladeChannelService.getPageByQuery(page, pageQuery);
		return R.data(pages);
	}


	@GetMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入bladePrize")
	public R<BladeChannel> detail(String id) {
		BladeChannel one = bladeChannelService.getOne(new LambdaQueryWrapper<BladeChannel>()
			.eq(BladeChannel::getId, id)
		);
		return R.data(one);
	}


	@GetMapping("/select/list")
	@ApiOperation(value = "渠道管理下拉列表")
	public R selectList() {
		List<BladeChannel> list = bladeChannelService.list();
		List<Map<String, Object>> data = list.stream()
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("id", obj.getId());
				map.put("name", obj.getName());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}

}

