package com.hzy.project.business.salary.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.salary.domain.SalaryVo;
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.salary.domain.Salary;
import com.hzy.project.business.salary.service.ISalaryService;
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/salary")
public class SalaryController extends BaseController
{
    private String prefix = "business/salary";

    @Autowired
    private ISalaryService salaryService;

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

    /**
     * 查询工资列表
     */
    @RequiresPermissions("business:salary:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SalaryVo salary)
    {
        salary.setDelFlag(EDelFlag.NO.getVal());
        startPage();
        List<SalaryVo> list = salaryService.selectSalaryList(salary);
        list.forEach(e->{
            e.setSumAmount(RMBUtil.fenToYuan(e.getSumAmount()));
        });
        return getDataTable(list);
    }

    /**
     * 导出工资列表
     */
    @RequiresPermissions("business:salary:export")
    @Log(title = "工资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SalaryVo salary)
    {
        salary.setDelFlag(EDelFlag.NO.getVal());
        List<SalaryVo> list = salaryService.selectSalaryList(salary);
        list.forEach(e->{
            e.setSumAmount(RMBUtil.fenToYuan(e.getSumAmount()));
        });
        ExcelUtil<SalaryVo> util = new ExcelUtil<SalaryVo>(SalaryVo.class);
        return util.exportExcel(list, "工资数据");
    }

    /**
     * 新增工资
     */
    @RequiresPermissions("business:salary:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工资
     */
    @RequiresPermissions("business:salary:add")
    @Log(title = "工资", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Salary salary)
    {
        salary.setSumAmount(RMBUtil.yuanToFen(salary.getSumAmount()));
        salary.setDelFlag(EDelFlag.NO.getVal());
        salary.setCreateBy(ShiroUtils.getLoginName());
        salary.getSalaryDetailList().forEach(e->{
            e.setPercent(RMBUtil.yuanToFen(e.getPercent()));
            e.setAmount(RMBUtil.yuanToFen(e.getAmount()));
            e.setCreateBy(ShiroUtils.getLoginName());
            e.setDelFlag(EDelFlag.NO.getVal());
        });
        return toAjax(salaryService.insertSalary(salary));
    }

    /**
     * 修改工资
     */
    @RequiresPermissions("business:salary:edit")
    @GetMapping("/edit/{salaryId}")
    public String edit(@PathVariable("salaryId") Long salaryId, ModelMap mmap)
    {
        Salary salary = salaryService.selectSalaryBySalaryId(salaryId);
        salary.setSumAmount(RMBUtil.fenToYuan(salary.getSumAmount()));
        salary.getSalaryDetailList().forEach(e->{
            e.setPercent(RMBUtil.fenToYuan(e.getPercent()));
            e.setAmount(RMBUtil.fenToYuan(e.getAmount()));
        });
        mmap.put("salary", salary);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资
     */
    @RequiresPermissions("business:salary:edit")
    @Log(title = "工资", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Salary salary)
    {
        salary.setSumAmount(RMBUtil.yuanToFen(salary.getSumAmount()));
        salary.setUpdateBy(ShiroUtils.getLoginName());
        salary.getSalaryDetailList().forEach(e->{
            e.setPercent(RMBUtil.yuanToFen(e.getPercent()));
            e.setAmount(RMBUtil.yuanToFen(e.getAmount()));
            e.setUpdateBy(ShiroUtils.getLoginName());
        });
        return toAjax(salaryService.updateSalary(salary));
    }

    /**
     * 删除工资
     */
    @RequiresPermissions("business:salary:remove")
    @Log(title = "工资", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(salaryService.deleteSalaryBySalaryIds(ids));
    }
}
