package com.iamberry.wechat.handles.admin;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.integral.UseIntegral;
import com.iamberry.wechat.core.entity.page.PageRequest;
import com.iamberry.wechat.core.entity.page.PagedResult;
import com.iamberry.wechat.core.entity.receive.ChargerReceive;
import com.iamberry.wechat.face.receive.ChargReceiveService;
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;

/**
 * 免费领取充电器
 * Created by wxm on 2017/10/11.
 */
@Controller
@RequestMapping("/admin/receive")
public class AdminChargReceiveHandler {

    @Autowired
    private ChargReceiveService chargReceiveService;

    /**
     * 查询积分记录 并分页 (支持查询条件模糊搜索)
     * 2018年2月24日
     * @return
     */
    @RequestMapping("/listReceive")
    public ModelAndView listIntegral(ChargerReceive chargerReceive,
                                     @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/receive/listReceive");
        // 封装请求数据
        PageRequest<ChargerReceive> pageRequest = new PageRequest<>(chargerReceive, pageNO, pageSize, totalNum == 0);
        // 查询订单列表
        PagedResult<ChargerReceive> result = chargReceiveService.listAdminChargerReceive(pageRequest);
        if (totalNum != 0) {
            result.setTotal(totalNum);
        }
        StitchAttrUtil.getSa().setModelAndView(chargerReceive, mv, "/admin/receive/listReceive", result);
        return mv;
    }

    /**
     * 发货
     * 2018年2月24日
     * @author mzx
     * @return
     */
    @ResponseBody
    @RequestMapping("/ship")
    public ResultMsg ship(ChargerReceive chargerReceive,HttpServletRequest request) throws Exception {
        ResultMsg msg = new ResultMsg();

        if(chargerReceive.getReceiveId() == null  || chargerReceive.getReceivePostFirm() == null || "".equals(chargerReceive.getReceivePostFirm())
                || chargerReceive.getReceivePostNum() == null || "".equals(chargerReceive.getReceivePostNum())){
            msg.setMessage(NameUtils.getConfig("ERRORCODE"));
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
        }
        chargerReceive.setReceiveStatus(3);
        Integer flag = chargReceiveService.ship(chargerReceive);

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


}
