/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yifu4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.yifu.cloud.plus.v1.yifu.salary.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.DateUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.log.annotation.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.salary.entity.TApprovalRecord;
import com.yifu.cloud.plus.v1.yifu.salary.entity.TSalaryStandard;
import com.yifu.cloud.plus.v1.yifu.salary.service.TApprovalRecordService;
import com.yifu.cloud.plus.v1.yifu.salary.service.TSalaryStandardService;
import com.yifu.cloud.plus.v1.yifu.salary.vo.TSalaryStandardSearchVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;


/**
 * 标准薪酬工资表
 *
 * @author hgw
 * @date 2022-08-05 11:40:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tsalarystandard")
@Tag(name = "标准薪酬工资表管理")
public class TSalaryStandardController {

	private final TSalaryStandardService tSalaryStandardService;

	private final TApprovalRecordService auditLogService;

	/**
	 * 简单分页查询
	 *
	 * @param page            分页对象
	 * @param tSalaryStandard 标准薪酬工资表
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/page")
	public R<IPage<TSalaryStandard>> getTSalaryStandardPage(Page<TSalaryStandard> page, TSalaryStandardSearchVo tSalaryStandard) {
		return new R<>(tSalaryStandardService.getTSalaryStandardPage(page, tSalaryStandard));
	}

	/**
	 * 通过id查询标准薪酬工资表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询：hasPermission('salary_tsalarystandard_get')")
	@GetMapping("/{id}")
	public R<TSalaryStandard> getById(@PathVariable("id") String id) {
		return R.ok(tSalaryStandardService.getById(id));
	}

	/**
	 * 修改标准薪酬工资表
	 *
	 * @param tSalaryStandard 标准薪酬工资表
	 * @return R
	 */
	@Operation(summary = "修改标准薪酬工资表", description = "修改标准薪酬工资表：hasPermission('salary_tsalarystandard_edit')")
	@SysLog("修改标准薪酬工资表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('salary_tsalarystandard_edit')")
	public R<Boolean> updateById(@RequestBody TSalaryStandard tSalaryStandard) {
		return R.ok(tSalaryStandardService.updateById(tSalaryStandard));
	}

	/**
	 * 通过id删除标准薪酬工资表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除标准薪酬工资表", description = "通过id删除标准薪酬工资表：hasPermission('salary_tsalarystandard_del')")
	@SysLog("通过id删除标准薪酬工资表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('salary_tsalarystandard_del')")
	public R<String> removeById(@PathVariable String id) {
		return tSalaryStandardService.deleteSalaryById(id);
	}

	/**
	 * 标准薪酬工资表 批量导出
	 *
	 * @author fxj
	 * @date 2022-08-05 11:40:14
	 **/
	@Operation(description = "导出标准薪酬工资表 hasPermission('salary_tsalarystandard-export')")
	@PostMapping("/export")
	//@PreAuthorize("@pms.hasPermission('salary_tsalarystandard-export')")
	public void export(HttpServletResponse response, @RequestBody TSalaryStandardSearchVo searchVo) {
		tSalaryStandardService.listExport(response, searchVo);
	}

	/**
	 * 审核页面简单分页查询
	 *
	 * @param page            分页对象
	 * @param tSalaryStandard 标准薪酬工资表
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/auditPage")
	public R<IPage<TSalaryStandard>> getTSalaryStandardAuditPage(Page<TSalaryStandard> page, TSalaryStandardSearchVo tSalaryStandard) {
		return new R<>(tSalaryStandardService.getTSalaryStandardAuditPage(page, tSalaryStandard));
	}

	/**
	 * 审核
	 *
	 * @param auditFlag 是否通过 0 是 1 否
	 * @param AuditRemark 审核意见
	 * @param id
	 * @return
	 */
	@Operation(description = "薪酬审核")
	@PostMapping("/salaryStandardAudit")
	public R salaryStandardAudit(@RequestParam String auditFlag, @RequestParam(required = false) String AuditRemark,
								@RequestParam String id) {
		return tSalaryStandardService.salaryStandardAudit(auditFlag, AuditRemark, id);
	}

	/**
	 * 保存并提交
	 * @return
	 */
	@Operation(description = "保存并提交")
	@PostMapping("/doSubmit")
	public R salaryStandardAudit(@RequestBody TSalaryStandard tSalaryStandard) {
		if (tSalaryStandard != null && Common.isNotNull(tSalaryStandard.getId())) {
			YifuUser user = SecurityUtils.getUser();
			if (user != null) {
				TApprovalRecord tApprovalRecord = new TApprovalRecord();
				tApprovalRecord.setApprovalResult(CommonConstants.TWO_STRING);
				tApprovalRecord.setApprovalOpinion("提交审核");
				tApprovalRecord.setSalaryId(tSalaryStandard.getId());
				tApprovalRecord.setNodeId("提交审核");
				tApprovalRecord.setApprovalMan(user.getId());
				tApprovalRecord.setApprovalManName(user.getNickname());
				tApprovalRecord.setApprovalTime(DateUtil.getCurrentDateTime());
				auditLogService.save(tApprovalRecord);
				tSalaryStandard.setSubmitTime(new Date());
				tSalaryStandardService.updateById(tSalaryStandard);
				return R.ok("提交成功！");
			} else {
				return R.failed("请登录！");
			}
		} else {
			return R.failed("请传参！");
		}
	}
}
