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.UserRoleService;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import com.sa.ibiz.demo.demo.domain.UserRole;
import com.sa.ibiz.demo.demo.service.dto.UserRoleSearchFilter;
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/userrole")
public class UserRoleController{



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



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

			UserRole targetEntity =new UserRole();
			sourceEntity.copyTo(targetEntity);
			form.fromUserRole(targetEntity,false);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
         }
            UserRole entity =form.toUserRole();
            getService().getDraft(entity);
            form.fromUserRole(entity);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="maineditform/get")
	public ResponseEntity<UserRole_EditForm_Main> mainEditFormGet(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            UserRole_EditForm_Main form=new UserRole_EditForm_Main();
            UserRole entity = new UserRole();
            entity.setUserroleid(srfkey);
            entity=getService().get(entity);
            form.setSrfuf("1");
            form.fromUserRole(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="maineditform/create")
	public ResponseEntity<UserRole_EditForm_Main> mainEditFormCreate(@Validated @RequestBody UserRole_EditForm_Main form){
            UserRole entity =form.toUserRole();
            getService().create(entity);
            form.fromUserRole(entity);
            form.setSrfuf("1");
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="main2grid/update")
	public UserRole main2GridUpdate(@Validated UserRole main2Item){
		return main2Item;
	}
	@PostMapping(value="main2grid/remove")
	public ResponseEntity<UserRole> main2GridRemove(@Validated @RequestBody Map args){
	    UserRole entity =new UserRole();
	    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.setUserroleid(srfkey);
				this.getService().remove(entity);
                }
            }
        }
		return ResponseEntity.ok().body(entity);
	}
	public UserRole main2GridGet(String userroleid){
		return null;
	}
	@PostMapping(value="main2grid/getdraft")
	public UserRole main2GridGetDraft(@Validated UserRole main2Item){
		return main2Item;
	}
	@PostMapping(value="main2grid/create")
	public UserRole main2GridCreate(@Validated UserRole main2Item){
		return main2Item;
	}
	@org.springframework.beans.factory.annotation.Value("${ibiz.filePath}")
    private String strFilePath;
	@Autowired
    private com.sa.ibiz.demo.ibizutil.service.IBZFILEService ibzfileService;
	/**
	 * [main2]表格数据导出
	 * @param searchFilter
	 * @return
	 * @throws
	 * @throws
	 */
	@PostMapping(value="main2grid/exportdata/searchdefault")
	public ResponseEntity<Page<JSONObject>> main2gridExportDataSearchDefault(@Validated @RequestBody UserRoleSearchFilter searchFilter) throws IOException, jxl.write.WriteException {
		String fileid=com.baomidou.mybatisplus.core.toolkit.IdWorker.get32UUID();
		String localPath=ExportDataInit(fileid);//输出文件相对路径
		Page<UserRole> searchResult = this.getService().searchDefault(searchFilter);//1.查询表格数据
		List<Map<String,Object>> datas=UserRole_Grid_Main2.pageToListDatas(searchResult);//2.将数据转换成list
		List<Map<String,String>> colnums=UserRole_Grid_Main2.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=UserRole_Grid_Main2.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="main2grid/searchdefault")
	public ResponseEntity<Page<UserRole_Grid_Main2>> main2GridSearchDefault(@Validated @RequestBody UserRoleSearchFilter searchFilter){
			Page<UserRole> searchResult = this.getService().searchDefault(searchFilter);
			Page<UserRole_Grid_Main2> searchResult_vo_data =UserRole_Grid_Main2.fromUserRole(searchResult);
					return ResponseEntity.ok().body(searchResult_vo_data);
	}
	@PostMapping(value="defaultsearchform/load")
	public void defaultSearchFormLoad(){

	}
   	@PostMapping(value="defaultsearchform/loaddraft")
	public ResponseEntity<UserRole_SearchForm_Default> defaultSearchFormLoadDraft(@Validated @RequestBody UserRole_SearchForm_Default searchform){
 		 
			UserRoleSearchFilter searchfilter =searchform.toUserRoleSearchFilter();
			searchform.fromUserRoleSearchFilter(searchfilter);
			return ResponseEntity.ok().body(searchform);
	} 
	@PostMapping(value="defaultsearchform/search")
	public void defaultSearchFormSearch(){
			 
	} 
 }