/*
 *    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.archives.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeContractInfo;
import com.yifu.cloud.plus.v1.yifu.archives.service.TEmployeeContractInfoService;
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.ErrorMessage;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 员工合同
 *
 * @author hgw
 * @date 2022-06-29 20:45:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/temployeecontractinfo")
@Tag(name = "员工合同管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TEmployeeContractInfoController {

	private final TEmployeeContractInfoService tEmployeeContractInfoService;

	/**
	 * 分页查询
	 *
	 * @param page                  分页对象
	 * @param tEmployeeContractInfo 员工合同
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R gettemployeecontractinfoPage(Page<TEmployeeContractInfo> page, TEmployeeContractInfo tEmployeeContractInfo) {
		return new R<>(tEmployeeContractInfoService.getTEmployeeContractInfoPage(page, tEmployeeContractInfo));
	}


	/**
	 * 通过id查询员工合同
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") String id) {
		return R.ok(tEmployeeContractInfoService.getById(id));
	}

	/**
	 * 新增员工合同
	 *
	 * @param tEmployeeContractInfo 员工合同
	 * @return R
	 */
	@Operation(summary = "新增员工合同auditStatus审核状态，前端传参（0：保存为待提交；1：提交为待审核）temployeecontractinfo_add附件传参attaList", description = "新增员工合同：hasPermission('temployeecontractinfo_add')")
	@SysLog("新增员工合同")
	@PostMapping("/saveNewContract")
	@PreAuthorize("@pms.hasPermission('temployeecontractinfo_add')")
	public R<List<ErrorMessage>> saveNewContract(@RequestBody TEmployeeContractInfo tEmployeeContractInfo) {
		return tEmployeeContractInfoService.saveNewContract(tEmployeeContractInfo);
	}

	/**
	 * 修改员工合同
	 *
	 * @param tEmployeeContractInfo 员工合同
	 * @return R
	 */
	@Operation(summary = "编辑员工合同auditStatus审核状态，前端传参（0：保存为待提交；1：提交为待审核）temployeecontractinfo_edit编辑的附件，前端操作，可以不传参attaList", description = "修改员工合同：hasPermission('temployeecontractinfo_edit')")
	@SysLog("修改员工合同")
	@PostMapping("/updateContract")
	@PreAuthorize("@pms.hasPermission('temployeecontractinfo_edit')")
	public R<List<ErrorMessage>> updateContract(@RequestBody TEmployeeContractInfo tEmployeeContractInfo) {
		return tEmployeeContractInfoService.updateContract(tEmployeeContractInfo);
	}

	/**
	 * 通过id删除员工合同
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id假删除员工合同temployeecontractinfo_del", description = "通过id删除员工合同：hasPermission('temployeecontractinfo_del')")
	@SysLog("通过id假删除员工合同")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('temployeecontractinfo_del')")
	public R<String> removeById(@PathVariable String id) {
		YifuUser user = SecurityUtils.getUser();
		if (user == null || Common.isEmpty(user.getId())) {
			return R.failed(CommonConstants.PLEASE_LOG_IN);
		}
		TEmployeeContractInfo contractInfo = tEmployeeContractInfoService.getById(id);
		if (contractInfo == null) {
			return R.failed("未找到记录");
		} else {
			if (!user.getId().equals(contractInfo.getCreateBy())) {
				return R.failed("不是合同创建人，无法删除");
			}
			if (contractInfo.getAuditStatus() != CommonConstants.ZERO_INT
					&& contractInfo.getAuditStatus() != CommonConstants.dingleDigitIntArray[3]) {
				return R.failed("不是待审核或审核不通过，无法删除");
			}
			contractInfo.setDeleteFlag(CommonConstants.ONE_INT);
			tEmployeeContractInfoService.updateById(contractInfo);
			return R.ok();
		}
	}



}
