package com.ruoyi.powerdistribution.controller;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.powerdistribution.domain.PdmExamineRequire;
import com.ruoyi.powerdistribution.service.IPdmExamineRequireService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.powerdistribution.domain.PdmExamineCounty;
import com.ruoyi.powerdistribution.service.IPdmExamineCountyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 四级单位（县公司）考核金额单位：元
Controller
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/powerdistribution/pdmExamineCounty")
public class PdmExamineCountyController extends BaseController
{
    @Autowired
    private IPdmExamineCountyService pdmExamineCountyService;

    @Autowired
    private IPdmExamineRequireService pdmExamineRequireService;

    /**
     * 查询四级单位（县公司）考核金额单位：元
列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PdmExamineCounty pdmExamineCounty)
    {
        startPage();
        List<PdmExamineCounty> list = pdmExamineCountyService.selectPdmExamineCountyList(pdmExamineCounty);
        return getDataTable(list);
    }

    /**
     * 导出四级单位（县公司）考核金额单位：元
列表
     */
    @Log(title = "四级单位（县公司）考核金额单位：元 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PdmExamineCounty pdmExamineCounty)
    {
        List<PdmExamineCounty> list = pdmExamineCountyService.selectPdmExamineCountyList(pdmExamineCounty);
        ExcelUtil<PdmExamineCounty> util = new ExcelUtil<PdmExamineCounty>(PdmExamineCounty.class);
        util.exportExcel(response, list, "四级单位（县公司）考核金额单位：元 数据");
    }

    /**
     * 获取四级单位（县公司）考核金额单位：元
详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pdmExamineCountyService.selectPdmExamineCountyById(id));
    }

    /**
     * 新增四级单位（县公司）考核金额单位：元

     */
    @Log(title = "四级单位（县公司）考核金额单位：元", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult save(@RequestBody PdmExamineCounty pdmExamineCounty)
    {
        if(pdmExamineCounty.getId()==null) {
            return toAjax(pdmExamineCountyService.insertPdmExamineCounty(pdmExamineCounty));
        }else {
            return toAjax(pdmExamineCountyService.updatePdmExamineCounty(pdmExamineCounty));
        }
    }
    @PostMapping("/saveBatch")
    public AjaxResult saveBatch(@RequestBody List<PdmExamineRequire> examineRequireList)
    {
        for(PdmExamineRequire pdmExamineRequire:examineRequireList){
            if(pdmExamineRequire.getChildren()!=null && pdmExamineRequire.getChildren().size()>0){
                for(PdmExamineRequire child:pdmExamineRequire.getChildren()){
                    List<PdmExamineCounty> examineCountyList=child.getExamineCountyList();
                    for (PdmExamineCounty pdmExamineCounty:examineCountyList ){
                        if(pdmExamineCounty.getId()==null) {
                          pdmExamineCountyService.insertPdmExamineCounty(pdmExamineCounty);
                        }
                        else  {pdmExamineCountyService.updatePdmExamineCounty(pdmExamineCounty);}
                    }
                }
            }
        }

        return  AjaxResult.success();
    }

    /**
     * 修改四级单位（县公司）考核金额单位：元

     */
    @Log(title = "四级单位（县公司）考核金额单位：元 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PdmExamineCounty pdmExamineCounty)
    {
        return toAjax(pdmExamineCountyService.updatePdmExamineCounty(pdmExamineCounty));
    }

    /**
     * 删除四级单位（县公司）考核金额单位：元

     */
    @Log(title = "四级单位（县公司）考核金额单位：元 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pdmExamineCountyService.deletePdmExamineCountyByIds(ids));
    }
    @GetMapping(value = "/getExamineCountyInfo")
    public AjaxResult getExamineCountyInfo()
    {
        return success(pdmExamineRequireService.selectExamineRequireTree());
    }
}
