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.CommonAppLink;
import cn.stylefeng.team.portal.modular.management.enums.CommonAppLinkExceptionEnum;
import cn.stylefeng.team.portal.modular.management.enums.PortalAppLinkExceptionEnum;
import cn.stylefeng.team.portal.modular.management.factory.CommonAppLinkFactory;
import cn.stylefeng.team.portal.modular.management.mapper.CommonAppLinkMapper;
import cn.stylefeng.team.portal.modular.management.pojo.request.CommonAppLinkRequest;
import cn.stylefeng.team.portal.modular.management.service.CommonAppLinkCategoryService;
import cn.stylefeng.team.portal.modular.management.service.CommonAppLinkService;
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 CommonAppLinkServiceImpl extends ServiceImpl<CommonAppLinkMapper, CommonAppLink> implements CommonAppLinkService {

    @Resource
    private CommonAppLinkCategoryService commonAppLinkCategoryService;

    @Override
    public void add(CommonAppLinkRequest commonAppLinkRequest) {

        this.validateAppLink(commonAppLinkRequest, false);

        CommonAppLink commonAppLink = new CommonAppLink();
        CommonAppLinkFactory.commonFillParam(commonAppLinkRequest, commonAppLink);
        this.save(commonAppLink);
    }

    @Override
    public void del(CommonAppLinkRequest commonAppLinkRequest) {
        CommonAppLink commonAppLink = this.queryCommonAppLink(commonAppLinkRequest);
        this.removeById(commonAppLink.getAppLinkId());
    }

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

    @Override
    public void edit(CommonAppLinkRequest commonAppLinkRequest) {

        this.validateAppLink(commonAppLinkRequest, true);

        CommonAppLink commonAppLink = this.queryCommonAppLink(commonAppLinkRequest);
        CommonAppLinkFactory.commonFillParam(commonAppLinkRequest, commonAppLink);
        this.updateById(commonAppLink);
    }

    @Override
    public CommonAppLink detail(CommonAppLinkRequest commonAppLinkRequest) {
        return this.queryCommonAppLink(commonAppLinkRequest);
    }

    @Override
    public PageResult<CommonAppLink> findPage(CommonAppLinkRequest commonAppLinkRequest) {
        LambdaQueryWrapper<CommonAppLink> wrapper = createWrapper(commonAppLinkRequest);

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

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

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

        return PageResultFactory.createPageResult(pageList);
    }

    private CommonAppLink queryCommonAppLink(CommonAppLinkRequest commonAppLinkRequest) {
        CommonAppLink commonAppLink = this.getById(commonAppLinkRequest.getAppLinkId());
        if (ObjectUtil.isEmpty(commonAppLink)) {
            throw new ServiceException(CommonAppLinkExceptionEnum.COMMON_APP_LINK_NOT_EXISTED);
        }
        return commonAppLink;
    }

    private LambdaQueryWrapper<CommonAppLink> createWrapper(CommonAppLinkRequest commonAppLinkRequest) {
        LambdaQueryWrapper<CommonAppLink> queryWrapper = new LambdaQueryWrapper<>();

        Long linkCategoryId = commonAppLinkRequest.getLinkCategoryId();
        queryWrapper.eq(ObjectUtil.isNotNull(linkCategoryId), CommonAppLink::getLinkCategoryId, linkCategoryId);

        String searchText = commonAppLinkRequest.getSearchText();
        queryWrapper.like(ObjectUtil.isNotEmpty(searchText), CommonAppLink::getLinkName, searchText);

        queryWrapper.orderByAsc(CommonAppLink::getFldSort);

        return queryWrapper;
    }

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

}
