package org.sky.scientific.controller;

import com.alibaba.fastjson.JSONObject;
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.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.sky.core.secure.utils.SecureUtil;
import org.sky.core.tool.api.R;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.mapper.XmCostDetailsMapper;
import org.sky.scientific.pojo.dto.CostSummaryDTO;
import org.sky.scientific.pojo.vo.CostSummaryVO;
import org.sky.scientific.service.IAssetService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@RestController
@AllArgsConstructor
@Tag(name = "研发费用统计")
public class CostController {

	private final IAssetService service;

	private final XmCostDetailsMapper xmCostDetailsMapper;

	@GetMapping("/cost/summary")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "研发费用统计-本年各月研发费用汇总表")
	public R<List<CostSummaryVO>> costSummary(CostSummaryDTO dto) {
		return R.data(service.costSummary(dto));
	}

	@PostMapping("/cost/summary/export")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "研发费用统计-本年各月研发费用汇总表-导出", description = "传入salary")
	public void exportCostSummary(HttpServletResponse response, @RequestBody CostSummaryDTO dto) {
		service.exportCostSummary(response, dto);
	}

	@GetMapping("xmysygjqst")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "首页-项目预算与归集趋势图")
	@Parameters({
		@Parameter(name = "year", description = "年份", in = ParameterIn.QUERY),
		@Parameter(name = "currentYear", description = "本年度=1，全周期=0", in = ParameterIn.QUERY)
	})
	public R<List<JSONObject>> xmysygjqst(String year, Boolean currentYear) {
		List<JSONObject> list = xmCostDetailsMapper.xmysygjqst(SecureUtil.getTenantId(), year, currentYear);
		for (JSONObject obj : list) {
			BigDecimal cost = obj.getBigDecimal("cost").multiply(new BigDecimal("0.0001")).setScale(2, RoundingMode.HALF_UP);
			obj.put("cost", cost);

			List<JSONObject> XMYSZEMX = JSONObject.parseArray(obj.getString("XMYSZEMX"), JSONObject.class);

			BigDecimal total = BigDecimal.valueOf(0.00);
			if (currentYear) {
				total = XMYSZEMX.stream().filter(t -> Func.equals(t.getString("year"), year)).map(item -> item.getBigDecimal("amount")).reduce(BigDecimal.valueOf(0.00), BigDecimal::add).setScale(4, RoundingMode.UP);
			} else {
				total = XMYSZEMX.stream().map(item -> item.getBigDecimal("amount")).reduce(BigDecimal.valueOf(0.00), BigDecimal::add).setScale(4, RoundingMode.UP);
			}
			obj.remove("XMYSZEMX");
			obj.put("xmys", total);
		}
		return R.data(list);
	}

}
