package com.ruoyi.web.controller.branch;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.controller.branch.vo.CalcVO;
import org.springframework.security.access.prepost.PreAuthorize;
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.branch.domain.TBranchDues;
import com.ruoyi.branch.service.ITBranchDuesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 支部党费收支管理Controller
 * 
 * @author ruoyi
 * @date 2023-07-14
 */
@RestController
@RequestMapping("/branch/dues")
public class TBranchDuesController extends BaseController
{
    @Autowired
    private ITBranchDuesService tBranchDuesService;

    /**
     * 查询支部党费计算
     */
    @PreAuthorize("@ss.hasPermi('branch:dues:list')")
    @GetMapping("/calc")
    public AjaxResult calc(TBranchDues tBranchDues)
    {
        List<TBranchDues> tBranchDuesList = tBranchDuesService.selectTBranchDuesList(tBranchDues);
        BigDecimal ndzmsr = BigDecimal.ZERO;//年度账面收入
        BigDecimal ndzmzc = BigDecimal.ZERO;//年度账面支出
        BigDecimal ndxjsr = BigDecimal.ZERO;//年度现金收入
        BigDecimal ndxjzc = BigDecimal.ZERO;//年度现金支出
        BigDecimal zmhj = BigDecimal.ZERO;//账面合计
        BigDecimal xjhj = BigDecimal.ZERO;//现金合计
        BigDecimal zhj = BigDecimal.ZERO;//总合计
        for (TBranchDues branchDues : tBranchDuesList) {
            if ("1".equals(branchDues.getDuesType())) {//账面
                if ("1".equals(branchDues.getDuesInOut())) {//收入
                    ndzmsr = ndzmsr.add(new BigDecimal(branchDues.getDuesAmount()));
                } else {//支出
                    ndzmzc = ndzmzc.add(new BigDecimal(branchDues.getDuesAmount()));
                }
            } else {//现金
                if ("1".equals(branchDues.getDuesInOut())) {//收入
                    ndxjsr = ndxjsr.add(new BigDecimal(branchDues.getDuesAmount()));
                } else {//支出
                    ndxjzc = ndxjzc.add(new BigDecimal(branchDues.getDuesAmount()));
                }
            }
        }
        zmhj = ndzmsr.subtract(ndzmzc);
        xjhj = ndxjsr.subtract(ndxjzc);
        zhj = zmhj.add(xjhj);
        return AjaxResult.success(new CalcVO(ndzmsr, ndzmzc, ndxjsr, ndxjzc, zmhj, xjhj, zhj));
    }

    /**
     * 查询支部党费收支管理列表
     */
    @PreAuthorize("@ss.hasPermi('branch:dues:list')")
    @GetMapping("/list")
    public AjaxResult list(TBranchDues tBranchDues)
    {
//        startPage();
//        List<TBranchDues> list = tBranchDuesService.selectTBranchDuesList(tBranchDues);
//        return getDataTable(list);
        return AjaxResult.success(tBranchDuesService.selectTBranchDuesList(tBranchDues));
    }

    /**
     * 导出支部党费收支管理列表
     */
    @PreAuthorize("@ss.hasPermi('branch:dues:export')")
    @Log(title = "支部党费收支管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBranchDues tBranchDues)
    {
        List<TBranchDues> list = tBranchDuesService.selectTBranchDuesList(tBranchDues);
        ExcelUtil<TBranchDues> util = new ExcelUtil<TBranchDues>(TBranchDues.class);
        util.exportExcel(response, list, "支部党费收支管理数据");
    }

    /**
     * 获取支部党费收支管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('branch:dues:query')")
    @GetMapping(value = "/{duesId}")
    public AjaxResult getInfo(@PathVariable("duesId") Long duesId)
    {
        return success(tBranchDuesService.selectTBranchDuesByDuesId(duesId));
    }

    /**
     * 新增支部党费收支管理
     */
    @PreAuthorize("@ss.hasPermi('branch:dues:add')")
    @Log(title = "支部党费收支管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBranchDues tBranchDues)
    {
        return toAjax(tBranchDuesService.insertTBranchDues(tBranchDues));
    }

    /**
     * 修改支部党费收支管理
     */
    @PreAuthorize("@ss.hasPermi('branch:dues:edit')")
    @Log(title = "支部党费收支管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBranchDues tBranchDues)
    {
        return toAjax(tBranchDuesService.updateTBranchDues(tBranchDues));
    }

    /**
     * 删除支部党费收支管理
     */
    @PreAuthorize("@ss.hasPermi('branch:dues:remove')")
    @Log(title = "支部党费收支管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{duesIds}")
    public AjaxResult remove(@PathVariable Long[] duesIds)
    {
        return toAjax(tBranchDuesService.deleteTBranchDuesByDuesIds(duesIds));
    }
}
