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.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.sys.api.OrganizationServiceApi;
import cn.stylefeng.roses.kernel.sys.api.SysUserServiceApi;
import cn.stylefeng.team.portal.modular.management.entity.PortalDesignPermission;
import cn.stylefeng.team.portal.modular.management.enums.PortalDesignPermissionExceptionEnum;
import cn.stylefeng.team.portal.modular.management.factory.PermissionFactory;
import cn.stylefeng.team.portal.modular.management.mapper.PortalDesignPermissionMapper;
import cn.stylefeng.team.portal.modular.management.pojo.request.PermissionUpdateRequest;
import cn.stylefeng.team.portal.modular.management.pojo.request.PortalDesignPermissionRequest;
import cn.stylefeng.team.portal.modular.management.service.PortalDesignPermissionService;
import cn.stylefeng.workflow.api.enums.FlowAuthLinkTypeEnum;
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;

@Service
public class PortalDesignPermissionServiceImpl extends ServiceImpl<PortalDesignPermissionMapper, PortalDesignPermission> implements PortalDesignPermissionService {

    @Resource
    private OrganizationServiceApi organizationServiceApi;

    @Resource
    private SysUserServiceApi sysUserServiceApi;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePermissionList(PermissionUpdateRequest permissionUpdateRequest) {

        LambdaQueryWrapper<PortalDesignPermission> removeWrapper = new LambdaQueryWrapper<>();
        removeWrapper.eq(PortalDesignPermission::getDesignId, permissionUpdateRequest.getDesignId());
        removeWrapper.eq(PortalDesignPermission::getLinkScopeType, permissionUpdateRequest.getLinkScopeType());
        removeWrapper.eq(PortalDesignPermission::getScopeType, permissionUpdateRequest.getScopeType());
        removeWrapper.in(PortalDesignPermission::getLinkBusinessId, permissionUpdateRequest.getLinkBusinessIdList());
        this.remove(removeWrapper);

        List<PortalDesignPermission> permission = PermissionFactory.createPermission(permissionUpdateRequest);
        this.saveBatch(permission);
    }

    @Override
    public void del(PortalDesignPermissionRequest portalDesignPermissionRequest) {
        PortalDesignPermission portalDesignPermission = this.queryPortalDesignPermission(portalDesignPermissionRequest);
        this.removeById(portalDesignPermission.getPermissionId());
    }

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

    @Override
    public PageResult<PortalDesignPermission> findPage(PortalDesignPermissionRequest portalDesignPermissionRequest) {
        LambdaQueryWrapper<PortalDesignPermission> wrapper = createWrapper(portalDesignPermissionRequest);
        Page<PortalDesignPermission> pageList = this.page(PageFactory.defaultPage(), wrapper);
        List<PortalDesignPermission> records = pageList.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return PageResultFactory.createPageResult(pageList);
        }

        for (PortalDesignPermission record : records) {
            Integer linkType = record.getLinkScopeType();

            if (FlowAuthLinkTypeEnum.ORG.getType().equals(linkType)) {
                String orgName = this.organizationServiceApi.getOrgNameById(record.getLinkBusinessId());
                record.setLinkBusinessName(orgName);
            }

            else if (FlowAuthLinkTypeEnum.USER.getType().equals(linkType)) {
                String userName = this.sysUserServiceApi.getUserRealName(record.getLinkBusinessId());
                record.setLinkBusinessName(userName);
            }
        }

        return PageResultFactory.createPageResult(pageList);
    }

    private PortalDesignPermission queryPortalDesignPermission(PortalDesignPermissionRequest portalDesignPermissionRequest) {
        PortalDesignPermission portalDesignPermission = this.getById(portalDesignPermissionRequest.getPermissionId());
        if (ObjectUtil.isEmpty(portalDesignPermission)) {
            throw new ServiceException(PortalDesignPermissionExceptionEnum.PORTAL_DESIGN_PERMISSION_NOT_EXISTED);
        }
        return portalDesignPermission;
    }

    private LambdaQueryWrapper<PortalDesignPermission> createWrapper(PortalDesignPermissionRequest portalDesignPermissionRequest) {
        LambdaQueryWrapper<PortalDesignPermission> queryWrapper = new LambdaQueryWrapper<>();

        Long designId = portalDesignPermissionRequest.getDesignId();
        queryWrapper.eq(PortalDesignPermission::getDesignId, designId);

        return queryWrapper;
    }

}
