package io.hmit.modules.fhjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.modules.fhjw.dao.FhiwFunctionDao;
import io.hmit.modules.fhjw.dto.FhiwFunctionDTO;
import io.hmit.modules.fhjw.dto.FhjwCaseDTO;
import io.hmit.modules.fhjw.dto.FhjwRoleFunctionDTO;
import io.hmit.modules.fhjw.dto.UserCaseDTO;
import io.hmit.modules.fhjw.entity.FhiwFunctionEntity;
import io.hmit.modules.fhjw.entity.UserCaseEntity;
import io.hmit.modules.fhjw.service.FhiwFunctionService;
import io.hmit.modules.fhjw.service.FhjwCaseService;
import io.hmit.modules.fhjw.service.FhjwRoleFunctionService;
import io.hmit.modules.fhjw.service.UserCaseService;
import io.hmit.modules.user.dto.UserRoleDTO;
import io.hmit.modules.user.service.RoleService;
import io.hmit.modules.user.service.UserRoleService;
import org.apache.commons.lang3.StringUtils;
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.Optional;
import java.util.stream.Collectors;

/**
 * 评价模块
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-06-22
 */
@Service
public class FhiwFunctionServiceImpl extends CrudServiceImpl<FhiwFunctionDao, FhiwFunctionEntity, FhiwFunctionDTO> implements FhiwFunctionService {

    @Autowired
    private FhjwRoleFunctionService fhjwRoleFunctionService;

    @Autowired
    private UserCaseService userCaseService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private FhjwCaseService fhjwCaseService;

    @Override
    public QueryWrapper<FhiwFunctionEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<FhiwFunctionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<FhiwFunctionDTO> getByRole(Map<String, Object> params) {

        //获取权限时传入的信息，用户权限ID,登录的用户ID，案件ID
        Long roleId = Long.parseLong(params.get("roleId").toString());
        Long userId = Long.parseLong(params.get("userId").toString());
        String caseNum = params.get("caseNum").toString().replace("%5B","[").replace("%5D","]");
        List<Long> caseIds = fhjwCaseService.selectByCaseNum(caseNum).stream()
                .map(FhjwCaseDTO::getId).collect(Collectors.toList());
//        Long caseId = fhjwCaseService.getByCaseNum(caseNum).getId();

        boolean flag = false;

        //当事人权限，则需要判断此案件是否有代理人，有代理人则不允许评价，无代理人则可以评价
        if("当事人".equals(roleService.get(roleId).getName())){
            for (Long caseId:caseIds) {
                System.out.println("caseId:"+caseId);

                Map<String, Object> query = new HashMap<>();
                query.put("caseId",caseId);
                query.put("userId",userId);
                List<UserCaseDTO> userCases = userCaseService.list(query);
                if(userCases.size()>0){
                    if(!"".equals(fhjwCaseService.get(caseId).getAgentName())){
                        flag = true;
                    }

                }
/*
                List<Long> userIds = new ArrayList<>();
                if(userCases.size()>0){
                    userIds = userCases.stream()
                            .map(UserCaseDTO::getUserId).collect(Collectors.toList());
                }


                for (Long Id:userIds) {
                    Optional<UserRoleDTO> userRole = Optional.ofNullable(userRoleService.getRoleByUser(Id));
                    Long role = userRoleService.getRoleByUser(Id).getRoleId();
                    if("代理人".equals(roleService.get(role).getName())){
                        flag = true;
                    }
                }
*/

            }
        }

        Long funcId = baseDao.selectOne(new QueryWrapper<FhiwFunctionEntity>().eq("name","我要填写评测")).getId();
        List<Long> funIds = new ArrayList<>();
        if(flag){
            funIds = fhjwRoleFunctionService.list(params).stream()
                    .filter(roleFunc -> !funcId.equals(roleFunc.getFunctionId()))
                    .map(FhjwRoleFunctionDTO::getFunctionId).collect(Collectors.toList());
        }else {
            funIds = fhjwRoleFunctionService.list(params).stream()
                    .map(FhjwRoleFunctionDTO::getFunctionId).collect(Collectors.toList());
        }
        List<FhiwFunctionDTO> dtos = new ArrayList<>();
        funIds.stream().forEach(id ->{
            FhiwFunctionDTO dto  = get(id);
            dtos.add(dto);
        });

        return dtos;
    }
}
