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

import java.util.List;

import com.hzy.common.enums.EDelFlag;
import com.hzy.common.utils.RMBUtil;
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.subject.domain.SalarySubject;
import com.hzy.project.business.subject.service.ISalarySubjectService;
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/subject")
public class SalarySubjectController extends BaseController
{
    private String prefix = "business/subject";

    @Autowired
    private ISalarySubjectService salarySubjectService;

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

    /**
     * 查询工资科目列表
     */
    @RequiresPermissions("business:subject:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SalarySubject salarySubject)
    {
        salarySubject.setDelFlag(EDelFlag.NO.getVal());
        startPage();
        List<SalarySubject> list = salarySubjectService.selectSalarySubjectList(salarySubject);
        list.forEach(e->{
            e.setPercent(RMBUtil.fenToYuan(e.getPercent()));
        });
        return getDataTable(list);
    }

    /**
     * 查询工资科目列表
     */
    @RequiresPermissions("business:subject:list")
    @GetMapping("/getlist/{subjectType}")
    @ResponseBody
    public TableDataInfo getlist(@PathVariable("subjectType") String subjectType)
    {
         SalarySubject salarySubject = new SalarySubject();
        salarySubject.setSubjectType(subjectType);
        salarySubject.setDelFlag(EDelFlag.NO.getVal());
        List<SalarySubject> list = salarySubjectService.selectSalarySubjectList(salarySubject);
        list.forEach(e->{
            e.setPercent(RMBUtil.fenToYuan(e.getPercent()));
        });
        return getDataTable(list);
    }

    /**
     * 导出工资科目列表
     */
    @RequiresPermissions("business:subject:export")
    @Log(title = "工资科目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SalarySubject salarySubject)
    {
        salarySubject.setDelFlag(EDelFlag.NO.getVal());
        List<SalarySubject> list = salarySubjectService.selectSalarySubjectList(salarySubject);
        list.forEach(e->{
            e.setPercent(RMBUtil.fenToYuan(e.getPercent()));
        });
        ExcelUtil<SalarySubject> util = new ExcelUtil<SalarySubject>(SalarySubject.class);
        return util.exportExcel(list, "工资科目数据");
    }

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

    /**
     * 新增保存工资科目
     */
    @RequiresPermissions("business:subject:add")
    @Log(title = "工资科目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SalarySubject salarySubject)
    {
        salarySubject.setPercent(RMBUtil.yuanToFen(salarySubject.getPercent()));
        salarySubject.setSort(999L);
        salarySubject.setDelFlag(EDelFlag.NO.getVal());
        return toAjax(salarySubjectService.insertSalarySubject(salarySubject));
    }

    /**
     * 修改工资科目
     */
    @RequiresPermissions("business:subject:edit")
    @GetMapping("/edit/{salarySubjectId}")
    public String edit(@PathVariable("salarySubjectId") Long salarySubjectId, ModelMap mmap)
    {
        SalarySubject salarySubject = salarySubjectService.selectSalarySubjectBySalarySubjectId(salarySubjectId);
        salarySubject.setPercent(RMBUtil.fenToYuan(salarySubject.getPercent()));
        mmap.put("salarySubject", salarySubject);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资科目
     */
    @RequiresPermissions("business:subject:edit")
    @Log(title = "工资科目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SalarySubject salarySubject)
    {
        salarySubject.setPercent(RMBUtil.yuanToFen(salarySubject.getPercent()));
        return toAjax(salarySubjectService.updateSalarySubject(salarySubject));
    }

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

    @RequiresPermissions("master:subject:edit")
    @Log(title = "工资科目拖动排序", businessType = BusinessType.UPDATE)
    @PostMapping("/sorted")
    @ResponseBody
    public AjaxResult saveSorted(SalarySubject salarySubject) {
        return toAjax(salarySubjectService.dragSorted(salarySubject));
    }
}
