package com.ruoyi.pay.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.pay.domain.PaySalaryReport;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pay.domain.vo.PaySalaryReportVo;
import com.ruoyi.pay.domain.bo.PaySalaryReportBo;
import com.ruoyi.pay.service.IPaySalaryReportService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工资明细总
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/salaryReport")
public class PaySalaryReportController extends BaseController {

    private final IPaySalaryReportService iPaySalaryReportService;

    /**
     * 查询工资明细总列表
     */
    @SaCheckPermission("pay:salaryReport:list")
    @GetMapping("/list")
    public TableDataInfo<PaySalaryReportVo> list(PaySalaryReportBo bo, PageQuery pageQuery) {
        //默认查询最近一个月的工资
        if(bo.getYear()==null&&bo.getMonth()==null&&bo.getDeptId()==null&&bo.getEmployeeId()==null){
            return   iPaySalaryReportService.queryLastMonthReport(bo.getTenantId(), pageQuery);
        }
        return iPaySalaryReportService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出工资明细总列表
     */
    @SaCheckPermission("pay:salaryReport:export")
    @Log(title = "工资明细总", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PaySalaryReportBo bo, HttpServletResponse response) {
        List<PaySalaryReportVo> list = iPaySalaryReportService.queryList(bo);
        ExcelUtil.exportExcel(list, "工资明细总", PaySalaryReportVo.class, response);
    }

    /**
     * 获取工资明细总详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pay:salaryReport:query")
    @GetMapping("/{id}")
    public R<PaySalaryReportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iPaySalaryReportService.queryById(id));
    }

    /**
     * 新增工资明细总
     */
    @SaCheckPermission("pay:salaryReport:add")
    @Log(title = "工资明细总", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PaySalaryReportBo bo) {
        return toAjax(iPaySalaryReportService.insertByBo(bo));
    }

    /**
     * 修改工资明细总
     */
    @SaCheckPermission("pay:salaryReport:edit")
    @Log(title = "工资明细总", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PaySalaryReportBo bo) {
        return toAjax(iPaySalaryReportService.updateByBo(bo));
    }
    /**
     * 修改工资普通员工是否可查询
     */
    @SaCheckPermission("pay:salaryReport:edit")
    @Log(title = "工资明细总", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/onoff")
    public R<Void> onOff( @RequestBody PaySalaryReport entity) {
        return toAjax(iPaySalaryReportService.updateByBo2(entity));
    }

    /**
     * 删除工资明细总
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pay:salaryReport:remove")
    @Log(title = "工资明细总", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPaySalaryReportService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
