package com.dxhy.sign.service.qsgz.impl;

import com.dxhy.common.vo.Tax;
import com.dxhy.common.vo.UserInfo;
import com.dxhy.sign.entity.TDxRecordInvoice;
import com.dxhy.sign.service.qsgz.RulesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service("1002")
public class Rules1002ServiceImpl implements RulesService {
    @Override
    public String validationRules(UserInfo userInfo, String qsType, Map<String, String> pramsMap, TDxRecordInvoice recordInvoice, String invoiceType) {
        // 1002 权限校验
        boolean isTaxNo = this.validUserPermissions(userInfo, recordInvoice.getGfTaxNo());
        if (!isTaxNo) {
            return "登录人无权操作非本集团的发票!";
        }
        return "";
    }

    @Override
    public String getEdiType() {
        return "1002";
    }

    /**
     * 校验用户是否对某个税号有操作权限
     *
     * @param userInfo 用户信息
     * @param taxNo    购方税号
     * @return true 表示有权限，false表示无权限
     */
    public boolean validUserPermissions(UserInfo userInfo, String taxNo) {
        List<Tax> taxList = userInfo.getOrg();
        List<String> taxNoList = taxList.stream().map(Tax::getTaxno).collect(Collectors.toList());
        return taxNoList.contains(taxNo);
    }

}
