package com.ruoyi.zhushi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.zhushi.entity.*;
import com.ruoyi.zhushi.mapper.DkAttendanceGroupAndUserMapper;
import com.ruoyi.zhushi.mapper.DkAttendanceGroupMapper;
import com.ruoyi.zhushi.service.DkAttendanceGroupService;
import com.ruoyi.zhushi.util.Constans;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DkAttendanceGroupServiceImpl implements DkAttendanceGroupService {


    @Autowired
    private DkAttendanceGroupMapper dkAttendanceGroupMapper;

    @Autowired
    private DkAttendanceGroupAndUserMapper dkAttendanceGroupAndUserMapper;


    @Override
    public TableDataInfo<DkAttendanceGroupDTO> queryPageList(DkAttendanceGroupDTO dkAttendanceGroupDTO) {
        List<DkAttendanceGroupDTO> dkAttendanceGroupDTOS = new ArrayList<>();
        List<DkAttendanceGroup> dkAttendanceGroups = dkAttendanceGroupMapper.selectList();
        for (DkAttendanceGroup dkAttendanceGroup : dkAttendanceGroups) {
            DkAttendanceGroupDTO dkAttendanceGroupDTO1 = BeanUtil.toBean(dkAttendanceGroup, DkAttendanceGroupDTO.class);
            LambdaQueryWrapper<DkAttendanceGroupAndUser> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(DkAttendanceGroupAndUser::getAttendanceTeamId, dkAttendanceGroup.getId());

            List<DkAttendanceGroupAndUser> dkUsers = dkAttendanceGroupAndUserMapper.selectList(queryWrapper);
            dkAttendanceGroupDTO1.setMembers(dkUsers);
            dkAttendanceGroupDTOS.add(dkAttendanceGroupDTO1);
        }
        return TableDataInfo.build(dkAttendanceGroupDTOS);
    }

    @Override
    public String add(DkAttendanceGroupDTO dkAttendanceGroupDTO) {
        StringBuffer stringBuffer = new StringBuffer();
        List<DkAttendanceGroupAndUser> members = new ArrayList<>();
        dkAttendanceGroupDTO.getMembers().forEach(e -> {
            LambdaQueryWrapper<DkAttendanceGroupAndUser> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(DkAttendanceGroupAndUser::getUserId, e.getUserId());
            List<DkAttendanceGroupAndUser> dkAttendanceGroupAndUsers = dkAttendanceGroupAndUserMapper.selectList(queryWrapper);
            if(dkAttendanceGroupAndUsers.size() > 0){
                if(dkAttendanceGroupAndUsers.get(0).getAttendanceTeamId() != dkAttendanceGroupDTO.getId()){
                    stringBuffer.append(e.getUserName() + "已加入" + dkAttendanceGroupAndUsers.get(0).getDeptName());
                    stringBuffer.append(StringUtils.repeat(" ", 5)); // 添加10个空格
                }
            }else{
                members.add(e);
            }
        });
//        if(stringBuffer.length() > 0){
//            throw new RuntimeException(stringBuffer.toString());
//        }
        DkAttendanceGroup dkAttendanceGroup = BeanUtil.toBean(dkAttendanceGroupDTO, DkAttendanceGroup.class);
        dkAttendanceGroupMapper.insertOrUpdate(dkAttendanceGroup);

        if(members.size() > 0){
            members.forEach(e -> {
                DkAttendanceGroupAndUser dkAttendanceGroupAndUser = new DkAttendanceGroupAndUser();
                dkAttendanceGroupAndUser.setAttendanceTeamId(dkAttendanceGroup.getId());
                dkAttendanceGroupAndUser.setUserId(e.getUserId());
                dkAttendanceGroupAndUser.setUserName(e.getUserName());
                dkAttendanceGroupAndUser.setDeptName(e.getDeptName());
                dkAttendanceGroupAndUserMapper.insertOrUpdate(dkAttendanceGroupAndUser);
            });
        }
        if(stringBuffer.length() > 0){
            return stringBuffer.toString();
        }else {
            return Constans.SUCCESS;
        }
    }

    @Override
    public int del(long id) {
        return dkAttendanceGroupMapper.deleteById(id);
    }

    public AppDTO queryAttendanceGroupBYUserId(long userId){
        AppDTO appDTO;
        appDTO = dkAttendanceGroupMapper.queryAttendanceGroupBYUserId(userId);
        return appDTO;
    }

    @Override
    public int deleteConfig(long id) {
//        LambdaQueryWrapper<DkAttendanceGroupAndUser> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(DkAttendanceGroupAndUser::getUserId, userId);
        return dkAttendanceGroupAndUserMapper.deleteById(id);

    }
}
