package com.sunxung.factoring.service.fundmanagement.impl;

import com.alibaba.fastjson.JSON;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.fundmanagement.vo.FundSearchVo;
import com.sunxung.factoring.entity.sys.Permission;
import com.sunxung.factoring.entity.sys.Role;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.entity.sys.UserRoleRel;
import com.sunxung.factoring.service.fundmanagement.OverviewOfCooperationService;
import com.sunxung.factoring.service.sys.OrgService;
import com.sunxung.factoring.service.sys.PermissionService;
import com.sunxung.factoring.service.sys.RoleService;
import com.sunxung.factoring.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

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

/**
 * @author liutao
 * @date 2023-02-17
 */
@Service
public class OverviewOfCooperationServiceImpl implements OverviewOfCooperationService {

    @Value("${config.overview.cooperation.list.url}")
    private String url;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrgService orgService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;



    @Override
    public GridPage list(FundSearchVo fundSearchVo) {
        User user = ThreadLocalUtil.getUser();
        List<Long> pers = permissionService.getPermissionIdByUserId(user.getId());
        Permission per = permissionService.getByEname(ConstantConversionUtil.VIEW_ALL_OVERVIEW_COOPERATION);
        if (per == null) {
            throw new ValidatorException(CodeUtil.FAIL, "数据可见-查看全员权限不能为空");
        }
        if (pers.contains(per.getId()) || ThreadLocalUtil.getUser().getLoginName().equals(ConstantConversionUtil.ADMINISTORIES)) {
            fundSearchVo.setViewAll(true);
        } else {
            List<UserRoleRel> roleRels = userService.getUserRoleByUserId(user.getId());
            boolean isManagement = roleRels.stream().anyMatch(userRoleRel -> {
                Role role = roleService.getDetail(userRoleRel.getRoleId());
                if (role.getName().equals("资方")) {
                    return true;
                }
                return false;
            });
            if(!isManagement){
                List<Long> orgIdByUserId = userService.getOrgIdByUserId(user.getId());
                String department = orgIdByUserId.stream().map(r -> orgService.get(r).getName()).collect(Collectors.joining(","));
                fundSearchVo.setDepartment(department);
            }else {
                fundSearchVo.setInstitutionName(user.getName());
            }
        }
        fundSearchVo.setUserName(user.getName());
        ResponseEntity<ResponseJson> response = restTemplate.postForEntity(url, fundSearchVo, ResponseJson.class);
        ResponseJson body = response.getBody();
        GridPage overviewOfCooperationList = JSON.parseObject(JSON.toJSONString(body.getData()), GridPage.class);
        return overviewOfCooperationList;
    }
}
