package com.sa.ibiz.demo.mob.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.XMService;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import com.sa.ibiz.demo.demo.domain.XM;
import com.sa.ibiz.demo.demo.service.dto.XMSearchFilter;
import com.sa.ibiz.demo.mob.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("/mob/demo/xm")
public class XMController{



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



	@PostMapping(value="mobformeditform/update")
	public ResponseEntity<XM_EditForm_MobForm> mobFormEditFormUpdate(@Validated @RequestBody XM_EditForm_MobForm form){
            XM entity =form.toXM();
            getService().update(entity);
            form.fromXM(entity);
            form.setSrfuf("1");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="mobformeditform/remove")
	public ResponseEntity<XM_EditForm_MobForm> mobFormEditFormRemove(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            XM_EditForm_MobForm form=new XM_EditForm_MobForm();
            XM entity = new XM();
            entity.setXmid(srfkey);
            getService().remove(entity);
            form.setSrfuf("0");
            form.fromXM(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="mobformeditform/getdraft")
	public ResponseEntity<XM_EditForm_MobForm> mobFormEditFormGetDraft(@RequestBody XM_EditForm_MobForm form){
        if(!StringUtils.isEmpty(form.getSrfsourcekey()))
        {
            XM sourceEntity =new XM();
            sourceEntity.setXmid(form.getSrfsourcekey());
            this.getService().get(sourceEntity);

			XM targetEntity =new XM();
			sourceEntity.copyTo(targetEntity);
			form.fromXM(targetEntity,false);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
         }
            XM entity =form.toXM();
            getService().getDraft(entity);
            form.fromXM(entity);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="mobformeditform/get")
	public ResponseEntity<XM_EditForm_MobForm> mobFormEditFormGet(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            XM_EditForm_MobForm form=new XM_EditForm_MobForm();
            XM entity = new XM();
            entity.setXmid(srfkey);
            entity=getService().get(entity);
            form.setSrfuf("1");
            form.fromXM(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="mobformeditform/create")
	public ResponseEntity<XM_EditForm_MobForm> mobFormEditFormCreate(@Validated @RequestBody XM_EditForm_MobForm form){
            XM entity =form.toXM();
            getService().create(entity);
            form.fromXM(entity);
            form.setSrfuf("1");
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="mobmobmdctrl/searchdefault")
	public ResponseEntity<Page<XM_MOBMDCTRL_Mob>> mobMOBMDCTRLSearchDefault(@Validated @RequestBody XMSearchFilter searchFilter){
			Page<XM> searchResult = this.getService().searchDefault(searchFilter);
			Page<XM_MOBMDCTRL_Mob> searchResult_vo_data =XM_MOBMDCTRL_Mob.fromXM(searchResult);
					return ResponseEntity.ok().body(searchResult_vo_data);
	}
	/**
	 * 移动端列表删除
	 * @param
	 * @return
	 */
	@PostMapping(value="mobmobmdctrl/remove")
	public ResponseEntity<XM> mobMOBMDCTRLRemove(@Validated @RequestBody Map args){
		XM entity =new XM();
		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.setXmid(srfkey);
				this.getService().remove(entity);
				}
			}
		}
		return ResponseEntity.ok().body(entity);
	}
 }