package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.excel.UserAccountRecordExport;
import com.yonge.cooleshow.biz.dal.dto.excel.UserOrderExport;
import com.yonge.cooleshow.biz.dal.dto.req.TotalReq;
import com.yonge.cooleshow.biz.dal.dto.search.OrderSearch;
import com.yonge.cooleshow.biz.dal.enums.InOrOutEnum;
import com.yonge.cooleshow.biz.dal.vo.UserAccountVo;
import com.yonge.cooleshow.biz.dal.vo.UserOrderVo;
import com.yonge.cooleshow.biz.dal.vo.res.AccountTotal;
import com.yonge.toolset.mybatis.support.PageUtil;
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.utils.easyexcel.ExcelUtils;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo;
import com.yonge.cooleshow.biz.dal.dto.search.UserAccountRecordSearch;
import com.yonge.cooleshow.biz.dal.service.UserAccountRecordService;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/userAccountRecord")
@Api(value = "用户账户流水表", tags = "用户账户流水表")
public class UserAccountRecordController extends BaseController {

    @Autowired
    private UserAccountRecordService userAccountRecordService;

    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('userAccountRecord/detail')")
    public HttpResponseResult<UserAccountRecordVo> detail(@PathVariable("id") Long id) {
        return succeed(userAccountRecordService.detail(id));
    }

    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入userAccountRecordSearch")
    @PreAuthorize("@pcs.hasPermissions('userAccountRecord/page')")
    public HttpResponseResult<PageInfo<UserAccountRecordVo>> page(@RequestBody UserAccountRecordSearch query) {
        IPage<UserAccountRecordVo> pages = userAccountRecordService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    /**
     * 查询导出
     */
    @PostMapping("/export")
    @ApiOperation(value = "查询导出", notes = "传入orderSearch")
    @PreAuthorize("@pcs.hasPermissions('userAccountRecord/export')")
    public void export(@RequestBody UserAccountRecordSearch query) {
        query.setInOrOut(InOrOutEnum.IN);
        List<UserAccountRecordVo> userAccountRecordVos = userAccountRecordService.selectAll(query);

        List<UserAccountRecordExport> list = new ArrayList<>();
        userAccountRecordVos.forEach(o -> {
            UserAccountRecordExport export = new UserAccountRecordExport();
            BeanUtils.copyProperties(o, export);
            if (null == export.getTransAmount()) {
                export.setTransAmount(BigDecimal.ZERO);
            }
            list.add(export);
        });
        ExcelUtils.exportExcel(list, "老师收入列表数据" + System.currentTimeMillis(),
                "老师收入列表数据");
    }

    @ApiOperation(value = "收入数据统计")
    @PostMapping("/accountTotal")
    @PreAuthorize("@pcs.hasPermissions('userAccountRecord/accountTotal')")
    public HttpResponseResult<UserAccountVo> accountTotal(@RequestBody UserAccountRecordSearch query) {
        query.setInOrOut(InOrOutEnum.IN);
        return userAccountRecordService.accountTotal(query);
    }


}