package com.tashow.cloud.system.controller.admin.notify;

import com.tashow.cloud.common.enums.UserTypeEnum;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import com.tashow.cloud.system.controller.admin.notify.vo.template.NotifyTemplatePageReqVO;
import com.tashow.cloud.system.controller.admin.notify.vo.template.NotifyTemplateRespVO;
import com.tashow.cloud.system.controller.admin.notify.vo.template.NotifyTemplateSaveReqVO;
import com.tashow.cloud.system.controller.admin.notify.vo.template.NotifyTemplateSendReqVO;
import com.tashow.cloud.system.dal.dataobject.notify.NotifyTemplateDO;
import com.tashow.cloud.system.service.notify.NotifySendService;
import com.tashow.cloud.system.service.notify.NotifyTemplateService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.tashow.cloud.common.pojo.CommonResult.success;

/**
 * 管理后台 - 站内信模版
 */
@RestController
@RequestMapping("/system/notify-template")
@Validated
public class NotifyTemplateController {

    @Resource
    private NotifyTemplateService notifyTemplateService;

    @Resource
    private NotifySendService notifySendService;

    /**
     * 创建站内信模版
     */
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('system:notify-template:create')")
    public CommonResult<Long> createNotifyTemplate(
            @Valid @RequestBody NotifyTemplateSaveReqVO createReqVO) {
        return success(notifyTemplateService.createNotifyTemplate(createReqVO));
    }

    /**
     * 更新站内信模版
     */
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermission('system:notify-template:update')")
    public CommonResult<Boolean> updateNotifyTemplate(
            @Valid @RequestBody NotifyTemplateSaveReqVO updateReqVO) {
        notifyTemplateService.updateNotifyTemplate(updateReqVO);
        return success(true);
    }

    /**
     * 删除站内信模版
     */
    @DeleteMapping("/delete")
    @PreAuthorize("@ss.hasPermission('system:notify-template:delete')")
    public CommonResult<Boolean> deleteNotifyTemplate(@RequestParam("id") Long id) {
        notifyTemplateService.deleteNotifyTemplate(id);
        return success(true);
    }

    /**
     * 获得站内信模版
     */
    @GetMapping("/get")
    @PreAuthorize("@ss.hasPermission('system:notify-template:query')")
    public CommonResult<NotifyTemplateRespVO> getNotifyTemplate(@RequestParam("id") Long id) {
        NotifyTemplateDO template = notifyTemplateService.getNotifyTemplate(id);
        return success(BeanUtils.toBean(template, NotifyTemplateRespVO.class));
    }

    /**
     * 获得站内信模版分页
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:notify-template:query')")
    public CommonResult<PageResult<NotifyTemplateRespVO>> getNotifyTemplatePage(
            @Valid NotifyTemplatePageReqVO pageVO) {
        PageResult<NotifyTemplateDO> pageResult = notifyTemplateService.getNotifyTemplatePage(pageVO);
        return success(BeanUtils.toBean(pageResult, NotifyTemplateRespVO.class));
    }

    /**
     * 发送站内信
     */
    @PostMapping("/send-notify")
    @PreAuthorize("@ss.hasPermission('system:notify-template:send-notify')")
    public CommonResult<Long> sendNotify(@Valid @RequestBody NotifyTemplateSendReqVO sendReqVO) {
        if (UserTypeEnum.MEMBER.getValue().equals(sendReqVO.getUserType())) {
            return success(
                    notifySendService.sendSingleNotifyToMember(
                            sendReqVO.getUserId(), sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
        } else {
            return success(
                    notifySendService.sendSingleNotifyToAdmin(
                            sendReqVO.getUserId(), sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
        }
    }
}
