/**
 * 
 */
package org.springblade.modules.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.DatasourceFunction;
import org.springblade.modules.system.enums.FunctionAttrEnum;
import org.springblade.modules.system.enums.FunctionTypeEnum;
import org.springblade.modules.system.service.normal.DatasourceFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author wookvn
 *
 */
@RestController
@RequestMapping("/cube/function")
@Api(value = "数据源函数接口", tags = "数据源函数相关接口")
public class DatasourceFunctionController extends BladeController {

	@Autowired
	private DatasourceFunctionService cubeDatasouceFunctionService;
	
	/**
	 * 
	 * @param dsType
	 * @return
	 */
	@GetMapping("/list")
	@ApiOperation(value = "函数列表")
	public R<List<DatasourceFunction>> list(@RequestParam Integer dsType, FunctionAttrEnum funcAttr, String name){
		QueryWrapper<DatasourceFunction> queryWrapper = Wrappers.<DatasourceFunction>query();
//		queryWrapper.in("ds_type", dsType.getValue(), -1); //数据源类型【-1 代表通用】
		queryWrapper.lambda()
				.in(DatasourceFunction::getDsType, Arrays.asList(dsType, -1))
				.like(StrUtil.isNotBlank(name), DatasourceFunction::getName, name)
				.eq(funcAttr != null, DatasourceFunction::getFuncAttr, funcAttr)
				.and(t -> t.eq(DatasourceFunction::getFuncType, FunctionTypeEnum.system).or().eq(DatasourceFunction::getTenantId,getUser().getTenantId()))
				.orderByDesc(DatasourceFunction::getSortNum);
		List<DatasourceFunction> list = cubeDatasouceFunctionService.list(queryWrapper);
		return R.data(list);
	}
	
	/**
	 * 
	 * @return
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增修改函数")
	public R<String> doSubmit(@RequestBody DatasourceFunction datasourceFunction){
		datasourceFunction.setCreateTime(new Date());
		return R.status(cubeDatasouceFunctionService.saveOrUpdate(datasourceFunction));
	}
	
	/**
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除函数")
	public R<String> doDelete(@ApiParam(value = "主键", required = true) @RequestParam String ids){
		return R.status(cubeDatasouceFunctionService.removeByIds(Func.toIntList(ids)));
	}
	
	/**
	 * 
	 * @return
	 */
	@GetMapping("/funcAttrList")
	@ApiOperation(value = "函数属性列表")
	public R<List<Kv>> funcAttrList() {
		List<Kv> dataList = new ArrayList<>();
		for(FunctionAttrEnum funcAttr : FunctionAttrEnum.values()) {
			dataList.add(Kv.create().set("value", funcAttr.getValue()).set("name", funcAttr.name()).set("message", funcAttr.getMessage()));
		}
		return R.data(dataList);
	}
}
