package com.red.circle.other.app.command.user;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.other.app.convertor.material.PropsRedPacketSkinAppConvertor;
import com.red.circle.other.infra.database.rds.entity.props.PropsRedPacketSkin;
import com.red.circle.other.infra.database.rds.service.props.PropsRedPacketSkinService;
import com.red.circle.other.inner.model.cmd.material.PropsRedPacketSkinCmd;
import com.red.circle.other.inner.model.dto.material.PropsRedPacketSkinDTO;
import com.red.circle.tool.core.date.TimestampUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author longli on  2024/3/22
 */

@Component
@RequiredArgsConstructor
public class PropsRedPacketSkinSaveExe {

  private final PropsRedPacketSkinService propsRedPacketSkinService;
  private final PropsRedPacketSkinAppConvertor propsRedPacketSkinAppConvertor;

  public void execute(PropsRedPacketSkinCmd cmd) {
    PropsRedPacketSkin skin = propsRedPacketSkinService.getById(cmd.getId());
    if (Objects.isNull(skin)) {
      propsRedPacketSkinService.save(propsRedPacketSkinAppConvertor.toPropsRedPacketSkin(cmd));
      return;
    }
    propsRedPacketSkinService.update()
        .set(PropsRedPacketSkin::getImOpenedUrl, skin.getImOpenedUrl())
        .set(PropsRedPacketSkin::getImNotOpenedUrl, skin.getImNotOpenedUrl())
        .set(PropsRedPacketSkin::getImSendCoverUrl, skin.getImSendCoverUrl())
        .set(PropsRedPacketSkin::getRoomOpenedUrl, skin.getRoomOpenedUrl())
        .set(PropsRedPacketSkin::getRoomNotOpenedUrl, skin.getRoomNotOpenedUrl())
        .set(PropsRedPacketSkin::getRoomSendCoverUrl, skin.getRoomSendCoverUrl())
        .set(PropsRedPacketSkin::getUpdateTime, TimestampUtils.now())
        .set(PropsRedPacketSkin::getUpdateUser, skin.getUpdateUser())
        .eq(PropsRedPacketSkin::getId, skin.getId())
        .last(PageConstant.LIMIT_ONE)
        .execute();

  }

}
