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.CommonAppLinkCategory;
import cn.stylefeng.team.portal.modular.management.enums.CommonAppLinkCategoryExceptionEnum;
import cn.stylefeng.team.portal.modular.management.mapper.CommonAppLinkCategoryMapper;
import cn.stylefeng.team.portal.modular.management.pojo.request.CommonAppLinkCategoryRequest;
import cn.stylefeng.team.portal.modular.management.service.CommonAppLinkCategoryService;
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 CommonAppLinkCategoryServiceImpl extends ServiceImpl<CommonAppLinkCategoryMapper, CommonAppLinkCategory> implements CommonAppLinkCategoryService {

    @Override
    public void add(CommonAppLinkCategoryRequest commonAppLinkCategoryRequest) {
        CommonAppLinkCategory portalAppLinkCategory = new CommonAppLinkCategory();

        portalAppLinkCategory.setIconType(commonAppLinkCategoryRequest.getIconType());
        portalAppLinkCategory.setIconCode(commonAppLinkCategoryRequest.getIconCode());
        portalAppLinkCategory.setCategoryIconFileId(commonAppLinkCategoryRequest.getCategoryIconFileId());
        portalAppLinkCategory.setCategoryName(commonAppLinkCategoryRequest.getCategoryName());
        portalAppLinkCategory.setFldSort(commonAppLinkCategoryRequest.getFldSort());

        this.save(portalAppLinkCategory);
    }

    @Override
    public void del(CommonAppLinkCategoryRequest commonAppLinkCategoryRequest) {
        CommonAppLinkCategory commonAppLinkCategory = this.queryCommonAppLinkCategory(commonAppLinkCategoryRequest);
        this.removeById(commonAppLinkCategory.getCategoryId());
    }

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

    @Override
    public void edit(CommonAppLinkCategoryRequest commonAppLinkCategoryRequest) {
        CommonAppLinkCategory oldCategory = this.queryCommonAppLinkCategory(commonAppLinkCategoryRequest);
        oldCategory.setIconType(commonAppLinkCategoryRequest.getIconType());
        oldCategory.setIconCode(commonAppLinkCategoryRequest.getIconCode());
        oldCategory.setCategoryIconFileId(commonAppLinkCategoryRequest.getCategoryIconFileId());
        oldCategory.setCategoryName(commonAppLinkCategoryRequest.getCategoryName());
        oldCategory.setFldSort(commonAppLinkCategoryRequest.getFldSort());
        this.updateById(oldCategory);
    }

    @Override
    public CommonAppLinkCategory detail(CommonAppLinkCategoryRequest commonAppLinkCategoryRequest) {
        return this.queryCommonAppLinkCategory(commonAppLinkCategoryRequest);
    }

    @Override
    public List<CommonAppLinkCategory> findList(CommonAppLinkCategoryRequest commonAppLinkCategoryRequest) {
        LambdaQueryWrapper<CommonAppLinkCategory> wrapper = this.createWrapper(commonAppLinkCategoryRequest);
        return this.list(wrapper);
    }

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

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

    private CommonAppLinkCategory queryCommonAppLinkCategory(CommonAppLinkCategoryRequest commonAppLinkCategoryRequest) {
        CommonAppLinkCategory commonAppLinkCategory = this.getById(commonAppLinkCategoryRequest.getCategoryId());
        if (ObjectUtil.isEmpty(commonAppLinkCategory)) {
            throw new ServiceException(CommonAppLinkCategoryExceptionEnum.COMMON_APP_LINK_CATEGORY_NOT_EXISTED);
        }
        return commonAppLinkCategory;
    }

    private LambdaQueryWrapper<CommonAppLinkCategory> createWrapper(CommonAppLinkCategoryRequest commonAppLinkCategoryRequest) {
        LambdaQueryWrapper<CommonAppLinkCategory> queryWrapper = new LambdaQueryWrapper<>();

        String searchText = commonAppLinkCategoryRequest.getSearchText();
        queryWrapper.like(ObjectUtil.isNotEmpty(searchText), CommonAppLinkCategory::getCategoryName, searchText);

        queryWrapper.orderByAsc(CommonAppLinkCategory::getFldSort);

        return queryWrapper;
    }

}
