package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.ResultData;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxDateAmountRecord;
import com.iformall.domain.po.WxMerchantBUser;
import com.iformall.domain.vo.AmountRecordVo;
import com.iformall.enums.EnumDateAmtType;
import com.iformall.service.WxDateAmountRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/wxDateAmountRecord")
@Api(description = "首页交易记录相关接口")
public class WxDateAmountRecordController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxDateAmountRecordService wxDateAmountRecordService;


    @ApiOperation("首页查询交易金额记录接口")
    @GetMapping("listOrder")
    public ResultData listOrder() {
        return list(EnumDateAmtType.PAY_RECORD.getCode());
    }

    @ApiOperation("首页查询核销金额记录接口")
    @GetMapping("listVerified")
    public ResultData listVerified() {
        return list(EnumDateAmtType.VERIFY_RECORD.getCode());
    }

    @ApiOperation("首页查询刷卡金额记录接口")
    @GetMapping("listMicropay")
    public ResultData listMicropay() {
        return list(EnumDateAmtType.MICROPAY_RECORD.getCode());
    }

    private ResultData list(int dateAmtType) {

        List<AmountRecordVo> list = new ArrayList<>();

        WxDateAmountRecord temp = new WxDateAmountRecord();
        WxMerchantBUser u = getUser();
        temp.setTenantId(u.getTenantId());
        temp.setMerchantId(u.getMerchantId());
        temp.setType(dateAmtType);
        temp.setSortColumns(BaseEntity.SortField.Date_DESC);

        SimpleDateFormat fmt = new SimpleDateFormat("MM/dd");

        PageInfo<WxDateAmountRecord> page = wxDateAmountRecordService.listAsPage(temp, 1, 7);
        if (page.getSize() > 0) {

            for (WxDateAmountRecord wxDateAmountRecord : page.getList()) {

                AmountRecordVo vo = new AmountRecordVo();
                vo.setAmount(wxDateAmountRecord.getPayPrice());
                vo.setAmountDetail(wxDateAmountRecord);
                vo.setMouthAndDate(fmt.format(wxDateAmountRecord.getDate()));
                list.add(vo);
            }
            Collections.reverse(list);
        }
        return new ResultData(list);
    }
}
