package cn.cecep.talroad.controller.analysis;

import cn.cecep.talroad.common.core.domain.R;
import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.analysis.SRaSelfMonitoringData;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.analysis.ISRaSelfMonitoringDataService;
import cn.cecep.talroad.vo.analysis.SRaSelfMonitoringDataVo;
import cn.cecep.talroad.vo.query.analysis.SRaSelfMonitoringDataQuery;
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 java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
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.common.core.web.controller.BasesController;


/**
 * * 开发公司：中节能天融科技有限公司
 * 关联分析系统-企业自行监测数据Controller 控制层
 *
 * @author szhpt
 * @date 2023-06-27*/
@Api(tags = "关联分析系统-企业自行监测数据")
@RestController
@RequestMapping("/self/monitoring")
public class SRaSelfMonitoringDataController extends BasesController
{
    @Autowired
    private ISRaSelfMonitoringDataService sRaSelfMonitoringDataService;
    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询关联分析系统-企业自行监测数据列表
     */
    @ApiOperation(value = "查询关联分析系统-企业自行监测数据")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaSelfMonitoringData>> list(SRaSelfMonitoringDataQuery sRaSelfMonitoringData)
    {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        List<String> regionCodes=new ArrayList<>();
        R<List<String>> r = baseRegionService.getRegionCodeListChild(regionCode);
        if(r.getCode()==200){
            regionCodes=r.getData();
        }
        sRaSelfMonitoringData.setRegionCodes(regionCodes);
        Page<SRaSelfMonitoringData> page = new Page<>(sRaSelfMonitoringData.getPageNum(), sRaSelfMonitoringData.getPageSize());
        IPage<SRaSelfMonitoringData> list = sRaSelfMonitoringDataService.page(page, SRaSelfMonitoringDataQuery.createLambdaQueryWrapper(sRaSelfMonitoringData));
        return AjaxResults.success(list);
    }
    

    /**
     * 导出关联分析系统-企业自行监测数据列表
     */
    @ApiOperation(value = "导出关联分析系统-企业自行监测数据",produces = "application/octet-stream")
    @Log(title = "关联分析系统-企业自行监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaSelfMonitoringDataQuery sRaSelfMonitoringData)
    {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        List<String> regionCodes=new ArrayList<>();
        R<List<String>> r = baseRegionService.getRegionCodeListChild(regionCode);
        if(r.getCode()==200){
            regionCodes=r.getData();
        }
        sRaSelfMonitoringData.setRegionCodes(regionCodes);
        List<SRaSelfMonitoringData> list = sRaSelfMonitoringDataService.selectSRaSelfMonitoringDataList(sRaSelfMonitoringData);
        ExcelUtil<SRaSelfMonitoringData> util = new ExcelUtil<SRaSelfMonitoringData>(SRaSelfMonitoringData.class);
        util.exportExcel(response, list, "关联分析系统-企业自行监测数据数据");
    }
    /**
     * 污染物名称options
     */
    @ApiOperation(value = "污染物名称options")
    @GetMapping("/pullPollutantNameOptions")
    public AjaxResults<List<Map<String, Object>>> pullPollutantNameOptions(@RequestParam(value = "name", required = false) String name) {
        return AjaxResults.success(sRaSelfMonitoringDataService.pullPollutantNameOptions(name));
    }
    /**
     * 获取关联分析系统-企业自行监测数据详细信息
     */
    @ApiOperation(value = "获取关联分析系统-企业自行监测数据详细信息")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaSelfMonitoringData> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(sRaSelfMonitoringDataService.getById(id));
    }
    /**
     * 新增关联分析系统-企业自行监测数据
     */
    @ApiOperation(value = "新增关联分析系统-企业自行监测数据")
    @Log(title = "关联分析系统-企业自行监测数据", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaSelfMonitoringData sRaSelfMonitoringData) {
        return toAjax(sRaSelfMonitoringDataService.save(sRaSelfMonitoringData));
    }
    /**
     * 修改关联分析系统-企业自行监测数据
     */
    @ApiOperation(value = "修改关联分析系统-企业自行监测数据")
    @Log(title = "关联分析系统-企业自行监测数据", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaSelfMonitoringDataVo sRaSelfMonitoringDataVo) {
        if(ObjectUtil.isEmpty(sRaSelfMonitoringDataVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(sRaSelfMonitoringDataVo.getDataId())){
            throw new InvalidParameterException("参数id为空");
        }
        SRaSelfMonitoringData sRaSelfMonitoringData = sRaSelfMonitoringDataService.getById(sRaSelfMonitoringDataVo.getDataId());
        if(ObjectUtil.isEmpty(sRaSelfMonitoringData)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaSelfMonitoringDataVo.getDataId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaSelfMonitoringDataVo,sRaSelfMonitoringData);
        return toAjax(sRaSelfMonitoringDataService.saveOrUpdate(sRaSelfMonitoringData));
    }

    /**
     * 删除关联分析系统-企业自行监测数据
     */
    @ApiOperation(value = "删除关联分析系统-企业自行监测数据")
    @RequiresPermissions("talroad:data:remove")
    @Log(title = "关联分析系统-企业自行监测数据", businessType = BusinessType.DELETE)
    @PostMapping("/{dataIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] dataIds) {
        return toAjax(sRaSelfMonitoringDataService.removeByIds(Arrays.asList(dataIds)));
    }
}
