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.KQService;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import com.sa.ibiz.demo.demo.domain.KQ;
import com.sa.ibiz.demo.demo.service.dto.KQSearchFilter;
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/kq")
public class KQController{



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



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

			KQ targetEntity =new KQ();
			sourceEntity.copyTo(targetEntity);
			form.fromKQ(targetEntity,false);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
         }
            KQ entity =form.toKQ();
            getService().getDraft(entity);
            form.fromKQ(entity);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="maineditform/get")
	public ResponseEntity<KQ_EditForm_Main> mainEditFormGet(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            KQ_EditForm_Main form=new KQ_EditForm_Main();
            KQ entity = new KQ();
            entity.setKqid(srfkey);
            entity=getService().get(entity);
            form.setSrfuf("1");
            form.fromKQ(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="maineditform/create")
	public ResponseEntity<KQ_EditForm_Main> mainEditFormCreate(@Validated @RequestBody KQ_EditForm_Main form){
            KQ entity =form.toKQ();
            getService().create(entity);
            form.fromKQ(entity);
            form.setSrfuf("1");
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="mobmobmdctrl/searchdefault")
	public ResponseEntity<Page<KQ_MOBMDCTRL_Mob>> mobMOBMDCTRLSearchDefault(@Validated @RequestBody KQSearchFilter searchFilter){
			Page<KQ> searchResult = this.getService().searchDefault(searchFilter);
			Page<KQ_MOBMDCTRL_Mob> searchResult_vo_data =KQ_MOBMDCTRL_Mob.fromKQ(searchResult);
					return ResponseEntity.ok().body(searchResult_vo_data);
	}
	/**
	 * 移动端列表删除
	 * @param
	 * @return
	 */
	@PostMapping(value="mobmobmdctrl/remove")
	public ResponseEntity<KQ> mobMOBMDCTRLRemove(@Validated @RequestBody Map args){
		KQ entity =new KQ();
		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.setKqid(srfkey);
				this.getService().remove(entity);
				}
			}
		}
		return ResponseEntity.ok().body(entity);
	}
 }