package com.ics.admin.controller.meeting;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.ics.admin.domain.*;
import com.ics.admin.domain.meeting.CustomerTicket;
import com.ics.admin.domain.meeting.RoomContent;
import com.ics.admin.domain.meeting.Ticket;
import com.ics.admin.service.*;
import com.ics.admin.service.meeting.ICustomerTicketService;
import com.ics.admin.service.meeting.IRoomContentService;
import com.ics.admin.service.meeting.ITicketService;
import com.ics.common.core.domain.IcsCustomerStaff;
import com.ics.system.domain.Tenant;
import com.ics.system.domain.User;
import com.ics.system.mapper.DictDataMapper;
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.Reservation;
import com.ics.admin.service.meeting.IReservationService;
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;

/**
 * 预约记录 提供者
 *
 * @author chen
 * @date 2024-02-25
 */
@RestController
@RequestMapping("/meeting/reservation")
public class ReservationController extends BaseController {

    @Autowired
    private IReservationService reservationService;

    @Autowired
    private IRoomContentService iRoomContentService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IIcsCustomerStaffService customerStaffService;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private IUserService userService;

    @Autowired
    private ICurrentUserService currentUserService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IParkService parkService;

    @Autowired
    private ITenantService tenantService;

    @Autowired
    private IBuildingService buildingService;

    @Autowired
    private IBuildingDetailService buildingDetailService;

    @Autowired
    private ITicketService ticketService;

    @Autowired
    private ICustomerTicketService customerTicketService;

    /**
     * 查询预约记录
     */
    @Ignore
    @GetMapping("get/{id}")
    public Reservation get(@PathVariable("id") Long id) {

        Reservation reservation = reservationService.selectReservationById(id);
        Date endDate = reservation.getEndDate();
        Date startTime = reservation.getStartTime();
        reservation.setStatusName(reservation.getStauts().getName());
        long duration = DateUtil.between(startTime, endDate, DateUnit.HOUR);
        reservation.setDuration(duration);
        IcsCustomerStaff customerStaff = customerStaffService.selectIcsCustomerStaffById(reservation.getUserId());
        Assert.isTrue(customerStaff != null, "用户不存在");
        reservation.setUserName(customerStaff.getUsername());
        reservation.setPhone(customerStaff.getMobile());
        RoomContent roomContent = iRoomContentService.selectRoomContentById(reservation.getRoomContentId());
//        roomContent.setTypeName(roomContent.getType().getName());
        String typeName = dictDataMapper.selectDictLabel("meeting_type", String.valueOf(roomContent.getType()));
        roomContent.setTypeName(typeName);
        //会议室名称
        reservation.setRoomContent(roomContent);
        Customer customer = customerService.selectCustomerById(reservation.getCustomerId());
        if (null != customer){
            reservation.setCustomerName(customer.getName());
        }

        Park park = parkService.selectParkById(reservation.getParkId());
        if (park !=null ){
            reservation.setParkName(park.getName());
        }

        Ticket ticket = ticketService.selectTicketById(reservation.getTicketId());
        if (ticket !=null){
            reservation.setTicketName(ticket.getTitle());
        }

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

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

        return reservation;
    }

    /**
     * 查询预约记录列表
     */
    @RequiresPermissions("meeting:reservation:list")
    @GetMapping("list")
    public R list(Reservation reservation) {
        startPage();
        boolean isAdmin = SubjectUtil.hasRole(getRequest(),"manager");
        if (isAdmin){
            Long parkId = currentUserService.getParkId();
            Long tenantId = currentUserService.getTenantId();
            reservation.setParkId(parkId);
//            reservation.setTenantId(tenantId);
        }
        boolean b = SubjectUtil.hasRole(getRequest(),"admin");
        if (b){
            User user = userService.selectUserById(getCurrentUserId());
            if (null != user.getCustomerId()){
                reservation.setCustomerId(user.getCustomerId());
            }
        }
        List<Reservation> reservations = reservationService.selectReservationList(reservation);
        for (Reservation reservation1 : reservations) {
            RoomContent roomContent = iRoomContentService.selectRoomContentById(reservation1.getRoomContentId());
            if (roomContent != null){
                String typeName = dictDataMapper.selectDictLabel("meeting_type", String.valueOf(roomContent.getType()));
                roomContent.setTypeName(typeName);

                Room room = roomService.selectRoomById(roomContent.getRoomId());
                if (null != room){
                    reservation1.setBuildingId(room.getBuildingId());
                }
            }
            if (reservation1.getOrderTime()!=null){
                // 已经支付
                reservation1.setIsPay("已支付");
            }else {
                reservation1.setIsPay("未支付");
            }
            //会议室名称
            reservation1.setRoomContent(roomContent);
            reservation1.setStatusName(reservation1.getStauts().getName());
            reservation1.setStatusValue(reservation1.getStauts().getValue());
            Customer customer = customerService.selectCustomerById(reservation1.getCustomerId());
            if (null != customer){
                reservation1.setCustomerName(customer.getName());
            }
            IcsCustomerStaff customerStaff = customerStaffService.selectIcsCustomerStaffById(reservation1.getUserId());
            if (null != customerStaff){
                reservation1.setUserName(customerStaff.getUsername());
            }
        }
        return result(reservations);
    }

    /**
     * 新增保存预约记录
     */
    @RequiresPermissions("meeting:reservation:add")
    @PostMapping("save")
    public R addSave(@RequestBody Reservation reservation) {
        reservation.setStauts(Reservation.Status.APPOINTMENT);

        long currentUserId1 = this.getCurrentUserId();
        User user = userService.selectUserById(currentUserId1);
        Assert.isTrue(user.getStaffId() !=null, "当前用户没有绑定小程序用户");

        reservation.setUserId(user.getStaffId());
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改保存预约记录
     */
    @RequiresPermissions("meeting:reservation:edit")
    @PostMapping("update")
    public R editSave(@RequestBody Reservation reservation) {

        if (reservation.getCancelResaon() !=null){
            reservation.setCancelResaon("管理员取消原因为:"+reservation.getCancelResaon());

            Long ticketId = reservation.getTicketId();
            if (ticketId !=null){
                Ticket ticket = ticketService.selectTicketById(ticketId);
                if (ticket != null){
                    Long customerId = reservation.getCustomerId();
                    CustomerTicket customerTicket = new CustomerTicket();
                    customerTicket.setTicketId(ticketId);
                    customerTicket.setCustomerId(customerId);
                    customerTicket.setType(ticket.getType());
                    customerTicket.setIsVerification(0);
                    int i1 = customerTicketService.insertCustomerTicket(customerTicket);
                }
            }
        }

        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约记录
     */
    @RequiresPermissions("meeting:reservation:remove")
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }

}
