/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.qs.mp.web.controller.api.channel;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.mp.channel.domain.ChannelMoneyLog;
import com.qs.mp.channel.domain.ChannelWithdraw;
import com.qs.mp.channel.domain.vo.ChannelMoneyLogVO;
import com.qs.mp.channel.service.IChannelMoneyLogService;
import com.qs.mp.channel.service.IChannelWithdrawService;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.common.enums.ChannelMoneyEnum;
import com.qs.mp.utils.SecurityUtils;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import java.util.List;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1/mp/channel")
@Api(tags = "账户明细接口")
@AllArgsConstructor
public class ChannelMoneyController extends BaseApiController {

  @Autowired
  private IChannelMoneyLogService channelMoneyLogService;

  @Autowired
  private IChannelWithdrawService channelWithdrawService;

  @Autowired
  private MapperFacade mapperFacade;

  @RequestMapping(value = "/money/log", method = RequestMethod.POST)
  @ResponseBody
  public TableDataInfo listMoneyLog(@RequestBody ChannelMoneyLog channelMoneyLog) {
    Long channelId = SecurityUtils.getLoginUser().getChannelId();
    QueryWrapper<ChannelMoneyLog> queryWrapper = new QueryWrapper();
    queryWrapper.lambda().eq(ChannelMoneyLog::getChannelId, channelId);
    queryWrapper.lambda().orderByDesc(ChannelMoneyLog::getCreatedTime);
    startPage();
    List<ChannelMoneyLog> list = channelMoneyLogService.list(queryWrapper);
    List<ChannelMoneyLogVO> channelMoneyLogVOS = mapperFacade.mapAsList(list, ChannelMoneyLogVO.class);
    for (ChannelMoneyLogVO logVO : channelMoneyLogVOS) {
      if (logVO.getType() == ChannelMoneyEnum.WITHDRAW) {
        ChannelWithdraw channelWithdraw = channelWithdrawService.getById(logVO.getRefId());
        if (null != channelWithdraw) {
          logVO.setWithdrawStatus(channelWithdraw.getStatus());
        }
      }
    }
    TableDataInfo resp = getDataTable(list);
    resp.setRows(channelMoneyLogVOS);
    return resp;
  }
}
