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.toolkit.Wrappers;
import com.ruoyi.common.annotation.Translation;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.zhushi.entity.*;
import com.ruoyi.zhushi.mapper.NjRuleConfigAndUserMapper;
import com.ruoyi.zhushi.mapper.NjRuleConfigMapper;
import com.ruoyi.zhushi.service.NjRuleConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class NjRuleConfigServiceImpl implements NjRuleConfigService {

    @Autowired
    private NjRuleConfigMapper njRuleConfigMapper;

    @Autowired
    private NjRuleConfigAndUserMapper njRuleConfigAndUserMapper;

    @Override
    public TableDataInfo<NjRuleConfigDTO> queryRuleConfig(NjRuleConfigDTO njRuleConfigDTO) {

        LambdaQueryWrapper<NjRuleConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(null != njRuleConfigDTO.getRuleName(), NjRuleConfig::getRuleName, njRuleConfigDTO.getRuleName());
        List<NjRuleConfigDTO> njRuleConfigDTOS = njRuleConfigMapper.selectVoList(queryWrapper, NjRuleConfigDTO.class);
        njRuleConfigDTOS.forEach(nj -> {
            if(!nj.getApplicableRange()){
                List<DkUsers> users = new ArrayList<>();
                LambdaQueryWrapper<NjRuleConfigAndUser> njRuleConfigAndUserLambdaQueryWrapper = Wrappers.lambdaQuery();
                njRuleConfigAndUserLambdaQueryWrapper.eq(NjRuleConfigAndUser::getNjRuleConfigId, nj.getId());
                List<NjRuleConfigAndUserDTO> njRuleConfigAndUserDTOS = njRuleConfigAndUserMapper.selectVoList(njRuleConfigAndUserLambdaQueryWrapper, NjRuleConfigAndUserDTO.class);
                njRuleConfigAndUserDTOS.forEach(njRuleConfigAndUser -> {
                    DkUsers dkUsers = new DkUsers();
                    BeanUtil.copyProperties(njRuleConfigAndUser, dkUsers);
                    users.add(dkUsers);
                });
                nj.setMembers(users);
            }
        });

        return TableDataInfo.build(njRuleConfigDTOS);
    }

    @Override
    public Boolean addRule(NjRuleConfigDTO njRuleConfigDTO) {
        NjRuleConfig njRuleConfig = new NjRuleConfig();
        BeanUtil.copyProperties(njRuleConfigDTO, njRuleConfig);
        njRuleConfigMapper.insertOrUpdate(njRuleConfig);

        List<NjRuleConfigAndUser> njRuleConfigAndUsers = new ArrayList<>();
        if(null != njRuleConfigDTO.getMembers() && njRuleConfigDTO.getMembers().size() > 0){
            njRuleConfigDTO.getMembers()
                .forEach(user -> {
                NjRuleConfigAndUser njRuleConfigAndUser = new NjRuleConfigAndUser();
                njRuleConfigAndUser.setNjRuleConfigId(njRuleConfig.getId());
                njRuleConfigAndUser.setRuleName(njRuleConfig.getRuleName());
                njRuleConfigAndUser.setUserId(user.getUserId());
                njRuleConfigAndUser.setUserName(user.getUserName());
                njRuleConfigAndUser.setNickName(user.getNickName());
                njRuleConfigAndUsers.add(njRuleConfigAndUser);
            });
        }
        return njRuleConfigAndUserMapper.insertOrUpdateBatch(njRuleConfigAndUsers);

    }

    @Override
    public int delRule(long id) {
        return njRuleConfigMapper.deleteById(id);
    }

    @Override
    public int delUser(long id) {
        return njRuleConfigAndUserMapper.deleteById(id);
    }
}
