package com.iamberry.wechat.handles.apparatus;

import com.iamberry.wechat.core.entity.PageBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.admin.ShopSystemRule;
import com.iamberry.wechat.core.entity.apparatus.Apparatus;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.admin.SystemService;
import com.iamberry.wechat.face.apparatus.ApparatusService;
import com.iamberry.wechat.tools.HttpClient431Util;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.payUtil.StringUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by liuzhiwei on 2017/12/28.
 */
@Controller
@RequestMapping("/wechat/apparatus")
public class ApparatusHandler {

    @Autowired
    private ApparatusService apparatusService;
    @Autowired
    private SystemService systemService;

    /**
     * 分页查询用户机器信息
     * @param request
     * @param pageNO
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/select_apparatus_page")
    public ResultMsg selectApparatusPage(HttpServletRequest request,
                                         @RequestParam(value = "pageNO", defaultValue = "1", required = false) int pageNO,
                                         @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize) throws Exception {
        ResultMsg msg = new ResultMsg();
        Member member = WechatUtils.getUserBySession(request);
        Apparatus apparatus = new Apparatus();
        apparatus.setUserOpenid(member.getUserOpenid());
        apparatus.setApparatusStatus(1);
        PageBean page = new PageBean();
        page.setPageNumber(pageNO);
        page.setPageSize(pageSize);
        apparatus.setPage(page);
        //分页查询用户绑定机器信息
        List<Apparatus> apparatusList = apparatusService.listApparatusPage(apparatus);
        msg.setData(apparatusList);
        msg.setResultCode(ResultInfo.SUCCESSCODE);
        msg.setStatus(true);
        return msg;
    }

    /**
     * 注册会员
     * @param request
     * @param member
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("add_apparatus")
    public ResultMsg addApparatus(HttpServletRequest request,Member member) throws Exception {
        ResultMsg msg = new ResultMsg();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Member memberInfo = WechatUtils.getUserBySession(request);
        //获取换新时间期限
        ShopSystemRule renewRule =  systemService.selectOneShopRuleById(246);
        member.setUserOpenid(memberInfo.getUserOpenid());
        //获取二维码中机器条形码
        String barCode = request.getParameter("barCode");
        String buyDate = request.getParameter("buyDate");
        String storeId = request.getParameter("storeId");

        if (!StringUtil.isNotEmpty(barCode)) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("机器条形码为空，请重新扫描二维码!");
            return msg;
        }

        Apparatus apparatus = new Apparatus();
        apparatus.setApparatusStatus(1);
        apparatus.setApparatusBarcode(barCode);
        //获取机器信息
        List<Apparatus> apparatusList = apparatusService.listApparatusPage(apparatus);
        if (apparatusList != null && apparatusList.size() > 0) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("机器条形码为空，请重新扫描二维码!");
            return msg;
        }
        apparatus.setUserOpenid(memberInfo.getUserOpenid());
        apparatus.setApparatusBuyDate(format.parse(buyDate));
        apparatus.setApparatusWarrantyDate(updateDate(format.parse(buyDate),renewRule.getRuleNum().intValue()));
        apparatus.setStoreId(Integer.getInteger(storeId));
        //注册会员
        boolean flag = apparatusService.registerMember(apparatus,member);
        if (flag) {
            msg.setResultCode(ResultInfo.SUCCESSCODE);
            msg.setStatus(true);
        } else {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("会员注册失败，请重新注册!");
            return msg;
        }
        return msg;
    }

    /**
     * 修改日期 增加月
     * @param date
     * @param month
     * @return
     * @throws Exception
     */
    public Date updateDate(Date date,int month) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }
}
