package cn.iocoder.yudao.module.member.service.group;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.group.vo.MemberGroupCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.group.vo.MemberGroupPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.group.vo.MemberGroupPageVO;
import cn.iocoder.yudao.module.member.controller.admin.group.vo.MemberGroupUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.group.vo.MemberGroupUserVo;
import cn.iocoder.yudao.module.member.controller.admin.group.vo.MemberGroupVO;
import cn.iocoder.yudao.module.member.controller.admin.group.vo.MemberUserVO;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserListVO;
import cn.iocoder.yudao.module.member.convert.group.MemberGroupConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.group.MemberGroupDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.group.MemberGroupMapper;
import cn.iocoder.yudao.module.member.service.card.CardService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.GROUP_HAS_USER;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.GROUP_NOT_EXISTS;

/**
 * 用户分组 Service 实现类
 *
 * @author owen
 */
@Service
@Validated
public class MemberGroupServiceImpl implements MemberGroupService {

    @Resource
    private MemberGroupMapper memberGroupMapper;

    @Resource
    private MemberUserService memberUserService;
    @Resource
    private CardService cardService;

    @Override
    public Long createGroup(MemberGroupCreateReqVO createReqVO) {
        // 插入
        MemberGroupDO group = MemberGroupConvert.INSTANCE.convert(createReqVO);
        memberGroupMapper.insert(group);
        // 返回
        return group.getId();
    }

    @Override
    public void updateGroup(MemberGroupUpdateReqVO updateReqVO) {
        // 校验存在
        validateGroupExists(updateReqVO.getId());
        // 更新
        MemberGroupDO updateObj = MemberGroupConvert.INSTANCE.convert(updateReqVO);
        memberGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteGroup(Long id) {
        // 校验存在
        validateGroupExists(id);
        // 校验分组下是否有用户
        validateGroupHasUser(id);
        // 删除
        memberGroupMapper.deleteById(id);
    }

    void validateGroupExists(Long id) {
        if (memberGroupMapper.selectById(id) == null) {
            throw exception(GROUP_NOT_EXISTS);
        }
    }

    void validateGroupHasUser(Long id) {
        Long count = memberUserService.getUserCountByGroupId(id);
        if (count > 0) {
            throw exception(GROUP_HAS_USER);
        }
    }

    @Override
    public MemberGroupDO getGroup(Long id) {
        return memberGroupMapper.selectById(id);
    }

    @Override
    public List<MemberGroupDO> getGroupList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberGroupMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberGroupDO> getGroupPage(MemberGroupPageReqVO pageReqVO) {
        return memberGroupMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberGroupDO> getGroupListByStatus(Integer status) {
        return memberGroupMapper.selectListByStatus(status);
    }

    @Override
    public void addMember(MemberGroupVO addVO) {
        memberGroupMapper.addMember(addVO.getGroupId(),addVO.getUserId());
    }

    @Override
    public PageResult<MemberUserVO> getMemberByGroupId(MemberGroupPageVO vo) {
        vo.setLimitOne((vo.getPageNo()-1)*vo.getPageSize());

        PageResult<MemberUserVO> result = new PageResult<>();
        List<MemberUserDO> list = memberGroupMapper.getMemberByGroupId(vo);
        ArrayList<MemberUserVO> userList = new ArrayList<>();
        for (MemberUserDO memberUserDO:list){
            BigDecimal money = cardService.getMoneyByUserId(memberUserDO.getId());
            MemberUserVO memberUserVO = new MemberUserVO();
            BeanUtil.copyProperties(memberUserDO,memberUserVO);
            memberUserVO.setMoney(money);
            userList.add(memberUserVO);
        }
        result.setList(userList);
        result.setTotal(memberGroupMapper.memberCount(vo));
        return result;
    }

    @Override
    public void deleteMember(MemberGroupVO deleteVO) {
        memberGroupMapper.deleteMember(deleteVO.getGroupId(),deleteVO.getUserId());
    }

    @Override
    public PageResult<MemberUserDO> getUserList(MemberUserListVO listVO) {
        //List<Long> memberList = memberGroupMapper.getMemberList(listVO.getGroupId());
        List<Long> memberList =  memberGroupMapper.getAllMember();
        listVO.setIds(memberList);
        PageResult<MemberUserDO> groupUserList = memberUserService.getGroupUserList(listVO);
        return groupUserList;

    }

    @Override
    public String getGroupNameByUserId(Long userId) {
        return memberGroupMapper.getGroupName(userId);
    }

    @Override
    public Map<Long, String> getGroupNameByUserIds(List<Long> userIds) {
        List<MemberGroupUserVo> groupList = memberGroupMapper.getGroupList(userIds);
        Map<Long, String> map = groupList.stream().collect(Collectors.toMap(MemberGroupUserVo::getMemberId, MemberGroupUserVo::getName));
        return map;
    }
}
