package com.red.circle.other.app.inner.service.sys.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.sys.SysMessageCopywritingTypeInnerConvertor;
import com.red.circle.other.app.inner.service.sys.SysMessageCopywritingTypeClientService;
import com.red.circle.other.infra.database.rds.entity.sys.SysMessageCopywritingTextType;
import com.red.circle.other.infra.database.rds.service.sys.SysMessageCopywritingContentService;
import com.red.circle.other.infra.database.rds.service.sys.SysMessageCopywritingTextTypeService;
import com.red.circle.other.inner.enums.sys.MessageBusinessSceneEnum;
import com.red.circle.other.inner.enums.sys.MessageCopywritingTypeEnum;
import com.red.circle.other.inner.model.cmd.sys.SysMessageCopywritingTypeQryCmd;
import com.red.circle.other.inner.model.dto.sys.SysMessageCopywritingTextTypeDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author zongpubin on 2023/11/10
 */
@Service
@RequiredArgsConstructor
public class SysMessageCopywritingTypeClientServiceImpl implements
    SysMessageCopywritingTypeClientService {

  private final SysMessageCopywritingContentService messageCopywritingContentService;
  private final SysMessageCopywritingTextTypeService messageCopywritingTextTypeService;
  private final SysMessageCopywritingTypeInnerConvertor sysMessageCopywritingTypeInnerConvertor;


  @Override
  public PageResult<SysMessageCopywritingTextTypeDTO> pageCopywritingTextType(
      SysMessageCopywritingTypeQryCmd query) {
    PageResult<SysMessageCopywritingTextType> pageResult = messageCopywritingTextTypeService.pageCopywritingTextType(
        query);
    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      pageResult.convert(
          sysMessageCopywritingTypeInnerConvertor::toSysMessageCopywritingTextTypeDTO);
    }

    return pageResult.convert(
        sysMessageCopywritingTextType -> {
          SysMessageCopywritingTextTypeDTO textTypeDTO = sysMessageCopywritingTypeInnerConvertor.toSysMessageCopywritingTextTypeDTO(
              sysMessageCopywritingTextType);
          textTypeDTO.setBusinessSceneName(
              MessageBusinessSceneEnum.getDesc(textTypeDTO.getBusinessScene()));
          textTypeDTO.setMsgTypeName(MessageCopywritingTypeEnum.getDesc(textTypeDTO.getMsgType()));
          return textTypeDTO;
        });
  }

  @Override
  public void addTypeAndContent(SysMessageCopywritingTextTypeDTO param) {
    messageCopywritingTextTypeService.save(
        sysMessageCopywritingTypeInnerConvertor.toSysMessageCopywritingTextType(param));
  }


  @Override
  public void deleteTypeAndContent(Long id) {
    messageCopywritingTextTypeService.deleteById(id);
    messageCopywritingContentService.deleteByTypeId(id);
  }

}
