package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.biz.dal.entity.Employee;
import com.yonge.cooleshow.biz.dal.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.TenantInfo;
import com.yonge.cooleshow.biz.dal.entity.TenantUnbindHistory;
import com.yonge.cooleshow.biz.dal.service.EmployeeService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
import com.yonge.cooleshow.biz.dal.service.TenantStaffService;
import com.yonge.cooleshow.biz.dal.service.TenantUnbindHistoryService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantUnbindHistoryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.Oneway;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Validated
@RestController
@RequestMapping("/tenantUnbindHistory")
@Api(tags = "机构解绑历史表")
public class TenantUnbindHistoryController {

    @Autowired
    private TenantUnbindHistoryService tenantUnbindHistoryService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private TenantInfoService tenantInfoService;

    @ApiOperation(value = "查询分页", notes = "机构解绑历史表- 传入 TenantUnbindHistoryWrapper.TenantUnbindHistoryQuery")
    @PostMapping("/page")
    public R<PageInfo<TenantUnbindHistoryWrapper.TenantUnbindHistory>> page(@RequestBody TenantUnbindHistoryWrapper.TenantUnbindHistoryQuery query) {

        IPage<TenantUnbindHistory> pages = tenantUnbindHistoryService.selectPage(QueryInfo.getPage(query), query);

        List<TenantUnbindHistory> records = pages.getRecords();
        if (records.isEmpty()) {
            return R.from(QueryInfo.pageInfo(pages, new ArrayList<>()));
        }
        List<Long> userIds =
                pages.getRecords().stream().map(TenantUnbindHistory::getVerifyUserId).distinct()
                        .collect(Collectors.toList());
        Map<Long, SysUser> mapByIds = sysUserService.getMapByIds(userIds);

        List<Long> tenantIds =
                pages.getRecords().stream().map(TenantUnbindHistory::getTenantId).distinct()
                        .collect(Collectors.toList());
        Map<Long, TenantInfo> tenantInfoMap = tenantInfoService.getMapByIds(tenantIds);
        List<TenantUnbindHistoryWrapper.TenantUnbindHistory> histories = records.stream().map(next -> {
            TenantUnbindHistoryWrapper.TenantUnbindHistory tenantUnbindHistory =
                    JSON.parseObject(JSON.toJSONString(next), TenantUnbindHistoryWrapper.TenantUnbindHistory.class);
            SysUser orDefault = mapByIds.getOrDefault(next.getVerifyUserId(), new SysUser());
            tenantUnbindHistory.setVerifyUserName(orDefault.getUsername());
            if(tenantInfoMap.containsKey(next.getTenantId())){
                TenantInfo tenantInfo = tenantInfoMap.get(next.getTenantId());
                tenantUnbindHistory.setTenantUserName(tenantInfo.getUsername());
                tenantUnbindHistory.setTenantPhone(tenantInfo.getPhone());
            }
            return tenantUnbindHistory;

        }).collect(Collectors.toList());
        return R.from(QueryInfo.pageInfo(pages, histories));
    }
}