package com.bonus.material.ma.controller;

import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.ma.domain.*;
import com.bonus.material.ma.service.DirectRotationService;
import com.bonus.material.settlement.domain.SltAgreementInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
* @description  直转申请
* @author hay
* @date   2025/2/17 19:27
*/
@Api(tags = " 直转申请")
@RestController
@RequestMapping("/directRotation")
public class DirectRotationController extends BaseController {

    @Resource(name = "directRotationService")
    private DirectRotationService service;

    /**
     * 根据协议Id查询在用数据
     */
    @ApiOperation(value = "根据协议Id查询在用数据")
    @GetMapping("/getUseringData")
    public AjaxResult getUseringData(SltAgreementInfo sltAgreementInfo) {
        try {
            List<SltAgreementInfo> useringData = service.getUseringData(sltAgreementInfo);
            return AjaxResult.success(useringData);
        } catch (Exception e) {
            return AjaxResult.success(new ArrayList<>());
        }
    }

    /**
     * 根据协议Id查询在用数据--类型名称下拉,规格型号下拉，在用数据列表
     * @param bean
     * @return
     */
    @ApiOperation(value = "标准配置下拉选")
    @PostMapping("/getTypeNameList")
    public AjaxResult getTypeNameList(@RequestBody SltAgreementInfo bean){
        return service.getTypeNameList(bean);
    }


    /**
     * 新增直转申请
     */
    @ApiOperation(value = "新增直转申请")
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody DirectApplyInfoDetails directApplyInfoDetails) {
        try {
            return service.submit(directApplyInfoDetails);
        } catch (Exception e) {
            System.err.println("保存失败:" + e.getMessage());
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 直转提交
     */
    @ApiOperation(value = "直转提交")
    @PostMapping("/submitNew")
    public AjaxResult submitNew(@RequestBody DirectApplyInfoDetails directApplyInfoDetails) {
        return service.submitNew(directApplyInfoDetails);
    }

    /**
     * 修改直转申请
     */
    @ApiOperation(value = "修改直转申请")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody DirectApplyInfoDetails directApplyInfoDetails) {
        try {
            return service.edit(directApplyInfoDetails);
        } catch (Exception e) {
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 直转记录查询列表
     */
    @ApiOperation(value = "直转记录查询列表")
    @GetMapping("/list")
    public TableDataInfo list(DirectApplyInfo directApplyInfo) {
        startPage();
        try {
            List<DirectApplyExportInfo> directApplyInfos = service.getList(directApplyInfo);
            return getDataTable(directApplyInfos);
        } catch (Exception e) {
            return getDataTable(new ArrayList<>());
        }
    }

    /**
     * 导出直转记录查询列表
     * @param response
     * @param directApplyInfo
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, DirectApplyInfo directApplyInfo) {
        List<DirectApplyExportInfo> list = service.getList(directApplyInfo);
        ExcelUtil<DirectApplyExportInfo> util = new ExcelUtil<>(DirectApplyExportInfo.class);
        util.exportExcel(response, list, "直转记录数据");
    }

    /**
     * 直转记录查询列表--app
     */
    @ApiOperation(value = "直转记录查询列表")
    @GetMapping("/appList")
    public AjaxResult appList(DirectApplyInfo directApplyInfo) {
        try {
            List<DirectApplyExportInfo> directApplyInfos = service.getList(directApplyInfo);
            return AjaxResult.success(directApplyInfos);
        } catch (Exception e) {
            return AjaxResult.success(new ArrayList<>());
        }
    }

    @ApiOperation(value = "查看详情")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(SltAgreementInfo sltAgreementInfo) {
        try {
            return AjaxResult.success(service.getInfoById(sltAgreementInfo));
        } catch (Exception e) {
            return AjaxResult.success(new DirectApplyInfo());
        }
    }

    /**
     * 用于APP
     */
    @ApiOperation(value = "查看详情")
    @GetMapping("/getInfoApp")
    public AjaxResult getInfoApp(SltAgreementInfo sltAgreementInfo) {
        try {
            return AjaxResult.success(service.getInfoApp(sltAgreementInfo));
        } catch (Exception e) {
            return AjaxResult.success(new DirectApplyInfo());
        }
    }

    /**
     * 删除直转申请
     */
    @ApiOperation(value = "删除直转申请")
    @PreventRepeatSubmit
    @SysLog(title = "直转记录查询", businessType = OperaType.DELETE, logType = 1,module = "直转记录查询->删除直转申请")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(service.delData(id));
    }


}

