package com.bonus.canteen.core.account.v4.app.service.impl;

import cn.hutool.json.JSONUtil;
import com.bonus.canteen.core.account.v3.api.vo.AccInfoDetailVO;
import com.bonus.canteen.core.account.v3.constants.AccTradeTypeEnum;
import com.bonus.canteen.core.account.v3.custom.AccForAppCustomBusiness;
import com.bonus.canteen.core.account.v3.model.AccTradeWalletDetail;
import com.bonus.canteen.core.account.v3.service.AccInfoService;
import com.bonus.canteen.core.account.v3.service.AccTradeService;
import com.bonus.canteen.core.account.v3.service.AccWalletInfoService;
import com.bonus.canteen.core.account.v3.web.dto.AccInfoDetailDTO;
import com.bonus.canteen.core.account.v4.app.dto.AppAccInfoV4DTO;
import com.bonus.canteen.core.account.v4.app.dto.AppTradeV4PageDTO;
import com.bonus.canteen.core.account.v4.app.vo.AppAccInfoV4VO;
import com.bonus.canteen.core.account.v4.app.vo.AppSubsidyTradeVO;
import com.bonus.canteen.core.account.v4.app.vo.AppWalletBalanceVO;
import com.bonus.canteen.core.common.page.PageVO;
import com.github.pagehelper.page.PageMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppAccV4Service {
   private static final Logger log = LoggerFactory.getLogger(AppAccV4Service.class);
   @Resource
   @Lazy
   private AccInfoService accInfoService;
   @Resource
   @Lazy
   private AccForAppCustomBusiness accForAppCustomBusiness;
   @Resource
   @Lazy
   private AccWalletInfoService accWalletInfoService;
   @Autowired
   @Lazy
   private AccTradeService accTradeService;

   public AppWalletBalanceVO queryWalletBalance(AppAccInfoV4DTO appAccInfoV4DTO) {
      log.info("[app]v4查询账户余额入参={}", JSONUtil.toJsonStr(appAccInfoV4DTO));
      AccInfoDetailVO accInfoDetailVO = this.accInfoService.getAccInfoDetail(AccInfoDetailDTO.builder().custId(appAccInfoV4DTO.getCustId()).ifQueryCardInfoBoolean(Boolean.FALSE).build());
      AppWalletBalanceVO walletBalanceVO = AppWalletBalanceVO.of(accInfoDetailVO);
      log.info("[app]v4查询账户余额入参={}", JSONUtil.toJsonStr(walletBalanceVO));
      return walletBalanceVO;
   }

   public AppAccInfoV4VO queryAccInfoForApp(AppAccInfoV4DTO appAccInfoV4DTO) {
      log.info("[app]v4查询账户信息入参={}", JSONUtil.toJsonStr(appAccInfoV4DTO));
      AccInfoDetailVO accInfoDetailVO = this.accInfoService.getAccInfoDetail(AccInfoDetailDTO.builder().custId(appAccInfoV4DTO.getCustId()).ifQueryCardInfoBoolean(Boolean.FALSE).build());
      AppAccInfoV4VO appAccInfoV4VO = AppAccInfoV4VO.of(accInfoDetailVO);
      appAccInfoV4VO.setSubsidyExpirationToast(this.calculateSubsidyExpirationAmount(appAccInfoV4VO.getCustId()));
      this.accForAppCustomBusiness.didGetAccInfoDetailV4(appAccInfoV4DTO, appAccInfoV4VO);
      log.info("[app]v4查询账户信息结果vo={}", JSONUtil.toJsonStr(appAccInfoV4VO));
      return appAccInfoV4VO;
   }

   protected String calculateSubsidyExpirationAmount(Long custId) {
      BigDecimal amountFen = this.accWalletInfoService.calculateSubsidyExpirationAmount(custId);
      if (amountFen.compareTo(BigDecimal.ZERO) > 0) {
         BigDecimal amountYuan = amountFen.divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
         return "本月￥" + String.valueOf(amountYuan) + "补贴即将过期";
      } else {
         return null;
      }
   }


   public PageVO<AppSubsidyTradeVO> queryAccSubsidyTradeForApp(AppTradeV4PageDTO content) {
      PageMethod.startPage(content);
      List<AccTradeWalletDetail> list = this.accTradeService.listAccSubsidyTrade(content.getCustId(), AccTradeTypeEnum.appSubTradeTypeList());
      PageVO<AccTradeWalletDetail> listPage = PageVO.of(list);
      List resultList = (List)list.stream().map(AppSubsidyTradeVO::of).collect(Collectors.toList());
      return (new PageVO(listPage.getCurrent(), listPage.getSize(), listPage.getTotal())).setRecords(resultList);
   }
}
