package com.hw.ems.base.controller;

import com.hw.common.core.utils.StringUtils;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.ems.base.domain.BaseMonitorInfo;
import com.hw.ems.base.domain.BaseTree;
import com.hw.ems.base.domain.BaseTypeRelation;
import com.hw.ems.base.service.IBaseMonitorInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

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

/**
 * 计量设备信息Controller
 *
 * @author YinQ
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/base/monitorInfo")
public class BaseMonitorInfoController extends BaseController {
    private String prefix = "base/monitorInfo";

    @Autowired
    private IBaseMonitorInfoService baseMonitorInfoService;

//    @Autowired
//    private IBaseBuildInfoService baseBuildInfoService;

    //@RequiresPermissions("base:monitorInfo:view")
    @GetMapping()
    public String monitorInfo(@RequestParam("id") int id) {
        String url = "";
        switch (id) {
            case 2:
                url = "/dnbMonitorInfo";
                break;
            case 3:
                url = "/waterMonitorInfo";
                break;
        }
        return prefix + url;
    }

    /**
     * 查询计量设备信息树列表
     */
    //@RequiresPermissions("base:monitorInfo:list")
    @PostMapping("/list")
    public List<BaseMonitorInfo> list(@RequestBody BaseMonitorInfo baseMonitorInfo) {
        List<BaseMonitorInfo> list = baseMonitorInfoService.selectBaseMonitorInfoList(baseMonitorInfo);
        return list;
    }

    /**
     * 导出计量设备信息列表
     */
    //@RequiresPermissions("base:monitorInfo:export")
    @Log(title = "计量设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseMonitorInfo baseMonitorInfo) {
        List<BaseMonitorInfo> list = baseMonitorInfoService.selectBaseMonitorInfoList(baseMonitorInfo);
        ExcelUtil<BaseMonitorInfo> util = new ExcelUtil<BaseMonitorInfo>(BaseMonitorInfo.class);
        util.exportExcel(response, list, "计量设备信息数据");
    }

    /**
     * 新增电计量设备信息
     */
    @GetMapping(value = {"/add/{objid}", "/add/"})
    public String add(@PathVariable(value = "objid", required = false) Long objid, ModelMap mmap) {
        if (StringUtils.isNotNull(objid)) {
            mmap.put("baseMonitorInfo", baseMonitorInfoService.selectBaseMonitorInfoByObjid(objid));
        }
        return prefix + "/dnbAdd";
    }

    /**
     * 新增水计量设备信息
     */
    @GetMapping(value = {"/waterAdd/{objid}", "/waterAdd/"})
    public String waterAdd(@PathVariable(value = "objid", required = false) Long objid, ModelMap mmap) {
        if (StringUtils.isNotNull(objid)) {
            mmap.put("baseMonitorInfo", baseMonitorInfoService.selectBaseMonitorInfoByObjid(objid));
        }
        return prefix + "/waterAdd";
    }

    /**
     * 新增保存计量设备信息
     */
    //@RequiresPermissions("base:monitorInfo:add")
    @Log(title = "计量设备信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(BaseMonitorInfo baseMonitorInfo) {
        return toAjax(baseMonitorInfoService.insertBaseMonitorInfo(baseMonitorInfo));
    }

    /**
     * 修改计量设备信息
     */
    //@RequiresPermissions("base:monitorInfo:edit")
    @GetMapping("/edit/{objid}")
    public String edit(@PathVariable("objid") Long objid, ModelMap mmap) {
        BaseMonitorInfo baseMonitorInfo = baseMonitorInfoService.selectBaseMonitorInfoByObjid(objid);
        mmap.put("baseMonitorInfo", baseMonitorInfo);
        return prefix + "/dnbEdit";
    }

    /**
     * 修改计量设备信息
     */
    //@RequiresPermissions("base:monitorInfo:edit")
    @GetMapping("/waterEdit/{objid}")
    public String waterEdit(@PathVariable("objid") Long objid, ModelMap mmap) {
        BaseMonitorInfo baseMonitorInfo = baseMonitorInfoService.selectBaseMonitorInfoByObjid(objid);
        mmap.put("baseMonitorInfo", baseMonitorInfo);
        return prefix + "/waterEdit";
    }

    /**
     * 修改保存计量设备信息
     */
    //@RequiresPermissions("base:monitorInfo:edit")
    @Log(title = "计量设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(BaseMonitorInfo baseMonitorInfo) {
//        List<BaseMonitorInfo> list = baseMonitorInfoService.selectBaseMonitorInfoList(new BaseMonitorInfo());
//        for (BaseMonitorInfo monitorInfo : list) {
//            baseMonitorInfoService.updateBaseMonitorInfo(monitorInfo);
//        }
//        return toAjax(1);
        return toAjax(baseMonitorInfoService.updateBaseMonitorInfo(baseMonitorInfo));
    }

    /**
     * 删除
     */
    //@RequiresPermissions("base:monitorInfo:remove")
    @Log(title = "计量设备信息", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{objid}")
    public AjaxResult remove(@PathVariable("objid") Long objid) {
        return toAjax(baseMonitorInfoService.deleteBaseMonitorInfoByObjid(objid));
    }

    /**
     * 选择计量设备信息树
     */
    @GetMapping(value = {"/selectMonitorInfoTree/{objid}", "/selectMonitorInfoTree/"})
    public String selectMonitorInfoTree(@PathVariable(value = "objid", required = false) Long objid, ModelMap mmap) {
        if (StringUtils.isNotNull(objid)) {
            mmap.put("baseMonitorInfo", baseMonitorInfoService.selectBaseMonitorInfoByObjid(objid));
        }
        return prefix + "/dnbTree";
    }

    /**
     * 选择计量设备信息树
     */
    @GetMapping(value = {"/selectMeterReadingTree/{objid}", "/selectMeterReadingTree/"})
    public String selectMeterReadingTree(@PathVariable(value = "objid", required = false) Long objid, ModelMap mmap) {
        if (StringUtils.isNotNull(objid)) {
            mmap.put("monitorType", objid);
        }
        return prefix + "/meterReadingTree";
    }


    /**
     * 选择类型信息树
     */
    @GetMapping(value = {"/selectTypeInfoTree/{type}/{objid}", "/selectTypeInfoTree/{type}"})
    public String selectTypeInfoTree(@PathVariable(value = "type", required = false) Long type, @PathVariable(value = "objid", required = false) String objid, ModelMap mmap) {
        mmap.put("typeTree", type);
        if (StringUtils.isNotNull(objid)) {
            mmap.put("typeInfoTreeList", objid);
        }
        return prefix + "/checkTree";
    }

    /**
     * 选择计量设备信息树
     */
    @GetMapping(value = {"/selectWaterMonitorInfoTree/{objid}", "/selectWaterMonitorInfoTree/"})
    public String selectWaterMonitorInfoTree(@PathVariable(value = "objid", required = false) Long objid, ModelMap mmap) {
        if (StringUtils.isNotNull(objid)) {
            mmap.put("baseMonitorInfo", baseMonitorInfoService.selectBaseMonitorInfoByObjid(objid));
        }
        return prefix + "/waterTree";
    }


    /**
     * 加载采集设备信息树列表
     */
    @GetMapping("/treeData/{monitorType}")
    public List<BaseTree> treeData(@PathVariable(value = "monitorType", required = false) Long monitorType) {
        List<BaseTree> ztrees = baseMonitorInfoService.selectBaseMonitorInfoTree(monitorType);
        return ztrees;
    }

    /**
     * 查询类型园区下第一级
     */
    @GetMapping("/selectTypeSubset/{type}")
    @ResponseBody
    public AjaxResult selectTypeSubset(@PathVariable("type") Long type)
    {
        ArrayList<BaseTypeRelation> typeRelations =  baseMonitorInfoService.selectTypeSubset(type);
        return AjaxResult.success(typeRelations);
    }
}
