package common.service.impl;

import common.model.Auth;
import common.model.User;
import common.repository.AuthRepository;
import common.repository.ContractBodyRepository;
import common.service.AuthService;
import dic.RoleEnum;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    AuthRepository authRepository;

    @Autowired
    ContractBodyRepository contractBodyRepository;

    @Override
    public Map<String, Object> getAuthExtends(User loginAccount) {


        Auth auth = authRepository.findByUser(loginAccount.getId());

        List<String> authArrs = new ArrayList<>();


        if(RoleEnum.FINANCE.getKey().equals(loginAccount.getRole())){
            //财务  按签约主体查看

              authArrs =  JSONArray.fromObject(auth.getAuthExtend());

//            authArrs = contractBodyRepository.findByIds(bodyids);

        }else if(RoleEnum.SALSEMAN.getKey().equals(loginAccount.getRole())){
            //销售 按签约合同人查看

            authArrs =  JSONArray.fromObject(auth.getAuthExtend());

        }else if(RoleEnum.PM.getKey().equals(loginAccount.getRole())){

            // 项目经理
            authArrs =  JSONArray.fromObject(auth.getAuthExtend());

        }

        Map<String, Object> collect = new HashMap<>();

        if(authArrs.size()>0){
            collect   = authArrs.stream().collect(Collectors.toMap(t -> t, t -> t));
            collect.put("needcheck","1");
        }

        return collect;
    }
}
