
package com.tmerclub.cloud.distribution.controller.platform;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.distribution.dto.DistributionMsgDTO;
import com.tmerclub.cloud.distribution.model.DistributionMsg;
import com.tmerclub.cloud.distribution.service.DistributionMsgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 分销公告信息
 *
 * @author cl
 * @date 2021-08-09 14:14:05
 */
@RestController("platformDistributionMsgController")
@RequestMapping("/p/distributionMsg")
@Tag(name = "分销公告信息")
public class DistributionMsgController {

    @Autowired
    private DistributionMsgService distributionMsgService;


    @GetMapping("/page")
    @Operation(summary = "获取分销公告信息列表", description = "分页获取分销公告信息列表")
    public ServerResponseEntity<PageVO<DistributionMsg>> page(@Valid PageDTO pageDTO, String msgTitle) {
        PageVO<DistributionMsg> distributionMsgPage = distributionMsgService.page(pageDTO, msgTitle);
        return ServerResponseEntity.success(distributionMsgPage);
    }

    @GetMapping
    @Operation(summary = "获取分销公告信息", description = "根据msgId获取分销公告信息")
    public ServerResponseEntity<DistributionMsg> getByMsgId(@RequestParam Long msgId) {
        return ServerResponseEntity.success(distributionMsgService.getByMsgId(msgId));
    }

    @PostMapping
    @Operation(summary = "保存分销公告信息", description = "保存分销公告信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody DistributionMsgDTO distributionMsgDTO) {
        DistributionMsg distributionMsg = BeanUtil.map(distributionMsgDTO, DistributionMsg.class);
        distributionMsg.setMsgId(null);
        distributionMsg.setUpdateTime(new Date());
        distributionMsgService.save(distributionMsg);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新分销公告信息", description = "更新分销公告信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody DistributionMsgDTO distributionMsgDTO) {
        DistributionMsg distributionMsg = BeanUtil.map(distributionMsgDTO, DistributionMsg.class);
        distributionMsg.setUpdateTime(new Date());
        distributionMsgService.update(distributionMsg);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除分销公告信息", description = "根据分销公告信息id删除分销公告信息")
    public ServerResponseEntity<Void> delete(@RequestBody List<Long> msgIds) {
        distributionMsgService.deleteBatch(msgIds);
        return ServerResponseEntity.success();
    }
}
