package com.ics.admin.controller.meeting;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.ics.admin.domain.*;
import com.ics.admin.domain.meeting.RoomContent;
import com.ics.admin.domain.meeting.Showroom;
import com.ics.admin.service.*;
import com.ics.admin.service.meeting.IShowroomService;
import com.ics.common.core.domain.IcsCustomerStaff;
import com.ics.system.domain.Tenant;
import com.ics.system.domain.User;
import com.ics.system.service.ICurrentUserService;
import com.ics.system.service.ITenantService;
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.ShowroomRecord;
import com.ics.admin.service.meeting.IShowroomRecordService;
import org.wf.jwtp.annotation.Ignore;
import org.wf.jwtp.annotation.RequiresPermissions;
import org.wf.jwtp.util.SubjectUtil;

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

/**
 * 展厅预约记录 提供者
 *
 * @author ics
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/meeting/showroomRecord")
public class ShowroomRecordController extends BaseController {

    @Autowired
    private IShowroomRecordService showroomRecordService;

    @Autowired
    private IShowroomService showroomService;

    @Autowired
    private IIcsCustomerStaffService customerStaffService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ICurrentUserService currentUserService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IIcsCustomerStaffService staffService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IParkService parkService;

    @Autowired
    private ITenantService tenantService;

    @Autowired
    private IBuildingService buildingService;

    @Autowired
    private IBuildingDetailService buildingDetailService;

    /**
     * 查询展厅预约记录
     */
    @Ignore
    @GetMapping("get/{id}")
    public ShowroomRecord get(@PathVariable("id") Long id) {
        ShowroomRecord showroomRecord = showroomRecordService.selectShowroomRecordById(id);

        Showroom showroom = showroomService.selectShowroomById(showroomRecord.getShowroomId());
        if (null != showroom) {
            showroomRecord.setShowRoomName(showroom.getMeetingName());
            Park park = parkService.selectParkById(showroomRecord.getParkId());
            if (park != null) {
                showroomRecord.setParkName(park.getName());
            }


            Tenant tenant = tenantService.selectTenantById(showroomRecord.getTenantId());
            if (tenant != null) {
                showroomRecord.setTenantName(tenant.getName());
            }
            Room room = roomService.selectRoomById(showroom.getRoomId());
            if (null != room) {
                showroomRecord.setRoomName(room.getName());

                Building building = buildingService.selectBuildingById(room.getBuildingId());
                if (null != building) {
                    showroomRecord.setBuildingName(building.getBuildingName());
                }
                BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(room.getBuildingDetailId());
                if (null != buildingDetail) {
                    showroomRecord.setBuildName(buildingDetail.getFloorName());
                }
            }
        }

        IcsCustomerStaff customerStaff = customerStaffService.selectIcsCustomerStaffById(showroomRecord.getUserId());
        if (null != customerStaff) {
            showroomRecord.setUserName(customerStaff.getUsername());
            showroomRecord.setUserPhone(customerStaff.getMobile());

            // todo  企业展示如何展示
            Customer customer = customerService.selectCustomerById(customerStaff.getIcsCustomerId());
            if (null != customer) {
                showroomRecord.setCustomerName(customer.getName());
            }

        }
        long duration = DateUtil.between(showroomRecord.getStartTime(), showroomRecord.getEndDate(), DateUnit.HOUR);
        showroomRecord.setDuration(duration);
        return showroomRecord;
    }

    /**
     * 查询展厅预约记录列表
     */
    @RequiresPermissions("meeting:showroomRecord:list")
    @GetMapping("list")
    public R list(ShowroomRecord showroomRecord) {
        startPage();
        boolean isAdmin = SubjectUtil.hasRole(getRequest(),"manager");
        if (isAdmin){
            Long parkId = currentUserService.getParkId();
            Long tenantId = currentUserService.getTenantId();
            showroomRecord.setParkId(parkId);
//            showroomRecord.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());
                if (null != customer){
                    List<IcsCustomerStaff> icsCustomerStaffs = staffService.selectUserByCustomer(customer.getId());
                    List<Long> ids = icsCustomerStaffs.stream().map(item -> {
                        return item.getId();
                    }).collect(Collectors.toList());
                    showroomRecord.setStaffIds(ids);
                }
            }
        }
        List<ShowroomRecord> showroomRecords = showroomRecordService.selectShowroomRecordList(showroomRecord);
        for (ShowroomRecord record : showroomRecords) {
            Showroom showroom = showroomService.selectShowroomById(record.getShowroomId());
            if (null != showroom){
                record.setShowRoomName(showroom.getMeetingName());

                Room room = roomService.selectRoomById(showroom.getRoomId());
                if (null != room){
                    record.setBuildingId(room.getBuildingId());
                }

            }
        }
        return result(showroomRecords);
    }


    /**
     * 新增保存展厅预约记录
     */
    @RequiresPermissions("meeting:showroomRecord:add")
    @PostMapping("save")
    public R addSave(@RequestBody ShowroomRecord showroomRecord) {

        User user = userService.selectUserById(getCurrentUserId());
        if (null != user.getCustomerId()){
            showroomRecord.setUserId(user.getCustomerId());
        }
        showroomRecord.setCreateTime(new Date());
        showroomRecord.setStatus(1);
        showroomRecord.setReservationNumber(RandomUtil.randomNumbers(18));
        return toAjax(showroomRecordService.insertShowroomRecord(showroomRecord));
    }

    /**
     * 修改保存展厅预约记录
     */
    @RequiresPermissions("meeting:showroomRecord:edit")
    @PostMapping("update")
    public R editSave(@RequestBody ShowroomRecord showroomRecord) {
        return toAjax(showroomRecordService.updateShowroomRecord(showroomRecord));
    }

    /**
     * 删除展厅预约记录
     */
    @RequiresPermissions("meeting:showroomRecord:remove")
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(showroomRecordService.deleteShowroomRecordByIds(ids));
    }



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




}
