package com.ruoyi.web.controller.oa;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.oa.domain.CmcCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.oa.service.ICmcCheckService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc项目核算Controller
 * 
 * @author cmc
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/oa/check")
public class CmcCheckController extends BaseController
{
    @Autowired
    private ICmcCheckService cmcCheckService;

    /**
     * 查询cmc项目核算列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcCheck cmcCheck)
    {
        startPage();
        List<CmcCheck> list = cmcCheckService.selectCmcCheckList(cmcCheck);
        return getDataTable(list);
    }

    /**
     * 导出cmc项目核算列表
     */
    @Log(title = "cmc项目核算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcCheck cmcCheck)
    {
        List<CmcCheck> list = cmcCheckService.selectCmcCheckList(cmcCheck);
        ExcelUtil<CmcCheck> util = new ExcelUtil<CmcCheck>(CmcCheck.class);
        util.exportExcel(response, list, "cmc项目核算数据");
    }

    /**
     * 获取cmc项目核算详细信息
     */
    @GetMapping(value = "/{checkId}")
    public AjaxResult getInfo(@PathVariable("checkId") String checkId)
    {
        return success(cmcCheckService.selectCmcCheckByCheckId(checkId));
    }

    /**
     * 获取cmc核算审批详细信息
     */
    @GetMapping("/statistic")
    public AjaxResult getCheckStatistic(CmcCheck cmcCheck) throws ParseException {
        JSONObject jsonObject = new JSONObject();
        JSONArray yearArray = new JSONArray();
        JSONObject yearObject = new JSONObject();
        JSONArray amountArray = new JSONArray();
        JSONObject amountObject = new JSONObject();
        JSONArray yearProjectCountArray = new JSONArray();
        JSONObject yearProjectCountObject = new JSONObject();
        JSONArray yearProjectAmountArray = new JSONArray();
        JSONObject yearProjectAmountObject = new JSONObject();
        JSONArray monthArray = new JSONArray();
        JSONObject monthObject = new JSONObject();
        JSONArray monthAmountArray = new JSONArray();
        JSONObject monthAmountObject = new JSONObject();
        //每年核算数量和金额
        if (cmcCheck.getCheckTime() == null) {
            for (int i = 2019; i <= Calendar.getInstance().get(Calendar.YEAR); i++) {
                cmcCheck.setCheckTime(new SimpleDateFormat("yyyy").parse(String.valueOf(i)));
                yearObject.put(String.valueOf(i), cmcCheckService.selectCmcCheckList(cmcCheck).size());
                BigDecimal amount = new BigDecimal(0);
                for (CmcCheck check : cmcCheckService.selectCmcCheckList(cmcCheck)) {
                    if (check.getTotalAdjust() != null)
                        amount = amount.add(check.getTotalAdjust());
                }
                amountObject.put(String.valueOf(i), amount);
            }
            cmcCheck.setCheckTime(null);
        }
        else {
            String yearStr = new SimpleDateFormat("yyyy").format(cmcCheck.getCheckTime());
            cmcCheck.setCheckTime(new SimpleDateFormat("yyyy").parse(yearStr));
            List<CmcCheck> yearList = cmcCheckService.selectCmcCheckList(cmcCheck);
            yearObject.put(yearStr, yearList.size());
            BigDecimal amount = new BigDecimal(0);
            for (CmcCheck check : yearList) {
                if (check.getTotalAdjust() != null)
                    amount = amount.add(check.getTotalAdjust());
            }
            amountObject.put(yearStr, amount);
            
            //月度统计
            for (int i = 1; i <= 12; i++) {
                String monthStr = String.format("%02d", i);
                String monthKey = yearStr + "-" + monthStr;
                String beginTimeString = monthKey + "-01";
                
                Map<String, Object> params = new HashMap<>();
                params.put("beginTime", beginTimeString + " 00:00:00");
                cmcCheck.setParams(params);
                cmcCheck.setCheckTime(new SimpleDateFormat("yyyy-MM").parse(beginTimeString));
                
                List<CmcCheck> monthList = cmcCheckService.selectCmcCheckList(cmcCheck);
                monthObject.put(monthKey, monthList.size());
                BigDecimal monthAmount = new BigDecimal(0);
                for (CmcCheck check : monthList) {
                    if (check.getTotalAdjust() != null)
                        monthAmount = monthAmount.add(check.getTotalAdjust());
                }
                monthAmountObject.put(monthKey, monthAmount);
                cmcCheck.setParams(null);
            }
        }
        cmcCheck.setCheckTime(null);
        getCheckProjectStatistic(cmcCheck, yearProjectCountObject, yearProjectAmountObject);
        yearArray.add(yearObject);
        amountArray.add(amountObject);
        yearProjectCountArray.add(yearProjectCountObject);
        yearProjectAmountArray.add(yearProjectAmountObject);
        monthArray.add(monthObject);
        monthAmountArray.add(monthAmountObject);
        jsonObject.put("year", yearArray);
        jsonObject.put("amount", amountArray);
        jsonObject.put("yearProjectCount", yearProjectCountArray);
        jsonObject.put("yearProjectAmount", yearProjectAmountArray);
        jsonObject.put("month", monthArray);
        jsonObject.put("monthAmount", monthAmountArray);
        return success(jsonObject);
    }

    /**
     * 新增cmc项目核算
     */
    @Log(title = "cmc项目核算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcCheck cmcCheck)
    {
        return toAjax(cmcCheckService.insertCmcCheck(cmcCheck));
    }

    /**
     * 修改cmc项目核算
     */
    @Log(title = "cmc项目核算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcCheck cmcCheck)
    {
        return toAjax(cmcCheckService.updateCmcCheck(cmcCheck));
    }

    /**
     * 删除cmc项目核算
     */
    @Log(title = "cmc项目核算", businessType = BusinessType.DELETE)
	@DeleteMapping("/{checkIds}")
    public AjaxResult remove(@PathVariable String[] checkIds)
    {
        return success(cmcCheckService.deleteCmcCheckByCheckIds(checkIds));
    }

    public void getCheckProjectStatistic(CmcCheck cmcCheck, JSONObject yearProjectCountObject, JSONObject yearProjectAmountObject) throws ParseException {
        if (cmcCheck.getCheckTime() == null)
            cmcCheck.setCheckTime(new SimpleDateFormat("yyyy").parse("2000-01-01"));
        for (int i = 2019; i <= Calendar.getInstance().get(Calendar.YEAR); i++) {
            BigDecimal pyAmount = new BigDecimal(0);
            int count = 0;
            for (CmcCheck check : cmcCheckService.selectCmcCheckList(cmcCheck)) {
                if (check.getTotalAdjust() != null && check.getProjectNumber() != null && check.getProjectNumber().contains(String.valueOf(i))) {
                    pyAmount = pyAmount.add(check.getTotalAdjust());
                    count ++;
                }
            }
            yearProjectCountObject.put(i + "项目", count);
            yearProjectAmountObject.put(i + "项目", pyAmount);
        }
    }
}
