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

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
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.RoomDeviceEntity;
import com.mortals.xhx.module.room.model.RoomDeviceQuery;
import com.mortals.xhx.module.room.model.RoomEntity;
import com.mortals.xhx.module.room.model.RoomQuery;
import com.mortals.xhx.module.room.service.RoomDeviceService;
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.RequestBody;
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;
    @Autowired
    private RoomDeviceService roomDeviceService;


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


    @Override
    @UnAuth
    public Rest<Object> list(@RequestBody MettingRecordEntity query) {
        return super.list(query);
    }

    @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("meetDate", OrderCol.DESCENDING),new OrderCol("meetTimeStart", OrderCol.DESCENDING)));
        }

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

        if (!ObjectUtils.isEmpty(query.getDeviceCode())) {
            //根据设备编码 查询房间room
            RoomDeviceEntity roomEntity = roomDeviceService.selectOne(new RoomDeviceQuery().deviceCode(query.getDeviceCode()));
            if (!ObjectUtils.isEmpty(roomEntity)) {
                query.setRoomId(roomEntity.getRoomId());
            } else {
                query.setRoomId(-1L);
            }
            query.setMeetDateStart(DateUtil.today());
            query.setMeetDateEnd(DateUtil.today());
        }
    }



    public static void main(String[] args) {
        MettingRecordEntity mettingRecordEntity = new MettingRecordEntity();
        mettingRecordEntity.setOrderColList(Arrays.asList(new OrderCol("createTime", OrderCol.DESCENDING)));

        System.out.println(JSON.toJSONString(mettingRecordEntity));
    }

}