package com.propertyManagement.web.controller.business.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.propertyManagement.business.domain.WxCarport;
import com.propertyManagement.business.domain.WxProprietorLedger;
import com.propertyManagement.business.domain.WxUserCommunityLedger;
import com.propertyManagement.business.domain.bo.WxCommunityBo;
import com.propertyManagement.business.domain.vo.LoginWxUser;
import com.propertyManagement.business.domain.vo.WxCarportVo;
import com.propertyManagement.business.domain.vo.WxCommunityVo;
import com.propertyManagement.business.domain.vo.WxProprietorLedgerVo;
import com.propertyManagement.business.mapper.WxCarportMapper;
import com.propertyManagement.business.mapper.WxProprietorLedgerMapper;
import com.propertyManagement.business.mapper.WxUserCommunityLedgerMapper;
import com.propertyManagement.business.service.IWxCommunityService;
import com.propertyManagement.business.service.WxUserTokenService;
import com.propertyManagement.business.support.util.AuthUtil;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 小区
 *
 * @author liushuai
 * @date 2025-05-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/community")
public class ApiWxCommunityController extends BaseController {

    private final IWxCommunityService iWxCommunityService;

    private final WxUserTokenService wxUserTokenService;

    private final WxUserCommunityLedgerMapper wxUserCommunityLedgerMapper;

    private final WxProprietorLedgerMapper wxProprietorLedgerMapper;

    private final WxCarportMapper wxCarportMapper;

    /**
     * 小区列表
     */
    @GetMapping("/listAll")
    public R<List<WxCommunityVo>> listAll(WxCommunityBo bo) {
        return R.ok(iWxCommunityService.queryList(bo));
    }

    /**
     * 根据小区ID查询户号列表
     */
    @GetMapping("/room/listAllByCommunityId/{communityId}")
    public R<List<WxProprietorLedgerVo>> roomListAll(@PathVariable Long communityId) {
        /**
         * 根据小区与当前用户查询用户与小区台账关联关系表
         */
        LambdaQueryWrapper<WxUserCommunityLedger> ledgerLambdaQueryWrapper = Wrappers.lambdaQuery();
        ledgerLambdaQueryWrapper.eq(WxUserCommunityLedger::getCommunityId, communityId);
        ledgerLambdaQueryWrapper.eq(WxUserCommunityLedger::getUserId, AuthUtil.getWxUser().getId());
        List<WxUserCommunityLedger> communityLedgers = wxUserCommunityLedgerMapper.selectList(ledgerLambdaQueryWrapper);
        /**
         * 根据台账ID列表查询台账信息
         */
        List<Long> collect = communityLedgers.stream().map(WxUserCommunityLedger::getProprietorId).collect(Collectors.toList());
        if (!collect.isEmpty()) {
            return R.ok(wxProprietorLedgerMapper.selectVoBatchIds(collect));
        }
        return R.ok();
    }

    /**
     * 切换小区及户号
     */
    @PostMapping("/cut/{communityId}/{proprietorId}")
    public R<LoginWxUser> cutCommunity(@PathVariable Long communityId, @PathVariable Long proprietorId) {
        WxProprietorLedger proprietorLedger = wxProprietorLedgerMapper.selectById(proprietorId);
        LoginWxUser wxUser = AuthUtil.getWxUser();
        wxUser.setCommunityId(communityId);
        wxUser.setCommunityName(proprietorLedger.getCommunityName());
        wxUser.setProprietorId(proprietorId);
        wxUser.setRoom(proprietorLedger.getRoom());
        wxUser.setName(proprietorLedger.getName());
        wxUser.setIdentityCard(proprietorLedger.getIdentityCard());
        wxUser.setSqm(proprietorLedger.getSqm());
        wxUser.setMonthMoney(proprietorLedger.getMonthMoney());
        /**
         * 查询车位信息
         */
        LambdaQueryWrapper<WxCarport> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WxCarport::getProprietorId, proprietorId);
        List<WxCarportVo> carportVoList = wxCarportMapper.selectVoList(queryWrapper);
        if (!carportVoList.isEmpty()) {
            wxUser.setCarportVoList(carportVoList);
        }
        wxUserTokenService.refreshToken(wxUser);
        return R.ok(wxUser);
    }

    /**
     * 我的房屋列表
     */
    @GetMapping("/myRoomListAll")
    public R<List<WxProprietorLedger>> myRoomListAll() {
        LoginWxUser wxUser = AuthUtil.getWxUser();
        LambdaQueryWrapper<WxUserCommunityLedger> wxUserCommunityLedgerLambdaQueryWrapper = Wrappers.lambdaQuery();
        wxUserCommunityLedgerLambdaQueryWrapper.eq(WxUserCommunityLedger::getUserId, wxUser.getId());
        List<WxUserCommunityLedger> ledgerList = wxUserCommunityLedgerMapper.selectList(wxUserCommunityLedgerLambdaQueryWrapper);
        if (ledgerList.isEmpty()) {
            return R.ok();
        }
        LambdaQueryWrapper<WxProprietorLedger> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(WxProprietorLedger::getProprietorId, ledgerList.stream().map(WxUserCommunityLedger::getProprietorId).collect(Collectors.toList()));
        List<WxProprietorLedger> selectList = wxProprietorLedgerMapper.selectList(queryWrapper);
        if (selectList.isEmpty()) {
            return R.ok();
        }
        return R.ok(selectList);
    }
}
