package org.springblade.business.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.manager.dto.SaveUserInfoByProjectDTO;
import org.springblade.manager.entity.ContractInfo;
import org.springblade.manager.entity.ProjectInfo;
import org.springblade.manager.feign.ContractClient;
import org.springblade.manager.feign.ProjectAssignmentUserClient;
import org.springblade.manager.feign.ProjectClient;
import org.springblade.manager.vo.ContractInfoVO;
import org.springblade.manager.vo.ProjectInfoVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@AllArgsConstructor
@RequestMapping("/userViewProjectContract")
@Api(tags = "客户端首页")
public class UserViewProjectContractController {

    private final ProjectClient projectClient;

    private final ContractClient contractClient;

    private final ProjectAssignmentUserClient projectAssignmentUserClient;

    /**
     * 获取客户端首页当前用户关联的项目及合同段信息
     * @return 项目及合同段结果集
     */
    @GetMapping("/queryUserViewProjectAndContract")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取客户端首页当前用户关联的项目及合同段信息")
    public R<List<ProjectInfoVO>> queryUserViewProjectAndContract(){
        BladeUser user = AuthUtil.getUser();
        if(user == null){
            return R.data(null);
        }
        //获取当前用户的所有相关项目及合同段ID
        List<SaveUserInfoByProjectDTO> userDownAll = this.projectAssignmentUserClient.queryCurrentUserDownAllContractAndProjectId(user.getUserId());

        if(userDownAll.size() == 0){
            return R.data(null);
        }

        //获取项目ID
        List<String> projectIds = userDownAll.stream().map(SaveUserInfoByProjectDTO::getProjectId).distinct().collect(Collectors.toList());
        //获取合同段ID
        List<String> contractIds = userDownAll.stream().map(SaveUserInfoByProjectDTO::getContractId).distinct().collect(Collectors.toList());

        //获取项目列表
        List<ProjectInfo> projectInfos = this.projectClient.queryProjectList(projectIds);
        if(projectInfos != null && projectInfos.size() != 0){
            List<Long> longProjectIds = projectInfos.stream().map(ProjectInfo::getId).distinct().collect(Collectors.toList());
            //获取当前所有合同段
            List<ContractInfo> contractInfos = this.contractClient.queryContractListByIds(longProjectIds);

            //转换VO
            List<ProjectInfoVO> projectInfoVOS = JSONArray.parseArray(JSONObject.toJSONString(projectInfos), ProjectInfoVO.class);
            if(contractInfos != null && contractInfos.size() != 0){
                //转换VO
                List<ContractInfoVO> contractInfoVOS = JSONArray.parseArray(JSONObject.toJSONString(contractInfos), ContractInfoVO.class);
                contractInfoVOS.removeIf(vo -> !contractIds.contains(String.valueOf(vo.getId())));

                projectInfoVOS.forEach(vo -> {
                    //统一返回格式
                    vo.setName(vo.getProjectName());

                    Iterator<ContractInfoVO> iterator = contractInfoVOS.iterator();
                    while (iterator.hasNext()){
                        ContractInfoVO next = iterator.next();
                        //统一返回格式
                        next.setName(next.getContractName());

                        //找到对应的合同段
                        if(String.valueOf(vo.getId()).equals(String.valueOf(next.getPId()))){
                            vo.getContractInfoList().add(next);
                            iterator.remove();
                        }
                    }
                });
            }
            return R.data(projectInfoVOS);
        }
        return R.data(-1, null, "数据查询失败");
    }

}
