package com.iamberry.wechat.handles.integral;



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.integral.RankRule;
import com.iamberry.wechat.core.entity.integral.UseIntegral;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.integral.UseIntegralService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
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.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员积分使用 Controller
 */
@Controller
@RequestMapping("/wechat/userIntegral")
public class UseIntegralHandler {

    @Autowired
    private UseIntegralService useIntegralService;
    @Autowired
    private MemberService memberService;
    /**
     * 收入记录 use_type=1
     * @param req
     * @param useType
     * @param pageSize
     * @param pageNO
     * @return
     */
        @RequestMapping("/listGainIntegralInfo")
    @ResponseBody
    public ResultMsg listGainIntegralInfo(HttpServletRequest req,
                                    @RequestParam(value="useType",defaultValue= "1", required=false)Integer useType,
                                    @RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize,
                                    @RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO){
        ResultMsg rm=new ResultMsg();
        Member m= WechatUtils.getUserBySession(req);
        if (m == null || m.getUserOpenid() == null) {
            rm.setMessage(ResultInfo.loginOutError);
            return rm;
        }
        int gainIntegralCount=useIntegralService.getGainIntegralCount(m.getUserOpenid());
        if(gainIntegralCount<0){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            return rm;
        }
        UseIntegral useIntegral=new UseIntegral();
        useIntegral.setUserOpenId(m.getUserOpenid());
        useIntegral.setUseType(useType);
        useIntegral.getPage().setPageSize(pageSize);
        useIntegral.getPage().setPageNumber(pageNO);
        useIntegral.getPage().initRecordBegin();
        List<UseIntegral> listGainIntegralInfo=useIntegralService.listIntegralInfo(useIntegral);
        System.out.println(listGainIntegralInfo);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("listGainIntegralInfo",listGainIntegralInfo);
        map.put("gainIntegralCount",gainIntegralCount);

        rm.setStatus(true);
        rm.setResultCode(ResultInfo.SUCCESSCODE);
        rm.setMessage(NameUtils.getConfig("SUCCESSINFO"));
        rm.setData(map);
        return rm;
    }

    /**
     * 待入账记录 use_type=3
     * @param req
     * @param useType
     * @param pageSize
     * @param pageNO
     * @return
     */
    @RequestMapping("/listStayIntegral")
    @ResponseBody
    public ResultMsg listStayIntegral(HttpServletRequest req,
                                      @RequestParam(value="useType",defaultValue= "3", required=false)Integer useType,
                                      @RequestParam(value= "pageSize",defaultValue= "6" ,required=false)Integer pageSize,
                                      @RequestParam(value = "pageNO", defaultValue = "1",required=false)Integer pageNO){
        ResultMsg rm=new ResultMsg();
        Member m= WechatUtils.getUserBySession(req);
        if (m == null || m.getUserOpenid() == null) {
            rm.setMessage(ResultInfo.loginOutError);
            return rm;
        }
        int stayIntegralCount=useIntegralService.getStayIntegralCount(m.getUserOpenid());
        if(stayIntegralCount<0){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            return rm;
        }
        UseIntegral useIntegral=new UseIntegral();
        useIntegral.setUserOpenId(m.getUserOpenid());
        useIntegral.setUseType(useType);
        useIntegral.getPage().setPageSize(pageSize);
        useIntegral.getPage().setPageNumber(pageNO);
        useIntegral.getPage().initRecordBegin();
        List<UseIntegral> listStayIntegral=useIntegralService.listIntegralInfo(useIntegral);

        Map<String,Object> map=new HashMap<String, Object>();
        map.put("listStayIntegralInfo",listStayIntegral);
        map.put("stayIntegralCount",stayIntegralCount);

        rm.setStatus(true);
        rm.setResultCode(ResultInfo.SUCCESSCODE);
        rm.setMessage(NameUtils.getConfig("SUCCESSINFO"));
        rm.setData(map);
        return rm;
    }
    /**
     * 全部记录 use_type=null
     * @param req
     * @param useType
     * @param pageSize
     * @param pageNO
     * @return
     */
    @RequestMapping("/listAllIntegralInfo")
    @ResponseBody
    public ResultMsg listAllIntegralInfo(HttpServletRequest req,
                                   @RequestParam(value="useType",defaultValue= "", required=false)Integer useType,
                                   @RequestParam(value= "pageSize",defaultValue= "6" ,required=false)Integer pageSize,
                                   @RequestParam(value = "pageNO", defaultValue = "1",required=false)Integer pageNO){
        ResultMsg rm=new ResultMsg();
        Member m=WechatUtils.getUserBySession(req);
        String openid=m.getUserOpenid();
        if (m == null || openid == null) {
            rm.setMessage(ResultInfo.loginOutError);
            return rm;
        }
        int useIntegralCount=useIntegralService.getUseIntegralCount(openid);
        int gainIntegralCount=useIntegralService.getGainIntegralCount(openid);
        int stayIntegralCount=useIntegralService.getStayIntegralCount(openid);
        if(useIntegralCount<0||gainIntegralCount<0||stayIntegralCount<0){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            return rm;
        }
        UseIntegral useIntegral=new UseIntegral();
        //useIntegral.setUserOpenId(m.getUserOpenid());
        useIntegral.setUseType(useType);
        useIntegral.getPage().setPageSize(pageSize);
        useIntegral.getPage().setPageNumber(pageNO);
        useIntegral.getPage().initRecordBegin();
        List<UseIntegral> listAllIntegralInfo=useIntegralService.listIntegralInfo(useIntegral);

        Map<String,Object> map=new HashMap<String, Object>();
        map.put("listAllIntegralInfo",listAllIntegralInfo);
        map.put("useIntegralCount",useIntegralCount);
        map.put("gainIntegralCount",gainIntegralCount);
        map.put("stayIntegralCount",stayIntegralCount);

        rm.setStatus(true);
        rm.setResultCode(ResultInfo.SUCCESSCODE);
        rm.setMessage(NameUtils.getConfig("SUCCSSINFO"));
        rm.setData(map);
        return rm;
    }

    /**
     * 获取会员信息、会员等级信息
     * @param req
     * @return
     */
    @RequestMapping("/getRankRule")
    @ResponseBody
    public ResultMsg getRankRule(HttpServletRequest req){
        ResultMsg rm=new ResultMsg();
        Map<String,Object> map=new HashMap<String, Object>();
        Member member=WechatUtils.getUserBySession(req);
        if (member == null || member.getUserOpenid() == null) {
            rm.setMessage(ResultInfo.loginOutError);
            return rm;
        }
        member=memberService.getMyMemberInfo(member.getUserOpenid());
        if(member==null){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            return rm;
        }
        int userIntegralCount=member.getUserIntegral();

        List<RankRule> list=useIntegralService.getRankRule();
        RankRule rankRule=null;
        for(RankRule r:list){
            if(r.getRankRuleIntegral()>=userIntegralCount){
                rankRule=r;
                break;
            }
        }
        map.put("rankRule",rankRule);
        map.put("memberInfo",member);
        rm.setStatus(true);
        rm.setResultCode(ResultInfo.SUCCESSCODE);
        rm.setMessage(NameUtils.getConfig("SUCCESSINFO"));
        rm.setData(map);
        return rm;
    }

}
