package org.springblade.responsible.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.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.responsible.dto.query.ResponsiblePageQuery;
import org.springblade.responsible.dto.vo.BladeResponsibleVo;
import org.springblade.responsible.entity.BladeResponsible;
import org.springblade.responsible.service.IBladeResponsibleService;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 负责人管理表 前端控制器
 * </p>
 *
 * @author zbc
 * @since 2021-04-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("Responsible/bladeResponsible")
@Api(value = "负责人管理", tags = "负责人管理接口 BladeResponsibleController")
public class BladeResponsibleController {

	private final IBladeResponsibleService bladeResponsibleService;


	@PostMapping("/save")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "新增负责人", notes = "传入bladeResponsible")
	public R save(@Valid @RequestBody BladeResponsible bladeResponsible) {
		bladeResponsible.setCreateTime(new Date());
		return R.status(bladeResponsibleService.save(bladeResponsible));
	}


	@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(bladeResponsibleService.removeByIds(Func.toLongList(ids)));
	}


	@PostMapping("/update")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "修改负责人", notes = "传入bladeResponsible")
	public R update(@Valid @RequestBody BladeResponsible bladeResponsible) {
		bladeResponsible.setUpdateTime(new Date());
		return R.status(bladeResponsibleService.updateById(bladeResponsible));
	}


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


	@GetMapping("/page")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "分页", notes = "传入productPageQuery")
	public R<IPage<BladeResponsibleVo>> list(ResponsiblePageQuery pageQuery, Query query) {
		IPage<BladeResponsibleVo> page = Condition.getPage(query);
		IPage<BladeResponsibleVo> pages = bladeResponsibleService.getPageByQuery(page, pageQuery);
		List<BladeResponsibleVo> records = pages.getRecords();
		List<BladeResponsibleVo> voList = records.stream()
			.map(obj -> {
				obj.setActivityNum(0);
				return obj;
			}).collect(Collectors.toList());
		pages.setRecords(voList);
		return R.data(pages);
	}


	@GetMapping("/detail")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "详情", notes = "传入bladeResponsible")
	public R<BladeResponsible> detail(BladeResponsible bladeResponsible) {
		BladeResponsible detail = bladeResponsibleService.getOne(Condition.getQueryWrapper(bladeResponsible));
		return R.data(detail);
	}


	@GetMapping("/select/list")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "负责人下拉列表")
	public R selectList() {
		List<BladeResponsible> list = bladeResponsibleService.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);
	}

}

