package com.xueyi.system.meeting.controller;

import com.alibaba.fastjson2.JSONObject;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.common.web.response.MyResponse;
import com.xueyi.system.api.meeting.domain.dto.DmMeetingOrdersDto;
import com.xueyi.system.api.meeting.domain.po.DmMeetingOrdersPo;
import com.xueyi.system.meeting.domain.model.DmMeetingOrdersConverter;
import com.xueyi.system.meeting.domain.query.DmMeetingOrdersQuery;
import com.xueyi.system.meeting.mapper.DmMeetingOrdersMapper;
import com.xueyi.system.meeting.service.IDmMeetingOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 会议室预约管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/meeting/orders")
public class DmMeetingOrdersController extends BaseController<DmMeetingOrdersQuery, DmMeetingOrdersDto, IDmMeetingOrdersService> {

    @Autowired
    private DmMeetingOrdersConverter dmMeetingOrdersConverter;

    @Autowired
    private DmMeetingOrdersMapper dmMeetingOrdersMapper;

    @Autowired
    private IDmMeetingOrdersService dmMeetingOrdersService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "会议室预约" ;
    }

    /**
     * 查询会议室预约列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.DM_MEETING_ORDERS_LIST)
    public AjaxResult list(DmMeetingOrdersQuery dmMeetingOrders) {
        startPage();
        List<DmMeetingOrdersDto> list = baseService.selectListScope(dmMeetingOrders);
        return getDataTable(list);
    }

    /**
     * 查询会议室预约详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.DM_MEETING_ORDERS_SINGLE)
    public AjaxResult getInfo(@PathVariable Serializable id) {
        return super.getInfo(id);
    }

    /**
     * 会议室预约新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.DM_MEETING_ORDERS_ADD)
    @Log(title = "会议室预约管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody DmMeetingOrdersDto dmMeetingOrders) {
        return super.add(dmMeetingOrders);
    }

    /**
     * 会议室预约修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.DM_MEETING_ORDERS_EDIT)
    @Log(title = "会议室预约管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody DmMeetingOrdersDto dmMeetingOrders) {
        return super.edit(dmMeetingOrders);
    }

    /**
     * 会议室预约批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.DM_MEETING_ORDERS_DEL)
    @Log(title = "会议室预约管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    @RequiresPermissions(Auth.DM_MEETING_ORDERS_LIST)
    @GetMapping("/lists")
    public JSONObject lists(@RequestParam("dateStr") String dateStr, @RequestParam("spaceId") Long spaceId) {
        List<DmMeetingOrdersPo> list = dmMeetingOrdersMapper.findListByDate(dateStr, spaceId);
        List<DmMeetingOrdersDto> res = new ArrayList<>();
        list.forEach(item -> {
            DmMeetingOrdersDto dto = dmMeetingOrdersConverter.mapperDto(item);
            dmMeetingOrdersService.fixColumns(dto);
            res.add(dto);
        });
        MyResponse myResponse = new MyResponse();
        myResponse.setData(res);
        myResponse.setErrMsg("");
        myResponse.setStatus(0);
        return myResponse.toJSON();
    }

    /**
     * 获取会议室预约选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 会议室预约管理 - 列表 */
        String DM_MEETING_ORDERS_LIST = "meeting:orders:meeting/orders:list";
        /** 系统 - 会议室预约管理 - 详情 */
        String DM_MEETING_ORDERS_SINGLE = "meeting:orders:meeting/orders:single";
        /** 系统 - 会议室预约管理 - 新增 */
        String DM_MEETING_ORDERS_ADD = "meeting:orders:meeting/orders:add";
        /** 系统 - 会议室预约管理 - 修改 */
        String DM_MEETING_ORDERS_EDIT = "meeting:orders:meeting/orders:edit";
        /** 系统 - 会议室预约管理 - 删除 */
        String DM_MEETING_ORDERS_DEL = "meeting:orders:meeting/orders:delete";
    }
}
