package com.iamberry.wechat.handles.agentInfo;

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.agentInfo.AgentTooth;
import com.iamberry.wechat.core.entity.agentInfo.OrderHint;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.admin.SystemService;
import com.iamberry.wechat.face.agentInfo.AgentInfoService;
import com.iamberry.wechat.face.agentInfo.AgentPurchaseOrderService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.order.CodeService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResponseJson;
import com.iamberry.wechat.tools.ResultInfo;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理商逻辑处理类
 * Created by liuzhiwei on 2017/10/11.
 */
@Controller
@RequestMapping("/wechat/agentTooth")
public class AgentToothHandler {

    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private AgentPurchaseOrderService agentPurchaseOrderService;
    @Autowired
    private SystemService systemService;

    /**
     * 查询代理商信息
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/select_agent_info")
    public ResultMsg selectAgentInfo(HttpServletRequest request) throws Exception {
        ResultMsg msg = new ResultMsg();
        Member member =  WechatUtils.getUserBySession(request);
        //查询用户信息
        Member user= memberService.getMemberByUserOpenId(member.getUserOpenid());
        if (user == null) {
            msg.setStatus(false);
            msg.setMessage("未找到该用户!");
            msg.setResultCode(ResultInfo.ERRORCODE);
            return msg;
        }
        msg.setStatus(true);
        msg.setResultCode(ResultInfo.SUCCESSCODE);
        msg.setData(user);
        return msg;
    }

    /**
     * 激活代理商信息
     * @param request
     * @param agentTooth
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/agent_activate")
    public ResultMsg agentActivate(HttpServletRequest request, AgentTooth agentTooth) throws Exception {
        ResultMsg msg = new ResultMsg();
        Member member = WechatUtils.getUserBySession(request);
        String phoneCode = request.getParameter("phoneCode");   //手机验证码
        if (!StringUtils.isNotEmpty(agentTooth.getAgentTel())) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("电话号码不能为空!");
            return msg;
        }
        //验证手机验证码是否正确
        ResponseJson code = codeService.validCode(agentTooth.getAgentTel(),phoneCode);
        if (code.getReturnCode() != 200) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("验证码不正确,请重新输入!");
            return msg;
        }
        AgentTooth agent = new AgentTooth();
        agent.setAgentTel(agentTooth.getAgentTel());
        agent.setAgentStatus(1);
        agent = agentInfoService.getAgentTooth(agent);
        if (agent == null) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("未找到该代理商信息,请重新输入有效的电话号码!");
            return msg;
        }
        if (StringUtils.isNotEmpty(agent.getAgentOpenid())) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("该代理商已被激活,无法再次激活!");
            return msg;
        }
        agentTooth.setAgentOpenid(member.getUserOpenid());
        agentTooth.setAgentStatus(1);
        //激活代理商信息
        int num = agentInfoService.updateAgentOpenId(agentTooth);
        if (num > 0) {
            msg.setResultCode(ResultInfo.SUCCESSCODE);
            msg.setStatus(true);
            return msg;
        } else {
            msg.setResultCode(ResultInfo.ERRORCODE) ;
            msg.setMessage("激活失败,请重新激活!");
            msg.setStatus(false);
            return msg;
        }
    }

    /**
     * 根据id查询更换刷头信息
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/select_order_hint")
    public ResultMsg selectOrderHint(HttpServletRequest request) throws Exception {
        ResultMsg msg = new ResultMsg();
        Map<String,Object> map = new HashMap<String,Object>();
        String hintId = request.getParameter("hintId");
        if (!StringUtils.isNotEmpty(hintId)) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            return msg;
        }
        OrderHint hint = new OrderHint();
        hint.setHintId(Integer.parseInt(hintId));
        //查询更换刷头列表
        List<OrderHint> hintList = agentPurchaseOrderService.listOrderHint(hint);
        if (hintList == null || hintList.size() <= 0) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            return msg;
        }

        //获取需要赠送的优惠券id
        ShopSystemRule rule = systemService.selectOneShopRuleById(105);
        hint = hintList.get(0);
        map.put("hint",hint);
        map.put("couponTypeId",rule.getRuleNum());
        msg.setData(map);
        msg.setResultCode(ResultInfo.SUCCESSCODE);
        msg.setStatus(true);
        return msg;
    }

    @RequestMapping("/go_agent_tooth")
    public void goAgentTooth(HttpServletRequest req,HttpServletResponse res) throws Exception {
        Member member = WechatUtils.getUserBySession(req);
        AgentTooth tooth = new AgentTooth();
        tooth.setAgentOpenid(member.getUserOpenid());
        tooth.setAgentStatus(1);
        //查询代理商信息
        List<AgentTooth> toothList = agentInfoService.selectAllAgentinfo(tooth);
        if (toothList == null || toothList.size() == 0) {
            res.sendRedirect(NameUtils.getConfig("WECHAT_AGENT_TOOT_ACTIVATE"));
            //req.getRequestDispatcher(NameUtils.getConfig("WECHAT_AGENT_TOOT_ACTIVATE")).forward(req, res);
        } else {
            res.sendRedirect(NameUtils.getConfig("WECHAT_AGENT_TOOT_CENTER"));
            //req.getRequestDispatcher(NameUtils.getConfig("WECHAT_AGENT_TOOT_CENTER")).forward(req, res);
        }
    }
}
