package com.fjhx.jushuitan.service.jushuitan.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.fjhx.jushuitan.entity.jushuitan.dto.JushuitanConfigDto;
import com.fjhx.jushuitan.entity.jushuitan.dto.JushuitanConfigSelectDto;
import com.fjhx.jushuitan.entity.jushuitan.po.JushuitanConfig;
import com.fjhx.jushuitan.entity.jushuitan.vo.JushuitanConfigVo;
import com.fjhx.jushuitan.mapper.jushuitan.JushuitanConfigMapper;
import com.fjhx.jushuitan.service.jushuitan.JushuitanConfigService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 聚水潭配置 服务实现类
 * </p>
 *
 * @author
 * @since 2024-01-18
 */
@Service
public class JushuitanConfigServiceImpl extends ServiceImpl<JushuitanConfigMapper, JushuitanConfig> implements JushuitanConfigService {

    @Autowired
    private ISysDeptService sysDeptService;

    @Override
    public Page<JushuitanConfigVo> getPage(JushuitanConfigSelectDto dto) {
        IWrapper<JushuitanConfig> wrapper = getWrapper();

        //部门id过滤
        wrapper.eq("jc", JushuitanConfig::getDeptId, dto.getDeptId());

        //关键字过滤
        String keyword = dto.getKeyword();
        if (ObjectUtil.isNotEmpty(dto.getKeyword())) {
            List<SysDept> deptKeyWord = sysDeptService.list(Wrappers.<SysDept>query().like("dept_name", keyword));
            List<Long> deptKwIds = deptKeyWord.stream().map(SysDept::getDeptId).collect(Collectors.toList());
            wrapper.and(q -> q
                    .like(JushuitanConfig::getCompanyName, keyword)
                    .or().like(JushuitanConfig::getAppKey, keyword)
                    .or().like(JushuitanConfig::getAppSecret, keyword)
                    .or().like(JushuitanConfig::getAccessToken, keyword)
                    .or().like(JushuitanConfig::getRefreshToken, keyword)
                    .or().like(JushuitanConfig::getRemark, keyword)
                    .or().in(JushuitanConfig::getDeptId, deptKwIds)
            );
        }

        wrapper.orderByDesc("jc", JushuitanConfig::getId);
        Page<JushuitanConfigVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<JushuitanConfigVo> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return page;
        }

        //获取部门信息
        List<Long> deptIds = records.stream().map(JushuitanConfig::getDeptId).collect(Collectors.toList());
        List<SysDept> deptList = sysDeptService.list(Wrappers.<SysDept>query().in("dept_id", deptIds));
        Map<Long, String> deprMap = deptList.stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName));

        for (JushuitanConfigVo record : records) {
            //赋值部门名称
            record.setDeptName(deprMap.get(record.getDeptId()));
        }
        return page;
    }

    @Override
    public JushuitanConfigVo detail(Long id) {
        JushuitanConfig JushuitanConfig = this.getById(id);
        JushuitanConfigVo result = BeanUtil.toBean(JushuitanConfig, JushuitanConfigVo.class);

        //赋值部门名称
        SysDept dept = sysDeptService.getOne(Wrappers.<SysDept>query().eq("dept_id", result.getDeptId()));
        if (ObjectUtil.isNotEmpty(dept)) {
            result.setDeptName(dept.getDeptName());
        }
        return result;
    }

    @Override
    public void add(JushuitanConfigDto jushuitanConfigDto) {
        this.save(jushuitanConfigDto);
    }

    @Override
    public void edit(JushuitanConfigDto jushuitanConfigDto) {
        this.updateById(jushuitanConfigDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
