package org.springblade.product.controller;


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 io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.product.dto.query.ProductPageQuery;
import org.springblade.product.entity.BladeProduct;
import org.springblade.product.service.IBladeProductService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品管理表 前端控制器
 * </p>
 *
 * @author zbc
 * @since 2021-04-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("Product/bladeProduct")
@Api(value = "产品管理", tags = "产品管理接口")
public class BladeProductController {


	private final IBladeProductService bladeProductService;


	@PostMapping("/save")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "新增产品", notes = "传入bladeProduct")
	public R save(@Valid @RequestBody BladeProduct bladeProduct) {
		return R.status(bladeProductService.save(bladeProduct));
	}


	@PostMapping("/remove")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "删除产品", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合  1,2,3", required = true) @RequestParam String ids) {
		return R.status(bladeProductService.removeByIds(Func.toLongList(ids)));
	}


	@PostMapping("/update")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "修改产品", notes = "传入bladeProduct")
	public R update(@Valid @RequestBody BladeProduct bladeProduct) {
		return R.status(bladeProductService.updateById(bladeProduct));
	}


	@GetMapping("/list")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "列表", notes = "传入bladeProduct")
	public R<IPage<BladeProduct>> list(BladeProduct bladeProduct, Query query) {
		IPage<BladeProduct> pages = bladeProductService.page(Condition.getPage(query), Condition.getQueryWrapper(bladeProduct));
		List<BladeProduct> records = pages.getRecords();
		List<BladeProduct> collect = records.stream().filter(e -> StringUtils.isNotBlank(e.getCpname())).collect(Collectors.toList());
		pages.setRecords(collect);
		return R.data(pages);
	}


	@GetMapping("/page")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "分页", notes = "传入productPageQuery")
	public R<IPage<BladeProduct>> list(ProductPageQuery pageQuery, Query query) {
		IPage<BladeProduct> page = Condition.getPage(query);
		IPage<BladeProduct> pages = bladeProductService.getPageByQuery(page, pageQuery);
		return R.data(pages);
	}


	@GetMapping("/detail")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "详情", notes = "传入bladeProduct")
	public R<BladeProduct> detail(Long id) {
		BladeProduct detail = bladeProductService.getById(id);
		return R.data(detail);
	}


	@GetMapping("/select/list")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "产品管理下拉列表")
	public R selectList() {
		List<BladeProduct> list = bladeProductService.list();
		List<Map<String, Object>> data = list.stream().filter(e-> StringUtils.isNotBlank(e.getCpname()))
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("id", obj.getId());
				map.put("name", obj.getSpname());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}

}

