/*
 *    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.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TFascCompany;
import com.yifu.cloud.plus.v1.yifu.archives.service.TFascCompanyService;
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.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.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 法大大专业版公司信息表
 *
 * @author hgw
 * @date 2025-10-22 17:43:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tfasccompany")
@Tag(name = "法大大专业版公司信息表管理")
public class TFascCompanyController {

	private final TFascCompanyService tFascCompanyService;


	/**
	 * 简单分页查询
	 *
	 * @param page         分页对象
	 * @param tFascCompany 法大大专业版公司信息表
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/page")
	public R<IPage<TFascCompany>> getTFascCompanyPage(Page<TFascCompany> page, TFascCompany tFascCompany) {
		return new R<>(tFascCompanyService.getTFascCompanyPage(page, tFascCompany));
	}

	/**
	 * 获取全量列表
	 */
	@Operation(description = "获取全量列表")
	@GetMapping("/getTFascCompanyAllList")
	public R<List<TFascCompany>> getTFascCompanyAllList(@RequestParam(required = false) String companyName) {
		return new R<>(tFascCompanyService.getTFascCompanyAllList(companyName));
	}

	/**
	 * 通过id查询法大大专业版公司信息表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R<TFascCompany> getById(@PathVariable("id") String id) {
		return R.ok(tFascCompanyService.getById(id));
	}

	/**
	 * 新增法大大专业版公司信息表
	 *
	 * @param tFascCompany 法大大专业版公司信息表
	 * @return R
	 */
	@Operation(summary = "新增法大大专业版公司信息表", description = "新增法大大专业版公司信息表：hasPermission('archives_tfasccompany_add')")
	@SysLog("新增法大大专业版公司信息表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('archives_tfasccompany_add')")
	public R<Boolean> save(@RequestBody TFascCompany tFascCompany) {
		if (Common.isEmpty(tFascCompany.getCompanyName())) {
			return R.failed("请填写公司主体！");
		}
		int num = tFascCompanyService.getTFascCompanyByNameAndId(tFascCompany.getCompanyName(), null);
		if (num > 0) {
			return R.failed("该公司主体已存在，请勿重复配置！");
		}
		R<Boolean> failed = judgeFieldMust(tFascCompany);
		if (failed != null) {
			return failed;
		}
		return R.ok(tFascCompanyService.save(tFascCompany));
	}

	private R<Boolean> judgeFieldMust(TFascCompany tFascCompany) {
		if (Common.isEmpty(tFascCompany.getAppId())) {
			return R.failed("请填写appId！");
		} else if (tFascCompany.getAppId().length() > CommonConstants.INTEGER_HUNDRED) {
			return R.failed("appId不能超过100个字符！");
		}
		if (Common.isEmpty(tFascCompany.getAppSecret())) {
			return R.failed("请填写appSecret！");
		} else if (tFascCompany.getAppSecret().length() > CommonConstants.INTEGER_HUNDRED) {
			return R.failed("appSecret不能超过100个字符！");
		}
		if (Common.isEmpty(tFascCompany.getAppUrl())) {
			return R.failed("请填写appUrl！");
		} else if (tFascCompany.getAppUrl().length() > CommonConstants.INTEGER_HUNDRED) {
			return R.failed("appUrl不能超过100个字符！");
		}
		if (Common.isEmpty(tFascCompany.getOpenId())) {
			return R.failed("请填写openCorpId！");
		} else if (tFascCompany.getOpenId().length() > CommonConstants.INTEGER_HUNDRED) {
			return R.failed("openCorpId不能超过100个字符！");
		}
		if (Common.isEmpty(tFascCompany.getAppUrl())) {
			return R.failed("请填写appUrl！");
		} else if (tFascCompany.getAppUrl().length() > CommonConstants.INTEGER_HUNDRED) {
			return R.failed("appUrl不能超过100个字符！");
		}
		if (Common.isEmpty(tFascCompany.getBusinessId())) {
			return R.failed("请填写businessId！");
		} else if (tFascCompany.getBusinessId().length() > CommonConstants.INTEGER_HUNDRED) {
			return R.failed("businessId不能超过100个字符！");
		}
		return null;
	}


	/**
	 * 修改法大大专业版公司信息表
	 *
	 * @param tFascCompany 法大大专业版公司信息表
	 * @return R
	 */
	@Operation(summary = "修改法大大专业版公司信息表", description = "修改法大大专业版公司信息表：hasPermission('archives_tfasccompany_edit')")
	@SysLog("修改法大大专业版公司信息表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('archives_tfasccompany_edit')")
	public R<Boolean> updateById(@RequestBody TFascCompany tFascCompany) {
		if (Common.isEmpty(tFascCompany.getId())) {
			return R.failed("请传参ID！");
		}
		if (Common.isEmpty(tFascCompany.getCompanyName())) {
			return R.failed("请传参公司主体！");
		}
		int num = tFascCompanyService.getTFascCompanyByNameAndId(tFascCompany.getCompanyName(), tFascCompany.getId());
		if (num > 0) {
			return R.failed("该公司主体已存在，请勿重复配置！");
		}
		R<Boolean> failed = judgeFieldMust(tFascCompany);
		if (failed != null) {
			return failed;
		}
		return R.ok(tFascCompanyService.updateById(tFascCompany));
	}

	/**
	 * 通过id删除法大大专业版公司信息表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除法大大专业版公司信息表", description = "通过id删除法大大专业版公司信息表：hasPermission('archives_tfasccompany_del')")
	@SysLog("通过id删除法大大专业版公司信息表")
	@GetMapping("/deleteCompanyById")
	@PreAuthorize("@pms.hasPermission('archives_tfasccompany_del')")
	public R<String> deleteCompanyById(@RequestParam String id) {
		if (CommonConstants.ONE_STRING.equals(id)) {
			return R.failed("请勿删除此条数据！");
		}
		YifuUser user = SecurityUtils.getUser();
		if (user == null || Common.isEmpty(user.getNickname())) {
			return R.failed("请登录！");
		}
		TFascCompany tFascCompany = tFascCompanyService.getById(id);
		if (tFascCompany == null || Common.isEmpty(tFascCompany.getId())) {
			return R.failed("根据ID未找到数据！");
		} else {
			tFascCompany.setDeleteFlag(CommonConstants.DELETE_FLAG);
			tFascCompany.setDeleteName(user.getNickname());
			tFascCompany.setDeleteTime(LocalDateTime.now());
			tFascCompanyService.updateById(tFascCompany);
			return R.ok("已删除！");
		}
	}
}
