package com.yonge.cooleshow.admin.controller;

import com.yonge.cooleshow.biz.dal.dto.excel.PlatformCashAccountRecordExport;
import com.yonge.cooleshow.biz.dal.dto.excel.UserAccountRecordExport;
import com.yonge.cooleshow.biz.dal.dto.search.UserAccountRecordSearch;
import com.yonge.cooleshow.biz.dal.enums.InOrOutEnum;
import com.yonge.cooleshow.biz.dal.vo.PlatformCashAccountPageVo;
import com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo;
import com.yonge.toolset.utils.easyexcel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yonge.cooleshow.biz.dal.dto.search.PlatformCashAccountRecordSearch;
import com.yonge.cooleshow.biz.dal.entity.PlatformCashAccountRecord;
import com.yonge.cooleshow.biz.dal.service.PlatformCashAccountRecordService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/platformCashAccountRecord")
@Api(value = "平台账户记录", tags = "平台账户记录")
public class PlatformCashAccountRecordController extends BaseController {

    @Autowired
    private PlatformCashAccountRecordService platformCashAccountRecordService;

	@ApiOperation(value = "平台账户记录-列表", httpMethod = "POST", consumes = "application/json", produces = "application/json")
	@PostMapping(value = "/list", consumes = "application/json", produces = "application/json")
	@PreAuthorize("@pcs.hasPermissions('platformCashAccountRecord/list')")
	public HttpResponseResult<PlatformCashAccountPageVo> list(@RequestBody PlatformCashAccountRecordSearch query) {
		PlatformCashAccountPageVo model = new PlatformCashAccountPageVo();
		PageInfo<PlatformCashAccountRecord> pageInfo = PageUtil.pageInfo(platformCashAccountRecordService.queryPage(PageUtil.getPage(query),query));
		model.setPageInfo(pageInfo);
		model.setSummary(platformCashAccountRecordService.querySummary(query));
		return succeed(model);
	}

	/**
	 * 查询导出
	 */
	@PostMapping("/export")
	@ApiOperation(value = "查询导出", notes = "传入orderSearch")
	@PreAuthorize("@pcs.hasPermissions('platformCashAccountRecord/export')")
	public void export(@RequestBody PlatformCashAccountRecordSearch query) {
		List<PlatformCashAccountRecord> platformCashAccountRecords = platformCashAccountRecordService.queryAll(query);

		List<PlatformCashAccountRecordExport> list = new ArrayList<>();
		platformCashAccountRecords.forEach(o -> {
			PlatformCashAccountRecordExport export = new PlatformCashAccountRecordExport();
			BeanUtils.copyProperties(o, export);
			if (null == export.getTransAmount()) {
				export.setTransAmount(BigDecimal.ZERO);
			}
			list.add(export);
		});
		ExcelUtils.exportExcel(list, "平台收支列表数据" + System.currentTimeMillis(),
				"平台收支列表数据");
	}

}