package com.red.circle.game.fruit.infrastructure.database.repository.impl;

import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.framework.mybatis.convertor.MybatisConvertor;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.game.fruit.application.dto.enums.MessageTypeTopic;
import com.red.circle.game.fruit.infrastructure.database.dao.GameFruitDaysRecordDAO;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitDaysRecord;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitDaysRecordService;
import com.red.circle.game.fruit.infrastructure.websocket.message.MessageSender;
import com.red.circle.game.fruit.infrastructure.websocket.message.TopicMessage;
import com.red.circle.game.fruit.inner.model.cmd.PageDaysRecordQryCmd;
import com.red.circle.game.fruit.inner.model.dto.PageGameFruitDaysRecordDTO;
import com.red.circle.tool.core.date.LocalDateUtils;
import com.red.circle.tool.core.num.ArithmeticUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 每日游戏记录 服务实现类.
 * </p>
 *
 * @author lisizhe on 2023-11-30 19:18
 */
@Service
@RequiredArgsConstructor
public class GameFruitDaysRecordServiceImpl extends
    BaseServiceImpl<GameFruitDaysRecordDAO, GameFruitDaysRecord> implements
    GameFruitDaysRecordService {

  private final MessageSender messageSender;

  @Override
  public GameFruitDaysRecord getDaysRecord(String origin) {

    GameFruitDaysRecord gameFruitDaysRecord = query().eq(GameFruitDaysRecord::getDate,
        LocalDateUtils.nowToInteger()).eq(GameFruitDaysRecord::getSysOrigin, origin).getOne();

    if (Objects.isNull(gameFruitDaysRecord)) {
      GameFruitDaysRecord daysRecord = new GameFruitDaysRecord().setDate(
          LocalDateUtils.nowToInteger().toString())
          .setSysOrigin(origin);
      save(daysRecord);
      return daysRecord.setUserExpend(BigDecimal.ZERO)
          .setNewNum(0).setPizzaNum(0).setPlayerNum(0).setPlatformExpend(BigDecimal.ZERO)
          .setVegetableNum(0);
    }
    return gameFruitDaysRecord;
  }

  @Override
  public void add(GameFruitDaysRecord gameFruitDaysRecord) {
    save(gameFruitDaysRecord);
  }

  @Override
  public void addDaysBet(Long userId, BigDecimal userExpand, String origin) {
    GameFruitDaysRecord gameFruitDays = baseDAO.getToDayForUpdate(
        LocalDateUtils.nowToInteger().toString(), origin);

    if (Objects.isNull(gameFruitDays)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.USER_BET_MSG.getTopicId(userId), ResultResponse.failure(
                  CommonErrorCode.NOT_FOUND_RECORD_INFO)));
      return;
    }
    update().set(GameFruitDaysRecord::getUserExpend,
            gameFruitDays.getUserExpend().add(userExpand))
        .eq(GameFruitDaysRecord::getSysOrigin, origin)
        .eq(GameFruitDaysRecord::getDate, LocalDateUtils.nowToInteger().toString()).execute();
  }

  @Override
  public void updatePlayerNum(Long userId, String origin) {
    GameFruitDaysRecord gameFruitDays = getDaysRecord(origin);
    if (Objects.isNull(gameFruitDays)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.GAME_FRUIT_MSG.getTopicId(userId), ResultResponse.failure(
                  CommonErrorCode.NOT_FOUND_RECORD_INFO)));
      return;
    }
    update().set(GameFruitDaysRecord::getPlayerNum,
            gameFruitDays.getPlayerNum() + 1)
        .eq(GameFruitDaysRecord::getSysOrigin, origin)
        .eq(GameFruitDaysRecord::getDate, LocalDateUtils.nowToInteger().toString()).execute();
  }

  @Override
  public void updateNewNum(Long userId, String origin) {
    GameFruitDaysRecord gameFruitDays = getDaysRecord(origin);
    if (Objects.isNull(gameFruitDays)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.GAME_FRUIT_MSG.getTopicId(userId), ResultResponse.failure(
                  CommonErrorCode.NOT_FOUND_RECORD_INFO)));
      return;
    }
    update().set(GameFruitDaysRecord::getNewNum,
            gameFruitDays.getNewNum() + 1)
        .set(GameFruitDaysRecord::getPlayerNum, gameFruitDays.getPlayerNum() + 1)
        .eq(GameFruitDaysRecord::getSysOrigin, origin)
        .eq(GameFruitDaysRecord::getDate, LocalDateUtils.nowToInteger().toString()).execute();
  }

  @Override
  public void addPlatformExpend(Integer expendSum, String origin) {
    GameFruitDaysRecord gameFruitDays = getDaysRecord(origin);
    if (Objects.isNull(gameFruitDays)) {
      messageSender.sendTopic(
          TopicMessage.of(
              "broadcast", ResultResponse.failure(
                  CommonErrorCode.NOT_FOUND_RECORD_INFO)));
      return;
    }
    update().set(GameFruitDaysRecord::getPlatformExpend,
            gameFruitDays.getPlatformExpend().add(ArithmeticUtils.toBigDecimal(expendSum)))
        .eq(GameFruitDaysRecord::getSysOrigin, origin)
        .eq(GameFruitDaysRecord::getDate, LocalDateUtils.nowToInteger().toString()).execute();
  }

  @Override
  public void addLotteryDrawNum(int type, String origin) {
    GameFruitDaysRecord gameFruitDaysRecord = getDaysRecord(origin);
    if (Objects.isNull(gameFruitDaysRecord)) {
      messageSender.sendTopic(
          TopicMessage.of(
              "broadcast", ResultResponse.failure(
                  CommonErrorCode.NOT_FOUND_RECORD_INFO)));
      return;
    }
    update().set(Objects.equals(type, 1), GameFruitDaysRecord::getVegetableNum,
            gameFruitDaysRecord.getVegetableNum() + 1)
        .set(Objects.equals(type, 2), GameFruitDaysRecord::getPizzaNum,
            gameFruitDaysRecord.getPizzaNum() + 1)
        .eq(GameFruitDaysRecord::getSysOrigin, origin)
        .eq(GameFruitDaysRecord::getDate,
            LocalDateUtils.nowToInteger()).execute();

  }

  @Override
  public PageResult<PageGameFruitDaysRecordDTO> pageGameFruitDaysRecord(PageDaysRecordQryCmd cmd) {
    PageResult<PageGameFruitDaysRecordDTO> pageGameFruitDaysRecords = MybatisConvertor.toPageResult(
        baseDAO.pageGameFruitDaysRecord(MybatisConvertor.toPage(cmd.getPageQuery()),
            cmd));
    return pageGameFruitDaysRecords;
  }

  @Override
  public List<GameFruitDaysRecord> listDaysRecord() {
    return query().list();
  }
}
