package com.ghy.web.controller.payment;

import com.ghy.common.annotation.Log;
import com.ghy.common.constant.UserConstants;
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.ExceptionUtil;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.payment.domain.FinancialDetail;
import com.ghy.payment.request.FinancialAccountBillReq;
import com.ghy.payment.response.FinancialAccountBillResp;
import com.ghy.payment.service.FinancialDetailService;
import com.ghy.web.pojo.vo.FinancialCountRequest;
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/detail")
public class FinancialDetailController extends BaseController {

    private final String prefix = "financial/detail";

    @Resource
    private FinancialDetailService financialDetailService;

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

    @PostMapping("/count")
    @ResponseBody
    public AjaxResult count(@RequestBody FinancialCountRequest request){
        try {
            return AjaxResult.success(financialDetailService.count(request.getCountTime(), request.getBillingState(), request.getFinancialDetailTypes(), request.getWorkerId()));
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

//    @RequiresPermissions("financial:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FinancialDetail financialDetail) {
        startPage();
        List<FinancialDetail> list = financialDetailService.selectFinancialDetailList(financialDetail);
        return getDataTable(list);
    }

    @PostMapping("/app/list")
    @ResponseBody
    public TableDataInfo appList(@RequestBody FinancialDetail financialDetail) {
        startPage();
        List<FinancialDetail> list = financialDetailService.selectFinancialDetailList(financialDetail);
        return getDataTable(list);
    }

    @PostMapping("/app/listV2")
    @ResponseBody
    public TableDataInfo appListV2(@RequestBody FinancialAccountBillReq financialAccountBillReq) {
        startPage();
        List<FinancialAccountBillResp> list = financialDetailService.selectFinancialDetailListV2(financialAccountBillReq);
        return getDataTable(list);
    }

    @Log(title = "财务细单管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("financial:detail:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FinancialDetail financialDetail) {
        List<FinancialDetail> list = financialDetailService.selectFinancialDetailList(financialDetail);
        ExcelUtil<FinancialDetail> util = new ExcelUtil<>(FinancialDetail.class);
        return util.exportExcel(list, "财务细单数据");
    }

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

    /**
     * 修改财务细单
     */
    @RequiresPermissions("financial:detail:edit")
    @GetMapping("/edit/{financialDetailId}")
    public String edit(@PathVariable("financialDetailId") Long financialDetailId, ModelMap mmap) {
        mmap.put("financialDetail", financialDetailService.selectById(financialDetailId));
        return prefix + "/edit";
    }

    /**
     * 修改保存财务细单
     */
    @RequiresPermissions("financial:detail:edit")
    @Log(title = "财务细单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated FinancialDetail financialDetail) {
        if (UserConstants.ORDER_CODE_NOT_UNIQUE.equals(financialDetailService.checkFinancialDetailCodeUnique(financialDetail))) {
            return error("修改财务细单'" + financialDetail.getCode() + "'失败，财务细单编码已存在");
        }
        financialDetail.setUpdateBy(getLoginName());
        return toAjax(financialDetailService.updateFinancialDetail(financialDetail));
    }

    /**
     * 校验财务细单编码
     */
    @PostMapping("/checkFinancialDetailCodeUnique")
    @ResponseBody
    public String checkFinancialDetailCodeUnique(FinancialDetail financialDetail) {
        return financialDetailService.checkFinancialDetailCodeUnique(financialDetail);
    }
}
