
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.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
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.Charsets;
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 org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
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>> qyyjkfxmqk107_1(String year) {
		return R.data(enterpriseInfoService.qyyjkfxmqk107_1(year));
	}

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

	@GetMapping("/qyyjkfxmqk/export")
	@ApiOperationSupport(order = 32)
	@Operation(summary = "107-1-企业研究开发项目情况-导出")
	public void export107_1(HttpServletResponse response, String yearAndMonth) {
		enterpriseInfoService.exportQyyjkfxmqk107_1(response, yearAndMonth);
	}

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

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

	@GetMapping("/qyyjkfhdjxgqk/export")
	@ApiOperationSupport(order = 35)
	@Operation(summary = "107-2-企业研究开发活动及相关情况-导出")
	public void exportQyyjkfhdjxgqk107_2(HttpServletResponse response, String year) {
		enterpriseInfoService.exportQyyjkfhdjxgqk107_2(response, year);
	}

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

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

	@GetMapping("/yffyjjkcyhmxb/export")
	@ApiOperationSupport(order = 38)
	@Operation(summary = "A107012研发费用加计扣除优惠明细表-导出")
	public void exportYffyjjkcyhmxbA107012(HttpServletResponse response, String year) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("A107012研发费用加计扣除优惠明细表.xlsx", Charsets.UTF_8));
		enterpriseInfoService.exportYffyjjkcyhmxbA107012(response.getOutputStream(), year);
	}

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

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

	@GetMapping("/gxjsqyyhqkjmxb/export")
	@ApiOperationSupport(order = 41)
	@Operation(summary = "A107041高新技术企业优惠情况及明细表-导出")
	public void exportQxjsqyyhqkjmxbA107041(HttpServletResponse response, String year) {
		enterpriseInfoService.exportQxjsqyyhqkjmxbA107041(response, year);
	}

	@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));
	}

	@SneakyThrows
	@GetMapping("/gqnb/export")
	@ApiOperationSupport(order = 44)
	@Operation(summary = "高企年报-导出")
 	public void exportGqnb(HttpServletResponse response, String year) {
		HighTechYBVO techYBVO = enterpriseInfoService.gqnb(year);
		response.setHeader("Content-disposition",
			"attachment;filename=" + URLEncoder.encode("年度高新技术企业发展情况报表.doc", StandardCharsets.UTF_8));
		response.setContentType("application/msword");// 定义输出类型

		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		new Thread(() -> {
			RequestContextHolder.setRequestAttributes(requestAttributes);
			try {
				enterpriseInfoService.exportGqnb(response.getOutputStream(), techYBVO);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}).start();
	}

	@GetMapping("hjfznb")
	@ApiOperationSupport(order = 50)
	@Operation(summary = "火炬发展年报")
	public R<TorchDevYBVO> torchDevYB(String year) {
		return R.data(enterpriseInfoService.torchDevYB(year));
	}

	@GetMapping("/hjfznb/export")
	@ApiOperationSupport(order = 51)
	@Operation(summary = "火炬发展年报-导出")
	public void exportTorchDevYB(HttpServletResponse response, String year) {
		enterpriseInfoService.exportTorchDevYB(response, year);
	}
}
