package cn.stylefeng.team.project.modular.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.sys.api.OrganizationServiceApi;
import cn.stylefeng.roses.kernel.sys.api.SysUserServiceApi;
import cn.stylefeng.team.project.api.entity.BusinessPermission;
import cn.stylefeng.team.project.modular.business.mapper.BusinessPermissionMapper;
import cn.stylefeng.team.project.modular.business.service.BusinessPermissionService;
import cn.stylefeng.workflow.api.enums.FlowAuthLinkTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BusinessPermissionServiceImpl extends ServiceImpl<BusinessPermissionMapper, BusinessPermission> implements BusinessPermissionService {

    @Resource
    private OrganizationServiceApi organizationServiceApi;

    @Resource
    private SysUserServiceApi sysUserServiceApi;

    @Override
    public void removeBusinessFilingPermission(Long businessId) {
        LambdaUpdateWrapper<BusinessPermission> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(BusinessPermission::getBusinessId, businessId);
        this.remove(wrapper);
    }

    @Override
    public void addBusinessFillingPermission(Long businessId, List<BusinessPermission> businessFilingPermissionList) {

        if (ObjectUtil.isEmpty(businessFilingPermissionList)) {
            return;
        }

        List<BusinessPermission> newList = new ArrayList<>();
        for (BusinessPermission businessFilingPermission : businessFilingPermissionList) {
            if (ObjectUtil.isEmpty(businessFilingPermission.getLinkType())
                    || ObjectUtil.isEmpty(businessFilingPermission.getLinkBusinessId())
                    || ObjectUtil.isEmpty(businessFilingPermission.getScopeType())
                    || ObjectUtil.isEmpty(businessFilingPermission.getOperateType())) {
                continue;
            }
            businessFilingPermission.setId(IdWorker.getId());
            businessFilingPermission.setBusinessId(businessId);
            newList.add(businessFilingPermission);
        }
        if (ObjectUtil.isEmpty(newList)) {
            return;
        }

        this.getBaseMapper().insertBatchSomeColumn(newList);
    }

    @Override
    public List<BusinessPermission> queryBusinessFilingPermissionList(Long businessId) {
        LambdaQueryWrapper<BusinessPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BusinessPermission::getBusinessId, businessId);
        return this.list(wrapper);
    }

    @Override
    public void processPermissionName(List<BusinessPermission> businessFilingPermissionList) {
        for (BusinessPermission businessFilingPermission : businessFilingPermissionList) {
            Integer linkType = businessFilingPermission.getLinkType();

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

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

}