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

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.biz.constant.NotifyType;
import com.tmerclub.cloud.biz.dto.NotifyTemplateDTO;
import com.tmerclub.cloud.biz.model.NotifyTemplate;
import com.tmerclub.cloud.biz.service.NotifyTemplateService;
import com.tmerclub.cloud.biz.service.NotifyTemplateTagService;
import com.tmerclub.cloud.biz.vo.NotifyTemplateVO;
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.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
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.List;
import java.util.Objects;

/**
 * @author cl
 * @date 2021-05-20 11:09:53
 */
@RestController("platformNotifyTemplateTagController")
@RequestMapping("/p/notifyTemplateTag")
@Tag(name = "platform-标签消息")
public class NotifyTemplateTagController {

    @Autowired
    private NotifyTemplateTagService notifyTemplateTagService;
    @Autowired
    private NotifyTemplateService notifyTemplateService;


    @GetMapping("/page")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<NotifyTemplateVO>> page(@Valid PageDTO pageDTO) {
        PageVO<NotifyTemplateVO> notifyTemplateTagPage = notifyTemplateTagService.pageTagNotify(pageDTO);
        return ServerResponseEntity.success(notifyTemplateTagPage);
    }

    @GetMapping("/info")
    @Operation(summary = "获取", description = "根据templateId获取")
    public ServerResponseEntity<NotifyTemplateVO> getByTemplateId(@RequestParam Long templateId) {
        return ServerResponseEntity.success(notifyTemplateTagService.getByTemplateId(templateId));
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Void> save(@Valid @RequestBody NotifyTemplateDTO notifyTemplateDTO) {
        List<Long> tagIds = notifyTemplateDTO.getTagIds();
        if (CollUtil.isEmpty(tagIds)) {
            throw new LuckException("请至少选择一个标签！");
        }
        NotifyTemplate notifyTemplate = BeanUtil.map(notifyTemplateDTO, NotifyTemplate.class);
        notifyTemplate.setTemplateId(null);
        notifyTemplate.setStatus(1);
        notifyTemplate.setSendType(SendTypeEnum.CUSTOMIZE.getValue());
        notifyTemplate.setNotifyTypes(NotifyType.APP.value().toString());
        notifyTemplateTagService.saveTagNotify(notifyTemplate, tagIds);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody NotifyTemplateDTO notifyTemplateDTO) {
        if (!Objects.equals(SendTypeEnum.CUSTOMIZE.getValue(), notifyTemplateDTO.getSendType())) {
            throw new LuckException("非自定义类型的消息模板不可以使用此接口更新");
        }
        notifyTemplateTagService.updateTemplateAndTag(notifyTemplateDTO);
        return ServerResponseEntity.success();
    }


    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long templateId) {
        NotifyTemplateVO notifyTemplateVO = notifyTemplateService.getByTemplateId(templateId);
        if (!Objects.equals(SendTypeEnum.CUSTOMIZE.getValue(), notifyTemplateVO.getSendType())) {
            throw new LuckException("非自定义类型的消息模板不可以使用此接口更新");
        }
        notifyTemplateTagService.deleteTemplateTagByTempLateId(templateId);
        return ServerResponseEntity.success();
    }


    @PutMapping("/send_msg")
    @Operation(summary = "给标签用户推送模板的站内消息消息", description = "给标签用户推送模板的站内消息")
    public ServerResponseEntity<Void> sendMsg(@RequestParam Long templateId) {
        notifyTemplateTagService.sendMsg(templateId);
        return ServerResponseEntity.success();
    }

}
