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.OrgSectorService;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import com.sa.ibiz.demo.demo.domain.OrgSector;
import com.sa.ibiz.demo.demo.service.dto.OrgSectorSearchFilter;
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/orgsector")
public class OrgSectorController{



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



	@PostMapping(value="main2editform/update")
	public ResponseEntity<OrgSector_EditForm_Main2> main2EditFormUpdate(@Validated @RequestBody OrgSector_EditForm_Main2 form){
            OrgSector entity =form.toOrgSector();
            getService().update(entity);
            form.fromOrgSector(entity);
            form.setSrfuf("1");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="main2editform/remove")
	public ResponseEntity<OrgSector_EditForm_Main2> main2EditFormRemove(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            OrgSector_EditForm_Main2 form=new OrgSector_EditForm_Main2();
            OrgSector entity = new OrgSector();
            entity.setOrgsectorid(srfkey);
            getService().remove(entity);
            form.setSrfuf("0");
            form.fromOrgSector(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="main2editform/getdraft")
	public ResponseEntity<OrgSector_EditForm_Main2> main2EditFormGetDraft(@RequestBody OrgSector_EditForm_Main2 form){
        if(!StringUtils.isEmpty(form.getSrfsourcekey()))
        {
            OrgSector sourceEntity =new OrgSector();
            sourceEntity.setOrgsectorid(form.getSrfsourcekey());
            this.getService().get(sourceEntity);

			OrgSector targetEntity =new OrgSector();
			sourceEntity.copyTo(targetEntity);
			form.fromOrgSector(targetEntity,false);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
         }
            OrgSector entity =form.toOrgSector();
            getService().getDraft(entity);
            form.fromOrgSector(entity);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="main2editform/get")
	public ResponseEntity<OrgSector_EditForm_Main2> main2EditFormGet(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            OrgSector_EditForm_Main2 form=new OrgSector_EditForm_Main2();
            OrgSector entity = new OrgSector();
            entity.setOrgsectorid(srfkey);
            entity=getService().get(entity);
            form.setSrfuf("1");
            form.fromOrgSector(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="main2editform/create")
	public ResponseEntity<OrgSector_EditForm_Main2> main2EditFormCreate(@Validated @RequestBody OrgSector_EditForm_Main2 form){
            OrgSector entity =form.toOrgSector();
            getService().create(entity);
            form.fromOrgSector(entity);
            form.setSrfuf("1");
            return ResponseEntity.ok().body(form);
	}
	@Autowired
	private com.sa.ibiz.demo.demo.service.OrgService orgService;
	@PostMapping(value="main2editform/orgname/ac")
	public ResponseEntity<List<AutoCompleteItem>> main2EditForm_orgname_ac(@RequestBody OrgSector_EditForm_Main2 form){
		List<AutoCompleteItem> list =new ArrayList<>();
		com.sa.ibiz.demo.demo.service.dto.OrgSearchFilter searchFilter = new com.sa.ibiz.demo.demo.service.dto.OrgSearchFilter();
		OrgSector entity =form.toOrgSector();
		String queryField=entity.getOrgname();
		searchFilter.getSelectCond().like("orgname",queryField);
		Page<com.sa.ibiz.demo.demo.domain.Org> searchResult = orgService.searchDefault(searchFilter);
		for(com.sa.ibiz.demo.demo.domain.Org entity2 :searchResult.getRecords()) {
			AutoCompleteItem acItem =new AutoCompleteItem();
			acItem.setValue(entity2.getOrgid());
			acItem.setText(entity2.getOrgname());
			acItem.setRealtext(entity2.getOrgname());
			list.add(acItem);
			}
		return ResponseEntity.ok().body(list);
	}
	@PostMapping(value="main2editform/porgsectorname/ac")
	public ResponseEntity<List<AutoCompleteItem>> main2EditForm_porgsectorname_ac(@RequestBody OrgSector_EditForm_Main2 form){
		List<AutoCompleteItem> list =new ArrayList<>();
		com.sa.ibiz.demo.demo.service.dto.OrgSectorSearchFilter searchFilter = new com.sa.ibiz.demo.demo.service.dto.OrgSectorSearchFilter();
		OrgSector entity =form.toOrgSector();
		String queryField=entity.getPorgsectorname();
		searchFilter.getSelectCond().like("orgsectorname",queryField);
		//OrgSectorSearchFilter.ActiveData activeData = new OrgSectorSearchFilter.ActiveData();
		//OrgSector orgSector = toOrgSector(main2);
		//	
		//Object value0 = this.getService().getActiveDataField(orgSector,"orgid","ORGSECTOR","ORGID","main2editform_porgsectorname");
		//if(value0!=null){
		//	BeanUtils.setProperty(activeData,"orgid",value0);
		//}
		//	
		//searchFilter.setActivedata(activeData);
		Page<com.sa.ibiz.demo.demo.domain.OrgSector> searchResult = orgsectorService.searchCurOrg(searchFilter);
		for(com.sa.ibiz.demo.demo.domain.OrgSector entity2 :searchResult.getRecords()) {
			AutoCompleteItem acItem =new AutoCompleteItem();
			acItem.setValue(entity2.getOrgsectorid());
			acItem.setText(entity2.getOrgsectorname());
			acItem.setRealtext(entity2.getOrgsectorname());
			list.add(acItem);
			}
		return ResponseEntity.ok().body(list);
	}
	@PostMapping(value="main2editform/reporgsectorname/ac")
	public ResponseEntity<List<AutoCompleteItem>> main2EditForm_reporgsectorname_ac(@RequestBody OrgSector_EditForm_Main2 form){
		List<AutoCompleteItem> list =new ArrayList<>();
		com.sa.ibiz.demo.demo.service.dto.OrgSectorSearchFilter searchFilter = new com.sa.ibiz.demo.demo.service.dto.OrgSectorSearchFilter();
		OrgSector entity =form.toOrgSector();
		String queryField=entity.getReporgsectorname();
		searchFilter.getSelectCond().like("orgsectorname",queryField);
		Page<com.sa.ibiz.demo.demo.domain.OrgSector> searchResult = orgsectorService.searchDefault(searchFilter);
		for(com.sa.ibiz.demo.demo.domain.OrgSector entity2 :searchResult.getRecords()) {
			AutoCompleteItem acItem =new AutoCompleteItem();
			acItem.setValue(entity2.getOrgsectorid());
			acItem.setText(entity2.getOrgsectorname());
			acItem.setRealtext(entity2.getOrgsectorname());
			list.add(acItem);
			}
		return ResponseEntity.ok().body(list);
	}
	@PostMapping(value="main2grid/update")
	public OrgSector main2GridUpdate(@Validated OrgSector main2Item){
		return main2Item;
	}
	@PostMapping(value="main2grid/remove")
	public ResponseEntity<OrgSector> main2GridRemove(@Validated @RequestBody Map args){
	    OrgSector entity =new OrgSector();
	    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.setOrgsectorid(srfkey);
				this.getService().remove(entity);
                }
            }
        }
		return ResponseEntity.ok().body(entity);
	}
	public OrgSector main2GridGet(String orgsectorid){
		return null;
	}
	@PostMapping(value="main2grid/getdraft")
	public OrgSector main2GridGetDraft(@Validated OrgSector main2Item){
		return main2Item;
	}
	@PostMapping(value="main2grid/create")
	public OrgSector main2GridCreate(@Validated OrgSector 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 OrgSectorSearchFilter searchFilter) throws IOException, jxl.write.WriteException {
		String fileid=com.baomidou.mybatisplus.core.toolkit.IdWorker.get32UUID();
		String localPath=ExportDataInit(fileid);//输出文件相对路径
		Page<OrgSector> searchResult = this.getService().searchDefault(searchFilter);//1.查询表格数据
		List<Map<String,Object>> datas=OrgSector_Grid_Main2.pageToListDatas(searchResult);//2.将数据转换成list
		List<Map<String,String>> colnums=OrgSector_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=OrgSector_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<OrgSector_Grid_Main2>> main2GridSearchDefault(@Validated @RequestBody OrgSectorSearchFilter searchFilter){
			Page<OrgSector> searchResult = this.getService().searchDefault(searchFilter);
			Page<OrgSector_Grid_Main2> searchResult_vo_data =OrgSector_Grid_Main2.fromOrgSector(searchResult);
					return ResponseEntity.ok().body(searchResult_vo_data);
	}
	/**
	 * [main2]表格数据导出
	 * @param searchFilter
	 * @return
	 * @throws
	 * @throws
	 */
	@PostMapping(value="main2grid/exportdata/searchcurorg")
	public ResponseEntity<Page<JSONObject>> main2gridExportDataSearchCurOrg(@Validated @RequestBody OrgSectorSearchFilter searchFilter) throws IOException, jxl.write.WriteException {
		String fileid=com.baomidou.mybatisplus.core.toolkit.IdWorker.get32UUID();
		String localPath=ExportDataInit(fileid);//输出文件相对路径
		Page<OrgSector> searchResult = this.getService().searchCurOrg(searchFilter);//1.查询表格数据
		List<Map<String,Object>> datas=OrgSector_Grid_Main2.pageToListDatas(searchResult);//2.将数据转换成list
		List<Map<String,String>> colnums=OrgSector_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=OrgSector_Grid_Main2.getResultPage(searchResult,strDownloadUrl);//7.获取输出对象
			return ResponseEntity.ok().body(resultObj);
	}
	@PostMapping(value="main2grid/searchcurorg")
	public ResponseEntity<Page<OrgSector_Grid_Main2>> main2GridSearchCurOrg(@Validated @RequestBody OrgSectorSearchFilter searchFilter){
			Page<OrgSector> searchResult = this.getService().searchCurOrg(searchFilter);
			Page<OrgSector_Grid_Main2> searchResult_vo_data =OrgSector_Grid_Main2.fromOrgSector(searchResult);
					return ResponseEntity.ok().body(searchResult_vo_data);
	}
	@PostMapping(value="defaultsearchform/load")
	public void defaultSearchFormLoad(){

	}
   	@PostMapping(value="defaultsearchform/loaddraft")
	public ResponseEntity<OrgSector_SearchForm_Default> defaultSearchFormLoadDraft(@Validated @RequestBody OrgSector_SearchForm_Default searchform){
 		 
			OrgSectorSearchFilter searchfilter =searchform.toOrgSectorSearchFilter();
			searchform.fromOrgSectorSearchFilter(searchfilter);
			return ResponseEntity.ok().body(searchform);
	} 
	@PostMapping(value="defaultsearchform/search")
	public void defaultSearchFormSearch(){
			 
	} 
 }