package com.hys.app.controller.erp.use;

import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.model.erp.dto.MsgConfigDTO;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.service.erp.MsgConfigManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "消息配置")
@RestController
@RequestMapping("/admin/erp/msgConfig")
@AllArgsConstructor
public class MsgConfigController {

    private MsgConfigManager msgConfigManager;

    @PostMapping("add")
    @ApiOperation(value = "新增消息")
    @Log(client = LogClient.admin, detail = "新增名称为[${msgConfigDTO.title}]的消息")
    public Long addMsg(@Valid @RequestBody MsgConfigDTO msgConfigDTO) {
        return msgConfigManager.addMsg(msgConfigDTO);
    }

    @PostMapping("changeTemplate/{id}/{templateCode}")
    @ApiOperation(value = "更换消息模板")
    @Log(client = LogClient.admin, detail = "创建名称为[${companyDO.name}]的公司信息")
    public void changeTemplate(@PathVariable String id, @PathVariable String templateCode) {
        msgConfigManager.changeTemplate(id, templateCode);
    }

    @PostMapping("deleteMsg/{id}")
    @ApiOperation(value = "更换消息模板")
    @Log(client = LogClient.admin, detail = "删除id为[${id}]的消息")
    public void deleteMsg(@PathVariable String id) {
        if (!AdminUserContext.hasFound()) {
            throw new ServiceException("只有平台管理员才能做此操作");
        }
        msgConfigManager.deleteMsg(id);
    }


    @GetMapping("getAllMsg")
    @ApiOperation(value = "获取所有消息不分组树状图")
    public void getAllMsg() {
        msgConfigManager.getAllMsg();
    }
}
