package com.fjhx.jushuitan.service.shop.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.shop.dto.ShopInfoDto;
import com.fjhx.jushuitan.entity.shop.dto.ShopInfoSelectDto;
import com.fjhx.jushuitan.entity.shop.po.ShopInfo;
import com.fjhx.jushuitan.entity.shop.vo.ShopInfoVo;
import com.fjhx.jushuitan.mapper.shop.ShopInfoMapper;
import com.fjhx.jushuitan.service.shop.ShopInfoService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.framework.mybatis.holder.TenantHolder;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 店铺管理 服务实现类
 * </p>
 *
 * @author
 * @since 2024-01-07
 */
@Service
public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> implements ShopInfoService {

    @Autowired
    private ISysDeptService deptService;

    @Override
    public Page<ShopInfoVo> getPage(ShopInfoSelectDto dto) {
        IWrapper<ShopInfo> wrapper = getWrapper();

        //关键字检索
        if (ObjectUtil.isNotEmpty(dto.getKeyword())) {
            TenantHolder.setIgnore(true);
            List<SysDept> deptList = deptService.list(Wrappers.<SysDept>query()
                    .like("dept_name", dto.getKeyword())
            );
            TenantHolder.clear();
            List<Long> deptIds = deptList.stream().map(SysDept::getDeptId).collect(Collectors.toList());
            wrapper.and(q -> q
                    .like(ShopInfo::getCode, dto.getKeyword())
                    .or().like(ShopInfo::getName, dto.getKeyword())
                    .or().in(ShopInfo::getDeptId, deptIds)
            );
        }

        wrapper.orderByDesc("si", ShopInfo::getId);
        Page<ShopInfoVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);

        if (ObjectUtil.isEmpty(page)) {
            return page;
        }
        List<ShopInfoVo> records = page.getRecords();

        List<Long> deptIds = records.stream().map(ShopInfo::getDeptId).collect(Collectors.toList());

        Map<Long, String> deptMap = new HashMap<>();
        if (ObjectUtil.isNotEmpty(deptIds)) {
            TenantHolder.setIgnore(true);
            List<SysDept> deptList = deptService.list(Wrappers.<SysDept>query().in("dept_id", deptIds));
            TenantHolder.clear();
            deptMap = deptList.stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName));
        }

        for (ShopInfoVo record : records) {
            //赋值部门id
            record.setDeptName(deptMap.get(record.getDeptId()));
        }

        return page;
    }

    @Override
    public ShopInfoVo detail(Long id) {
        ShopInfo ShopInfo = this.getById(id);
        ShopInfoVo result = BeanUtil.toBean(ShopInfo, ShopInfoVo.class);
        return result;
    }

    @Override
    public void add(ShopInfoDto shopInfoDto) {
        this.save(shopInfoDto);
    }

    @Override
    public void edit(ShopInfoDto shopInfoDto) {
        this.updateById(shopInfoDto);
    }

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

}
