package com.iamberry.wechat.handles.integral;



import com.github.pagehelper.PageHelper;
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.StayIntegral;
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.face.order.AdminOrderService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
import org.apache.poi.ss.formula.functions.Rank;
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 javax.servlet.http.HttpSession;
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;
    @Autowired
    private AdminOrderService adminOrderService;
    /**
     * 收入记录 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);
        String openid=m.getUserOpenid();
        if (m == null || openid == null) {
            rm.setMessage(ResultInfo.loginOutError);
            return rm;
        }
        Integer gainIntegralCount=useIntegralService.getGainIntegralCount(openid);
            if(gainIntegralCount != null && gainIntegralCount<0){
                rm.setStatus(false);
                rm.setResultCode(ResultInfo.ERRORCODE);
                rm.setMessage(NameUtils.getConfig("ERRORINFO"));
                return rm;
            }
        UseIntegral useIntegral=new UseIntegral();
        useIntegral.setUserOpenId(openid);
        useIntegral.setIntegralType(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 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);
        String openid=m.getUserOpenid();
        if (m == null || openid == null) {
            rm.setMessage(ResultInfo.loginOutError);
            return rm;
        }
        Integer stayIntegralCount=useIntegralService.getStayIntegralCount(openid);
        if(stayIntegralCount != null && stayIntegralCount<0){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            return rm;
        }
        UseIntegral useIntegral=new UseIntegral();
        useIntegral.setUserOpenId(openid);
        useIntegral.setIntegralType(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;
        }
        Map<String,Object> map=new HashMap<String, Object>();
        UseIntegral useIntegral=new UseIntegral();
        useIntegral.setUserOpenId(openid);
        /**********判断useType=1 为收入记录**************/
        if(useType != null && useType==1){
            Integer gainIntegralCount=useIntegralService.getGainIntegralCount(openid);
            if(gainIntegralCount != null && gainIntegralCount<0){
                rm.setStatus(false);
                rm.setResultCode(ResultInfo.ERRORCODE);
                rm.setMessage(NameUtils.getConfig("ERRORINFO"));
                return rm;
            }

            useIntegral.getPage().setPageSize(pageSize);
            useIntegral.getPage().setPageNumber(pageNO);
            useIntegral.getPage().initRecordBegin();
            useIntegral.setIntegralType(useType);
            List<UseIntegral> listIntegralInfo=useIntegralService.listIntegralInfo(useIntegral);

            map.put("listIntegralInfo",listIntegralInfo);
            map.put("gainIntegralCount",gainIntegralCount);

            rm.setStatus(true);
            rm.setResultCode(ResultInfo.SUCCESSCODE);
            rm.setMessage(NameUtils.getConfig("SUCCESSINFO"));
            rm.setData(map);
            return rm;
        }
        /**********判断useType=3 为待入账记录**************/
        if(useType != null && useType==3){
            Integer stayIntegralCount=useIntegralService.getStayIntegralCount(openid);
            if(stayIntegralCount != null && stayIntegralCount<0){
                rm.setStatus(false);
                rm.setResultCode(ResultInfo.ERRORCODE);
                rm.setMessage(NameUtils.getConfig("ERRORINFO"));
                return rm;
            }

            useIntegral.getPage().setPageSize(pageSize);
            useIntegral.getPage().setPageNumber(pageNO);
            useIntegral.getPage().initRecordBegin();
            useIntegral.setIntegralType(useType);
            List<UseIntegral> listIntegralInfo=useIntegralService.listIntegralInfo(useIntegral);

            map.put("listIntegralInfo",listIntegralInfo);
            map.put("stayIntegralCount",stayIntegralCount);

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

        /**********否则 为全部记录**************/
        Integer stayIntegralCount=useIntegralService.getStayIntegralCount(openid);
        Integer useIntegralCount=useIntegralService.getUseIntegralCount(openid);
        Integer gainIntegralCount=useIntegralService.getGainIntegralCount(openid);

        if(gainIntegralCount != null && gainIntegralCount<0){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            return rm;
        }
        if(stayIntegralCount != null && stayIntegralCount<0){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            return rm;
        }
        if(useIntegralCount != null && useIntegralCount<0){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            return rm;
        }

        useIntegral.getPage().setPageSize(pageSize);
        useIntegral.getPage().setPageNumber(pageNO);
        useIntegral.getPage().initRecordBegin();
        useIntegral.setIntegralType(null);
        List<UseIntegral> listIntegralInfo=useIntegralService.listIntegralInfo(useIntegral);

        map.put("listIntegralInfo",listIntegralInfo);
        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);
        String openid=member.getUserOpenid();
        if (member == null || openid == null) {
            rm.setMessage(ResultInfo.loginOutError);
            return rm;
        }

        /*************会员等级***************/
        Member memberInfo=memberService.getMyMemberInfo(openid);
        if(memberInfo==null){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            rm.setData("");
            return rm;
        }
        Integer userIntegralCount=memberInfo.getUserIntegral();
        if(userIntegralCount != null && userIntegralCount<0){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            return rm;
        }
        List<RankRule> list=useIntegralService.getRankRule();
        if((list!=null&&list.size()<0)||list==null){
            rm.setStatus(false);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage(NameUtils.getConfig("ERRORINFO"));
            rm.setData("");
            return rm;
        }
        List<RankRule> lists=new ArrayList<RankRule>();
        for(RankRule r:list){
            if(r.getRankRuleIntegral()>userIntegralCount){
                lists.add(r);
            }
        }
        /**************未发货数量*****************/
        Integer notSendCount=adminOrderService.getNotSendCount(openid);

        map.put("listRankRule",lists);
        map.put("notSendCount",notSendCount);
        map.put("memberInfo",memberInfo);
        rm.setStatus(true);
        rm.setResultCode(ResultInfo.SUCCESSCODE);
        rm.setMessage(NameUtils.getConfig("SUCCESSINFO"));
        rm.setData(map);
        return rm;
    }

}
