package com.xiaoniu.system.operation.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoniu.common.core.constant.Constants;
import com.xiaoniu.common.core.exception.ServiceException;
import com.xiaoniu.common.core.resdata.RowsData;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.common.core.utils.bean.BeanUtils;
import com.xiaoniu.common.core.vo.LabelAndValueVO;
import com.xiaoniu.common.security.utils.SecurityUtils;
import com.xiaoniu.system.operation.member.domain.TbMember;
import com.xiaoniu.system.operation.member.dto.AddMemberDTO;
import com.xiaoniu.system.operation.member.dto.EditMemberDTO;
import com.xiaoniu.system.operation.member.dto.MemberDetailsVO;
import com.xiaoniu.system.operation.member.dto.MemberListQueryDTO;
import com.xiaoniu.system.operation.member.mapper.TbMemberMapper;
import com.xiaoniu.system.operation.member.service.ITbMemberService;
import com.xiaoniu.system.operation.member.vo.MemberListVO;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员权益说明 服务实现类
 * </p>
 *
 * @author haoxuyang
 * @since 2024-10-17 09:52:31
 */
@Service
public class TbMemberServiceImpl extends ServiceImpl<TbMemberMapper, TbMember> implements ITbMemberService {

    @Override
    public RowsData<MemberListVO> memberList(MemberListQueryDTO dto) {
        if (StringUtils.isNotNull(dto.getMemberStatus()) && dto.getMemberStatus().equals(0)) {
            dto.setMemberStatus(null);
        }
        try (Page<Object> page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize())) {
            List<MemberListVO> list = baseMapper.getMemberList(dto);
            return new RowsData<>(list, page.getTotal(), dto.getPageNum());
        }
    }

    @Override
    public void addMember(AddMemberDTO dto) {
        if (StringUtils.isEmpty(dto.getMemberType())) {
            throw new ServiceException("会员类型不能为空");
        }
        TbMember tbMember = new TbMember();
        BeanUtils.copyProperties(dto, tbMember);
        tbMember.setMemberType(dto.getMemberType());
        tbMember.setCreateTime(new Date());
        try {
            tbMember.setCreateBy(SecurityUtils.getUsername());
        } catch (Exception e) {
            log.error("获取当前登陆人id失败");
        }
        baseMapper.insert(tbMember);
    }

    @Override
    public void editMember(EditMemberDTO dto) {
        if (StringUtils.isEmpty(dto.getMemberType())) {
            throw new ServiceException("会员类型不能为空");
        }
        TbMember tbMember = baseMapper.selectById(dto.getId());
        if (StringUtils.isNull(tbMember)) {
            throw new ServiceException("当前会员权益不存在");
        }
        if (Constants.TWO.equals(tbMember.getDelFlag())) {
            throw new ServiceException("当前会员权益已被删除");
        }
        BeanUtils.copyProperties(dto, tbMember);
        tbMember.setMemberType(dto.getMemberType());
        tbMember.setUpdateTime(new Date());
        try {
            tbMember.setUpdateBy(SecurityUtils.getUsername());
        } catch (Exception e) {
            log.error("获取当前登陆人id失败");
        }
        baseMapper.updateById(tbMember);

    }

    @Override
    public MemberDetailsVO details(Long id) {
        TbMember tbMember = baseMapper.selectById(id);
        if (StringUtils.isNull(tbMember)) {
            throw new ServiceException("当前会员权益不存在");
        }
        if (Constants.TWO.equals(tbMember.getDelFlag())) {
            throw new ServiceException("当前会员权益已被删除");
        }
        MemberDetailsVO detailsVO = new MemberDetailsVO();
        BeanUtils.copyProperties(tbMember, detailsVO);
        return detailsVO;
    }

    @Override
    public void delMember(Long id) {
        baseMapper.update(new LambdaUpdateWrapper<TbMember>().eq(TbMember::getId, id).set(TbMember::getDelFlag, Constants.TWO));
    }

    @Override
    public List<LabelAndValueVO> getMemberList(String memberName) {
        LambdaQueryWrapper<TbMember> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(memberName)) {
            wrapper.like(TbMember::getMemberName, memberName);
        }
        wrapper.eq(TbMember::getMemberStatus, Constants.ONE);
        wrapper.eq(TbMember::getDelFlag, Constants.ZERO);
        wrapper.orderByAsc(TbMember::getId);
        wrapper.orderByDesc(TbMember::getCreateTime);
        List<TbMember> tbMembers = baseMapper.selectList(wrapper);
        return tbMembers.stream().map(item -> {
            LabelAndValueVO labelAndValueVO = new LabelAndValueVO();
            labelAndValueVO.setLabel(item.getMemberName());
            labelAndValueVO.setValue(String.valueOf(item.getId()));
            return labelAndValueVO;
        }).toList();
    }
}
