package com.iamberry.wechat.handles.integral;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
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.core.entity.page.PageRequest;
import com.iamberry.wechat.core.entity.page.PagedResult;
import com.iamberry.wechat.core.entity.product.Product;
import com.iamberry.wechat.face.integral.UseIntegralService;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * Created by Administrator on 2018/1/23.
 */
@Controller
@RequestMapping("/admin/userIntegral")
public class AdminIntegrlhHandler {

    @Autowired
    private UseIntegralService useIntegralService;
    /**
     * 查询积分记录 并分页 (支持查询条件模糊搜索)
     * 2018年2月24日
     * @author mzx
     * @param pageNO	分页信息
     * @return
     */
    @RequestMapping("/listIntegral")
    public ModelAndView listIntegral(UseIntegral useIntegral,
                                     @RequestParam(value= "pageSize",defaultValue= "10" ,required=false) Integer pageSize,
                                     @RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
                                     @RequestParam(value = "totalNum", defaultValue = "0", required = false) Integer totalNum,
                                     HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView("admin/integralRecord/listIntegral");
        // 封装请求数据
        PageRequest<UseIntegral> pageRequest = new PageRequest<>(useIntegral, pageNO, pageSize, totalNum == 0);
        // 查询订单列表
        PagedResult<UseIntegral> result = useIntegralService.listUseIntegral(pageRequest);
        if (totalNum != 0) {
            result.setTotal(totalNum);
        }
        StitchAttrUtil.getSa().setModelAndView(useIntegral, mv, "/admin/userIntegral/listIntegral", result);
        return mv;
    }

    /**
     * 查询待入账积分记录 并分页 (支持查询条件模糊搜索)
     * 2018年2月24日
     * @author mzx
     * @param pageNO	分页信息
     * @return
     */
    @RequestMapping("/listStayIntegral")
    public ModelAndView listStayIntegral(StayIntegral stayIntegral,
                                        @RequestParam(value= "pageSize",defaultValue= "10" ,required=false) Integer pageSize,
                                         @RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
                                         @RequestParam(value = "totalNum", defaultValue = "0", required = false) Integer totalNum,
                                         HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView("admin/integralRecord/listStayIntegral");
        // 封装请求数据
        PageRequest<StayIntegral> pageRequest = new PageRequest<>(stayIntegral, pageNO, pageSize, totalNum == 0);
        // 查询订单列表
        PagedResult<StayIntegral> result = useIntegralService.listAdminStayIntegral(pageRequest);
        if (totalNum != 0) {
            result.setTotal(totalNum);
        }
        StitchAttrUtil.getSa().setModelAndView(stayIntegral, mv, "/admin/userIntegral/listStayIntegral", result);
        return mv;
    }

    /**
     *取消待入账积分
     */
    @ResponseBody
    @RequestMapping("/updatestayIntegralStatus")
    public ResultMsg updatestayIntegralStatus(HttpServletRequest request){
        ResultMsg resultMsg = new ResultMsg();
        String stayId = request.getParameter("stayId");
        String stayStatus = request.getParameter("stayStatus");
        if (null == stayId || "".equals(stayId)) {
            resultMsg.setStatus(false);
            resultMsg.setResultCode("200");
            return resultMsg;
        }
        if (null == stayStatus || "".equals(stayStatus)) {
            resultMsg.setStatus(false);
            resultMsg.setResultCode("200");
            return resultMsg;
        }
        StayIntegral stayIntegral = new StayIntegral();
        stayIntegral.setStayId(Integer.valueOf(stayId));
        stayIntegral.setStayStatus(Integer.valueOf(stayStatus));
        Integer msg = useIntegralService.updatestayIntegralStatus(stayIntegral);
        if(msg < 1){
            resultMsg.setStatus(false);
            resultMsg.setResultCode("200");
            return resultMsg;
        }else{
            resultMsg.setStatus(true);
            resultMsg.setResultCode("200");
            return resultMsg;
        }
    }
}
