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.LoginAccountService;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import com.sa.ibiz.demo.demo.domain.LoginAccount;
import com.sa.ibiz.demo.demo.service.dto.LoginAccountSearchFilter;
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/loginaccount")
public class LoginAccountController{



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



	@PostMapping(value="maineditform/savehashmode")
	public ResponseEntity<LoginAccount_EditForm_Main> mainEditFormSaveHashMode(@RequestBody LoginAccount_EditForm_Main form){
            LoginAccount entity =form.toLoginAccount();
            getService().saveHashMode(entity);
            form.fromLoginAccount(entity);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="maineditform/remove")
	public ResponseEntity<LoginAccount_EditForm_Main> mainEditFormRemove(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            LoginAccount_EditForm_Main form=new LoginAccount_EditForm_Main();
            LoginAccount entity = new LoginAccount();
            entity.setLoginaccountid(srfkey);
            getService().remove(entity);
            form.setSrfuf("0");
            form.fromLoginAccount(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="maineditform/getdraft")
	public ResponseEntity<LoginAccount_EditForm_Main> mainEditFormGetDraft(@RequestBody LoginAccount_EditForm_Main form){
        if(!StringUtils.isEmpty(form.getSrfsourcekey()))
        {
            LoginAccount sourceEntity =new LoginAccount();
            sourceEntity.setLoginaccountid(form.getSrfsourcekey());
            this.getService().get(sourceEntity);

			LoginAccount targetEntity =new LoginAccount();
			sourceEntity.copyTo(targetEntity);
			form.fromLoginAccount(targetEntity,false);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
         }
            LoginAccount entity =form.toLoginAccount();
            getService().getDraft(entity);
            form.fromLoginAccount(entity);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="maineditform/gethashmode")
	public ResponseEntity<LoginAccount_EditForm_Main> mainEditFormGetHashMode(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            LoginAccount_EditForm_Main form=new LoginAccount_EditForm_Main();
            LoginAccount entity = new LoginAccount();
            entity.setLoginaccountid(srfkey);
            entity=getService().get(entity);
            form.setSrfuf("1");
            form.fromLoginAccount(entity);
            return ResponseEntity.ok().body(form);
	}
	@Autowired
	private com.sa.ibiz.demo.demo.service.UserService userService;
	@PostMapping(value="maineditform/username/ac")
	public ResponseEntity<List<AutoCompleteItem>> mainEditForm_username_ac(@RequestBody LoginAccount_EditForm_Main form){
		List<AutoCompleteItem> list =new ArrayList<>();
		com.sa.ibiz.demo.demo.service.dto.UserSearchFilter searchFilter = new com.sa.ibiz.demo.demo.service.dto.UserSearchFilter();
		LoginAccount entity =form.toLoginAccount();
		String queryField=entity.getUsername();
		searchFilter.getSelectCond().like("username",queryField);
		Page<com.sa.ibiz.demo.demo.domain.User> searchResult = userService.searchDefault(searchFilter);
		for(com.sa.ibiz.demo.demo.domain.User entity2 :searchResult.getRecords()) {
			AutoCompleteItem acItem =new AutoCompleteItem();
			acItem.setValue(entity2.getUserid());
			acItem.setText(entity2.getUsername());
			acItem.setRealtext(entity2.getUsername());
			list.add(acItem);
			}
		return ResponseEntity.ok().body(list);
	}
	@PostMapping(value="maingrid/update")
	public LoginAccount mainGridUpdate(@Validated LoginAccount mainItem){
		return mainItem;
	}
	@PostMapping(value="maingrid/remove")
	public ResponseEntity<LoginAccount> mainGridRemove(@Validated @RequestBody Map args){
	    LoginAccount entity =new LoginAccount();
	    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.setLoginaccountid(srfkey);
				this.getService().remove(entity);
                }
            }
        }
		return ResponseEntity.ok().body(entity);
	}
	public LoginAccount mainGridGet(String loginaccountid){
		return null;
	}
	@PostMapping(value="maingrid/getdraft")
	public LoginAccount mainGridGetDraft(@Validated LoginAccount mainItem){
		return mainItem;
	}
	@PostMapping(value="maingrid/create")
	public LoginAccount mainGridCreate(@Validated LoginAccount 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 LoginAccountSearchFilter searchFilter) throws IOException, jxl.write.WriteException {
		String fileid=com.baomidou.mybatisplus.core.toolkit.IdWorker.get32UUID();
		String localPath=ExportDataInit(fileid);//输出文件相对路径
		Page<LoginAccount> searchResult = this.getService().searchDefault(searchFilter);//1.查询表格数据
		List<Map<String,Object>> datas=LoginAccount_Grid_Main.pageToListDatas(searchResult);//2.将数据转换成list
		List<Map<String,String>> colnums=LoginAccount_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=LoginAccount_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<LoginAccount_Grid_Main>> mainGridSearchDefault(@Validated @RequestBody LoginAccountSearchFilter searchFilter){
			Page<LoginAccount> searchResult = this.getService().searchDefault(searchFilter);
			Page<LoginAccount_Grid_Main> searchResult_vo_data =LoginAccount_Grid_Main.fromLoginAccount(searchResult);
					return ResponseEntity.ok().body(searchResult_vo_data);
	}
	@PostMapping(value="defaultsearchform/load")
	public void defaultSearchFormLoad(){

	}
   	@PostMapping(value="defaultsearchform/loaddraft")
	public ResponseEntity<LoginAccount_SearchForm_Default> defaultSearchFormLoadDraft(@Validated @RequestBody LoginAccount_SearchForm_Default searchform){
 		 
			LoginAccountSearchFilter searchfilter =searchform.toLoginAccountSearchFilter();
			searchform.fromLoginAccountSearchFilter(searchfilter);
			return ResponseEntity.ok().body(searchform);
	} 
	@PostMapping(value="defaultsearchform/search")
	public void defaultSearchFormSearch(){
			 
	} 
 }