package cn.com.ty.lift.system.user.service.impl;

import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.common.utils.ProjectUtils;
import cn.com.ty.lift.system.user.dao.entity.model.*;
import cn.com.ty.lift.system.user.service.IProjectService;
import cn.com.ty.lift.system.user.service.IPropertyUserService;
import cn.com.ty.lift.system.user.service.IUserService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author huangyuan
 * @date 2020-01-05
 * @description
 */
@Service
public class PropertyUserService implements IPropertyUserService {

    @Resource
    private IUserService userService;

    @Resource
    private IProjectService projectService;

    @Override
    public RestResponse list(PropertyUserRequest propertyUserRequest) {
        List<Long> userQueryIdList = null;
        //通过用户名或手机号查找用户id
        if (StringUtils.isNotBlank(propertyUserRequest.getQueryCondition())) {
            userQueryIdList = userService.getIdListByQueryCondition(propertyUserRequest.getQueryCondition());
        }
        propertyUserRequest.setUserIdList(userQueryIdList);
        //获取物业用户集合
        List<ProjectUser> projectUserList = projectService.getProjectUserList(propertyUserRequest);
        //获取物业用户条数
        Long projectUserCount = projectService.getProjectUserCount(propertyUserRequest);
        //分页信息初始化
        IPage propertyUserPage = new Page(propertyUserRequest.getPageNum(), propertyUserRequest.getPageSize());
        if (projectUserList != null && projectUserList.size() > 0) {
            //获取用户信息
            List<Long> userIdList = ProjectUtils.getAttrList(projectUserList, "userId", null);
            Map<Long, UserResponse> userIdToUserResponse = userService.getUserResponseByUserIdList(userIdList);
            //获取项目信息
            List<Long> projectIdList = ProjectUtils.getAttrList(projectUserList,
                    "projectId", null);
            Map<Long, ProjectInfo> projectIdToProjectInfo = projectService.getProjectIdToProjectByProjectIdList(projectIdList);
            List<PropertyUserResponse> propertyUserResponseList = new ArrayList<>();
            for (ProjectUser projectUser : projectUserList) {
                PropertyUserResponse propertyUserResponse = new PropertyUserResponse();
                //设置权限
                propertyUserResponse.setUserRole(projectUser.getUserRole());
                //封装用户信息
                UserResponse userResponse = userIdToUserResponse.get(projectUser.getUserId());
                if (userResponse != null) {
                    propertyUserResponse.setUserName(userResponse.getName());
                    propertyUserResponse.setUserMobile(userResponse.getMobile());
                    propertyUserResponse.setUserId(userResponse.getUserId());
                }
                //封装项目信息
                ProjectInfo projectInfo = projectIdToProjectInfo.get(projectUser.getProjectId());
                if (projectInfo != null) {
                    propertyUserResponse.setProjectId(projectInfo.getProjectId());
                    propertyUserResponse.setProjectName(projectInfo.getProjectName());
                    propertyUserResponse.setAreaName(projectInfo.getAreaName());
                }
                propertyUserResponseList.add(propertyUserResponse);
            }
            propertyUserPage.setRecords(propertyUserResponseList);
            propertyUserPage.setTotal(projectUserCount);
            return RestResponse.success(propertyUserPage, ApiConstants.RESULT_SUCCESS, "获取物业用户成功");
        }
        return RestResponse.success(propertyUserPage, ApiConstants.RESULT_SUCCESS, "获取信息成功");
    }

    @Override
    public RestResponse cancelPermission(PropertyUserRequest propertyUserRequest) {
        Long deleteCount = projectService.deleteProjectUser(propertyUserRequest);
        if(deleteCount == 0){
            return RestResponse.fail(ApiConstants.RESULT_ERROR, "操作失败");
        }
        return RestResponse.success(ApiConstants.RESULT_SUCCESS, "操作成功");
    }
}
