package com.jwipc.message.controller.rest;

import cn.hutool.core.util.ObjectUtil;
import com.jwipc.message.dto.AddresseeUserDTO;
import com.jwipc.message.dto.MessageCenterDTO;
import com.jwipc.message.dto.MessageCenterParamDTO;
import com.jwipc.message.logic.MessageCenterLogic;
import com.jwipc.message.model.query.AddresseeUserQuery;
import com.jwipc.message.model.query.MessageCenterQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.jwipc.message.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 我发起的 前端控制器
 * </p>
 *
 * @author shenyang.ou
 * @since 2024-07-03
 */
@RestController
@RequestMapping(MODULE_ROOT + "/launch/rest")
@RequiredArgsConstructor
public class MessageLaunchRestController {
    private final MessageCenterLogic messageCenterLogic;

    @RequestMapping("/list")
    public PageView list(MessageCenterQuery query) {
        if (!"admin".equals(LoginUtil.getAccount())){
            query.setCreateUser(LoginUtil.getUserId());
        }
        return messageCenterLogic.findByPage(query);
    }

    @PostMapping("/user/list")
    public List<AddresseeUserDTO> userList(AddresseeUserQuery query){
        return messageCenterLogic.userList(query);
    }


    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody MessageCenterParamDTO param) {
        messageCenterLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

    /**
     * 发布
     *
     * @param id 询价id
     */
    @PostMapping("/publish")
    public ResponseEntity publish(Long id) {
        messageCenterLogic.publish(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 删除
     *
     * @param id 询价id
     */
    @PostMapping("/remove")
    public ResponseEntity remove(Long id) {
        messageCenterLogic.remove(id);
        return ResponseEntity.ok(true);
    }
}
