1d4dc8388fea6e8965aaab15ec6046f343399db3.svn-base 5.62 KB
/**
 * Copyright &copy; 2015-2018 <a href="#">J-Site</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.reg.web.base;

import java.io.IOException;
import java.net.InetAddress;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.ClientProtocolException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.pudp.bae.service.MemberService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.reg.entity.base.DmlRecords;
import com.thinkgem.jeesite.modules.reg.service.base.DmlRecordsService;
import com.thinkgem.jeesite.modules.reg.web.httprest.httpRestService.HttpactivityService;

/**
 * 数据库dml记录Controller
 * @author hsg
 * @version 2019-11-07
 */
@Controller
@RequestMapping(value = "${adminPath}/reg/base/dmlRecords")
public class DmlRecordsController extends BaseController {

	@Autowired
	private DmlRecordsService dmlRecordsService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private HttpactivityService httpactivityService;
	
	@ModelAttribute
	public DmlRecords get(@RequestParam(required=false) String id) {
		DmlRecords entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dmlRecordsService.get(id);
		}
		if (entity == null){
			entity = new DmlRecords();
		}
		return entity;
	}
	
	@RequiresPermissions("reg:base:dmlRecords:view")
	@RequestMapping(value = {"list", ""})
	public String list(DmlRecords dmlRecords, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DmlRecords> page = dmlRecordsService.findPage(new Page<DmlRecords>(request, response), dmlRecords); 
		model.addAttribute("page", page);
		return "modules/reg/base/dmlRecordsList";
	}

	@RequiresPermissions("reg:base:dmlRecords:view")
	@RequestMapping(value = "form")
	public String form(DmlRecords dmlRecords, Model model) {
		model.addAttribute("dmlRecords", dmlRecords);
		return "modules/reg/base/dmlRecordsForm";
	}

	@RequiresPermissions("reg:base:dmlRecords:edit")
	@RequestMapping(value = "save")
	public String save(DmlRecords dmlRecords, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dmlRecords)){
			return form(dmlRecords, model);
		}
		dmlRecordsService.save(dmlRecords);
		addMessage(redirectAttributes, "保存数据库dml记录成功");
		return "redirect:"+Global.getAdminPath()+"/reg/base/dmlRecords/?repage";
	}
	
	@RequiresPermissions("reg:base:dmlRecords:edit")
	@RequestMapping(value = "delete")
	public String delete(DmlRecords dmlRecords, RedirectAttributes redirectAttributes) {
		dmlRecordsService.delete(dmlRecords);
		addMessage(redirectAttributes, "删除数据库dml记录成功");
		return "redirect:"+Global.getAdminPath()+"/reg/base/dmlRecords/?repage";
	}

	/**
	 * redis批量同步
	 * @throws SQLException
	 * @throws IOException
	 */
	public void selRedis() throws SQLException, IOException {
		int rowNum = updRedis(Integer.valueOf(Global.getsyncRowNum()));
	}

	private int updRedis(int rowNum) throws SQLException, IOException {
		DmlRecords dmlRecords = new DmlRecords();
//		dmlRecords.setTablename("REG_BASE_ZRZ");
//		dmlRecords.setDmllx("insert");
		dmlRecords.setRowNum(rowNum);
		List<DmlRecords> findList = dmlRecordsService.findList(dmlRecords);
		if(findList.size() > 0) {
			for (int i = 0; i < findList.size(); i++) {
				dmlRecords = findList.get(i);
				//根据dml操作记录保存redis
				String success = memberService.selectDmlRecords(dmlRecords);
				if("success".equals(success)) {
					dmlRecords.setSyncbz("1");
					dmlRecords.setSyncmsg("同步redis成功");
				}else {
					dmlRecords.setSyncbz("2");
					dmlRecords.setSyncmsg(success);
				}
				dmlRecords.setSyncdate(new Date());
				dmlRecords.setSyncby("root");
				InetAddress addr = InetAddress.getLocalHost();
				dmlRecords.setSyncip(addr.getHostAddress());
				dmlRecordsService.update1(dmlRecords);
			}
			return updRedis(rowNum);
		}else {
			return 1;
		}
	}
	
	/**
	 * 汇总操作记录复核
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public void pushDmlRecService() throws ClientProtocolException, IOException {
		int rowNum = fhDmlRec(Integer.valueOf(Global.getfhRowNum()));
	}

	private int fhDmlRec(Integer rowNum) throws ClientProtocolException, IOException {
		DmlRecords dmlRecords = new DmlRecords();
		dmlRecords.setRowNum(rowNum);
		String ids = dmlRecordsService.fhList(dmlRecords);
		if(StringUtils.isNotBlank(ids)) {
			Map data = Maps.newHashMap();
			data.put("ids", ids);
			Map map = httpactivityService.hzpostRequest(data);
			String successData = map.get("successData").toString();
			String failData = map.get("failData").toString();
			if(StringUtils.isNotBlank(successData)) {
				dmlRecords.setId(successData);
				dmlRecordsService.update2(dmlRecords);
			}
			if(StringUtils.isNotBlank(failData)) {
				dmlRecords.setId(failData);
				dmlRecordsService.update3(dmlRecords);
			}
			return fhDmlRec(rowNum);
		}else {
			return 1;
		}
	}
}