package com.goldenwater.slaj.hidd.controller;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.slaj.hidd.domain.BisOrgMonRepPeri;
import com.goldenwater.slaj.hidd.service.IBisOrgMonRepPeriService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.enums.BusinessType;
import com.goldenwater.common.utils.poi.ExcelUtil;

@RestController
@RequestMapping("/hidd/monrepperi")
public class BisOrgMonRepPeriController extends BaseController {
    @Autowired
    private IBisOrgMonRepPeriService bisOrgMonRepPeriService;

    @GetMapping("/list")
    public TableDataInfo list(BisOrgMonRepPeri bisOrgMonRepPeri) {
        startPage();
        List<BisOrgMonRepPeri> list = bisOrgMonRepPeriService.selectBisOrgMonRepPeriList(bisOrgMonRepPeri);
        return getDataTable(list);
    }

    @GetMapping("/{guid}")
    public AjaxResult getInfo(@PathVariable String guid) {
        return AjaxResult.success(bisOrgMonRepPeriService.selectBisOrgMonRepPeriByGuid(guid));
    }

    @PostMapping
    public AjaxResult add(@RequestBody BisOrgMonRepPeri bisOrgMonRepPeri) {
        return toAjax(bisOrgMonRepPeriService.insertBisOrgMonRepPeri(bisOrgMonRepPeri));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody BisOrgMonRepPeri bisOrgMonRepPeri) {
        return toAjax(bisOrgMonRepPeriService.updateBisOrgMonRepPeri(bisOrgMonRepPeri));
    }

    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(bisOrgMonRepPeriService.deleteBisOrgMonRepPeriByGuids(guids));
    }

    @Log(title = "月报周期管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('hidd:monrepperi:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BisOrgMonRepPeri bisOrgMonRepPeri) {
        List<BisOrgMonRepPeri> list = bisOrgMonRepPeriService.selectBisOrgMonRepPeriList(bisOrgMonRepPeri);
        ExcelUtil<BisOrgMonRepPeri> util = new ExcelUtil<>(BisOrgMonRepPeri.class);
        util.exportExcel(response, list, "月报周期数据");
    }

    @GetMapping("/monthTotalList")
    public AjaxResult monthTotalList(@RequestParam(required = false) String repTime) {
        Map<String, Object> params = new java.util.HashMap<>();
        params.put("repTime", repTime);
        List<Map<String, Object>> list = bisOrgMonRepPeriService.selectMonthTotalList(params);
        return AjaxResult.success(list);
    }

    @Log(title = "月报上报", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('hidd:monrepperi:edit')")
    @PostMapping("/submit")
    public AjaxResult submit(@RequestParam String guid, @RequestParam(required = false) String hiddGuids) {
        try {
            String[] ids = org.springframework.util.StringUtils.hasText(hiddGuids) ? hiddGuids.split(",") : new String[0];
            bisOrgMonRepPeriService.submitMonth(guid, ids);
            return AjaxResult.success("月报上报成功");
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Log(title = "月报退回", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('hidd:monrepperi:edit')")
    @PutMapping("/return")
    public AjaxResult returnMonth(@RequestParam String guid, @RequestParam String returnDesc) {
        return toAjax(bisOrgMonRepPeriService.returnMonth(guid, returnDesc));
    }

    @Log(title = "月报撤销", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('hidd:monrepperi:edit')")
    @PutMapping("/revoke")
    public AjaxResult revoke(@RequestParam String guid) {
        return toAjax(bisOrgMonRepPeriService.revokeMonth(guid));
    }

    @Log(title = "月报撤销审核", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('hidd:monrepperi:edit')")
    @PutMapping("/approveRevoke")
    public AjaxResult approveRevoke(@RequestParam String guid, @RequestParam boolean approved,
                                    @RequestParam(required = false) String opinion) {
        return toAjax(bisOrgMonRepPeriService.approveRevoke(guid, approved, opinion));
    }

    @Log(title = "月报批量解锁", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('hidd:monrepperi:edit')")
    @PutMapping("/batchUnlock")
    public AjaxResult batchUnlock(@RequestParam String[] guids) {
        return toAjax(bisOrgMonRepPeriService.batchUnlock(guids));
    }

    @Log(title = "督促上报", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('hidd:monrepperi:edit')")
    @PostMapping("/urge")
    public AjaxResult urge(@RequestParam String orgGuids, @RequestParam(required = false) String repName) {
        // 这里可以调用通知服务发送督促通知
        // 暂时返回成功
        return AjaxResult.success("督促通知已发送");
    }
}
