package org.springblade.flow.engine.controller;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import liquibase.pro.packaged.J;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.flow.engine.entity.ActHistoricActivityInstance;
import org.springblade.flow.engine.entity.FlowCate;
import org.springblade.flow.engine.entity.FlowEngineEntity;
import org.springblade.flow.engine.model.flowengine.FlowCateParam;
import org.springblade.flow.engine.service.FlowCateService;
import org.springblade.flow.engine.service.FlowEngineService;
import org.springblade.flow.util.JsonUtil;

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.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

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

/**
 * 
 * @author William
 * @date 2023年2月17日 上午11:19:18
 */
@Api(tags = "工作流分类", value = "FlowCate")
@RestController
@RequestMapping("/flow/cate")
public class FlowCateController {

	@Autowired
	private FlowCateService cateService;

	@Autowired
	private FlowEngineService flowEngineService;

	@GetMapping("/list")
	@ApiOperation(value = "列表查询")
	public R<List<FlowCate>> list() {
		return R.data(cateService.list());
	}

	@GetMapping("/page")
	@ApiOperation(value = "分页查询")
	public R<IPage<FlowCate>> page(Integer current, Integer size) {
		LambdaQueryWrapper<FlowCate> wrapper = Wrappers.<FlowCate>lambdaQuery();
		wrapper.orderByDesc(FlowCate::getId);
		Query query = new Query();
		query.setCurrent(current);
		query.setSize(size);
		return R.data(cateService.page(Condition.getPage(query), wrapper));
	}

	@ApiOperation("增加")
	@PostMapping("/add")
	public R<String> add(@RequestBody @Valid FlowCateParam param) {
		if (cateService.exist(param.getName(),SecureUtil.getTenantId())) {
			return R.fail("名称不可重复");
		}
		FlowCate data = JsonUtil.getJsonToBean(param, FlowCate.class);
		data.setCode(IdUtil.fastSimpleUUID());
		data.setCreateTime(new Date());
		data.setCreator(SecureUtil.getNickName());
		data.setTenantId(SecureUtil.getTenantId());
		return R.status(cateService.save(data));
	}

	@ApiOperation("更新")
	@PostMapping("/update")
	public R<String> update(@RequestBody @Valid FlowCateParam param) {
		FlowCate old = cateService.getById(param.getId());
		if (old == null) {
			return R.fail("数据不存在");
		}
		if (cateService.exist(param.getName(),SecureUtil.getTenantId()) && !param.getName().equalsIgnoreCase(old.getName())) {
			return R.fail("名称不可重复");
		}
		FlowCate data = JsonUtil.getJsonToBean(param, FlowCate.class);
		data.setCode(old.getCode());
		data.setModifier(SecureUtil.getNickName());
		data.setTenantId(SecureUtil.getTenantId());
		data.setModifierId(String.valueOf(SecureUtil.getUserId()));
		data.setUpdateTime(new Date());
		return R.status(cateService.updateById(data));
	}

	@ApiOperation("详情")
	@GetMapping("/info")
	public R<FlowCate> info(String id) {
		return R.data(cateService.getById(id));
	}

	@ApiOperation("删除")
	@GetMapping("/delete")
	public R<String> delete(String id) {
		return R.status(cateService.removeById(id));
	}


	@GetMapping("/flowList")
	@ApiOperation(value = "列表查询")
	public R flowList() {
		JSONArray jsonArray =new JSONArray();
		List<FlowCate> flowCateList = cateService.list();
		for (FlowCate flowCate : flowCateList){
			JSONObject data =new JSONObject();
			data.put("id",flowCate.getId());
			data.put("name",flowCate.getName());
			QueryWrapper<FlowEngineEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(FlowEngineEntity::getCateId,flowCate.getId());
			List<FlowEngineEntity> engineEntities = flowEngineService.list(queryWrapper);
			JSONArray engineArray =new JSONArray();
			for (FlowEngineEntity flowEngineEntity : engineEntities){
				JSONObject engine =new JSONObject();
				engine.put("id",flowEngineEntity.getId());
				engine.put("name",flowEngineEntity.getFullName());
				engineArray.add(engine);
			}
			data.put("children", engineArray);
			jsonArray.add(data);
		}
		return R.data(jsonArray);
	}
}
