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

import java.util.List;

import com.hzy.common.enums.EDelFlag;
import com.hzy.common.utils.RMBUtil;
import com.hzy.common.utils.security.ShiroUtils;
import com.hzy.project.business.income.domain.IncomeVo;
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.income.domain.Income;
import com.hzy.project.business.income.service.IIncomeService;
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-04
 */
@Controller
@RequestMapping("/business/income")
public class IncomeController extends BaseController
{
    private String prefix = "business/income";

    @Autowired
    private IIncomeService incomeService;

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

    /**
     * 查询收入列表
     */
    @RequiresPermissions("business:income:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IncomeVo income)
    {
        income.setDelFlag(EDelFlag.NO.getVal());
        startPage();
        List<IncomeVo> list = incomeService.selectIncomeVoList(income);
        list.forEach(e->{
            e.setGrossAmount(RMBUtil.fenToYuan(e.getGrossAmount()));
            e.setNetAmount(RMBUtil.fenToYuan(e.getNetAmount()));
            e.setTaxAmount(RMBUtil.fenToYuan(e.getTaxAmount()));
            e.setRate(RMBUtil.fenToYuan(e.getRate()));
        });
        return getDataTable(list);
    }

    /**
     * 导出收入列表
     */
    @RequiresPermissions("business:income:export")
    @Log(title = "收入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IncomeVo income)
    {
        income.setDelFlag(EDelFlag.NO.getVal());
        List<IncomeVo> list = incomeService.selectIncomeVoList(income);
        list.forEach(e->{
            e.setGrossAmount(RMBUtil.fenToYuan(e.getGrossAmount()));
            e.setNetAmount(RMBUtil.fenToYuan(e.getNetAmount()));
            e.setTaxAmount(RMBUtil.fenToYuan(e.getTaxAmount()));
            e.setRate(RMBUtil.fenToYuan(e.getRate()));
        });
        ExcelUtil<IncomeVo> util = new ExcelUtil<IncomeVo>(IncomeVo.class);
        return util.exportExcel(list, "收入数据");
    }

    /**
     * 新增收入
     */
    @RequiresPermissions("business:income:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存收入
     */
    @RequiresPermissions("business:income:add")
    @Log(title = "收入", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Income income)
    {
        income.setGrossAmount(RMBUtil.yuanToFen(income.getGrossAmount()));
        income.setNetAmount(RMBUtil.yuanToFen(income.getNetAmount()));
        income.setTaxAmount(RMBUtil.yuanToFen(income.getTaxAmount()));
        income.setRate(RMBUtil.yuanToFen(income.getRate()));
        income.setDelFlag(EDelFlag.NO.getVal());
        income.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(incomeService.insertIncome(income));
    }

    /**
     * 修改收入
     */
    @RequiresPermissions("business:income:edit")
    @GetMapping("/edit/{incomeId}")
    public String edit(@PathVariable("incomeId") Long incomeId, ModelMap mmap)
    {
        Income income = incomeService.selectIncomeByIncomeId(incomeId);

        income.setGrossAmount(RMBUtil.fenToYuan(income.getGrossAmount()));
        income.setNetAmount(RMBUtil.fenToYuan(income.getNetAmount()));
        income.setTaxAmount(RMBUtil.fenToYuan(income.getTaxAmount()));
        income.setRate(RMBUtil.fenToYuan(income.getRate()));

        mmap.put("income", income);
        return prefix + "/edit";
    }

    /**
     * 修改保存收入
     */
    @RequiresPermissions("business:income:edit")
    @Log(title = "收入", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Income income)
    {
        income.setGrossAmount(RMBUtil.yuanToFen(income.getGrossAmount()));
        income.setNetAmount(RMBUtil.yuanToFen(income.getNetAmount()));
        income.setTaxAmount(RMBUtil.yuanToFen(income.getTaxAmount()));
        income.setRate(RMBUtil.yuanToFen(income.getRate()));
        income.setUpdateBy(ShiroUtils.getLoginName());

        return toAjax(incomeService.updateIncome(income));
    }

    /**
     * 删除收入
     */
    @RequiresPermissions("business:income:remove")
    @Log(title = "收入", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(incomeService.deleteIncomeByIncomeIds(ids));
    }
}
