package com.ruoyi.xq.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.xq.domain.ActivityShop;
import com.ruoyi.xq.domain.AreaCode;
import com.ruoyi.xq.domain.Group;
import com.ruoyi.xq.domain.GroupOrder;
import com.ruoyi.xq.dto.app.activity.ActivityShopAppInfo;
import com.ruoyi.xq.dto.app.group.GroupInfoAppVo;
import com.ruoyi.xq.dto.app.group.GroupListAppVo;
import com.ruoyi.xq.dto.app.group.GroupQuery;
import com.ruoyi.xq.mapper.GroupMapper;
import com.ruoyi.xq.service.AreaCodeService;
import com.ruoyi.xq.service.GroupOrderService;
import com.ruoyi.xq.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 相亲群Service业务层处理
 *
 * @author 77
 * @date 2024-05-15
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper,Group> implements GroupService {

    @Autowired
    private AreaCodeService areaCodeService;
    @Autowired
    private GroupOrderService groupOrderService;

    @Override
    public void removeData(List<Long> ids) {
        this.update(Wrappers.lambdaUpdate(Group.class)
                .in(Group::getId, ids)
                .set(Group::getDeleteFlag, 1));
    }

    @Override
    public void updateData(Group bo) {
        AreaCode areaCode = areaCodeService.getById(bo.getCityCode());
        bo.setCityName(areaCode.getName());
        this.updateById(bo);
    }

    @Override
    public void saveData(Group bo) {
        AreaCode areaCode = areaCodeService.getById(bo.getCityCode());
        bo.setCityName(areaCode.getName());
        this.save(bo);
    }

    @Override
    public Page<GroupListAppVo> pageApp(PageQuery pageQuery, GroupQuery groupQuery) {
        return baseMapper.pageApp(pageQuery.build(), groupQuery);
    }

    @Override
    public GroupInfoAppVo info(Long groupId) {
        Long userId = LoginHelper.getUserId();
        Group group = this.getById(groupId);
        GroupInfoAppVo vo = new GroupInfoAppVo();
        vo.setGroupRemark(group.getGroupRemark());
        vo.setJoinStatus(0);
        vo.setId(group.getId());
        vo.setGroupImage(group.getGroupImage());
        vo.setGroupName(group.getGroupName());
        vo.setGroupWomanRate(group.getGroupWomanRate());
        vo.setGroupManRate(group.getGroupManRate());
        vo.setCityName(group.getCityName());
        vo.setCityCode(group.getCityCode());
        vo.setJoinPrice(group.getJoinPrice());
        if(userId != null){
            GroupOrder groupOrder = groupOrderService.getSuccessByGroupIdAndUserId(groupId,userId);
            if(groupOrder != null){
                vo.setJoinStatus(1);
            }
        }
        return vo;
    }
}
