package com.iamberry.wechat.handles.lease;

import java.util.List;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.machine.MachineDto;
import com.iamberry.wechat.core.entity.machine.MachineShareRecord;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.home.HomeService;
import com.iamberry.wechat.face.lease.LeaseMachineService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;

@Controller
@RequestMapping("/wechat/machine")
public class LeaseMachineHandler {

	@Autowired
	private LeaseMachineService leaseMachineService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private HomeService homeService;
	@ResponseBody
	@RequestMapping("/selectMachineList")
	public ResultMsg selectMachineList(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ResultMsg msg = new ResultMsg();
		Member member = WechatUtils.getUserBySession(request); // 当前用户
		MachineDto machineDto = new MachineDto();
		machineDto.getPage().setPageNumber(pageNO);
		machineDto.getPage().setPageSize(pageSize);
		machineDto.setMachineOpenid(member.getUserOpenid());
		List<MachineDto> list = leaseMachineService.selectMachineList(machineDto);
		msg.setData(list);
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		return msg;
	}
	
	/**
	 * 添加分享记录
	 * @param request
	 * @param shareContent
	 * @return
	 */
	public ResultMsg shareRecord(HttpServletRequest request,String shareContent){
		ResultMsg msg = new ResultMsg();
		Member member = WechatUtils.getUserBySession(request); // 当前用户
		MachineShareRecord machineShare = new MachineShareRecord();
		machineShare.setShareContent(shareContent);
		machineShare.setShareType(0);
		machineShare.setShareUserOpenid(member.getUserOpenid());
		Integer info = leaseMachineService.insertShareRecord(machineShare);
		if(info == 0){
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
		}else{
			msg.setResultCode(ResultInfo.SUCCESSCODE);
			msg.setStatus(true);
		}
		return msg;
		
	}
	
	/**
     * 分享机器页面，当用户点进来后即绑定关系
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/goMachineLease", method = RequestMethod.GET)
	public void goWantLease(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//获取当前用户
		Member member = WechatUtils.getUserBySession(request);
		
		String shareOpenid = request.getParameter("shareOpenid");
		if(StringUtils.isNotEmpty(shareOpenid)){
			memberService.buildChildUser(shareOpenid, member,1,2);
			member.setUserIsFlag(2);
			homeService.updateMemberIsFlagByOpenid(member);
		}
		//页面未设计，展示跳转到我要租赁页面
		request.getRequestDispatcher(NameUtils.getConfig("WECHAT_GO_I_WANT_LEASE")).forward(request, response);
	}
}
