/*
 *    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.exception.ErrorCodes;
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.dapr.util.MenuUtil;
import com.yifu.cloud.plus.v1.yifu.common.log.annotation.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.mybatis.base.BaseEntity;
import com.yifu.cloud.plus.v1.yifu.common.security.annotation.Inner;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.salary.entity.TSalaryZero;
import com.yifu.cloud.plus.v1.yifu.salary.service.TSalaryZeroService;
import com.yifu.cloud.plus.v1.yifu.salary.vo.TSalaryZeroSearchVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 0申报
 *
 * @author hgw
 * @date 2026-03-18 14:47:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tsalaryzero")
@Tag(name = "0申报管理")
public class TSalaryZeroController {

	private final TSalaryZeroService tSalaryZeroService;

	private final MenuUtil menuUtil;

	/**
	 * 定时任务-每月1日1点，生成0申报
	 */
	@Operation(description = "每月1日1点生成0申报")
	@PostMapping("/inner/createZeroByMonth")
	@Inner
	public void createZeroByMonth() {
		tSalaryZeroService.createZeroByMonth();
	}

	/**
	 * 定时任务-每日23点50分，生成增量0申报
	 */
	@Operation(description = "每日23点生成增量0申报")
	@PostMapping("/inner/createZeroByDay")
	@Inner
	public void createZeroByDay() {
		tSalaryZeroService.createZeroByDay();
	}

	/**
	 * 每月1日1点生成0申报
	 */
	@Operation(description = "测试-每月1日1点生成0申报")
	@GetMapping("/testCreateZeroByMonth")
	public void testCreateZeroByMonth() {
		tSalaryZeroService.createZeroByMonth();
	}

	/**
	 * 每日23点生成增量0申报
	 */
	@Operation(description = "测试-每日23点生成增量0申报")
	@GetMapping("/testCreateZeroByDay")
	public void testCreateZeroByDay() {
		tSalaryZeroService.createZeroByDay();
	}

	/**
	 * 简单分页查询
	 *
	 * @param page     分页对象
	 * @param searchVo 0申报
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/page")
	public R<IPage<TSalaryZero>> getTSalaryZeroPage(Page<TSalaryZero> page, TSalaryZeroSearchVo searchVo) {
		this.setAuth(searchVo);
		return new R<>(tSalaryZeroService.getTSalaryZeroPage(page, searchVo));
	}

	/**
	 * 设置权限
	 */
	private void setAuth(TSalaryZeroSearchVo tSalaryZero) {
		YifuUser user = SecurityUtils.getUser();
		BaseEntity baseEntity = new BaseEntity();
		baseEntity.setMId(tSalaryZero.getMId());
		baseEntity.setMenuInfo(tSalaryZero.getMenuInfo());
		menuUtil.setAuthSql(user, baseEntity);
		tSalaryZero.setAuthSql(baseEntity.getAuthSql());
	}

	/**
	 * 通过id查询0申报
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询：hasPermission('salary_tsalaryzero_get')")
	@GetMapping("/{id}")
	public R<TSalaryZero> getById(@PathVariable("id") String id) {
		return R.ok(tSalaryZeroService.getTSalaryZeroById(id));
	}

	/**
	 * 通过id删除0申报
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除0申报", description = "通过id删除0申报")
	@SysLog("通过id删除0申报")
	@GetMapping("/deleteTSalaryZeroById")
	public R<Boolean> deleteTSalaryZeroById(@RequestParam String id) {
		boolean a = tSalaryZeroService.deleteTSalaryZeroById(id);
		if (a) {
			return R.ok();
		} else {
			return R.failed();
		}
	}

	/**
	 * 通过id删除0申报
	 *
	 * @param idList id
	 * @return R
	 */
	@Operation(summary = "通过idList批量删除0申报", description = "通过idList批量删除0申报")
	@SysLog("通过idList批量删除0申报")
	@PostMapping("/deleteZeroByIdList")
	public R<Boolean> deleteZeroByIdList(@RequestBody List<String> idList) {
		if (Common.isEmpty(idList)) {
			return R.failed(ErrorCodes.PARAM_NOT_EMPTY);
		} else {
			tSalaryZeroService.deleteTSalaryZeroByIdList(idList);
		}
		return R.ok();
	}

	/**
	 * 0申报 批量导入( hasPermission('salary_tsalaryzero-batch-import'))
	 *
	 * @author hgw
	 * @date 2026-03-18 14:47:00
	 **/
	@SneakyThrows
	@Operation(description = "批量导入0申报")
	@SysLog("批量导入0申报")
	@PostMapping("/importSalaryZero")
	public R<List<ErrorMessage>> importSalaryZero(@RequestBody MultipartFile file, @RequestParam String settleMonth, @RequestParam String invoiceTitle) {
		return tSalaryZeroService.importSalaryZero(file.getInputStream(), settleMonth, invoiceTitle);
	}

	/**
	 * 0申报 批量导出
	 *
	 * @author hgw
	 * @date 2026-03-18 14:47:00
	 **/
	@Operation(description = "导出0申报 hasPermission('salary_tsalaryzero-export')")
	@PostMapping("/exportZero")
	public void exportZero(HttpServletResponse response, @RequestBody TSalaryZeroSearchVo searchVo) {
		this.setAuth(searchVo);
		tSalaryZeroService.listExport(response, searchVo);
	}
}
