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

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.extension.entity.FormSetting;
import com.jeeplus.modules.extension.service.FormSettingService;

/**
 * 表单只读配置Controller
 * @author 刘高峰
 * @version 2019-10-17
 */
@RestController
@RequestMapping("/extension/formSetting")
public class FormSettingController extends BaseController {

	@Autowired
	private FormSettingService formSettingService;


	/**
	 * 表单只读配置列表数据
	 */
	@GetMapping("list")
	public AjaxJson list(FormSetting formSetting, HttpServletRequest request, HttpServletResponse response) {
		Page<FormSetting> page = formSettingService.findPage(new Page<FormSetting>(request, response), formSetting);
		return AjaxJson.success().put("page",page);
	}

	/**
	 * 查看，增加，编辑表单只读配置表单页面
	 * params:
	 * 	mode: add, edit, view, 代表三种模式的页面
	 */
	@GetMapping("queryByDefIdAndTaskId")
	public AjaxJson queryById(String processDefId, String taskDefId) {

		FormSetting formSetting =formSettingService.queryByDefIdAndTaskId(processDefId, taskDefId);
		return AjaxJson.success().put("formSetting", formSetting);
	}

	/**
	 * 保存表单只读配置
	 */
	@PostMapping("save")
	public AjaxJson save(@RequestBody List<FormSetting> formSettingList, Model model) throws Exception{

		for(FormSetting formSetting: formSettingList){
			formSettingService.deleteByDefIdAndTaskId(formSetting);//删除旧的
			//新增或编辑表单保存
			formSettingService.save(formSetting);//保存
		}
		return AjaxJson.success("保存表单只读配置成功");
	}


	/**
	 * 批量删除表单只读配置
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			formSettingService.delete(formSettingService.get(id));
		}
		return AjaxJson.success("删除表单只读配置成功");
	}


}
