package com.hwrj.cloud.portal.controller;

import com.github.pagehelper.PageInfo;
import com.hwrj.cloud.common.api.CommonPage;
import com.hwrj.cloud.common.api.CommonResult;
import com.hwrj.cloud.portal.dao.OmsPreItemDao;
import com.hwrj.cloud.portal.model.OmsPreItem;
import com.hwrj.cloud.portal.model.UmsMember;
import com.hwrj.cloud.portal.model.UmsMemberPlatform;
import com.hwrj.cloud.portal.service.OmsPreItemService;
import com.hwrj.cloud.portal.service.UmsMemberService;
import com.hwrj.cloud.portal.util.MemberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "预购信息处理", description = "OmePreItemController")
@RequestMapping("/pre")
public class OmePreItemController {


    @Autowired
    private OmsPreItemService omsPreItemService;

    @Autowired
    private UmsMemberService umsMemberService;


    @PutMapping(value = "/add")
    @ApiOperation("添加有意向商品")
    @ResponseBody
    public CommonResult add(@RequestBody OmsPreItem omsPreItem){

        UmsMember currentMember = umsMemberService.getCurrentMember();
        if (StringUtils.isEmpty(currentMember.getId())){
            return CommonResult.unauthorized(null);
        }
        omsPreItem.setMemberId(currentMember.getId());
        omsPreItem.setMemberNickname(currentMember.getNickname());
        omsPreItemService.addOmsPreItem(omsPreItem);
        return CommonResult.success(null);


    }

    @GetMapping(value = "/detail")
    @ApiOperation("获取有意向详情")
    @ResponseBody
    public CommonResult detail(@RequestParam(value = "意向id") Long preId){

        OmsPreItem omsPreItemDetail = omsPreItemService.getOmsPreItemDetail(preId);
        return CommonResult.success(omsPreItemDetail);
    }
    @Autowired
    private UmsMemberService memberService;
    @GetMapping(value = "/own")
    @ApiOperation("获取有意向详情")
    @ResponseBody
    public CommonResult getOwn(@RequestParam(defaultValue = "10") int pageSize,
                               @RequestParam(defaultValue = "1") int pageNO,HttpServletRequest request){
        Map<String,Object> map = memberService.getCurrentWxUser();
        UmsMemberPlatform wxUsers = memberService.getWxUser();
        List<OmsPreItemDao> omsPreItemListOwn = omsPreItemService.getOmsPreItemListOwn(wxUsers.getId(), pageSize, pageNO);
        return CommonResult.success(CommonPage.restPage(omsPreItemListOwn));
    }

    @GetMapping(value = "/other")
    @ApiOperation("获取有意向详情")
    @ResponseBody
    public CommonResult getOtherToOwn(@RequestParam(defaultValue = "10") int pageSize,
                               @RequestParam(defaultValue = "1") int pageNO,HttpServletRequest request){
        UmsMember currentMember = umsMemberService.getCurrentMember();

        List<OmsPreItemDao> omsPreItemListOwn = omsPreItemService.getOmsPreItemListOther(currentMember.getId(), pageSize, pageNO);



        return CommonResult.success(CommonPage.restPage(omsPreItemListOwn));
    }


}
