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.List;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.domain.CmcBudget;
import com.ruoyi.oa.service.ICmcBudgetService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc预算管理Controller
 * 
 * @author cmc
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/oa/budget")
public class CmcBudgetController extends BaseController
{
    @Autowired
    private ICmcBudgetService cmcBudgetService;

    /**
     * 查询cmc预算管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcBudget cmcBudget)
    {
        startPage();
        List<CmcBudget> list = cmcBudgetService.selectCmcBudgetList(cmcBudget);
        return getDataTable(list);
    }

    /**
     * 导出cmc预算管理列表
     */
    @Log(title = "cmc预算管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcBudget cmcBudget)
    {
        List<CmcBudget> list = cmcBudgetService.selectCmcBudgetList(cmcBudget);
        ExcelUtil<CmcBudget> util = new ExcelUtil<CmcBudget>(CmcBudget.class);
        util.exportExcel(response, list, "cmc预算管理数据");
    }

    /**
     * 获取cmc预算管理详细信息
     */
    @GetMapping(value = "/{budgetId}")
    public AjaxResult getInfo(@PathVariable("budgetId") String budgetId)
    {
        return success(cmcBudgetService.selectCmcBudgetByBudgetId(budgetId));
    }

    /**
     * 获取cmc结算审批详细信息
     */
    @GetMapping("/statistic")
    public AjaxResult getBudgetStatistic(CmcBudget cmcBudget) throws ParseException {
        JSONObject jsonObject = new JSONObject();
        JSONArray yearArray = new JSONArray();
        JSONObject yearObject = new JSONObject();
        JSONArray amountArray = new JSONArray();
        JSONObject amountObject = new JSONObject();
        //每年借款金额
        if (cmcBudget.getZjlTime() == null) {
            for (int i = 2019; i <= Calendar.getInstance().get(Calendar.YEAR); i++) {
                cmcBudget.setZjlTime(new SimpleDateFormat("yyyy").parse(String.valueOf(i)));
                yearObject.put(String.valueOf(i), cmcBudgetService.selectCmcBudgetList(cmcBudget).size());
                BigDecimal amount = new BigDecimal(0);
                for (CmcBudget budget : cmcBudgetService.selectCmcBudgetList(cmcBudget)) {
                    if (budget.getTotalBudget() != null)
                        amount = amount.add(budget.getTotalBudget());
                }
                amountObject.put(String.valueOf(i), amount);
            }
            cmcBudget.setZjlTime(null);
        }
        else {
            yearObject.put(new SimpleDateFormat("yyyy").format(cmcBudget.getZjlTime()), cmcBudgetService.selectCmcBudgetList(cmcBudget).size());
            BigDecimal amount = new BigDecimal(0);
            for (CmcBudget budget : cmcBudgetService.selectCmcBudgetList(cmcBudget)) {
                if (budget.getTotalBudget() != null)
                    amount = amount.add(budget.getTotalBudget());
            }
            amountObject.put(new SimpleDateFormat("yyyy").format(cmcBudget.getZjlTime()), amount);
        }
        yearArray.add(yearObject);
        amountArray.add(amountObject);
        jsonObject.put("year", yearArray);
        jsonObject.put("amount", amountArray);
        return success(jsonObject);
    }

    /**
     * 新增cmc预算管理
     */
    @Log(title = "cmc预算管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcBudget cmcBudget)
    {
        cmcBudget.setCompiler(getLoginUser().getUserId());
        return toAjax(cmcBudgetService.insertCmcBudget(cmcBudget));
    }

    /**
     * 修改cmc预算管理
     */
    @Log(title = "cmc预算管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcBudget cmcBudget)
    {
        return toAjax(cmcBudgetService.updateCmcBudget(cmcBudget));
    }

    /**
     * 删除cmc预算管理
     */
    @Log(title = "cmc预算管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{budgetIds}")
    public AjaxResult remove(@PathVariable String[] budgetIds)
    {
        return success(cmcBudgetService.deleteCmcBudgetByBudgetIds(budgetIds));
    }
}
