/*
 *    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.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.dapr.util.MenuUtil;
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.TSalaryStandard;
import com.yifu.cloud.plus.v1.yifu.salary.service.TSalaryStandardService;
import com.yifu.cloud.plus.v1.yifu.salary.util.SalaryConstants;
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.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 MenuUtil menuUtil;
	/**
	 * 简单分页查询-申请
	 *
	 * @param page            分页对象
	 * @param tSalaryStandard 标准薪酬工资表
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/pageApply")
	public R<IPage<TSalaryStandard>> getTSalaryStandardPageApply(Page<TSalaryStandard> page, TSalaryStandardSearchVo tSalaryStandard) {
		tSalaryStandard.setDeleteFlag(CommonConstants.ZERO_INT);
		YifuUser user = SecurityUtils.getUser();
		menuUtil.setAuthSql(user, tSalaryStandard);
		return new R<>(tSalaryStandardService.getTSalaryStandardPageApply(page, tSalaryStandard));
	}

	/**
	 * 简单分页查询
	 *
	 * @param page            分页对象
	 * @param tSalaryStandard 标准薪酬工资表
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/page")
	public R<IPage<TSalaryStandard>> getTSalaryStandardPage(Page<TSalaryStandard> page, TSalaryStandardSearchVo tSalaryStandard) {
		YifuUser user = SecurityUtils.getUser();
		tSalaryStandard.setDeleteFlag(CommonConstants.ZERO_INT);
		menuUtil.setAuthSql(user, 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) {
		YifuUser user = SecurityUtils.getUser();
		if (user != null && Common.isNotNull(user.getId())) {
			menuUtil.setAuthSql(user, 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<String> 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<String> salaryStandardAudit(@RequestBody TSalaryStandard tSalaryStandard) {
		if (tSalaryStandard != null && Common.isNotNull(tSalaryStandard.getId())) {
			TSalaryStandard s = tSalaryStandardService.getById(tSalaryStandard.getId());
			if (s == null) {
				return R.failed("未找到工资表");
			} else if (s.getStatus() == SalaryConstants.STATUS[0]
					|| s.getStatus() == SalaryConstants.STATUS[5]
					|| s.getStatus() == SalaryConstants.STATUS[7]) {
				YifuUser user = SecurityUtils.getUser();
				if (user != null) {
					// 添加流程进展明细
					tSalaryStandardService.saveRecordLog(s, user, CommonConstants.TWO_STRING, "提交审核");
					tSalaryStandard.setSubmitTime(new Date());
					tSalaryStandard.setStatus(CommonConstants.ONE_INT);
					tSalaryStandardService.updateById(tSalaryStandard);
					return R.ok("提交成功！");
				} else {
					return R.failed("请登录！");
				}
			} else {
				return R.failed("请刷新后检查薪资表状态！");
			}
		} else {
			return R.failed("请传参！");
		}
	}

	/**
	 * @param id 主键
	 * @Description: 发送至EKP
	 * @Author: hgw
	 * @Date: 2022/8/26 11:40
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R
	 **/
	@Operation(description = "发送至EKP")
	@PostMapping("/doSend")
	public R<String> doSend(@RequestParam String id) {
		return tSalaryStandardService.doSend(id);
	}
}
