package cn.stylefeng.team.portal.modular.management.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.portal.modular.management.entity.PortalAppLinkCategory;
import cn.stylefeng.team.portal.modular.management.enums.PortalAppLinkCategoryExceptionEnum;
import cn.stylefeng.team.portal.modular.management.mapper.PortalAppLinkCategoryMapper;
import cn.stylefeng.team.portal.modular.management.pojo.request.PortalAppLinkCategoryRequest;
import cn.stylefeng.team.portal.modular.management.service.PortalAppLinkCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class PortalAppLinkCategoryServiceImpl extends ServiceImpl<PortalAppLinkCategoryMapper, PortalAppLinkCategory> implements PortalAppLinkCategoryService {

    @Override
    public void add(PortalAppLinkCategoryRequest portalAppLinkCategoryRequest) {
        PortalAppLinkCategory portalAppLinkCategory = new PortalAppLinkCategory();

        portalAppLinkCategory.setDesignId(portalAppLinkCategoryRequest.getDesignId());
        portalAppLinkCategory.setCategoryName(portalAppLinkCategoryRequest.getCategoryName());
        portalAppLinkCategory.setFldSort(portalAppLinkCategoryRequest.getFldSort());

        this.save(portalAppLinkCategory);
    }

    @Override
    public void del(PortalAppLinkCategoryRequest portalAppLinkCategoryRequest) {
        PortalAppLinkCategory portalAppLinkCategory = this.queryPortalAppLinkCategory(portalAppLinkCategoryRequest);
        this.removeById(portalAppLinkCategory.getCategoryId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(PortalAppLinkCategoryRequest portalAppLinkCategoryRequest) {
        this.removeByIds(portalAppLinkCategoryRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(PortalAppLinkCategoryRequest portalAppLinkCategoryRequest) {
        PortalAppLinkCategory oldCategory = this.queryPortalAppLinkCategory(portalAppLinkCategoryRequest);
        oldCategory.setCategoryName(portalAppLinkCategoryRequest.getCategoryName());
        oldCategory.setFldSort(portalAppLinkCategoryRequest.getFldSort());
        this.updateById(oldCategory);
    }

    @Override
    public PortalAppLinkCategory detail(PortalAppLinkCategoryRequest portalAppLinkCategoryRequest) {
        return this.queryPortalAppLinkCategory(portalAppLinkCategoryRequest);
    }

    @Override
    public List<PortalAppLinkCategory> findList(PortalAppLinkCategoryRequest portalAppLinkCategoryRequest) {
        LambdaQueryWrapper<PortalAppLinkCategory> wrapper = this.createWrapper(portalAppLinkCategoryRequest);
        wrapper.select(PortalAppLinkCategory::getCategoryId, PortalAppLinkCategory::getCategoryName);
        return this.list(wrapper);
    }

    @Override
    public Map<Long, String> getAppLinkCategoryNameMap(List<Long> categoryIdList) {
        if (ObjectUtil.isEmpty(categoryIdList)) {
            return new HashMap<>();
        }

        LambdaQueryWrapper<PortalAppLinkCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PortalAppLinkCategory::getCategoryId, categoryIdList);
        queryWrapper.select(PortalAppLinkCategory::getCategoryId, PortalAppLinkCategory::getCategoryName);
        List<PortalAppLinkCategory> portalAppLinkCategories = this.list(queryWrapper);
        return portalAppLinkCategories.stream().collect(Collectors.toMap(PortalAppLinkCategory::getCategoryId, PortalAppLinkCategory::getCategoryName));
    }

    private PortalAppLinkCategory queryPortalAppLinkCategory(PortalAppLinkCategoryRequest portalAppLinkCategoryRequest) {
        PortalAppLinkCategory portalAppLinkCategory = this.getById(portalAppLinkCategoryRequest.getCategoryId());
        if (ObjectUtil.isEmpty(portalAppLinkCategory)) {
            throw new ServiceException(PortalAppLinkCategoryExceptionEnum.PORTAL_APP_LINK_CATEGORY_NOT_EXISTED);
        }
        return portalAppLinkCategory;
    }

    private LambdaQueryWrapper<PortalAppLinkCategory> createWrapper(PortalAppLinkCategoryRequest portalAppLinkCategoryRequest) {
        LambdaQueryWrapper<PortalAppLinkCategory> queryWrapper = new LambdaQueryWrapper<>();

        Long designId = portalAppLinkCategoryRequest.getDesignId();
        queryWrapper.eq(PortalAppLinkCategory::getDesignId, designId);

        String searchText = portalAppLinkCategoryRequest.getSearchText();
        queryWrapper.like(ObjectUtil.isNotEmpty(searchText), PortalAppLinkCategory::getCategoryName, searchText);

        queryWrapper.orderByAsc(PortalAppLinkCategory::getFldSort);

        return queryWrapper;
    }

}
