package com.goldenwater.slaj.hidd.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.common.enums.BusinessType;
import com.goldenwater.slaj.hidd.domain.BisMajHiddSup;
import com.goldenwater.slaj.hidd.domain.ObjHidd;
import com.goldenwater.slaj.hidd.service.IBisMajHiddSupService;
import com.goldenwater.slaj.hidd.service.IObjHiddService;
import com.goldenwater.slaj.acci.service.impl.AcciNotifyService;

@RestController
@RequestMapping("/hidd/majhiddsup")
public class BisMajHiddSupController extends BaseController {
    @Autowired
    private IBisMajHiddSupService bisMajHiddSupService;

    @Autowired
    private IObjHiddService objHiddService;

    @Autowired
    private AcciNotifyService acciNotifyService;

    @GetMapping("/list")
    public TableDataInfo list(BisMajHiddSup bisMajHiddSup) {
        startPage();
        List<BisMajHiddSup> list = bisMajHiddSupService.selectBisMajHiddSupList(bisMajHiddSup);
        return getDataTable(list);
    }

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

    @Log(title = "隐患督办", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('hidd:majhiddsup:add')")
    @PostMapping
    public AjaxResult add(@RequestBody BisMajHiddSup bisMajHiddSup) {
        bisMajHiddSup.setSupStat("1");
        int result = bisMajHiddSupService.insertBisMajHiddSup(bisMajHiddSup);
        if (result > 0 && bisMajHiddSup.getHiddGuid() != null) {
            ObjHidd hidd = objHiddService.selectObjHiddByGuid(bisMajHiddSup.getHiddGuid());
            String orgGuid = hidd != null ? hidd.getOrgGuid() : null;
            if (orgGuid != null) {
                acciNotifyService.sendNotice(orgGuid, "隐患督办通知", "您有一条隐患督办信息，请及时处理", "hidd");
            }
        }
        return toAjax(result);
    }

    @Log(title = "隐患督办", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('hidd:majhiddsup:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody BisMajHiddSup bisMajHiddSup) {
        return toAjax(bisMajHiddSupService.updateBisMajHiddSup(bisMajHiddSup));
    }

    @Log(title = "隐患督办", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('hidd:majhiddsup:remove')")
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(bisMajHiddSupService.deleteBisMajHiddSupByGuids(guids));
    }

    @Log(title = "隐患督办销号", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('hidd:majhiddsup:edit')")
    @PutMapping("/writeOff")
    public AjaxResult writeOff(@RequestBody BisMajHiddSup bisMajHiddSup) {
        bisMajHiddSup.setSupStat("3");
        return toAjax(bisMajHiddSupService.updateBisMajHiddSup(bisMajHiddSup));
    }
}
