package com.ruoyi.web.work.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.work.domain.Statement;
import com.ruoyi.web.work.domain.StatementDetail;
import com.ruoyi.web.work.domain.dto.*;
import com.ruoyi.web.work.domain.vo.StatementExportListVo;
import com.ruoyi.web.work.service.ICommonService;
import com.ruoyi.web.work.service.IStatementDetailService;
import com.ruoyi.web.work.service.IStatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.rmi.ServerException;
import java.util.List;

/**
 * 结算单管理
 *
 * @author lsw
 * @date 2024-04-20
 */
@RestController
@RequestMapping("/work/statement")
public class StatementController extends BaseController {
    @Autowired
    private IStatementService statementService;

    @Autowired
    private IStatementDetailService detailService;

    @Autowired
    private ICommonService commonService;

    /**
     * 平台业务发放
     */
    @PreAuthorize("@ss.hasPermi('work:statement:pay')")
    @GetMapping("/pay")
    public TableDataInfo out(Statement statement) {
        startPage();
        List<Statement> list = statementService.selectPayList(statement);
        return getDataTable(list);
    }

    /**
     * 制单管理
     */
    @PreAuthorize("@ss.hasPermi('work:statement:make')")
    @GetMapping("/make")
    public TableDataInfo make(Statement statement) {
        statement.setAuditors(false);
        statement.setCompanyId(getLoginUser().getUser().getCompanyId());
        startPage();
        List<Statement> list = statementService.selectList(statement);
        return getDataTable(list);
    }

    /**
     * 结算单管理
     */
    @PreAuthorize("@ss.hasPermi('work:statement:manage')")
    @GetMapping("/manage")
    public TableDataInfo manage(Statement statement) {
        statement.setState(1);
        statement.setAudit(null);
        statement.setGive(null);
        statement.setAuditors(false);
        statement.setCompanyId(getLoginUser().getUser().getCompanyId());
        startPage();
        List<Statement> list = statementService.selectList(statement);
        return getDataTable(list);
    }

    /**
     * 结算单审核
     */
    @PreAuthorize("@ss.hasPermi('work:statement:audit')")
    @GetMapping("/audit")
    public TableDataInfo audit(Statement statement) {
        statement.setState(1);
        statement.setAuditors(true);
        statement.setCompanyId(getLoginUser().getUser().getCompanyId());
        startPage();
        List<Statement> list = statementService.selectList(statement);
        return getDataTable(list);
    }

    /**
     * 发放业务费
     */
    @PreAuthorize("@ss.hasPermi('work:statement:give')")
    @GetMapping("/give")
    public TableDataInfo give(Statement statement) {
        statement.setState(1);
        statement.setAudit(2);
        statement.setAuditors(false);
        statement.setCompanyId(getLoginUser().getUser().getCompanyId());
        startPage();
        List<Statement> list = statementService.selectList(statement);
        return getDataTable(list);
    }

    /**
     * 财务对账
     */
    @PreAuthorize("@ss.hasPermi('work:statement:bill')")
    @GetMapping("/bill")
    public TableDataInfo bill(Statement statement) {
        statement.setGive(2);
        statement.setAuditors(false);
        statement.setCompanyId(getLoginUser().getUser().getCompanyId());
        startPage();
        List<Statement> list = statementService.selectList(statement);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:ok')")
    @Log(title = "平台确认发放业务费", businessType = BusinessType.UPDATE)
    @GetMapping("/pay/ok/{id}")
    public AjaxResult payOk(@PathVariable("id") Long id) throws ServerException {
        return statementService.payOk(id);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:export')")
    @Log(title = "平台导出网银助手表单", businessType = BusinessType.EXPORT)
    @PostMapping("/pay/export")
    public void payExport(HttpServletResponse response, @Validated StatementExportDto dto) {
        List<StatementExportListVo> list = statementService.exportList(dto);
        ExcelUtil<StatementExportListVo> util = new ExcelUtil<StatementExportListVo>(StatementExportListVo.class);
        util.exportExcel(response, list, "网银助手表单");
    }

    @PreAuthorize("@ss.hasPermi('work:statement:delete')")
    @Log(title = "平台删除发放业务费", businessType = BusinessType.UPDATE)
    @GetMapping("/pay/delete/{id}")
    public AjaxResult payDelete(@PathVariable("id") Long id) throws ServerException {
        return statementService.payDelete(id);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:give')")
    @Log(title = "发放业务费获取验证码", businessType = BusinessType.INSERT)
    @GetMapping("/give/sms")
    public AjaxResult sms() {
        return commonService.sms(new SmsDto().setTemplateCode("SMS_465690916"));
    }

    @PreAuthorize("@ss.hasPermi('work:statement:give')")
    @Log(title = "发放业务费", businessType = BusinessType.UPDATE)
    @PostMapping("/give/send")
    public AjaxResult giveSend(@Validated @RequestBody StatementDto dto) throws ServerException {
        return statementService.giveSend(dto);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:audit')")
    @Log(title = "结算单审核通过", businessType = BusinessType.UPDATE)
    @GetMapping("/audit/success/{id}")
    public AjaxResult auditSuccess(@PathVariable("id") Long id) {
        return statementService.auditSuccess(id);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:audit')")
    @Log(title = "结算单审核驳回", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@Validated @RequestBody RejectDto dto) {
        return statementService.auditReject(dto);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:manage')")
    @Log(title = "提交结算单审核", businessType = BusinessType.UPDATE)
    @GetMapping("/manage/submit/{id}")
    public AjaxResult manageSubmit(@PathVariable("id") Long id) {
        return statementService.manageSubmit(id);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:manage')")
    @Log(title = "撤回结算单审核", businessType = BusinessType.UPDATE)
    @GetMapping("/manage/cancel/{id}")
    public AjaxResult manageCancel(@PathVariable("id") Long id) {
        return statementService.manageCancel(id);
    }

    @GetMapping("/detailList")
    public TableDataInfo detailList(StatementDetail detail) {
        startPage();
        List<StatementDetail> list = detailService.selectList(detail);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:make')")
    @Log(title = "生成结算单(手动)", businessType = BusinessType.UPDATE)
    @GetMapping("/make/generate/{id}")
    public AjaxResult makeGenerate(@PathVariable("id") Long id) {
        return statementService.makeGenerate(id);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:make')")
    @Log(title = "生成结算单(自动)", businessType = BusinessType.UPDATE)
    @PostMapping("/make/generate/project")
    public AjaxResult makeGenerateByProject(@Validated @RequestBody GenerateDto dto) {
        return statementService.makeGenerateByProject(dto);
    }

    @PreAuthorize("@ss.hasAnyPermi('system:user:add,work:statement:give')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        return AjaxResult.success(statementService.getById(id));
    }


    @PreAuthorize("@ss.hasPermi('work:statement:make')")
    @Log(title = "创建结算单", businessType = BusinessType.INSERT)
    @PostMapping("/make/add")
    public AjaxResult makeAdd(@Validated Statement statement) throws Exception {
        ExcelUtil<StatementDetail> util = new ExcelUtil<StatementDetail>(StatementDetail.class);
        List<StatementDetail> detailList = util.importExcel(statement.getFile().getInputStream());
        return statementService.makeAdd(detailList, statement);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:make')")
    @Log(title = "编辑结算单明细", businessType = BusinessType.UPDATE)
    @PostMapping("/make/statementDetail/edit")
    public AjaxResult makeStatementDetailEdit(@Validated @RequestBody StatementDetail detail) {
        return statementService.makeStatementDetailEdit(detail);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:make')")
    @Log(title = "删除结算单明细", businessType = BusinessType.DELETE)
    @GetMapping("/make/statementDetail/remove/{id}")
    public AjaxResult makeStatementDetailRemove(@PathVariable Long id) {
        return statementService.makeStatementDetailRemove(id);
    }

    @PreAuthorize("@ss.hasPermi('work:statement:make')")
    @Log(title = "删除结算单", businessType = BusinessType.DELETE)
    @GetMapping("/make/remove/{id}")
    public AjaxResult makeRemove(@PathVariable Long id) {
        return statementService.makeRemove(id);
    }

    /**
     * 结算单模板下载
     */
    @PreAuthorize("@ss.hasPermi('work:statement:make')")
    @Log(title = "下载结算单模板", businessType = BusinessType.DOWNLOAD)
    @PostMapping("/make/download")
    public void download(HttpServletResponse response) {
        ExcelUtil<StatementDetail> util = new ExcelUtil<StatementDetail>(StatementDetail.class);
        util.importTemplateExcel(response, "结算单");
    }
}
