package com.fs.sop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.PubFun;
import com.fs.company.domain.Company;
import com.fs.company.service.ICompanyService;
import com.fs.sop.domain.CompanySopRole;
import com.fs.sop.mapper.CompanySopRoleMapper;
import com.fs.sop.service.ICompanySopRoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 公司SOP权限Service业务层处理
 * 
 * @author fs
 * @date 2025-04-22
 */
@Service
@AllArgsConstructor
public class CompanySopRoleServiceImpl extends ServiceImpl<CompanySopRoleMapper, CompanySopRole> implements ICompanySopRoleService {

    private final ICompanyService companyService;

    /**
     * 查询公司SOP权限
     * 
     * @param id 公司SOP权限主键
     * @return 公司SOP权限
     */
    @Override
    public CompanySopRole selectCompanySopRoleById(Long id){
        return baseMapper.selectCompanySopRoleById(id);
    }

    /**
     * 查询公司SOP权限列表
     *
     * @param companySopRole 公司SOP权限
     * @return 公司SOP权限
     */
    @Override
    public List<Company> selectCompanySopRoleList(CompanySopRole companySopRole){
        Company company = new Company();
        company.setIsDel(0);
        List<Company> companyList = companyService.selectCompanyList(company);
        List<Long> companyIdList = PubFun.listToNewList(companyList, Company::getCompanyId);
        companySopRole.setCompanyIds(companyIdList);
        List<CompanySopRole> roleList = baseMapper.selectCompanySopRoleList(companySopRole);
        Map<Long, List<String>> collect = roleList.stream().collect(Collectors.groupingBy(CompanySopRole::getCompanyId, Collectors.mapping(CompanySopRole::getRoleValue, Collectors.toList())));
        companyList.stream().filter(e -> collect.containsKey(e.getCompanyId())).forEach( e -> e.setRules(collect.get(e.getCompanyId())));
        return companyList;
    }

    /**
     * 新增公司SOP权限
     * 
     * @param companySopRole 公司SOP权限
     * @return 结果
     */
    @Override
    public int insertCompanySopRole(CompanySopRole companySopRole){
        return add(companySopRole) ? 1 : 0;
    }

    /**
     * 修改公司SOP权限
     * 
     * @param companySopRole 公司SOP权限
     * @return 结果
     */
    @Override
    public int updateCompanySopRole(CompanySopRole companySopRole)
    {
        return add(companySopRole) ? 1 : 0;
    }

    private boolean add(CompanySopRole companySopRole){
        baseMapper.delete(new QueryWrapper<CompanySopRole>().eq("company_id", companySopRole.getCompanyId()));
        List<CompanySopRole> collect = companySopRole.getRules().stream().map(e -> {
            CompanySopRole role = new CompanySopRole();
            role.setCompanyId(companySopRole.getCompanyId());
            role.setRoleValue(e);
            return role;
        }).collect(Collectors.toList());
        return super.saveBatch(collect);
    }

    /**
     * 批量删除公司SOP权限
     * 
     * @param ids 需要删除的公司SOP权限主键
     * @return 结果
     */
    @Override
    public int deleteCompanySopRoleByIds(Long[] ids)
    {
        return baseMapper.deleteCompanySopRoleByIds(ids);
    }

    /**
     * 删除公司SOP权限信息
     * 
     * @param id 公司SOP权限主键
     * @return 结果
     */
    @Override
    public int deleteCompanySopRoleById(Long id)
    {
        return baseMapper.deleteCompanySopRoleById(id);
    }

    @Override
    public List<String> getRolesByCompanyId(Long id) {
        return list(new QueryWrapper<CompanySopRole>().eq("company_id", id)).stream().map(CompanySopRole::getRoleValue).collect(Collectors.toList());
    }
}
