package com.iamberry.wechat.handles.ism;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.ism.IntegralLog;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.page.PageRequest;
import com.iamberry.wechat.core.entity.page.PagedResult;
import com.iamberry.wechat.face.ism.IntegralLogService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.utils.StitchAttrUtil;
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 javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 积分
 */
@Controller
@RequestMapping("/wechat/integralLog")
public class IntegralLogHandler {
    @Autowired
    private MemberService memberService;
    @Autowired
    private IntegralLogService integralLogService;

    /**
     * 获取积分记录
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getIntegral", method = RequestMethod.POST)
    public ResultMsg payBefore(HttpServletRequest request,
                               @RequestParam(value= "pageSize",defaultValue= "3" ,required=false) Integer pageSize,
                               @RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
                               @RequestParam(value = "totalNum", defaultValue = "0", required = false) Integer totalNum
    ) {
        ResultMsg msg = new ResultMsg();
        msg.setResultCode(ResultInfo.ERRORCODE);

        Member member = WechatUtils.getUserBySession(request); // 当前用户
        member = memberService.getMemberByUserOpenId(member.getUserOpenid());

        IntegralLog integralLog = new IntegralLog();
        integralLog.setUserOpenId(member.getUserOpenid());
        integralLog.setInteLogStatus(1);
//        List<IntegralLog> integralLogList = integralLogService.getIntegralLogListByAccoUser(integralLog);

        // 封装请求数据
        PageRequest<IntegralLog> pageRequest = new PageRequest<>(integralLog, pageNO, pageSize, totalNum == 0);
        // 查询订单列表
        PagedResult<IntegralLog> result = integralLogService.listIntegralLog(pageRequest);
        if (totalNum != 0) {
            result.setTotal(totalNum);
        }
        StitchAttrUtil.setPage(result);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("list",result.getDataList());

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

}
