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.IntegralProduct;
import com.iamberry.wechat.core.entity.integral.IntegralProductPicture;
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.integral.IntegralProductGradeService;
import com.iamberry.wechat.face.integral.IntegralProductPictureService;
import com.iamberry.wechat.face.integral.IntegralProductService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wechat/integral_pro")
public class IntegralProductHandler {

    @Autowired
    private IntegralProductService integralProductService;
    @Autowired
    private IntegralProductPictureService integralProductPictureService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private IntegralProductGradeService integralProductGradeService;

    /**
     *积分产品集合
     * @param req
     * @param pageSize
     * @param pageNO
     * @return
     */
    @RequestMapping("/list_product")
    @ResponseBody
    public ResultMsg listProduct(HttpServletRequest req,
                                 @RequestParam(value= "pageSize",defaultValue= "10" ,required=false) Integer pageSize,
                                 @RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO){
        ResultMsg rm=new ResultMsg();
        Member m= WechatUtils.getUserBySession(req);
        m = memberService.getMemberByUserOpenId(m.getUserOpenid());

        String openid=m.getUserOpenid();
        if (m == null || openid == null) {
            rm.setMessage(ResultInfo.loginOutError);
            return rm;
        }
        IntegralProduct  integralProduct = new IntegralProduct();
        integralProduct.setIntegralProductStatus(1);

        Integer totalNum = 0;
        // 封装请求数据
        PageRequest<IntegralProduct> pageRequest = new PageRequest<>(integralProduct, pageNO, pageSize, totalNum == 0);
        // 查询订单列表
        PagedResult<IntegralProduct> result = integralProductService.listIntegralProductPage(pageRequest);

        Map<String,Object> map=new HashMap<String, Object>();
        map.put("integralProductList",result.getDataList());
        map.put("integral",m.getUserSurplusIntegral());

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

    /**
     *积分产品详情
     * @param req
     * @param integralProductId
     * @return
     */
    @RequestMapping("/product_desc")
    @ResponseBody
    public ResultMsg productDesc(HttpServletRequest req,Integer integralProductId) throws  Exception{
        ResultMsg rm=new ResultMsg();
        Member m= WechatUtils.getUserBySession(req);

        if(integralProductId == null){
            rm.setStatus(true);
            rm.setResultCode(ResultInfo.ERRORCODE);
            rm.setMessage("未获取到积分详情！");
            return rm;
        }
        IntegralProduct  integralProduct =  integralProductService.getIntegralProductById(integralProductId);

        IntegralProductPicture integralProductPicture = new IntegralProductPicture();
        integralProductPicture.setIntegralProductId(integralProductId);
        List<IntegralProductPicture> pictureList =  integralProductPictureService.getIntegralProductPictureList(integralProductPicture);

        Map<String,Object> exchangeInfo = integralProductGradeService.getIsExchange(m.getUserId(),integralProductId);

        Map<String,Object> map = new HashMap<>();
        map.put("integralProduct",integralProduct);
        map.put("pictureList",pictureList);
        map.put("exchangeInfo",exchangeInfo);

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


}
