package com.mortals.xhx.module.metting.web;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.metting.model.MettingRecordEntity;
import com.mortals.xhx.module.metting.service.MettingRecordService;
import com.mortals.xhx.module.room.model.RoomEntity;
import com.mortals.xhx.module.room.model.RoomQuery;
import com.mortals.xhx.module.room.service.RoomService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 会议记录
 *
 * @author zxfei
 * @date 2023-05-22
 */
@RestController
@RequestMapping("metting/record")
public class MettingRecordController extends BaseCRUDJsonBodyMappingController<MettingRecordService, MettingRecordEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private RoomService roomService;


    public MettingRecordController() {
        super.setModuleDesc("会议记录");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "meetStatus", paramService.getParamBySecondOrganize("MettingRecord", "meetStatus"));
        this.addDict(model, "finishMethod", paramService.getParamBySecondOrganize("MettingRecord", "finishMethod"));

        this.addDict(model, "roomId", roomService.find(new RoomQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getRoomName(), (o, n) -> n)));
        super.init(model, context);
    }

    @Override
    protected void doListBefore(MettingRecordEntity query, Map<String, Object> model, Context context) throws AppException {
        super.doListBefore(query, model, context);

        if(ObjectUtils.isEmpty(query.getOrderColList())){
            query.setOrderColList(Arrays.asList(new OrderCol("createTime", OrderCol.DESCENDING)));
        }else{
            query.getOrderColList().add(new OrderCol("createTime", OrderCol.DESCENDING));
        }

        if (!ObjectUtils.isEmpty(query.getDeviceCode())) {
            //根据设备编码 查询房间room
            RoomEntity roomEntity = roomService.selectOne(new RoomQuery().deviceCode(query.getDeviceCode()));
            if (!ObjectUtils.isEmpty(roomEntity)) {
                query.setRoomId(roomEntity.getId());
            }
        }
    }
}