package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.domain.BasParam;
import cn.cecep.talroad.service.IBasParamService;
import cn.cecep.talroad.vo.BasParamVo;
import cn.cecep.talroad.vo.query.BasParamQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * * 开发公司：中节能天融科技有限公司
 * 监测参数汇总(有组织)Controller 控制层
 *
 * @author szhpt
 * @date 2023-01-10*/
@Api(tags = "基础信息--监测参数汇总(有组织)")
@RestController
@RequestMapping("/basParam")
public class BasParamController extends BasesController
{
    @Autowired
    private IBasParamService basParamService;

    /**
     * 查询监测参数汇总(有组织)列表
     */
    @ApiOperation(value = "查询监测参数汇总(有组织)")
    @GetMapping("/list")
    public AjaxResults<IPage<BasParam>> list(BasParamQuery basParam)
    {
        Page<BasParam> page = new Page<>(basParam.getPageNum(), basParam.getPageSize());
        IPage<BasParam> list = basParamService.page(page, BasParamQuery.createLambdaQueryWrapper(basParam));
        return AjaxResults.success(list);
    }
    

    /**
     * 导出监测参数汇总(有组织)列表
     */
    @ApiOperation(value = "导出监测参数汇总(有组织)")
    @Log(title = "监测参数汇总(有组织)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasParam basParam)
    {
        List<BasParam> list = basParamService.selectBasParamList(basParam);
        ExcelUtil<BasParam> util = new ExcelUtil<BasParam>(BasParam.class);
        util.exportExcel(response, list, "监测参数汇总(有组织)数据");
    }

    /**
     * 获取监测参数汇总(有组织)详细信息
     */
    @ApiOperation(value = "获取监测参数汇总(有组织)详细信息")
    @GetMapping(value = "/{paramId}")
    public AjaxResults<BasParam> getInfo(@PathVariable("id") String id) {
        return AjaxResults.success(basParamService.getById(id));
    }
    /**
     * 新增监测参数汇总(有组织)
     */
    @ApiOperation(value = "新增监测参数汇总(有组织)")
    @Log(title = "监测参数汇总(有组织)", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody BasParam basParam) {
        return toAjax(basParamService.save(basParam));
    }
    /**
     * 修改监测参数汇总(有组织)
     */
    @ApiOperation(value = "修改监测参数汇总(有组织)")
    @Log(title = "监测参数汇总(有组织)", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody BasParamVo basParamVo) {
        if(ObjectUtil.isEmpty(basParamVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(basParamVo.getParamId())){
            throw new InvalidParameterException("参数id为空");
        }
        BasParam basParam = basParamService.getById(basParamVo.getParamId());
        if(ObjectUtil.isEmpty(basParam)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", basParamVo.getParamId()));
        }
        //赋值
        BeanUtil.copyProperties(basParamVo,basParam);
        return toAjax(basParamService.saveOrUpdate(basParam));
    }

    /**
     * 删除监测参数汇总(有组织)
     */
    @ApiOperation(value = "删除监测参数汇总(有组织)")
    @RequiresPermissions("talroad:param:remove")
    @Log(title = "监测参数汇总(有组织)", businessType = BusinessType.DELETE)
    @PostMapping("/{paramIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] paramIds) {
        return toAjax(basParamService.removeByIds(Arrays.asList(paramIds)));
    }

    @GetMapping("/monitoringParam")
    @ApiOperation(value = "DCS监测数据表头数据")
    public AjaxResults<Map<String, Object>>  getMonitoringParam(){
        Map<String, Object> data = basParamService.getMonitroingParam();
        return AjaxResults.success(data);
    }
}
