
package com.tmerclub.cloud.biz.controller.admin;

import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.biz.dto.NotifyTemplateRemindDTO;
import com.tmerclub.cloud.biz.model.NotifyTemplateRemind;
import com.tmerclub.cloud.biz.service.NotifyTemplateRemindService;
import com.tmerclub.cloud.biz.vo.NotifyTemplateRemindVO;
import com.tmerclub.cloud.common.constant.SendTypeEnum;
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.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
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.Objects;

/**
 * 消息提醒设置
 *
 * @author tmerclub
 * @date 2022-10-24 13:48:38
 */
@RestController("adminNotifyTemplateRemindController")
@RequestMapping("/mp/notifyRemind")
@Tag(name = "商家端和供应商端消息提醒设置")
public class NotifyTemplateRemindController {

    @Autowired
    private NotifyTemplateRemindService notifyTemplateRemindService;


    @GetMapping("/page")
    @Operation(summary = "获取消息提醒设置列表", description = "分页获取消息提醒设置列表")
    public ServerResponseEntity<PageVO<NotifyTemplateRemindVO>> page(@Valid PageDTO pageDTO) {
        NotifyTemplateRemindDTO notifyTemplateRemindDTO = new NotifyTemplateRemindDTO();
        notifyTemplateRemindDTO.setSysType(AuthUserContext.get().getSysType());
        notifyTemplateRemindDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<NotifyTemplateRemindVO> notifyTemplateRemindPage = notifyTemplateRemindService.page(pageDTO, notifyTemplateRemindDTO);

        //没有消息提醒设置记录，默认开启提醒
        if (CollectionUtil.isNotEmpty(notifyTemplateRemindPage.getList())) {
            notifyTemplateRemindPage.getList().forEach(notifyTemplateRemindVO -> {
                if (Objects.isNull(notifyTemplateRemindVO.getNotifyTemplateRemindId())) {
                    notifyTemplateRemindVO.setIsRemind(1);
                }
                notifyTemplateRemindVO.setMenu(Objects.requireNonNull(SendTypeEnum.instance(notifyTemplateRemindVO.getSendType())).getMenu());
                notifyTemplateRemindVO.setNodeName(Objects.requireNonNull(SendTypeEnum.instance(notifyTemplateRemindVO.getSendType())).getNodeName());
            });
        }
        return ServerResponseEntity.success(notifyTemplateRemindPage);
    }

    @GetMapping
    @Operation(summary = "获取消息提醒设置", description = "根据notifyTemplateRemindId获取消息提醒设置")
    public ServerResponseEntity<NotifyTemplateRemind> getByNotifyTemplateRemindId(@RequestParam Long notifyTemplateRemindId) {
        return ServerResponseEntity.success(notifyTemplateRemindService.getByNotifyTemplateRemindId(notifyTemplateRemindId));
    }

    @PostMapping
    @Operation(summary = "保存消息提醒设置", description = "保存消息提醒设置")
    public ServerResponseEntity<Void> save(@Valid @RequestBody NotifyTemplateRemindDTO notifyTemplateRemindDTO) {
        NotifyTemplateRemind notifyTemplateRemind = BeanUtil.map(notifyTemplateRemindDTO, NotifyTemplateRemind.class);
        notifyTemplateRemind.setNotifyTemplateRemindId(null);
        notifyTemplateRemindService.save(notifyTemplateRemind);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新消息提醒设置", description = "更新消息提醒设置")
    public ServerResponseEntity<Void> update(@Valid @RequestBody NotifyTemplateRemindDTO notifyTemplateRemindDTO) {
        NotifyTemplateRemind notifyTemplateRemind = BeanUtil.map(notifyTemplateRemindDTO, NotifyTemplateRemind.class);
        notifyTemplateRemind.setShopId(AuthUserContext.get().getTenantId());
        notifyTemplateRemind.setSysType(AuthUserContext.get().getSysType());
        //提醒设置记录为空，则保存记录
        if (Objects.isNull(notifyTemplateRemindDTO.getNotifyTemplateRemindId())) {
            notifyTemplateRemindService.save(notifyTemplateRemind);
        } else {
            notifyTemplateRemindService.update(notifyTemplateRemind);
        }
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除消息提醒设置", description = "根据消息提醒设置id删除消息提醒设置")
    public ServerResponseEntity<Void> delete(@RequestParam Long notifyTemplateRemindId) {
        notifyTemplateRemindService.deleteById(notifyTemplateRemindId);
        return ServerResponseEntity.success();
    }
}
