package com.ruoyi.oa.controller;

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

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.oa.domain.vo.EmployeeSalaryRecordVo;
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.oa.domain.vo.OaEmployeeTemplateBindingVo;
import com.ruoyi.oa.domain.bo.OaEmployeeTemplateBindingBo;
import com.ruoyi.oa.service.IOaEmployeeTemplateBindingService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工模板绑定及月度发放记录
 *
 * @author ruoyi
 * @date 2025-06-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/employeeTemplateBinding")
public class OaEmployeeTemplateBindingController extends BaseController {

    private final IOaEmployeeTemplateBindingService iOaEmployeeTemplateBindingService;

    /**
     * 查询员工模板绑定及月度发放记录列表
     */

    @GetMapping("/list")
    public TableDataInfo<OaEmployeeTemplateBindingVo> list(OaEmployeeTemplateBindingBo bo, PageQuery pageQuery) {
        return iOaEmployeeTemplateBindingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出员工模板绑定及月度发放记录列表
     */

    @Log(title = "员工模板绑定及月度发放记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaEmployeeTemplateBindingBo bo, HttpServletResponse response) {
        List<OaEmployeeTemplateBindingVo> list = iOaEmployeeTemplateBindingService.queryList(bo);
        ExcelUtil.exportExcel(list, "员工模板绑定及月度发放记录", OaEmployeeTemplateBindingVo.class, response);
    }

    /**
     * 获取员工模板绑定及月度发放记录详细信息
     *
     * @param bindingId 主键
     */

    @GetMapping("/{bindingId}")
    public R<OaEmployeeTemplateBindingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long bindingId) {
        return R.ok(iOaEmployeeTemplateBindingService.queryById(bindingId));
    }

    /**
     * 新增员工模板绑定及月度发放记录
     */

    @Log(title = "员工模板绑定及月度发放记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaEmployeeTemplateBindingBo bo) {
        return toAjax(iOaEmployeeTemplateBindingService.insertByBo(bo));
    }

    /**
     * 修改员工模板绑定及月度发放记录
     */

    @Log(title = "员工模板绑定及月度发放记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaEmployeeTemplateBindingBo bo) {
        return toAjax(iOaEmployeeTemplateBindingService.updateByBo(bo));
    }

    /**
     * 删除员工模板绑定及月度发放记录
     *
     * @param bindingIds 主键串
     */

    @Log(title = "员工模板绑定及月度发放记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bindingIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] bindingIds) {
        return toAjax(iOaEmployeeTemplateBindingService.deleteWithValidByIds(Arrays.asList(bindingIds), true));
    }

    // 6. 薪资批量计算/一键复制上月
    @PostMapping("/calculate")
    public R<Void> calculateSalary(@RequestBody OaEmployeeTemplateBindingBo bo) {
        if (CollectionUtils.isEmpty(bo.getEmployeeIds())) {
            return R.fail("请选择员工");
        }
        if (bo.getPayYear() == null || bo.getPayMonth() == null) {
            return R.fail("请选择发薪年月");
        }
        if (bo.getDefaultInsuranceTemplateId() == null || bo.getDefaultSalaryTemplateId() == null) {
            return R.fail("请选择默认模板");
        }
        return toAjax(iOaEmployeeTemplateBindingService.calculateSalary(bo.getEmployeeIds(), bo.getPayYear(), bo.getPayMonth(), bo.getDefaultInsuranceTemplateId(), bo.getDefaultSalaryTemplateId()));
    }
    // 7. 批量确认发放
    @PostMapping("/finalize")
    public R<Void> finalizeSalary(@RequestBody List<String> bindingIds ) {
        return toAjax(iOaEmployeeTemplateBindingService.finalizeSalary(bindingIds));
    }
    // 8. 查询单员工历史发放记录
    @GetMapping("/history/{employeeId}")
    public R<List<EmployeeSalaryRecordVo>> history(@PathVariable Long employeeId) {
        return R.ok(iOaEmployeeTemplateBindingService.querySalaryHistory(employeeId));
    }

}
