package com.ghy.web.controller.payment;

import com.ghy.common.annotation.Log;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.ghy.common.enums.BusinessType;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.payment.domain.FinancialMaster;
import com.ghy.payment.service.FinancialMasterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 主财务单API
 *
 * @author HH 2022/4/25
 */
@Controller
@RequestMapping("/financial/master")
public class FinancialMasterController extends BaseController {

    private final String prefix = "financial/master";

    @Resource
    private FinancialMasterService financialMasterService;

    @RequiresPermissions("financial:master:view")
    @GetMapping()
    public String financialMaster() {
        return prefix;
    }

    @RequiresPermissions("financial:master:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FinancialMaster financialMaster) {
        startPage();
        List<FinancialMaster> list = financialMasterService.selectFinancialMasterList(financialMaster);
        return getDataTable(list);
    }

    @PostMapping("/app/list")
    @ResponseBody
    public TableDataInfo appList(FinancialMaster request) {
        startPage();
        List<FinancialMaster> list = financialMasterService.selectAppList(request);
        return getDataTable(list);
    }

    @Log(title = "主财务单管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("financial:master:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FinancialMaster financialMaster) {
        List<FinancialMaster> list = financialMasterService.selectFinancialMasterList(financialMaster);
        ExcelUtil<FinancialMaster> util = new ExcelUtil<>(FinancialMaster.class);
        return util.exportExcel(list, "主财务单数据");
    }

    @RequiresPermissions("financial:master:remove")
    @Log(title = "主财务单管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(financialMasterService.deleteFinancialMasterByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改主财务单
     */
    @RequiresPermissions("financial:master:edit")
    @GetMapping("/edit/{financialMasterId}")
    public String edit(@PathVariable("financialMasterId") Long financialMasterId, ModelMap mmap) {
        mmap.put("financialMaster", financialMasterService.selectById(financialMasterId));
        return prefix + "/edit";
    }

    /**
     * 修改保存主财务单
     */
    @RequiresPermissions("financial:master:edit")
    @Log(title = "主财务单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated FinancialMaster financialMaster) {
        financialMaster.setUpdateBy(getLoginName());
        return toAjax(financialMasterService.updateFinancialMaster(financialMaster));
    }

}
