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.*;
import com.ics.admin.service.IBuildingDetailService;
import com.ics.admin.service.ICustomerService;
import com.ics.admin.service.IRoomService;
import com.ics.admin.service.meeting.IRoomContentService;
import com.ics.admin.service.meeting.IRoomItemByRoomService;
import com.ics.admin.service.meeting.IRoomServeByRoomService;
import com.ics.common.core.controller.BaseController;
import com.ics.system.domain.User;
import com.ics.system.mapper.DictDataMapper;
import com.ics.system.service.ICurrentUserService;
import com.ics.system.service.IUserService;
import org.checkerframework.checker.units.qual.A;
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.RoomContent;
import com.ics.admin.service.meeting.IRoomContentService;
import org.wf.jwtp.annotation.Ignore;
import org.wf.jwtp.annotation.RequiresPermissions;
import org.wf.jwtp.util.SubjectUtil;

import javax.security.auth.Subject;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 房间主体内容 提供者
 *
 * @author ics
 * @date 2024-02-22
 */
@RestController
@RequestMapping("/meeting/roomContent")
public class RoomContentController extends BaseController {

    @Autowired
    private IRoomContentService roomContentService;

    @Autowired
    private IRoomServeByRoomService roomServeByRoomService;

    @Autowired
    private IRoomItemByRoomService roomItemByRoomService;

    @Autowired
    private IBuildingDetailService buildingDetailService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ICurrentUserService currentUserService;

    @Autowired
    private IUserService userService;


    /**
     * 查询房间主体内容
     */
    @Ignore
    @GetMapping("get/{id}")
    public RoomContent get(@PathVariable("id") Long id) {

        RoomContent roomContent = roomContentService.selectRoomContentById(id);
        Long roomId = roomContent.getRoomId();
        Room room = roomService.selectRoomById(roomId);
        roomContent.setBuildId(room.getBuildingId());
        roomContent.setArea(room.getArea());
        roomContent.setRoomName(room.getName());
        BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(room.getBuildingDetailId());
        roomContent.setBuildingName(buildingDetail.getFloorName());

        return roomContent;
    }

    /**
     * 查询房间主体内容列表
     */
    @RequiresPermissions("meeting:roomContent:list")
    @GetMapping("list")
    public R list(RoomContent roomContent) {
        startPage();
        boolean isAdmin = SubjectUtil.hasRole(getRequest(),"manager");
        if (isAdmin){
            Long parkId = currentUserService.getParkId();
            Long tenantId = currentUserService.getTenantId();
            roomContent.setParkId(parkId);
//            roomContent.setTenantId(tenantId);
        }
        boolean b = SubjectUtil.hasRole(getRequest(),"admin");
        if (b){
            Long parkId = currentUserService.getParkId();
            Long tenantId = currentUserService.getTenantId();
            roomContent.setParkId(parkId);
//            roomContent.setTenantId(tenantId);

//            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());
//                roomContent.setRoomIds(collect);
//            }
        }
        List<RoomContent> roomContents = roomContentService.selectRoomContentList(roomContent);
        for (RoomContent content : roomContents) {
            RoomServeByRoom roomServeByRoom = new RoomServeByRoom();
            roomServeByRoom.setRomeContentId(content.getId());
            List<RoomServeByRoom> serveByRooms = roomServeByRoomService.selectRoomServeByRoomList(roomServeByRoom);
            content.setServeCount(serveByRooms.size());

            RoomItemByRoom roomItemByRoom = new RoomItemByRoom();
            roomItemByRoom.setRoomContentId(content.getId());
            List<RoomItemByRoom> roomItemByRooms = roomItemByRoomService.selectRoomItemByRoomList(roomItemByRoom);
            content.setItemCount(roomItemByRooms.size());
            Long roomId = content.getRoomId();
            Room room = roomService.selectRoomById(roomId);
            if (room != null){
                content.setBuildId(room.getBuildingDetailId());
                content.setArea(room.getArea());
                content.setRoomName(room.getName());
                content.setBuildingId(room.getBuildingId());
            }
        }
        return result(roomContents);
    }

    /**
     * 新增保存房间主体内容
     */
    @RequiresPermissions("meeting:roomContent:add")
    @PostMapping("save")
    public R addSave(@RequestBody RoomContent roomContent) {
        System.out.println(roomContent);
        if (roomContent.getIsToll() ==1){
            roomContent.setMoney("0");
        }
        int i = roomContentService.insertRoomContent(roomContent);
        Assert.isTrue(i > 0, "新增失败");

        //如果新增成功，把房间的状态改为已启用
        Room room = roomService.selectRoomById(roomContent.getRoomId());
        room.setStatus(Room.Status.YES);
        int i1 = roomService.updateRoom(room);
        Assert.isTrue(i1 > 0, "修改房间已租状态失败");


        return toAjax(i);
    }

    /**
     * 修改保存房间主体内容
     */
    @RequiresPermissions("meeting:roomContent:edit")
    @PostMapping("update")
    public R editSave(@RequestBody RoomContent roomContent) {
        System.out.println(roomContent.getType());;
        if (roomContent.getIsToll() ==1){
            roomContent.setMoney("0");
        }
        return toAjax(roomContentService.updateRoomContent(roomContent));
    }

    /**
     * 删除房间主体内容
     */
    @RequiresPermissions("meeting:roomContent:remove")
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(roomContentService.deleteRoomContentByIds(ids));
    }


    @Ignore
    @GetMapping("/searchInfo/{type}")
    public R searchInfo(@PathVariable("type") Integer type) {
        //  1.会议室类型，2.人数，3.会议室设备，4.形式

        Map<String, Object> map = roomContentService.selectSearchInfoByType(type);
        return R.ok(map);
    }

    @Ignore
    @PostMapping("/addRoomServe")
    public R addRoomServe(@RequestBody RoomServeByRoom roomServeByRoom) {
        //  1.会议室类型，2.人数，3.会议室设备，4.形式

        int count = roomContentService.roomServeByRoom(roomServeByRoom);
        return toAjax(count);
    }

    @Ignore
    @PostMapping("/addRoomItem")
    public R addItemServe(@RequestBody RoomItemByRoom roomItemByRoom) {
        //  1.会议室类型，2.人数，3.会议室设备，4.形式

        int count = roomContentService.addItemServe(roomItemByRoom);

        return toAjax(count);
    }

    //根据会议查询服务
    @Ignore
    @PostMapping("/selectRoomServeByRoom")
    public R selectRoomServeByRoom(@RequestBody RoomServeByRoom roomServeByRoom) {
        //  1.会议室类型，2.人数，3.会议室设备，4.形式
        List<RoomServeByRoom> roomServeByRooms = roomServeByRoomService.selectRoomServeByRoomList(roomServeByRoom);
        return R.ok().put("data",roomServeByRooms);
    }

    @Ignore
    @PostMapping("/selectRoomItemByRoom")
    public R selectRoomItemByRoom(@RequestBody RoomItemByRoom roomItemByRoom) {
        //  1.会议室类型，2.人数，3.会议室设备，4.形式
        List<RoomItemByRoom> roomItemByRooms = roomItemByRoomService.selectRoomItemByRoomList(roomItemByRoom);
        return R.ok().put("data",roomItemByRooms);
    }

    /**
     * 所属楼层
     */

    @Ignore
    @GetMapping("/getFloorList")
    public R list(BuildingDetail buildingDetail) {
        buildingDetail.setBuildingId(165L);
        return R.ok().put("data",buildingDetailService.selectBuildingDetailList(buildingDetail));
    }


    @Ignore
    @GetMapping("/getRoomListByFloorId")
    public R list(Room room) {
        room.setDeleteFlag(0);
        room.setBuildingDetailId(room.getBuildingDetailId());
        List<Room> rooms = roomService.selectRoomList(room);

        if (room.getType() !=null){
            if (room.getId() != null ){
                Room room1 = roomService.selectRoomById(room.getId());
                rooms.add(room1);
            }
        }

        return R.ok().put("data",rooms);
    }
    @Ignore
    @PostMapping("/getRoomListByBuildIds")
    public R getRoomListByBuildIds(@RequestBody Room room) {
        room.setDeleteFlag(0);
        room.setBuildingDetailIds(room.getBuildingDetailIds());
        List<Room> rooms = roomService.selectRoomList(room);


        if (StrUtil.isNotBlank(room.getCustomerId())){
            String customerId = room.getCustomerId();
            Customer customer = customerService.selectCustomerById(Long.valueOf(customerId));
            String roomId = customer.getRoomId();
            List<String> roomIds = StrUtil.split(roomId, ',');


            if (CollUtil.isNotEmpty(roomIds) ){
                for (String id : roomIds) {
                    Room room1 = roomService.selectRoomById(Long.valueOf(id));
                    if (null != room1){
                        rooms.add(room1);
                    }
                }

            }
        }

        return R.ok().put("data",rooms);
    }

    @Ignore
    @GetMapping("/selectRoomById")
    public R selectRoomById(Room room) {
        return R.ok().put("data",roomService.selectRoomById(room.getId()));
    }



    @Ignore
    @GetMapping("/customerList")
    public R list(Customer customer) {
        List<Customer> customers = customerService.selectCustomerList(customer);
        return R.data(customers);
    }

    @Ignore
//    @RequiresPermissions("meeting:roomContent:list")
    @GetMapping("/roomContentList")
    public R roomContentList(RoomContent roomContent) {

        if (null != roomContent.getBuildingId()){
            List<Room> rooms = roomService.selectRoomByBuildingId(roomContent.getBuildingId());
            if (CollUtil.isNotEmpty(rooms)){
                roomContent.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();
            roomContent.setParkId(parkId);
//            roomContent.setTenantId(tenantId);
        }
        boolean b = SubjectUtil.hasRole(getRequest(),"admin");
        if (b){

            Long parkId = currentUserService.getParkId();
            Long tenantId = currentUserService.getTenantId();
            roomContent.setParkId(parkId);
//            roomContent.setTenantId(tenantId);

//            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());
//                roomContent.setRoomIds(collect);
//            }
        }
        return R.data(roomContentService.selectRoomContentList(roomContent));
    }

}