package com.hzy.project.business.balance.controller;

import java.util.List;

import com.hzy.common.enums.EDelFlag;
import com.hzy.common.utils.DateUtils;
import com.hzy.common.utils.RMBUtil;
import com.hzy.common.utils.security.ShiroUtils;
import com.hzy.project.business.balance.domain.UserBalanceVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hzy.framework.aspectj.lang.annotation.Log;
import com.hzy.framework.aspectj.lang.enums.BusinessType;
import com.hzy.project.business.balance.domain.UserBalance;
import com.hzy.project.business.balance.service.IUserBalanceService;
import com.hzy.framework.web.controller.BaseController;
import com.hzy.framework.web.domain.AjaxResult;
import com.hzy.common.utils.poi.ExcelUtil;
import com.hzy.framework.web.page.TableDataInfo;

/**
 * 用户结余Controller
 * 
 * @author ruoyi
 * @date 2026-01-02
 */
@Controller
@RequestMapping("/business/balance")
public class UserBalanceController extends BaseController
{
    private String prefix = "business/balance";

    @Autowired
    private IUserBalanceService userBalanceService;

    @RequiresPermissions("business:balance:view")
    @GetMapping()
    public String balance()
    {
        return prefix + "/balance";
    }

    /**
     * 查询用户结余列表
     */
    @RequiresPermissions("business:balance:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserBalance userBalance)
    {
        startPage();
        userBalance.setDelFlag(EDelFlag.NO.getVal());
        List<UserBalanceVo> list = userBalanceService.selectUserBalanceVoList(userBalance);
        list.forEach(e->{
            e.setOpeningBalance(RMBUtil.fenToYuan(e.getOpeningBalance()));
            e.setIncome(RMBUtil.fenToYuan(e.getIncome()));
            e.setPayment(RMBUtil.fenToYuan(e.getPayment()));
            e.setClosingBalance(RMBUtil.fenToYuan(e.getClosingBalance()));
        });
        return getDataTable(list);
    }

    /**
     * 导出用户结余列表
     */
    @RequiresPermissions("business:balance:export")
    @Log(title = "用户结余", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserBalance userBalance)
    {
        userBalance.setDelFlag(EDelFlag.NO.getVal());
        List<UserBalanceVo> list = userBalanceService.selectUserBalanceVoList(userBalance);
        list.forEach(e->{
            e.setOpeningBalance(RMBUtil.fenToYuan(e.getOpeningBalance()));
            e.setIncome(RMBUtil.fenToYuan(e.getIncome()));
            e.setPayment(RMBUtil.fenToYuan(e.getPayment()));
            e.setClosingBalance(RMBUtil.fenToYuan(e.getClosingBalance()));
        });
        ExcelUtil<UserBalanceVo> util = new ExcelUtil<UserBalanceVo>(UserBalanceVo.class);
        return util.exportExcel(list, "用户结余数据");
    }

    /**
     * 新增用户结余
     */
    @RequiresPermissions("business:balance:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户结余
     */
    @RequiresPermissions("business:balance:add")
    @Log(title = "用户结余", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserBalance userBalance)
    {
        return toAjax(userBalanceService.insertUserBalance(userBalance));
    }

    /**
     * 修改用户结余
     */
    @RequiresPermissions("business:balance:edit")
    @GetMapping("/edit/{balanceId}")
    public String edit(@PathVariable("balanceId") Long balanceId, ModelMap mmap)
    {
        UserBalance userBalance = userBalanceService.selectUserBalanceByBalanceId(balanceId);
        userBalance.setOpeningBalance(RMBUtil.fenToYuan(userBalance.getOpeningBalance()));
        userBalance.setIncome(RMBUtil.fenToYuan(userBalance.getIncome()));
        userBalance.setPayment(RMBUtil.fenToYuan(userBalance.getPayment()));
        userBalance.setClosingBalance(RMBUtil.fenToYuan(userBalance.getClosingBalance()));
        mmap.put("userBalance", userBalance);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户结余
     */
    @RequiresPermissions("business:balance:edit")
    @Log(title = "用户结余", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserBalance userBalance)
    {
        userBalance.setOpeningBalance(RMBUtil.yuanToFen(userBalance.getOpeningBalance()));
        userBalance.setIncome(RMBUtil.yuanToFen(userBalance.getIncome()));
        userBalance.setPayment(RMBUtil.yuanToFen(userBalance.getPayment()));
        userBalance.setClosingBalance(RMBUtil.yuanToFen(userBalance.getClosingBalance()));

        return toAjax(userBalanceService.updateUserBalance(userBalance));
    }

    /**
     * 删除用户结余
     */
    @RequiresPermissions("business:balance:remove")
    @Log(title = "用户结余", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userBalanceService.deleteUserBalanceByBalanceIds(ids));
    }

    /**
     * 修改保存用户结余
     */
    @RequiresPermissions("business:balance:edit")
    @Log(title = "用户结余", businessType = BusinessType.UPDATE)
    @PostMapping("/adjustOpening")
    @ResponseBody
    public AjaxResult adjustOpening(UserBalance userBalance)
    {
        UserBalance userBalance1 = userBalanceService.selectUserBalanceByBalanceId(userBalance.getBalanceId());

        userBalance1.setOpeningBalance(RMBUtil.yuanToFen(userBalance.getOpeningBalance()));
        userBalance1.setClosingBalance(userBalance1.getOpeningBalance().add(userBalance1.getIncome().subtract(userBalance1.getPayment())));
        userBalance1.setUpdateBy(ShiroUtils.getLoginName());

        return toAjax(userBalanceService.updateUserBalance(userBalance1));
    }

}
