package com.sa.ibiz.demo.demoapp.controller.demo;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.Timestamp;
import javax.annotation.Resource;
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.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import com.sa.ibiz.demo.ibizutil.domain.ActionResult;
import com.sa.ibiz.demo.demo.service.UserRoleDatasService;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import com.sa.ibiz.demo.demo.domain.UserRoleDatas;
import com.sa.ibiz.demo.demo.service.dto.UserRoleDatasSearchFilter;
import com.sa.ibiz.demo.demoapp.controller.demo.vo.*;
import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.util.StringUtils;
import java.io.IOException;
import com.sa.ibiz.demo.ibizutil.errors.BadRequestAlertException;
import org.springframework.validation.annotation.Validated;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import com.sa.ibiz.demo.ibizutil.domain.RedirectResult;
import javax.validation.constraints.NotBlank;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import java.math.BigInteger;
import com.alibaba.fastjson.JSONObject;

@RestController
@RequestMapping("/demoapp/demo/userroledatas")
public class UserRoleDatasController{



    @Autowired
	private UserRoleDatasService userroledatasService;
	/**
	 * 获取服务对象
	*/
	protected UserRoleDatasService getService(){
		return this.userroledatasService;
	}



	@PostMapping(value="maineditform/update")
	public ResponseEntity<UserRoleDatas_EditForm_Main> mainEditFormUpdate(@Validated @RequestBody UserRoleDatas_EditForm_Main form){
            UserRoleDatas entity =form.toUserRoleDatas();
            getService().update(entity);
            form.fromUserRoleDatas(entity);
            form.setSrfuf("1");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="maineditform/remove")
	public ResponseEntity<UserRoleDatas_EditForm_Main> mainEditFormRemove(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            UserRoleDatas_EditForm_Main form=new UserRoleDatas_EditForm_Main();
            UserRoleDatas entity = new UserRoleDatas();
            entity.setUserroledatasid(srfkey);
            getService().remove(entity);
            form.setSrfuf("0");
            form.fromUserRoleDatas(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="maineditform/getdraft")
	public ResponseEntity<UserRoleDatas_EditForm_Main> mainEditFormGetDraft(@RequestBody UserRoleDatas_EditForm_Main form){
        if(!StringUtils.isEmpty(form.getSrfsourcekey()))
        {
            UserRoleDatas sourceEntity =new UserRoleDatas();
            sourceEntity.setUserroledatasid(form.getSrfsourcekey());
            this.getService().get(sourceEntity);

			UserRoleDatas targetEntity =new UserRoleDatas();
			sourceEntity.copyTo(targetEntity);
			form.fromUserRoleDatas(targetEntity,false);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
         }
            UserRoleDatas entity =form.toUserRoleDatas();
            getService().getDraft(entity);
            form.fromUserRoleDatas(entity);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="maineditform/get")
	public ResponseEntity<UserRoleDatas_EditForm_Main> mainEditFormGet(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            UserRoleDatas_EditForm_Main form=new UserRoleDatas_EditForm_Main();
            UserRoleDatas entity = new UserRoleDatas();
            entity.setUserroledatasid(srfkey);
            entity=getService().get(entity);
            form.setSrfuf("1");
            form.fromUserRoleDatas(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="maineditform/create")
	public ResponseEntity<UserRoleDatas_EditForm_Main> mainEditFormCreate(@Validated @RequestBody UserRoleDatas_EditForm_Main form){
            UserRoleDatas entity =form.toUserRoleDatas();
            getService().create(entity);
            form.fromUserRoleDatas(entity);
            form.setSrfuf("1");
            return ResponseEntity.ok().body(form);
	}
	@Autowired
	private com.sa.ibiz.demo.demo.service.UserRoleService userroleService;
	@PostMapping(value="maineditform/userrolename/ac")
	public ResponseEntity<List<AutoCompleteItem>> mainEditForm_userrolename_ac(@RequestBody UserRoleDatas_EditForm_Main form){
		List<AutoCompleteItem> list =new ArrayList<>();
		com.sa.ibiz.demo.demo.service.dto.UserRoleSearchFilter searchFilter = new com.sa.ibiz.demo.demo.service.dto.UserRoleSearchFilter();
		UserRoleDatas entity =form.toUserRoleDatas();
		String queryField=entity.getUserrolename();
		searchFilter.getSelectCond().like("userrolename",queryField);
		Page<com.sa.ibiz.demo.demo.domain.UserRole> searchResult = userroleService.searchDefault(searchFilter);
		for(com.sa.ibiz.demo.demo.domain.UserRole entity2 :searchResult.getRecords()) {
			AutoCompleteItem acItem =new AutoCompleteItem();
			acItem.setValue(entity2.getUserroleid());
			acItem.setText(entity2.getUserrolename());
			acItem.setRealtext(entity2.getUserrolename());
			list.add(acItem);
			}
		return ResponseEntity.ok().body(list);
	}
	@Autowired
	private com.sa.ibiz.demo.demo.service.UserRoleDataService userroledataService;
	@PostMapping(value="maineditform/userroledataname/ac")
	public ResponseEntity<List<AutoCompleteItem>> mainEditForm_userroledataname_ac(@RequestBody UserRoleDatas_EditForm_Main form){
		List<AutoCompleteItem> list =new ArrayList<>();
		com.sa.ibiz.demo.demo.service.dto.UserRoleDataSearchFilter searchFilter = new com.sa.ibiz.demo.demo.service.dto.UserRoleDataSearchFilter();
		UserRoleDatas entity =form.toUserRoleDatas();
		String queryField=entity.getUserroledataname();
		searchFilter.getSelectCond().like("userroledataname",queryField);
		Page<com.sa.ibiz.demo.demo.domain.UserRoleData> searchResult = userroledataService.searchDefault(searchFilter);
		for(com.sa.ibiz.demo.demo.domain.UserRoleData entity2 :searchResult.getRecords()) {
			AutoCompleteItem acItem =new AutoCompleteItem();
			acItem.setValue(entity2.getUserroledataid());
			acItem.setText(entity2.getUserroledataname());
			acItem.setRealtext(entity2.getUserroledataname());
			list.add(acItem);
			}
		return ResponseEntity.ok().body(list);
	}
	@PostMapping(value="maingrid/update")
	public UserRoleDatas mainGridUpdate(@Validated UserRoleDatas mainItem){
		return mainItem;
	}
	@PostMapping(value="maingrid/remove")
	public ResponseEntity<UserRoleDatas> mainGridRemove(@Validated @RequestBody Map args){
	    UserRoleDatas entity =new UserRoleDatas();
	    if ( !StringUtils.isEmpty(args.get("srfkeys"))) {
			String srfkeys=args.get("srfkeys").toString();
			String srfkeyArr[] =srfkeys.split(";");
			for(String srfkey : srfkeyArr)
			{
				if(!StringUtils.isEmpty(srfkey)){
				entity.setUserroledatasid(srfkey);
				this.getService().remove(entity);
                }
            }
        }
		return ResponseEntity.ok().body(entity);
	}
	public UserRoleDatas mainGridGet(String userroledatasid){
		return null;
	}
	@PostMapping(value="maingrid/getdraft")
	public UserRoleDatas mainGridGetDraft(@Validated UserRoleDatas mainItem){
		return mainItem;
	}
	@PostMapping(value="maingrid/create")
	public UserRoleDatas mainGridCreate(@Validated UserRoleDatas mainItem){
		return mainItem;
	}
	@org.springframework.beans.factory.annotation.Value("${ibiz.filePath}")
    private String strFilePath;
	@Autowired
    private com.sa.ibiz.demo.ibizutil.service.IBZFILEService ibzfileService;
	/**
	 * [main]表格数据导出
	 * @param searchFilter
	 * @return
	 * @throws
	 * @throws
	 */
	@PostMapping(value="maingrid/exportdata/searchdefault")
	public ResponseEntity<Page<JSONObject>> maingridExportDataSearchDefault(@Validated @RequestBody UserRoleDatasSearchFilter searchFilter) throws IOException, jxl.write.WriteException {
		String fileid=com.baomidou.mybatisplus.core.toolkit.IdWorker.get32UUID();
		String localPath=ExportDataInit(fileid);//输出文件相对路径
		Page<UserRoleDatas> searchResult = this.getService().searchDefault(searchFilter);//1.查询表格数据
		List<Map<String,Object>> datas=UserRoleDatas_Grid_Main.pageToListDatas(searchResult);//2.将数据转换成list
		List<Map<String,String>> colnums=UserRoleDatas_Grid_Main.getGridColumnModels();//3.获取表格列头
        java.io.File outputFile=com.sa.ibiz.demo.ibizutil.helper.DEDataExportHelper.output(strFilePath+localPath,colnums,datas);//4.生成导出文件
		com.sa.ibiz.demo.ibizutil.helper.DEDataExportHelper.saveFileData(outputFile,localPath,fileid,ibzfileService); //5.保存file表记录
		String strDownloadUrl =String.format("ibizutil/download/"+fileid);//6.回传文件路径给前台
        Page<JSONObject> resultObj=UserRoleDatas_Grid_Main.getResultPage(searchResult,strDownloadUrl);//7.获取输出对象
			return ResponseEntity.ok().body(resultObj);
	}
	/**
	 * 表格数据导出
	 * @param fileid
	 * @return
	 */
	private String ExportDataInit(String fileid) {
		java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyyMMdd");
        String filepath=dateFormat.format(new java.util.Date())+ java.io.File.separator;
        java.text.SimpleDateFormat dateFormat2 = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
		String strTempFileName = fileid+"-"+dateFormat2.format(new java.util.Date())+".xls";
		java.io.File file =new java.io.File(strFilePath+filepath);
		if(!file.exists())
           file.mkdirs();
		return filepath+strTempFileName;
	}
	@PostMapping(value="maingrid/searchdefault")
	public ResponseEntity<Page<UserRoleDatas_Grid_Main>> mainGridSearchDefault(@Validated @RequestBody UserRoleDatasSearchFilter searchFilter){
			Page<UserRoleDatas> searchResult = this.getService().searchDefault(searchFilter);
			Page<UserRoleDatas_Grid_Main> searchResult_vo_data =UserRoleDatas_Grid_Main.fromUserRoleDatas(searchResult);
					return ResponseEntity.ok().body(searchResult_vo_data);
	}
	@PostMapping(value="defaultsearchform/load")
	public void defaultSearchFormLoad(){

	}
   	@PostMapping(value="defaultsearchform/loaddraft")
	public ResponseEntity<UserRoleDatas_SearchForm_Default> defaultSearchFormLoadDraft(@Validated @RequestBody UserRoleDatas_SearchForm_Default searchform){
 		 
			UserRoleDatasSearchFilter searchfilter =searchform.toUserRoleDatasSearchFilter();
			searchform.fromUserRoleDatasSearchFilter(searchfilter);
			return ResponseEntity.ok().body(searchform);
	} 
	@PostMapping(value="defaultsearchform/search")
	public void defaultSearchFormSearch(){
			 
	} 
 }