package com.ym.mec.web.controller;

import com.ym.mec.auth.api.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.dto.CashAccountDetail;
import com.ym.mec.biz.dal.entity.SysUserCashAccountDetail;
import com.ym.mec.biz.service.SysUserCashAccountDetailService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.page.PageInfo;

@RequestMapping("userCashAccountDetail")
@Api(tags = "用户交易明细服务")
@RestController
public class SysUserCashAccountDetailController extends BaseController {
    @Autowired
    private SysUserCashAccountDetailService sysUserCashAccountDetailService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SysConfigDao sysConfigDao;

    @ApiOperation(value = "新增用户交易明细")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('userCashAccountDetail/add')")
    public Object add(SysUserCashAccountDetail sysUserCashAccountDetail) {
        sysUserCashAccountDetailService.insert(sysUserCashAccountDetail);
        return succeed();
    }

    @ApiOperation(value = "分页查询用户交易明细")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('userCashAccountDetail/queryPage')")
    public Object queryPage(CashAccountDetail queryInfo) {
        PageInfo<SysUserCashAccountDetail> sysUserCashAccountDetailPageInfo = sysUserCashAccountDetailService.queryPage(queryInfo);

        int openHideMode = Integer.parseInt(sysConfigDao.findConfigValue("open_hide_mode"));
        if(openHideMode == 1){
            for (SysUserCashAccountDetail row : sysUserCashAccountDetailPageInfo.getRows()) {
                BigDecimal comAmount = row.getComAmount() == null ? BigDecimal.ZERO : row.getComAmount();
                row.setAmount(comAmount.add(comAmount));
            }
        }
        return succeed(sysUserCashAccountDetailPageInfo);
    }

    @ApiOperation(value = "流水明细查询-包含账户余额与课程余额")
    @GetMapping("/queryAccountDetail")
    public Object queryAccountDetail(CashAccountDetail queryInfo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if(user == null || user.getId() != null){
            return failed("请重新登录");
        }
        return succeed(sysUserCashAccountDetailService.queryAccountDetail(queryInfo));
    }

}
