package org.springblade.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.constant.AppConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.system.entity.PlatomixTemplate;
import org.springblade.modules.system.entity.PlatomixTemplateCate;
import org.springblade.modules.system.service.PlatomixTemplateCateService;
import org.springblade.modules.system.service.PlatomixTemplateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/template")
@Api(value = "模版管理", tags = "模版管理")
public class TemplateController extends BladeController {

	@Resource
	private PlatomixTemplateCateService templateCateService;

	@Resource
	private PlatomixTemplateService templateService;

	/**
	 * 获取模板分类列表
	 */
	@GetMapping("/cate/list")
	@ApiOperation(value = "获取模板分类列表", notes = "获取模板分类列表")
	public R<IPage<PlatomixTemplateCate>> cateList(PlatomixTemplateCate entity, Query query) {
		LambdaQueryWrapper queryWrapper =  new LambdaQueryWrapper<PlatomixTemplateCate>().orderByDesc(PlatomixTemplateCate::getId);
		IPage<PlatomixTemplateCate> page = Condition.getPage(query);
		IPage<PlatomixTemplateCate> pages = templateCateService.page(page, queryWrapper);
		return R.data(pages);
	}

	/**
	 * 添加编辑模板分类
	 */
	@PostMapping("/cate/save")
	@ApiOperation(value = "添加编辑模板分类", notes = "添加编辑模板分类")
	public R<Object> cateSave(@RequestBody PlatomixTemplateCate entity) {
		if(StringUtils.isBlank(entity.getName())){
			return R.fail("名称不能为空");
		}
		Boolean result = templateCateService.customSave(entity);
		if(result){
			return R.data("保存成功.");
		}else{
			return R.fail("保存失败.");
		}
	}


	/**
	 * 删除模板分类
	 */
	@PostMapping("/cate/remove")
	@ApiOperation(value = "删除模板分类", notes = "删除模板分类")
	public R<Object> cateRemove(@ApiParam(value = "主键集合", required = true) @RequestBody List<Long> idList) {
		if(CollectionUtils.isEmpty(idList)){
			return R.fail("模板分类ID值不能为空.");
		}
		Boolean result = templateCateService.customRemove(idList);
		if(result){
			return R.data("删除成功.");
		}else{
			return R.fail("删除失败.");
		}
	}

	/**
	 * 获取模板列表
	 */
	@GetMapping("/list")
	@ApiOperation(value = "获取模板列表", notes = "获取模板列表")
	public R<IPage<PlatomixTemplate>> list(PlatomixTemplate entity, Query query) {
		LambdaQueryWrapper queryWrapper =  new LambdaQueryWrapper<PlatomixTemplate>()
				.eq(ObjectUtils.isNotEmpty(entity.getCateId()), PlatomixTemplate::getCateId, entity.getCateId())
				.like(StringUtils.isNotBlank(entity.getName()), PlatomixTemplate::getName, entity.getName())
				.orderByDesc(PlatomixTemplate::getCreateTime);
		IPage<PlatomixTemplate> page = Condition.getPage(query);
		IPage<PlatomixTemplate> pages = templateService.page(page, queryWrapper);
		if(pages.getRecords()!=null){
			List<Long> cateIdList = pages.getRecords().stream().map(obj->obj.getCateId()).collect(Collectors.toList());
			if(CollectionUtils.isNotEmpty(cateIdList)){
				LambdaQueryWrapper queryW =  new LambdaQueryWrapper<PlatomixTemplateCate>().in(PlatomixTemplateCate::getId, cateIdList);
				List<PlatomixTemplateCate> cateList = templateCateService.list(queryW);
				if(CollectionUtils.isNotEmpty(cateIdList)){
					Map<Long, String> cateMap = cateList.stream().collect(Collectors.toMap(PlatomixTemplateCate::getId, PlatomixTemplateCate::getName, (key1, key2)->key1));
					for(PlatomixTemplate templateEntity: pages.getRecords()){
						if(cateMap.containsKey(templateEntity.getCateId())){
							templateEntity.setCateName(cateMap.get(templateEntity.getCateId()));
						}
					}
				}
			}
		}
		return R.data(pages);
	}

	/**
	 * 添加编辑模板
	 */
	@PostMapping("/save")
	@ApiOperation(value = "添加编辑模板", notes = "添加编辑模板")
	public R<Object> save(@RequestBody PlatomixTemplate entity) {
		if(StringUtils.isBlank(entity.getName())){
			return R.fail("名称不能为空.");
		}
		if(ObjectUtils.isEmpty(entity.getCateId())){
			return R.fail("模板分类ID不能为空.");
		}
		Boolean result = templateService.customSave(entity);
		if(result){
			return R.data("保存成功.");
		}else{
			return R.fail("保存失败.");
		}
	}


	/**
	 * 删除模板
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除模板", notes = "删除模板")
	public R<Object> remove(@ApiParam(value = "主键集合", required = true) @RequestBody List<Long> idList) {
		if(CollectionUtils.isEmpty(idList)){
			return R.fail("模板ID值不能为空.");
		}
		Boolean result = templateService.customRemove(idList);
		if(result){
			return R.data("删除成功.");
		}else{
			return R.fail("删除失败.");
		}
	}
}
