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

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.portal.modular.management.entity.PortalAppLink;
import cn.stylefeng.team.portal.modular.management.enums.PortalAppLinkExceptionEnum;
import cn.stylefeng.team.portal.modular.management.factory.AppLinkFactory;
import cn.stylefeng.team.portal.modular.management.mapper.PortalAppLinkMapper;
import cn.stylefeng.team.portal.modular.management.pojo.request.PortalAppLinkRequest;
import cn.stylefeng.team.portal.modular.management.service.PortalAppLinkCategoryService;
import cn.stylefeng.team.portal.modular.management.service.PortalAppLinkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class PortalAppLinkServiceImpl extends ServiceImpl<PortalAppLinkMapper, PortalAppLink> implements PortalAppLinkService {

    @Resource
    private PortalAppLinkCategoryService portalAppLinkCategoryService;

    @Override
    public void add(PortalAppLinkRequest portalAppLinkRequest) {

        this.validateAppLink(portalAppLinkRequest, false);

        PortalAppLink portalAppLink = new PortalAppLink();
        AppLinkFactory.commonFillParam(portalAppLinkRequest, portalAppLink);
        this.save(portalAppLink);
    }

    @Override
    public void del(PortalAppLinkRequest portalAppLinkRequest) {
        PortalAppLink portalAppLink = this.queryPortalAppLink(portalAppLinkRequest);
        this.removeById(portalAppLink.getAppLinkId());
    }

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

    @Override
    public void edit(PortalAppLinkRequest portalAppLinkRequest) {

        this.validateAppLink(portalAppLinkRequest, true);

        PortalAppLink portalAppLink = this.queryPortalAppLink(portalAppLinkRequest);
        AppLinkFactory.commonFillParam(portalAppLinkRequest, portalAppLink);
        this.updateById(portalAppLink);
    }

    @Override
    public PortalAppLink detail(PortalAppLinkRequest portalAppLinkRequest) {
        return this.queryPortalAppLink(portalAppLinkRequest);
    }

    @Override
    public PageResult<PortalAppLink> findPage(PortalAppLinkRequest portalAppLinkRequest) {
        LambdaQueryWrapper<PortalAppLink> wrapper = createWrapper(portalAppLinkRequest);

        wrapper.select(PortalAppLink::getAppLinkId, PortalAppLink::getLinkName,
                PortalAppLink::getLinkIconFileId, PortalAppLink::getLinkCategoryId,
                PortalAppLink::getRedirectType, PortalAppLink::getRedirectUrl,
                PortalAppLink::getOpenType, PortalAppLink::getFldSort, BaseEntity::getCreateTime);

        Page<PortalAppLink> pageList = this.page(PageFactory.defaultPage(), wrapper);
        List<PortalAppLink> records = pageList.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return PageResultFactory.createPageResult(pageList);
        }

        Map<Long, String> appLinkCategoryNameMap = portalAppLinkCategoryService.getAppLinkCategoryNameMap(records.stream().map(PortalAppLink::getLinkCategoryId).toList());
        for (PortalAppLink record : records) {
            Long linkCategoryId = record.getLinkCategoryId();
            String appLinkCategoryName = appLinkCategoryNameMap.get(linkCategoryId);
            record.setLinkCategoryName(appLinkCategoryName);
        }

        return PageResultFactory.createPageResult(pageList);
    }

    private PortalAppLink queryPortalAppLink(PortalAppLinkRequest portalAppLinkRequest) {
        PortalAppLink portalAppLink = this.getById(portalAppLinkRequest.getAppLinkId());
        if (ObjectUtil.isEmpty(portalAppLink)) {
            throw new ServiceException(PortalAppLinkExceptionEnum.PORTAL_APP_LINK_NOT_EXISTED);
        }
        return portalAppLink;
    }

    private LambdaQueryWrapper<PortalAppLink> createWrapper(PortalAppLinkRequest portalAppLinkRequest) {
        LambdaQueryWrapper<PortalAppLink> queryWrapper = new LambdaQueryWrapper<>();

        Long designId = portalAppLinkRequest.getDesignId();
        queryWrapper.eq(PortalAppLink::getDesignId, designId);

        Long linkCategoryId = portalAppLinkRequest.getLinkCategoryId();
        queryWrapper.eq(ObjectUtil.isNotNull(linkCategoryId), PortalAppLink::getLinkCategoryId, linkCategoryId);

        String searchText = portalAppLinkRequest.getSearchText();
        queryWrapper.like(ObjectUtil.isNotEmpty(searchText), PortalAppLink::getLinkName, searchText);

        queryWrapper.orderByAsc(PortalAppLink::getFldSort);

        return queryWrapper;
    }

    private void validateAppLink(PortalAppLinkRequest portalAppLinkRequest, boolean isEdit) {
        LambdaQueryWrapper<PortalAppLink> portalAppLinkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        portalAppLinkLambdaQueryWrapper.eq(PortalAppLink::getLinkName, portalAppLinkRequest.getLinkName());
        portalAppLinkLambdaQueryWrapper.eq(PortalAppLink::getDesignId, portalAppLinkRequest.getDesignId());
        if (isEdit) {
            Long appLinkId = portalAppLinkRequest.getAppLinkId();
            portalAppLinkLambdaQueryWrapper.ne(PortalAppLink::getAppLinkId, appLinkId);
        }
        if (this.count(portalAppLinkLambdaQueryWrapper) > 0) {
            throw new ServiceException(PortalAppLinkExceptionEnum.LINK_NAME_REPEAT);
        }
    }

}
