package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.domain.R;
import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.cecep.talroad.vo.query.EarlyWarningQuery;

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 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;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.domain.EarlyWarning;
import cn.cecep.talroad.vo.EarlyWarningVo;
import cn.cecep.talroad.service.IEarlyWarningService;


/**
 * * 开发公司：中节能天融科技有限公司
 * 管控浓度超标预警Controller 控制层
 *
 * @author szhpt
 * @date 2023-04-24*/
@Api(tags = "管控浓度超标预警")
@RestController
@RequestMapping("/earlyWarning")
public class EarlyWarningController extends BasesController
{
    @Autowired
    private IEarlyWarningService earlyWarningService;

    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询管控浓度超标预警列表
     */
    @ApiOperation(value = "查询管控浓度超标预警")
    @GetMapping("/list")
    public AjaxResults<IPage<EarlyWarning>> list(EarlyWarningQuery earlyWarning)
    {
        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();
        }
        earlyWarning.setRegionCodes(regionCodes);
        Page<EarlyWarning> page = new Page<>(earlyWarning.getPageNum(), earlyWarning.getPageSize());
        IPage<EarlyWarning> list = earlyWarningService.page(page, EarlyWarningQuery.createLambdaQueryWrapper(earlyWarning));
        return AjaxResults.success(list);
    }
    

    /**
     * 导出管控浓度超标预警列表
     */
    @ApiOperation(value = "导出管控浓度超标预警")
    @Log(title = "管控浓度超标预警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody EarlyWarningQuery earlyWarning)
    {
        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();
        }
        earlyWarning.setRegionCodes(regionCodes);
        List<EarlyWarning> list = earlyWarningService.selectEarlyWarningList(earlyWarning);
        ExcelUtil<EarlyWarning> util = new ExcelUtil<EarlyWarning>(EarlyWarning.class);
        util.exportExcel(response, list, "管控浓度超标预警数据");
    }

    /**
     * 获取管控浓度超标预警详细信息
     */
    @ApiOperation(value = "获取管控浓度超标预警详细信息")
    @PostMapping(value = "/{ewId}")
    public AjaxResults<EarlyWarning> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(earlyWarningService.getById(id));
    }
    /**
     * 新增管控浓度超标预警
     */
    @ApiOperation(value = "新增管控浓度超标预警")
    @Log(title = "管控浓度超标预警", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody EarlyWarning earlyWarning) {
        return toAjax(earlyWarningService.save(earlyWarning));
    }
    /**
     * 修改管控浓度超标预警
     */
    @ApiOperation(value = "修改管控浓度超标预警")
    @Log(title = "管控浓度超标预警", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody EarlyWarningVo earlyWarningVo) {
        if(ObjectUtil.isEmpty(earlyWarningVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(earlyWarningVo.getEwId())){
            throw new InvalidParameterException("参数id为空");
        }
        EarlyWarning earlyWarning = earlyWarningService.getById(earlyWarningVo.getEwId());
        if(ObjectUtil.isEmpty(earlyWarning)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", earlyWarningVo.getEwId()));
        }
        //赋值
        BeanUtil.copyProperties(earlyWarningVo,earlyWarning);
        return toAjax(earlyWarningService.saveOrUpdate(earlyWarning));
    }

    /**
     * 删除管控浓度超标预警
     */
    @ApiOperation(value = "删除管控浓度超标预警")
    @RequiresPermissions("talroad:warning:remove")
    @Log(title = "管控浓度超标预警", businessType = BusinessType.DELETE)
    @PostMapping("/{ewIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ewIds) {
        return toAjax(earlyWarningService.removeByIds(Arrays.asList(ewIds)));
    }
}
