package com.ycwl.basic.service.pc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.mapper.MemberMapper;
import com.ycwl.basic.model.pc.member.entity.MemberEntity;
import com.ycwl.basic.model.pc.member.req.MemberReqQuery;
import com.ycwl.basic.model.pc.member.resp.MemberRespVO;
import com.ycwl.basic.service.pc.MemberService;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：longbinbin
 * @Date：2024/12/2 17:23
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public ApiResponse<PageInfo<MemberRespVO>> pageQuery(MemberReqQuery memberReqQuery) {
        PageHelper.startPage(memberReqQuery.getPageNum(), memberReqQuery.getPageSize());
        List<MemberRespVO> list = memberMapper.list(memberReqQuery);
        PageInfo<MemberRespVO> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);

    }

    @Override
    public ApiResponse<List<MemberRespVO>> list(MemberReqQuery memberReqQuery) {
        return ApiResponse.success(memberMapper.list(memberReqQuery));
    }

    @Override
    public ApiResponse<MemberRespVO> getById(Long id) {
        return ApiResponse.success(memberMapper.getById(id));
    }

    @Override
    public ApiResponse<Integer> add(MemberEntity member) {
        member.setId(SnowFlakeUtil.getLongId());
        int i = memberMapper.add(member);
        if (i == 0) {
            return ApiResponse.fail("添加失败");
        }
        return ApiResponse.success(i);
    }

    @Override
    public ApiResponse<Integer> deleteById(Long id) {
        int i = memberMapper.deleteById(id);
        if (i == 0) {
            return ApiResponse.fail("删除失败");
        }
        return ApiResponse.success(i);
    }

    @Override
    public ApiResponse<Integer> update(MemberEntity member) {
        int update = memberMapper.update(member);
        if (update == 0) {
            return ApiResponse.fail("更新失败");
        }
        return ApiResponse.success(update);
    }
}
