package com.goldenwater.slaj.hidd.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import jakarta.servlet.http.HttpServletResponse;
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.common.utils.poi.ExcelUtil;
import com.goldenwater.slaj.hidd.domain.BisMajHiddSup;
import com.goldenwater.slaj.hidd.domain.BisMajHiddSupAddVo;
import com.goldenwater.slaj.hidd.domain.BisMajHiddSupVo;
import com.goldenwater.slaj.hidd.domain.ObjHidd;
import com.goldenwater.slaj.hidd.domain.RelBisMed;
import com.goldenwater.slaj.hidd.service.IBisMajHiddSupService;
import com.goldenwater.slaj.hidd.service.IObjHiddService;
import com.goldenwater.slaj.hidd.service.IRelBisMedService;
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 IRelBisMedService relBisMedService;

    @Autowired
    private AcciNotifyService acciNotifyService;

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

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

    @PreAuthorize("@ss.hasPermi('hidd:majhiddsup:export')")
    @Log(title = "隐患督办", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BisMajHiddSupVo bisMajHiddSup, HttpServletResponse response) {
        List<BisMajHiddSupVo> list = bisMajHiddSupService.selectBisMajHiddSupList(bisMajHiddSup);
        ExcelUtil<BisMajHiddSupVo> util = new ExcelUtil<BisMajHiddSupVo>(BisMajHiddSupVo.class);
        util.exportExcel(response, list, "隐患督办数据");
    }

    @Log(title = "隐患督办", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('hidd:majhiddsup:add')")
    @PostMapping
    public AjaxResult add(@RequestBody BisMajHiddSupAddVo bisMajHiddSup) {
        bisMajHiddSup.setSupDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        
        BisMajHiddSup sup = new BisMajHiddSup();
        sup.setGuid(bisMajHiddSup.getGuid());
        sup.setHiddGuid(bisMajHiddSup.getHiddGuid());
        sup.setIsList(bisMajHiddSup.getIsList());
        sup.setRectPeri(bisMajHiddSup.getRectPeri());
        sup.setSupLareId(bisMajHiddSup.getSupLareId());
        sup.setSupDate(bisMajHiddSup.getSupDate());
        sup.setSupWiunCode(bisMajHiddSup.getSupWiunCode());
        sup.setSupLegPers(bisMajHiddSup.getSupLegPers());
        sup.setSupOpin(bisMajHiddSup.getSupOpin());
        sup.setNote(bisMajHiddSup.getNote());
        sup.setCollTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        sup.setUpdTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        sup.setRecPers(bisMajHiddSup.getRecPers());
        
        int result = bisMajHiddSupService.insertBisMajHiddSup(sup);

        // 保存附件关联关系
        if (result > 0 && bisMajHiddSup.getMedGuid() != null && !bisMajHiddSup.getMedGuid().isEmpty()) {
            RelBisMed relBisMed = new RelBisMed();
            relBisMed.setGuid(UUID.randomUUID().toString().replace("-", ""));
            relBisMed.setBisGuid(sup.getGuid());
            relBisMed.setBisTablename("BIS_MAJ_HIDD_SUP");
            relBisMed.setMedGuid(bisMajHiddSup.getMedGuid());
            relBisMed.setFromDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            relBisMedService.insertRelBisMed(relBisMed);
        }

        // 发送通知
        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) {
        return toAjax(bisMajHiddSupService.updateBisMajHiddSup(bisMajHiddSup));
    }

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