
package org.sky.scientific.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.sky.core.boot.ctrl.KdController;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.tool.api.R;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.pojo.entity.*;
import org.sky.scientific.pojo.vo.*;
import org.sky.scientific.service.IDepartmentService;
import org.sky.scientific.service.IEnterpriseInfoService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业基础信息管理 控制器
 *
 * @author Kd
 * @since 2025-07-01
 */
@RestController
@AllArgsConstructor
@Tag(name = "企业基础信息管理")
public class EnterpriseController extends KdController {

	private final IDepartmentService departmentService;

	private final IEnterpriseInfoService enterpriseInfoService;

	@GetMapping("enterpriseInfo/detail/{yearAndMonth}")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "企业基础信息表-获取指定年份的企业信息")
	public R<EnterpriseInfoEntity> enterpriseDetail(@PathVariable("yearAndMonth") String yearAndMonth) {
		EnterpriseInfoEntity detail = enterpriseInfoService.getOne(Wrappers.<EnterpriseInfoEntity>lambdaQuery().eq(EnterpriseInfoEntity::getYearAndMonth, yearAndMonth));
		return R.data(detail);
	}

	@PostMapping("enterpriseInfo/submit")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "企业基础信息表-新增或修改", description = "传入enterpriseInfo")
	public R submitEnterprise(@Valid @RequestBody EnterpriseInfoEntity enterpriseInfo) {
		return R.status(enterpriseInfoService.submit(enterpriseInfo));
	}

	@GetMapping("enterpriseInfo/fetchLastYearData")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "企业基础信息表-读取上年度数据")
	public R<Boolean> fetchEnterpriseLastYearData(EnterpriseInfoEntity enterpriseInfo) {
		return R.status(enterpriseInfoService.fetchLastYearData(enterpriseInfo));
	}

	@GetMapping("/department/list")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "二级单位/部门-列表")
	public R<List<DepartmentEntity>> departmentList(DepartmentEntity dto) {
		if (Func.isBlank(dto.getYearAndMonth())) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		List<DepartmentEntity> list = departmentService.selectDepartmentList(dto.getYearAndMonth());
		return R.data(list);
	}

	@PostMapping("/department/submit")
	@ApiOperationSupport(order = 12)
	@Operation(summary = "二级单位/部门-新增或修改")
	public R submitDepartment(@Valid @RequestBody DepartmentEntity department) {

		return R.status(departmentService.submit(department));
	}

	@GetMapping("/fetchLastYearData")
	@ApiOperationSupport(order = 14)
	@Operation(summary = "二级单位/部门-读取上年度数据")
	public R<Boolean> fetchLastYearData(DepartmentEntity department) {
		return R.status(departmentService.fetchLastYearData(department));
	}

	@DeleteMapping("department/remove")
	@Operation(summary = "二级单位/部门-删除")
	@ApiOperationSupport(order = 18)
	public R removeDepartment(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(departmentService.deleteByIds(Func.toLongList(ids)));
	}

	@GetMapping("qyyjkfxmqk")
	@ApiOperationSupport(order = 30)
	@Operation(summary = "107-1-企业研究开发项目情况")
	public R<List<A107_1VO>> qyyjkfxmqk(String year) {
		return R.data(enterpriseInfoService.qyyjkfxmqk(year));
	}
	@PostMapping("qyyjkfxmqk")
	@ApiOperationSupport(order = 30)
	@Operation(summary = "107-1-企业研究开发项目情况-修改")
	public R<Boolean> qyyjkfxmqkUpdate(@Valid @RequestBody A107_1 a1071) {
		return R.data(enterpriseInfoService.qyyjkfxmqkUpdate(a1071));
	}

	@GetMapping("qyyjkfhdjxgqk")
	@ApiOperationSupport(order = 33)
	@Operation(summary = "107-2-企业研究开发活动及相关情况")
	public R<A107_2VO> qyyjkfhdjxgfy(String year) {
		return R.data(enterpriseInfoService.qyyjkfhdjxgfy(year));
	}

	@PostMapping("qyyjkfhdjxgqk")
	@ApiOperationSupport(order = 34)
	@Operation(summary = "107-2-企业研究开发活动及相关情况-填写")
	public R<Boolean> qyyjkfhdjxgfyUpdate(@Valid @RequestBody A107_2 a1072) {
		return R.status(enterpriseInfoService.qyyjkfhdjxgfyUpdate(a1072));
	}

	@GetMapping("yffyjjkcyhmxb")
	@ApiOperationSupport(order = 36)
	@Operation(summary = "A107012研发费用加计扣除优惠明细表")
	public R<A107012VO> yffyjjkcyhmxb(String year) {
		return R.data(enterpriseInfoService.yffyjjkcyhmxb(year));
	}

	@PostMapping("yffyjjkcyhmxb")
	@ApiOperationSupport(order = 37)
	@Operation(summary = "A107012研发费用加计扣除优惠明细表-填写")
	public R<Boolean> yffyjjkcyhmxbUpdate(@Valid @RequestBody A107012 a107012) {
		return R.status(enterpriseInfoService.yffyjjkcyhmxbUpdate(a107012));
	}

	@GetMapping("gxjsqyyhqkjmxb")
	@ApiOperationSupport(order = 39)
	@Operation(summary = "A107041高新技术企业优惠情况及明细表")
	public R<A107041VO> gxjsqyyhqkjmxb(String year) {
		return R.data(enterpriseInfoService.gxjsqyyhqkjmxb(year));
	}

	@PostMapping("gxjsqyyhqkjmxb")
	@ApiOperationSupport(order = 40)
	@Operation(summary = "A107041高新技术企业优惠情况及明细表-填写")
	public R<Boolean> gxjsqyyhqkjmxbUpdate(@Valid @RequestBody A107041 a107041) {
		return R.status(enterpriseInfoService.gxjsqyyhqkjmxbUpdate(a107041));
	}

	@GetMapping("gqnb")
	@ApiOperationSupport(order = 42)
	@Operation(summary = "高企年报")
	public R<HighTechYBVO> gqnb(String year) {
		return R.data(enterpriseInfoService.gqnb(year));
	}

	@PostMapping("gqnb")
	@ApiOperationSupport(order = 43)
	@Operation(summary = "高企年报-填写")
	public R<Boolean> gqnbUpdate(@Valid @RequestBody HighTechYB highTechYB) {
		return R.status(enterpriseInfoService.gqnbUpdate(highTechYB));
	}

	@GetMapping("fjbczl")
	@ApiOperationSupport(order = 50)
	@Operation(summary = "科研档案管理-附件补充资料")
	public R<ArchiveAttachEntity> fjbczl(String year) {
		return R.data(enterpriseInfoService.fjbczl(year));
	}

	@PostMapping("fjbczl")
	@ApiOperationSupport(order = 51)
	@Operation(summary = "科研档案管理-附件补充资料-保存")
	public R<Boolean> fjbczlUpdate(@Valid @RequestBody ArchiveAttachEntity attach) {
		return R.status(enterpriseInfoService.fjbczlUpdate(attach));
	}

}
