package com.ics.admin.controller.meeting;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.ics.admin.domain.BuildingDetail;
import com.ics.admin.domain.Customer;
import com.ics.admin.domain.Room;
import com.ics.admin.domain.meeting.RoomContent;
import com.ics.admin.service.IBuildingDetailService;
import com.ics.admin.service.ICustomerService;
import com.ics.admin.service.IRoomService;
import com.ics.system.domain.User;
import com.ics.system.service.ICurrentUserService;
import com.ics.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 com.ics.common.core.domain.R;
import com.ics.common.core.controller.BaseController;
import com.ics.admin.domain.meeting.Showroom;
import com.ics.admin.service.meeting.IShowroomService;
import org.wf.jwtp.annotation.Ignore;
import org.wf.jwtp.annotation.RequiresPermissions;
import org.wf.jwtp.util.SubjectUtil;

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

/**
 * 展厅管理 提供者
 *
 * @author ics
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/meeting/showroom")
public class ShowroomController extends BaseController {

    @Autowired
    private IShowroomService showroomService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IBuildingDetailService buildingDetailService;

    @Autowired
    private ICurrentUserService currentUserService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ICustomerService customerService;



    /**
     * 查询展厅管理
     */
    @Ignore
    @GetMapping("get/{id}")
    public Showroom get(@PathVariable("id") Long id) {
        Showroom showroom = showroomService.selectShowroomById(id);
        Long roomId = showroom.getRoomId();
        Room room = roomService.selectRoomById(roomId);
        showroom.setBuildId(room.getBuildingDetailId());
        showroom.setArea(String.valueOf(room.getArea()));
        showroom.setRoomName(room.getName());
        BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(room.getBuildingDetailId());
        showroom.setBuildingName(buildingDetail.getFloorName());

        return showroom;
    }

    /**
     * 查询展厅管理列表
     */
    @RequiresPermissions("meeting:showroom:list")
    @GetMapping("list")
    public R list(Showroom showroom) {
        startPage();
        boolean isAdmin = SubjectUtil.hasRole(getRequest(),"manager");
        if (isAdmin){
            Long parkId = currentUserService.getParkId();
            Long tenantId = currentUserService.getTenantId();
            showroom.setParkId(parkId);
//            showroom.setTenantId(tenantId);
        }
        boolean b = SubjectUtil.hasRole(getRequest(),"admin");
        if (b){
            User user = userService.selectUserById(getCurrentUserId());
            if (null != user.getCustomerId()){
                Customer customer = customerService.selectCustomerById(user.getCustomerId());
                String roomId = customer.getRoomId();
                List<String> roomIds = StrUtil.split(roomId, ',');
                List<Long> collect = roomIds.stream().map(Long::valueOf).collect(Collectors.toList());
                showroom.setRoomIds(collect);

            }
        }
        List<Showroom> showrooms = showroomService.selectShowroomList(showroom);
        for (Showroom showroom1 : showrooms) {
            Long roomId = showroom1.getRoomId();
            Room room = roomService.selectRoomById(roomId);
            if (null != room){
                showroom1.setBuildId(room.getBuildingDetailId());
                showroom1.setRoomName(room.getName());
            }


        }
        return result(showrooms);
    }


    /**
     * 新增保存展厅管理
     */
    @RequiresPermissions("meeting:showroom:add")
    @PostMapping("save")
    public R addSave(@RequestBody Showroom showroom) {

        List<Showroom> showrooms = showroomService.selectShowroomList(showroom);
        Room room = roomService.selectRoomById(showroom.getRoomId());
        room.setStatus(Room.Status.YES);
        int i1 = roomService.updateRoom(room);
        Assert.isTrue(i1 > 0, "修改房间已租状态失败");

        return toAjax(showroomService.insertShowroom(showroom));
    }

    /**
     * 修改保存展厅管理
     */
    @RequiresPermissions("meeting:showroom:edit")
    @PostMapping("update")
    public R editSave(@RequestBody Showroom showroom) {
        return toAjax(showroomService.updateShowroom(showroom));
    }

    /**
     * 删除展厅管理
     */
    @RequiresPermissions("meeting:showroom:remove")
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(showroomService.deleteShowroomByIds(ids));
    }



    @RequiresPermissions("meeting:showroom:list")
    @GetMapping("/showroomList")
    public R roomContentList(Showroom showroom) {


        if (null != showroom.getBuildingId()){
            List<Room> rooms = roomService.selectRoomByBuildingId(showroom.getBuildingId());
            if (CollUtil.isNotEmpty(rooms)){
                showroom.setRoomIds(rooms.stream().map(Room::getId).collect(Collectors.toList()));
            }
        }

        boolean isAdmin = SubjectUtil.hasRole(getRequest(),"manager");
        if (isAdmin){
            Long parkId = currentUserService.getParkId();
            Long tenantId = currentUserService.getTenantId();
            showroom.setParkId(parkId);
//            showroom.setTenantId(tenantId);
        }
        boolean b = SubjectUtil.hasRole(getRequest(),"admin");
        if (b){
            User user = userService.selectUserById(getCurrentUserId());
            if (null != user.getCustomerId()){
                Customer customer = customerService.selectCustomerById(user.getCustomerId());
                String roomId = customer.getRoomId();
                List<String> roomIds = StrUtil.split(roomId, ',');
                List<Long> collect = roomIds.stream().map(Long::valueOf).collect(Collectors.toList());
                showroom.setRoomIds(collect);
            }
        }
        return R.data(showroomService.selectShowroomList(showroom));
    }
}
