package com.iamberry.wechat.handles.apparatus;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.apparatus.Apparatus;
import com.iamberry.wechat.core.entity.page.PageRequest;
import com.iamberry.wechat.core.entity.page.PagedResult;
import com.iamberry.wechat.face.admin.SystemService;
import com.iamberry.wechat.face.apparatus.ApparatusService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.order.CodeService;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.SendMessageUtil;
import com.iamberry.wechat.utils.StitchAttrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 liuzhiwei on 2017/12/28.
 */
@Controller
@RequestMapping("/admin/apparatus")
public class AdminApparatusHandler {

    private Logger logger = LoggerFactory.getLogger(AdminApparatusHandler.class);

    @Autowired
    private SendMessageUtil sendMessageUtil;
    @Autowired
    private ApparatusService apparatusService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private MemberService memberService;


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

    /**
     * 删除牙刷
     * @param request
     * @param code
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/del_tooth")
    public ResultMsg addApparatus(HttpServletRequest request,String code) throws Exception {
        if(code == null){
            return new ResultMsg(false, ResultInfo.ERRORCODE, "删除失败，牙刷编号为空",null);
        }
        Integer flag = 0;
        String msg = "删除失败，请重试";
        try{
            flag = apparatusService.delTooth(code);
        }catch (RuntimeException e){
            e.printStackTrace();
            msg = e.getMessage();
        }
        if(flag < 1){
            return new ResultMsg(false, ResultInfo.ERRORCODE, msg ,null);
        }
        return new ResultMsg(true, ResultInfo.SUCCESSCODE, "删除成功!",null);
    }

}
