package com.ics.controller.mobile.meeting;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ics.admin.domain.BuildingDetail;
import com.ics.admin.domain.Room;
import com.ics.admin.domain.meeting.*;
import com.ics.admin.service.IBuildingDetailService;
import com.ics.admin.service.IIcsCustomerStaffService;
import com.ics.admin.service.IRoomService;
import com.ics.admin.service.meeting.IShowroomRecordService;
import com.ics.admin.service.meeting.IShowroomService;
import com.ics.common.annotation.LoginStaff;
import com.ics.common.annotation.LoginUser;
import com.ics.common.core.controller.BaseController;
import com.ics.common.core.domain.IcsCustomerStaff;
import com.ics.common.core.domain.R;
import com.ics.common.core.page.PageDomain;
import com.ics.common.core.page.TableSupport;
import com.ics.system.domain.DictData;
import com.ics.system.service.IDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wf.jwtp.annotation.Ignore;
import org.wf.jwtp.annotation.RequiresPermissions;

import java.util.Date;
import java.util.List;

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

    @Autowired
    private IShowroomService showroomService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IBuildingDetailService buildingDetailService;

    @Autowired
    private IShowroomRecordService recordService;

    @Autowired
    private IDictDataService dictDataService;

    @Autowired
    private IIcsCustomerStaffService staffService;

    /**
     * 查询展厅管理
     */
    @RequiresPermissions("member:center:view")
    @GetMapping("get/{id}")
    public R get(@PathVariable("id") Long id) {
        Showroom showroom = showroomService.selectShowroomById(id);
        if (null != showroom){
            Room room = roomService.selectRoomById(showroom.getRoomId());
            if (room != null) {
                showroom.setRoomName(room.getName());
                showroom.setArea(String.valueOf(room.getArea()));
                showroom.setRenArea(room.getRentArea());
                //查询楼层号
                BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(room.getBuildingDetailId());
                if (buildingDetail != null) {
                    showroom.setBuildingName(buildingDetail.getFloorName());
                }
            }
        }
        return R.data(showroom);
    }

    /**
     * 查询展厅管理列表
     */
    @RequiresPermissions("member:center:view")
    @PostMapping("list")
    public R list(@LoginUser @RequestBody Showroom showroom) {
        List<Showroom> showrooms = showroomService.selectShowroomList(showroom);
        for (Showroom showroom1 : showrooms) {
            Room room = roomService.selectRoomById(showroom1.getRoomId());
            if (room != null) {
                showroom1.setRoomName(room.getName());



                //查询楼层号
                BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(room.getBuildingDetailId());
                if (buildingDetail != null) {
                    showroom1.setBuildingName(buildingDetail.getFloorName());
                }
            }
        }
        return R.ok().put("data",showrooms);
    }


    /**
     * 新增保存展厅管理
     */
    @PostMapping("save")
    public R addSave(@RequestBody Showroom showroom) {
        return toAjax(showroomService.insertShowroom(showroom));
    }

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

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


    /**
     * 查询展厅已经预约的记录
     */
    @RequiresPermissions("member:center:view")
    @PostMapping("/appointmentRecord")
    public R appointmentRecord(@RequestBody ShowroomRecord showroomRecord)
    {
       List<ShowroomRecordDTO> list =  recordService.appointmentRecord(showroomRecord);
       return  R.ok().put("data",list);
    }

    /**
     *  查询空闲的会议室
     * @return
     */
    @RequiresPermissions("member:center:view")
    @PostMapping("/selectFreeShowRoom")
    public R selectFreeMeetingRoom(@RequestBody ShowroomRecord ShowroomRecord) {
        boolean count =recordService.selectFreeMeetingRoom(ShowroomRecord);
        String msg =count?"展厅不可用":"展厅可用";
        return R.ok().put("count",count).put("msg",msg);
    }

    /**
     * 会议预约记录
     */
    @RequiresPermissions("member:center:view")
    @PostMapping("/saveShowRoomRecord")
    public R saveShowRoomRecord(@RequestBody ShowroomRecord showroomRecord) {
        //根据用户获取对应的企业id，查询该企业下对应的优惠卷
        showroomRecord.setReservationNumber(RandomUtil.randomNumbers(8));
        boolean b = recordService.selectFreeMeetingRoom(showroomRecord);
        Assert.isFalse(b, "会议室不可用");
        showroomRecord.setStatus(1);
        showroomRecord.setCreateTime(new Date());
        boolean save = recordService.save(showroomRecord);

        if (showroomRecord.getParkId() == 25){

        }



        Long id = showroomRecord.getId();
        return toAjax(save).put("showroomRecord",id);
    }

    /**
     * 查询字典
     */
    @Ignore
//    @RequiresPermissions("member:center:view")
    @PostMapping("/listByType")
    public R list(@RequestBody DictData dictData) {
        return R.ok().put("data",dictDataService.selectDictDataList(dictData));
    }

    /**
     * 查询预约记录
     */
    @RequiresPermissions("member:center:view")
    @PostMapping("/selectShowroomRecord")
    public R selectShowroomRecord(@RequestBody ShowroomRecord showroomRecord) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        IPage<ShowroomRecord> showroomRecords = recordService.selectShowroomRecord(showroomRecord,pageNum,pageSize);
        for (ShowroomRecord showroomRecord1 : showroomRecords.getRecords()) {
            Long userId = showroomRecord1.getUserId();
            IcsCustomerStaff icsCustomerStaffs = staffService.selectIcsCustomerStaffById(userId);
            if (null != icsCustomerStaffs ){
                showroomRecord1.setCreateBy(icsCustomerStaffs.getUsername());
            }


            if (showroomRecord1.getStatus() == 0){
                showroomRecord1.setStatusName("待审核");
            }else if (showroomRecord1.getStatus() == 1){
                showroomRecord1.setStatusName("审核通过");
            }else if (showroomRecord1.getStatus() == 4){
                showroomRecord1.setStatusName("已取消");
            }

            Showroom showroom = showroomService.selectShowroomById(showroomRecord1.getShowroomId());
            if (null != showroom){

                showroomRecord1.setIndoorPicUrl(showroom.getIndoorPicUrl());
                showroomRecord1.setCapacityNum(showroom.getCapacityNum());
                    Room room = roomService.selectRoomById(showroom.getRoomId());
                    if (room != null) {
                        showroomRecord1.setRoomName(room.getName());
                        //查询楼层号
                        BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(room.getBuildingDetailId());
                        if (buildingDetail != null) {
                            showroomRecord1.setBuildingName(buildingDetail.getFloorName() +"F");
                        }
                }
            }
        }
        return R.ok().put("data",showroomRecords);
    }

    /**
     * 展厅详情
     */
    @RequiresPermissions("member:center:view")
    @GetMapping("/selectShowroomRecordById/{id}")
    public R selectShowroomRecordById(@PathVariable("id") Long id) {
        ShowroomRecord showroomRecord = recordService.selectShowroomRecordById(id);
        if (null != showroomRecord) {
            if (showroomRecord.getStatus() == 0) {
                showroomRecord.setStatusName("待审核");
            } else if (showroomRecord.getStatus() == 1) {
                showroomRecord.setStatusName("审核通过");
            } else if (showroomRecord.getStatus() == 4) {
                showroomRecord.setStatusName("已取消");
            }

            IcsCustomerStaff icsCustomerStaff = staffService.selectIcsCustomerStaffById(showroomRecord.getUserId());
            if (null != icsCustomerStaff){
                showroomRecord.setUserName(icsCustomerStaff.getUsername());
                showroomRecord.setUserPhone(icsCustomerStaff.getMobile());
            }

            String typeName = dictDataService.selectDictLabel("visit_type", showroomRecord.getVisitType() + "");
            showroomRecord.setVisitTypeName(typeName);


            Showroom showroom = showroomService.selectShowroomById(showroomRecord.getShowroomId());
            if (null != showroom) {
                showroomRecord.setIndoorPicUrl(showroom.getIndoorPicUrl());
                showroomRecord.setCapacityNum(showroom.getCapacityNum());
                Room room = roomService.selectRoomById(showroom.getRoomId());
                if (room != null) {
                    showroomRecord.setRoomName(room.getName());
                    //查询楼层号
                    BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(room.getBuildingDetailId());
                    if (buildingDetail != null) {
                        showroomRecord.setBuildingName(buildingDetail.getFloorName() + "F");
                    }
                }
            }
        }
        return R.ok().put("data",showroomRecord);
    }
}
