package com.gree.mall.miniapp.logic.goods;

import com.gree.mall.miniapp.bean.Page;
import com.gree.mall.miniapp.bean.goods.GoodsFavoriteBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.CustomGoodsFavoriteMapper;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.GoodsFavorite;
import com.gree.mall.miniapp.plus.service.GoodsFavoriteService;
import com.gree.mall.miniapp.plus.service.UserService;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

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

@Service
public class GoodsFavoriteLogic {

    @Autowired
    private GoodsFavoriteService goodsFavoriteService;

    @Autowired
    private CustomGoodsFavoriteMapper customGoodsFavoriteMapper;

    @Autowired
    private CommonLogic commonLogic;

    public GoodsFavorite saveGoodsFavorite(HttpServletRequest request,String userId, String goodsId, String goodsSpecId) throws Exception {
        CurrentCompanyWechat companyWechat = commonLogic.getCurrentCompanyWechat(request);

        if(StringUtils.isBlank(userId)){
            throw new RemoteServiceException("无效的userId");
        }
        if(StringUtils.isBlank(goodsId)){
            throw new RemoteServiceException("无效的goodsId");
        }
        GoodsFavorite goodsFavorite =new GoodsFavorite();
        goodsFavorite.setGoodsId(goodsId);
        goodsFavorite.setUserId(userId);
        goodsFavorite.setGoodsSpecId(goodsSpecId);
        goodsFavorite.setCreateTime(new Date());
        goodsFavorite.setCompanyWechatId(companyWechat.getCurrentCompanyWechatId());
        goodsFavorite.setCompanyName(companyWechat.getCurrentCompanyName());
        goodsFavoriteService.save(goodsFavorite);
        return goodsFavorite;
    }

    public void delGoodsFavorite(String goodFavoriteId){
        goodsFavoriteService.lambdaUpdate()
                .eq(StringUtils.isNotBlank(goodFavoriteId),GoodsFavorite::getGoodsFavoriteId,goodFavoriteId)
                .remove();
    }

    public void detailDelGoodsFavorite(String goodsId,String userId,String goodSpecId) throws RemoteServiceException {
        goodsFavoriteService.lambdaUpdate()
                .eq(StringUtils.isNotBlank(goodsId),GoodsFavorite::getGoodsId,goodsId)
                .eq(StringUtils.isNotBlank(userId),GoodsFavorite::getUserId,userId)
                .eq(StringUtils.isNotBlank(goodSpecId),GoodsFavorite::getGoodsSpecId,goodSpecId)
                .remove();
    }


    public Page pageGoodsFavorite(String userId,Integer pageNum, Integer pageSize){
        Integer count = customGoodsFavoriteMapper.queryGoodsFavoriteCount(userId);
        List<GoodsFavoriteBean> goodsFavoriteBeanList = null;
        if (count > 0) {
            goodsFavoriteBeanList = customGoodsFavoriteMapper.queryGoodsFavorite(userId,(pageNum-1)*pageSize,pageSize);
        }
        Integer totalPage = (count+pageSize-1)/pageSize;
        Page page = new Page<GoodsFavoriteBean>();
        page.setTotal(count);
        page.setTotalPage(totalPage);
        page.setPageNum(pageNum);
        page.setPageSize(goodsFavoriteBeanList==null?0:goodsFavoriteBeanList.size());
        page.setList(goodsFavoriteBeanList);

        return page;
    }



}

