package com.ruoyi.mall.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.mall.domain.MallMemberCoupon;
import com.ruoyi.mall.domain.vo.IndexGoodsVO;
import com.ruoyi.mall.domain.vo.MemberCollectVO;
import com.ruoyi.mall.domain.vo.MemberFootMarkVO;
import com.ruoyi.mall.domain.vo.MemberUserVO;
import com.ruoyi.mall.service.IMallCouponService;
import com.ruoyi.mall.service.IMallFeedbackService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.service.IMallMemberUserService;

/**
 * 会员Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-25
 */
@Service
public class MallMemberUserServiceImpl implements IMallMemberUserService
{
    @Autowired
    private MallMemberUserMapper mallMemberUserMapper;

    @Autowired
    private IMallCouponService couponService;

    /**
     * 查询会员
     *
     * @param id 会员主键
     * @return 会员
     */
    @Override
    public MallMemberUser selectMallMemberUserById(Long id)
    {
        return mallMemberUserMapper.selectMallMemberUserById(id);
    }

    @Override
    public MallMemberUser queryByOpenid(String openid) {
        return mallMemberUserMapper.queryByOpenid(openid);
    }
    @Override
    public MallMemberUser queryByPhone(String phone) {
        return mallMemberUserMapper.queryByPhone(phone);
    }

    /**
     * 查询会员列表
     *
     * @param mallMemberUser 会员
     * @return 会员
     */
    @Override
    public List<MallMemberUser> selectMallMemberUserList(MallMemberUser mallMemberUser)
    {
        return mallMemberUserMapper.selectMallMemberUserList(mallMemberUser);
    }

    /**
     * 新增会员
     *
     * @param mallMemberUser 会员
     * @return 结果
     */
    @Override
    public int insertMallMemberUser(MallMemberUser mallMemberUser)
    {
        return mallMemberUserMapper.insertMallMemberUser(mallMemberUser);
    }


    /**
     * 修改会员
     *
     * @param mallMemberUser 会员
     * @return 结果
     */
    @Override
    public int updateMallMemberUser(MallMemberUser mallMemberUser)
    {
        return mallMemberUserMapper.updateMallMemberUser(mallMemberUser);
    }

    /**
     * 批量删除会员
     *
     * @param ids 需要删除的会员主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberUserByIds(Long[] ids)
    {
        return mallMemberUserMapper.deleteMallMemberUserByIds(ids);
    }

    /**
     * 删除会员信息
     *
     * @param id 会员主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberUserById(Long id)
    {
        return mallMemberUserMapper.deleteMallMemberUserById(id);
    }

    @Override
    public MemberUserVO selectMemberVOUserById(Long l) {
        return mallMemberUserMapper.selectMemberVOUserById(l);
    }

    @Override
    public int selectCollectCount(Map<String, Object> params) {
        return mallMemberUserMapper.selectCollectCount(params);
    }

    @Override
    public void insertMemberGoods(MemberCollectVO memberGoods) {
        mallMemberUserMapper.insertMemberGoods(memberGoods);
    }

    @Override
    public void deleteMemberGoodsByParam(Map<String, Object> params) {
        mallMemberUserMapper.deleteMemberGoodsByParam(params);
    }

    @Override
    public PageInfo<IndexGoodsVO> queryByUserId(Map<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()),Integer.parseInt(params.get("pageSize").toString()));

        List<IndexGoodsVO> list = mallMemberUserMapper.queryCollectByUserUserId(Long.parseLong(params.get("userId").toString()));
        PageInfo<IndexGoodsVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<IndexGoodsVO> queryFoodMarkByUserId(Map<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()),Integer.parseInt(params.get("pageSize").toString()));

        List<IndexGoodsVO> list = mallMemberUserMapper.queryFoodMarkByUserUserId(Long.parseLong(params.get("userId").toString()));
        PageInfo<IndexGoodsVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public synchronized void insertIntoFoodMarkBD(Map<String, Object> params) {
        MemberFootMarkVO foodMarkVO = new MemberFootMarkVO();
        foodMarkVO.setGoodsId(Long.parseLong(params.get("goodsId").toString()));
        foodMarkVO.setMemberId(Long.parseLong(params.get("userId").toString()));
        MemberFootMarkVO result = mallMemberUserMapper.selectFootMarkVOUserById(foodMarkVO);
        if (ObjectUtils.isNotEmpty(result)){
            result.setUpdateTime(DateUtils.getTime());
            mallMemberUserMapper.updateFootMarkVO(result);
        }else {
            foodMarkVO.setFootmarkId(IdUtils.fastSimpleUUID());
            foodMarkVO.setUpdateTime(DateUtils.getTime());
            mallMemberUserMapper.insetIntoFootMarkVO(foodMarkVO);
        }
    }

    @Override
    public MemberCollectVO selectIsCollectBy(MemberCollectVO collectVO) {

        return mallMemberUserMapper.selectIsCollectBy(collectVO);
    }

    @Override
    public Map<String, Object> checkUserCoupon(Long userId, String couponId) {
        return mallMemberUserMapper.checkUserCoupon(userId,couponId);
    }

    @Override
    public void insertUserCoupon(MallMemberCoupon memberCoupon) {
        mallMemberUserMapper.insertUserCoupon(memberCoupon);
    }

    @Override
    public List<Map<String, Object>> getMyCoupons(Long userId) {
        return mallMemberUserMapper.getMyCoupons(userId);
    }

    @Override
    public Map<String, Object> checkCouponUsed(String couponId, Long userId) {
        return mallMemberUserMapper.checkCouponUsed(couponId,userId);
    }

    @Override
    public void updateCouponUsed(String couponId, Long userId) {
        mallMemberUserMapper.updateCouponUsed(couponId,userId);
    }

    @Override
    public Map<String, Object> getMyRecordCount(Long userId) {
        int collectCount = mallMemberUserMapper.selectCollectCountByUserId(userId);
        int couponCount = mallMemberUserMapper.selectCouponCountByUserId(userId);
        int footCount = mallMemberUserMapper.selectFootCountByUserId(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("collectCount",collectCount);
        map.put("couponCount",couponCount);
        map.put("footCount",footCount);
        return map;
    }


}
