package com.starry.admin.modules.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.shop.mapper.PlayCommodityInfoMapper;
import com.starry.admin.modules.shop.module.entity.PlayCommodityInfoEntity;
import com.starry.admin.modules.shop.module.vo.PlayCommodityInfoVo;
import com.starry.admin.modules.shop.service.IPlayCommodityInfoService;
import com.starry.admin.modules.weichat.entity.PlayCommodityReturnVo;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.utils.ConvertUtil;
import com.starry.common.utils.IdUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 服务项目Service业务层处理
 *
 * @author admin
 * @since 2024-03-31
 */
@Service
public class PlayCommodityInfoServiceImpl extends ServiceImpl<PlayCommodityInfoMapper, PlayCommodityInfoEntity>
        implements
            IPlayCommodityInfoService {
    @Resource
    private PlayCommodityInfoMapper playCommodityInfoMapper;

    @Override
    public void initPlayCommodityInfo(String tenantId) {
        LambdaQueryWrapper<PlayCommodityInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayCommodityInfoEntity::getTenantId, tenantId);
        List<PlayCommodityInfoEntity> list = playCommodityInfoMapper.selectList(lambdaQueryWrapper);
        if (list == null || list.isEmpty()) {
            // 查询默认租户的服务信息，然后复制其属性
            SecurityUtils.setTenantId("default");
            LambdaQueryWrapper<PlayCommodityInfoEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(PlayCommodityInfoEntity::getPId, "00");
            for (PlayCommodityInfoEntity entity : playCommodityInfoMapper.selectList(lambdaQueryWrapper1)) {
                initPlayCommodityInfo(entity, tenantId);
            }
        }
    }

    public void initPlayCommodityInfo(PlayCommodityInfoEntity entity, String tenantId) {
        LambdaQueryWrapper<PlayCommodityInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayCommodityInfoEntity::getPId, entity.getId());
        entity.setId(IdUtils.getUuid());
        entity.setTenantId(tenantId);
        this.baseMapper.insert(entity);
        List<PlayCommodityInfoEntity> list = playCommodityInfoMapper.selectList(lambdaQueryWrapper);
        for (PlayCommodityInfoEntity commodityInfo : list) {
            commodityInfo.setPId(entity.getId());
            initPlayCommodityInfo(commodityInfo, tenantId);
        }
    }

    @Override
    public List<PlayCommodityReturnVo> selectTree() {
        MPJLambdaWrapper<PlayCommodityInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(PlayCommodityInfoEntity.class);
        lambdaQueryWrapper.orderByDesc(PlayCommodityInfoEntity::getSort);
        List<PlayCommodityInfoEntity> list = this.baseMapper.selectJoinList(PlayCommodityInfoEntity.class,
                lambdaQueryWrapper);
        Map<String, List<PlayCommodityInfoEntity>> collect = list.stream().filter(a -> a != null && a.getId() != null)
                .collect(Collectors.groupingBy(PlayCommodityInfoEntity::getPId));
        return this.assembleTree(collect, collect.get("00"));
    }

    /**
     * 组装数据
     *
     * @param data
     *            数据，key=区域编码,value=区域列表
     * @param list
     *            区域略表
     * @return List<com.starry.admin.modules.system.entity.SysAdministrativeAreaDictInfoEntity>
     * @author admin
     * @since 2024/4/10 15:14
     **/
    public List<PlayCommodityReturnVo> assembleTree(Map<String, List<PlayCommodityInfoEntity>> data,
            List<PlayCommodityInfoEntity> list) {
        if (list == null) {
            return new ArrayList<>();
        }
        List<PlayCommodityReturnVo> result = ConvertUtil.entityToVoList(list, PlayCommodityReturnVo.class);
        for (PlayCommodityReturnVo entity : result) {
            entity.setChild(assembleTree(data, data.get(entity.getId())));
        }
        return result;
    }

    @Override
    public List<PlayCommodityInfoEntity> selectByType() {
        LambdaQueryWrapper<PlayCommodityInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String tenantId = SecurityUtils.getTenantId();
        if (StrUtil.isNotBlank(tenantId)) {
            lambdaQueryWrapper.eq(PlayCommodityInfoEntity::getTenantId, tenantId);
        }
        lambdaQueryWrapper.eq(PlayCommodityInfoEntity::getPId, "00");
        lambdaQueryWrapper.orderByDesc(PlayCommodityInfoEntity::getSort);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<PlayCommodityInfoEntity> selectAll() {
        return this.baseMapper.selectList(new LambdaQueryWrapper<>());
    }

    @Override
    public PlayCommodityInfoVo queryCommodityInfo(String id, String levelId) {
        PlayCommodityInfoVo vo = this.baseMapper.queryCommodityInfo(id, levelId);
        if (vo == null) {
            throw new CustomException("服务项目不存在");
        }
        return vo;
    }

    /**
     * 查询服务项目
     *
     * @param id
     *            服务项目主键
     * @return 服务项目
     */
    @Override
    public PlayCommodityInfoEntity selectPlayCommodityInfoById(String id) {
        PlayCommodityInfoEntity entity = this.baseMapper.selectById(id);
        if (entity == null) {
            throw new CustomException("服务项目不存在");
        }
        return entity;
    }

    @Override
    public List<PlayCommodityInfoEntity> selectByPId(String pId) {
        LambdaQueryWrapper<PlayCommodityInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayCommodityInfoEntity::getPId, pId);
        lambdaQueryWrapper.orderByDesc(PlayCommodityInfoEntity::getSort);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 查询服务项目列表
     *
     * @param playCommodityInfo
     *            服务项目
     * @return 服务项目
     */
    @Override
    public IPage<PlayCommodityInfoEntity> selectPlayCommodityInfoByPage(PlayCommodityInfoEntity playCommodityInfo) {
        Page<PlayCommodityInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增服务项目
     *
     * @param playCommodityInfo
     *            服务项目
     * @return 结果
     */
    @Override
    public boolean create(PlayCommodityInfoEntity playCommodityInfo) {
        if (StrUtil.isBlankIfStr(playCommodityInfo.getId())) {
            playCommodityInfo.setId(IdUtils.getUuid());
        }
        return save(playCommodityInfo);
    }

    /**
     * 修改服务项目
     *
     * @param playCommodityInfo
     *            服务项目
     * @return 结果
     */
    @Override
    public boolean update(PlayCommodityInfoEntity playCommodityInfo) {
        return updateById(playCommodityInfo);
    }

    /**
     * 批量删除服务项目
     *
     * @param ids
     *            需要删除的服务项目主键
     * @return 结果
     */
    @Override
    public int deletePlayCommodityInfoByIds(String[] ids) {
        return playCommodityInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除服务项目信息
     *
     * @param id
     *            服务项目主键
     * @return 结果
     */
    @Override
    public int deletePlayCommodityInfoById(String id) {
        return playCommodityInfoMapper.deleteById(id);
    }
}
