package com.ics.admin.controller.meeting;

import com.ics.admin.domain.meeting.Reservation;
import com.ics.admin.domain.meeting.RoomContent;
import com.ics.admin.domain.meeting.RoomRecord;
import com.ics.admin.service.IIcsCustomerStaffService;
import com.ics.admin.service.meeting.IReservationService;
import com.ics.admin.service.meeting.IRoomContentService;
import com.ics.admin.service.meeting.IRoomRecordService;
import com.ics.common.core.domain.IcsCustomerStaff;
import com.ics.system.mapper.DictDataMapper;
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.ReservationPerson;
import com.ics.admin.service.meeting.IReservationPersonService;
import org.wf.jwtp.annotation.Ignore;
import org.wf.jwtp.annotation.RequiresPermissions;

import java.util.List;

/**
 * 预约参观人员 提供者
 *
 * @author chen
 * @date 2024-03-06
 */
@RestController
@RequestMapping("person")
public class ReservationPersonController extends BaseController {

    @Autowired
    private IReservationPersonService reservationPersonService;

    @Autowired
    private IIcsCustomerStaffService customerStaffService;

    @Autowired
    private IReservationService iReservationService;

    @Autowired
    private IRoomContentService iRoomContentService;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private IRoomRecordService roomRecordService;

    /**
     * 查询预约参观人员
     */
    @Ignore
    @GetMapping("get/{id}")
    public ReservationPerson get(@PathVariable("id") Long id) {
        ReservationPerson reservationPerson = reservationPersonService.selectReservationPersonById(id);
        Reservation reservation = iReservationService.selectReservationById(reservationPerson.getReservationId());
        if (null != reservation){
            RoomContent roomContent = iRoomContentService.selectRoomContentById(reservationPerson.getReservationId());

            String typeName = dictDataMapper.selectDictLabel("meeting_type", String.valueOf(roomContent.getType()));
            reservationPerson.setTypeName(typeName);
        }

        return reservationPerson;
    }

    /**
     * 查询预约参观人员列表
     */
    @RequiresPermissions("meeting:person:list")
    @GetMapping("list")
    public R list(ReservationPerson reservationPerson) {
        startPage();
        int num = 0;
        List<ReservationPerson> reservationPeople = reservationPersonService.selectReservationPersonList(reservationPerson);
        for (ReservationPerson person : reservationPeople) {
            IcsCustomerStaff customerStaff = customerStaffService.selectIcsCustomerStaffById(person.getUserId());
            if (null != customerStaff){
                person.setUserName(customerStaff.getUsername());
            }
            IcsCustomerStaff participant = customerStaffService.selectIcsCustomerStaffById(person.getParticipantId());
            if (null != participant){
                person.setAvatar(participant.getAvatar());
            }

            //到访时间
            Long reservationId = person.getReservationId();
            Reservation reservation = iReservationService.selectReservationById(reservationId);
            if (null != reservation){
                //根据预约时间去查询用户开门时间
                RoomRecord roomRecord = roomRecordService.selectTimeByUserId(reservation.getStartTime(),reservation.getEndDate(),person.getParticipantId());
                if (null != roomRecord){
                    person.setStatus("已到访");
                    person.setVisitTime(roomRecord.getCreateTime());
                    num++;
                }else {
                    person.setStatus("未到访");
                }
            }
        }
        R result = result(reservationPeople);
        result.put("actualNum", reservationPeople.size());
        result.put("attainNum", num);
        return result;
    }


    /**
     * 新增保存预约参观人员
     */
    @RequiresPermissions("meeting:person:add")
    @PostMapping("save")
    public R addSave(@RequestBody ReservationPerson reservationPerson) {
        //发起人 和联系电话需要 自己添加

        return toAjax(reservationPersonService.insertReservationPerson(reservationPerson));
    }

    /**
     * 修改保存预约参观人员
     */
    @RequiresPermissions("meeting:person:edit")
    @PostMapping("update")
    public R editSave(@RequestBody ReservationPerson reservationPerson) {
        return toAjax(reservationPersonService.updateReservationPerson(reservationPerson));
    }

    /**
     * 删除预约参观人员
     */
    @RequiresPermissions("meeting:person:remove")
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(reservationPersonService.deleteReservationPersonByIds(ids));
    }

}
