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.HTService;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import com.sa.ibiz.demo.demo.domain.HT;
import com.sa.ibiz.demo.demo.service.dto.HTSearchFilter;
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/ht")
public class HTController{



    @Autowired
	private HTService htService;
	/**
	 * 获取服务对象
	*/
	protected HTService getService(){
		return this.htService;
	}
 /**
 * 表单项更新
 * @param form vo对象
 * @return
 */
@PostMapping(value="mobformeditform/updateformitem/getxminfo")
public ResponseEntity<JSONObject> mobFormEditForm_updateformitem_getXminfo(@RequestBody HT_EditForm_MobForm form){
		HT entity=form.toHT();
		this.getService().getXminfo(entity);
		form.fromHT(entity);
        return ResponseEntity.ok().body(form.getxminfoFormItemUpdateFilter());//VO值过滤,只输出表单项更新的明细项
}				



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

			HT targetEntity =new HT();
			sourceEntity.copyTo(targetEntity);
			form.fromHT(targetEntity,false);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
         }
            HT entity =form.toHT();
            getService().getDraft(entity);
            form.fromHT(entity);
            form.setSrfuf("0");
            return ResponseEntity.ok().body(form);
	}
	@GetMapping(value="mobformeditform/get")
	public ResponseEntity<HT_EditForm_MobForm> mobFormEditFormGet(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey")String srfkey){
            HT_EditForm_MobForm form=new HT_EditForm_MobForm();
            HT entity = new HT();
            entity.setHtid(srfkey);
            entity=getService().get(entity);
            form.setSrfuf("1");
            form.fromHT(entity);
            return ResponseEntity.ok().body(form);
	}
	@PostMapping(value="mobformeditform/create")
	public ResponseEntity<HT_EditForm_MobForm> mobFormEditFormCreate(@Validated @RequestBody HT_EditForm_MobForm form){
            HT entity =form.toHT();
            getService().create(entity);
            form.fromHT(entity);
            form.setSrfuf("1");
            return ResponseEntity.ok().body(form);
	}
	@Autowired
	private com.sa.ibiz.demo.demo.service.XMService xmService;
	@PostMapping(value="mobformeditform/xmname/ac")
	public ResponseEntity<List<AutoCompleteItem>> mobFormEditForm_xmname_ac(@RequestBody HT_EditForm_MobForm form){
		List<AutoCompleteItem> list =new ArrayList<>();
		com.sa.ibiz.demo.demo.service.dto.XMSearchFilter searchFilter = new com.sa.ibiz.demo.demo.service.dto.XMSearchFilter();
		HT entity =form.toHT();
		String queryField=entity.getXmname();
		searchFilter.getSelectCond().like("xmname",queryField);
		Page<com.sa.ibiz.demo.demo.domain.XM> searchResult = xmService.searchDefault(searchFilter);
		for(com.sa.ibiz.demo.demo.domain.XM entity2 :searchResult.getRecords()) {
			AutoCompleteItem acItem =new AutoCompleteItem();
			acItem.setValue(entity2.getXmid());
			acItem.setText(entity2.getXmname());
			acItem.setRealtext(entity2.getXmname());
			list.add(acItem);
			}
		return ResponseEntity.ok().body(list);
	}
	@PostMapping(value="mobmobmdctrl/searchdefault")
	public ResponseEntity<Page<HT_MOBMDCTRL_Mob>> mobMOBMDCTRLSearchDefault(@Validated @RequestBody HTSearchFilter searchFilter){
			Page<HT> searchResult = this.getService().searchDefault(searchFilter);
			Page<HT_MOBMDCTRL_Mob> searchResult_vo_data =HT_MOBMDCTRL_Mob.fromHT(searchResult);
					return ResponseEntity.ok().body(searchResult_vo_data);
	}
	/**
	 * 移动端列表删除
	 * @param
	 * @return
	 */
	@PostMapping(value="mobmobmdctrl/remove")
	public ResponseEntity<HT> mobMOBMDCTRLRemove(@Validated @RequestBody Map args){
		HT entity =new HT();
		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.setHtid(srfkey);
				this.getService().remove(entity);
				}
			}
		}
		return ResponseEntity.ok().body(entity);
	}
 }