package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.system.entity.Role;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.shop.mapper.MemberMapper;
import com.gxwebsoft.shop.service.MemberService;
import com.gxwebsoft.shop.entity.Member;
import com.gxwebsoft.shop.param.MemberParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商城会员Service实现
 *
 * @author 科技小王子
 * @since 2022-11-25 14:02:24
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Resource
    private UserService userService;

    @Override
    public PageResult<Member> pageRel(MemberParam param) {
        PageParam<Member, MemberParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<Member> list = baseMapper.selectPageRel(page, param);
        // 查询会员的关联信息
        selectUser(list);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<Member> listRel(MemberParam param) {
        List<Member> list = baseMapper.selectListRel(param);
        // 查询会员的关联信息
        selectUser(list);
        // 排序
        PageParam<Member, MemberParam> page = new PageParam<>();
        //page.setDefaultOrder("create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public Member getByIdRel(Integer memberId) {
        MemberParam param = new MemberParam();
        param.setMemberId(memberId);
        return param.getOne(baseMapper.selectListRel(param));
    }

  /**
   * 批量查询会员的关联信息
   *
   * @param members 会员集合
   */
  private void selectUser(List<Member> members) {
    if (members != null && members.size() > 0) {
      for (Member member : members) {
        member.setUser(userService.getById(member.getUserId()));
      }
    }
  }

}
