/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.form.web;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.form.entity.Column;
import com.jeeplus.modules.form.entity.Form;
import com.jeeplus.modules.form.service.FormService;
import com.jeeplus.modules.form.utils.FormTableBuilder;
import com.jeeplus.modules.sys.entity.Menu;
import com.jeeplus.modules.sys.service.MenuService;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据表单Controller
 * @author 刘高峰
 * @version 2019-12-24
 */
@RestController
@RequestMapping(value = "/form/make")
public class MakeFormController extends BaseController {

	@Autowired
	private FormService formService;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private MenuService menuService;
	@ModelAttribute
	public Form get(@RequestParam(required=false) String id) {
		Form entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = formService.get(id);
		}
		if (entity == null){
			entity = new Form();
		}
		return entity;
	}

	/**
	 * 数据表单列表数据
	 */
	@RequiresPermissions("form:make:list")
	@GetMapping("list")
	public AjaxJson list(Form form, HttpServletRequest request, HttpServletResponse response) {
		Page<Form> page = formService.findPage(new Page<Form>(request, response), form);
		return AjaxJson.success().put("page",page);
	}

	/**
	 * 根据Id获取数据表单数据
	 */
	@RequiresPermissions(value={"form:make:view","form:make:add","form:make:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(Form form) {
		return AjaxJson.success().put("form", form);
	}

	/**
	 * 保存数据表单
	 */
	@RequiresPermissions(value={"form:make:add","form:make:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(Form form, @RequestParam("fields") String fields, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(form);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		if(StringUtils.isEmpty(form.getTableName())){
			form.setTableName(FormTableBuilder.getTablePrefix() + System.currentTimeMillis() + RandomUtils.nextInt(2));
		}
		FormTableBuilder formTableBuilder = new FormTableBuilder(jdbcTemplate, form.getTableName());
		List<Column> fieldArra = JSON.parseArray(fields, Column.class);
		formTableBuilder.createTable(fieldArra);
		//新增或编辑表单保存
		if(StringUtils.isBlank(form.getVersion())){
			form.setVersion("1");
		}else{
			form.setVersion(String.valueOf(Integer.valueOf(form.getVersion())+1));
		}
		formService.save(form);//保存
		return AjaxJson.success("保存数据表单成功");
	}


	/**
	 * 批量删除数据表单
	 */
	@RequiresPermissions("form:make:del")
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			Form form = formService.get(id);
			//需要删除表
			if (StringUtils.isNotBlank(form.getTableName())) {
				String sql = "drop table if exists " + form.getTableName();
				formService.executeDeleteSql(sql);
			}
			formService.delete(form);

		}
		return AjaxJson.success("删除数据表单成功");
	}
	/*
	 *部署
	 */
	@RequiresPermissions("form:make:deploy")
	@RequestMapping("createMenu")
	public AjaxJson createMenu(@RequestParam("formId")String formId, Menu menu) {
		if (jeePlusProperites.isDemoMode()) {
			return AjaxJson.error("演示模式，不允许操作！");
		}

		// 获取排序号，最末节点排序号+30
		if (StringUtils.isBlank(menu.getId())) {
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(menu.getId())) {
				List<Menu> list = Lists.newArrayList();
				List<Menu> sourcelist = menuService.findAllMenu();
				Menu.sortList(list, sourcelist, menu.getParentId(), false);
				if (list.size() > 0) {
					menu.setSort(list.get(list.size() - 1).getSort() + 30);
				}
			}
		}

		Form form = formService.get(formId);
		formService.createMenu(form, menu);
		return AjaxJson.success("创建菜单'" + form.getName() + "'成功<br/>");
	}

}
