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.BMainHazardousWasteOutput;
import cn.cecep.talroad.service.IBMainHazardousWasteOutputService;
import cn.cecep.talroad.vo.BMainHazardousWasteOutputVo;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.query.BMainHazardousWasteOutputQuery;
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.jsonwebtoken.lang.Assert;
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;


/**
 * * 开发公司：中节能天融科技有限公司
 * 危废产生与处理Controller 控制层
 *
 * @author szhpt
 * &#064;date  2023-01-10 */
@Api(tags = "基本档案--危废产生与处理")
@RestController
@RequestMapping("/bMainHazardousWasteOutput")
public class BMainHazardousWasteOutputController extends BasesController {
    @Autowired
    private IBMainHazardousWasteOutputService bMainHazardousWasteOutputService;

    /**
     * 查询危废产生与处理列表
     */
    @ApiOperation(value = "查询危废产生与处理")
    @GetMapping("/list")
    public AjaxResults<IPage<BMainHazardousWasteOutput>> list(BMainHazardousWasteOutputQuery bMainHazardousWasteOutput) {
        if (null == bMainHazardousWasteOutput.getPageNum()) {
            bMainHazardousWasteOutput.setPageNum(1);
        }
        if (null == bMainHazardousWasteOutput.getPageSize()) {
            bMainHazardousWasteOutput.setPageSize(10);
        }
        Page<BMainHazardousWasteOutput> page = new Page<>(bMainHazardousWasteOutput.getPageNum(), bMainHazardousWasteOutput.getPageSize());
        IPage<BMainHazardousWasteOutput> list = bMainHazardousWasteOutputService.page(page, BMainHazardousWasteOutputQuery.createLambdaQueryWrapper(bMainHazardousWasteOutput));
        return AjaxResults.success(list);
    }

    @ApiOperation(value = "查询危废产生与处理曲线图数据")
    @GetMapping("/charData")
    public AjaxResults<EChartsVO> getCharData(BMainHazardousWasteOutputQuery bMainHazardousWasteOutput) {
        Assert.notNull(bMainHazardousWasteOutput.getStartDate(), "开始年月不能为空!");
        Assert.notNull(bMainHazardousWasteOutput.getEndDate(), "结束年月不能为空!");
        EChartsVO charData = bMainHazardousWasteOutputService.getCharData(bMainHazardousWasteOutput);
        return AjaxResults.success(charData);
    }

    @ApiOperation(value = "app端-查询危废产生与处理曲线图数据")
    @GetMapping("/appCharData")
    public AjaxResults<EChartsVO> getAppCharData(BMainHazardousWasteOutputQuery bMainHazardousWasteOutput) {
        Assert.notNull(bMainHazardousWasteOutput.getStartDate(), "开始年月不能为空!");
        Assert.notNull(bMainHazardousWasteOutput.getEndDate(), "结束年月不能为空!");
        EChartsVO charData = bMainHazardousWasteOutputService.getAppCharData(bMainHazardousWasteOutput);
        return AjaxResults.success(charData);
    }


    /**
     * 导出危废产生与处理列表
     */
    @ApiOperation(value = "导出危废产生与处理")
    @Log(title = "危废产生与处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody BMainHazardousWasteOutputQuery bMainHazardousWasteOutput) {
        List<BMainHazardousWasteOutput> list = bMainHazardousWasteOutputService.list(BMainHazardousWasteOutputQuery.createLambdaQueryWrapper(bMainHazardousWasteOutput));
        ExcelUtil<BMainHazardousWasteOutput> util = new ExcelUtil<BMainHazardousWasteOutput>(BMainHazardousWasteOutput.class);
        util.exportExcel(response, list, "危废产生与处理数据");
    }

    /**
     * 获取危废产生与处理详细信息
     */
    @ApiOperation(value = "获取危废产生与处理详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<BMainHazardousWasteOutput> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(bMainHazardousWasteOutputService.getById(id));
    }

    /**
     * 新增危废产生与处理
     */
    @ApiOperation(value = "新增危废产生与处理")
    @Log(title = "危废产生与处理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody BMainHazardousWasteOutput bMainHazardousWasteOutput) {
        return toAjax(bMainHazardousWasteOutputService.save(bMainHazardousWasteOutput));
    }

    /**
     * 修改危废产生与处理
     */
    @ApiOperation(value = "修改危废产生与处理")
    @Log(title = "危废产生与处理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody BMainHazardousWasteOutputVo bMainHazardousWasteOutputVo) {
        if (ObjectUtil.isEmpty(bMainHazardousWasteOutputVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(bMainHazardousWasteOutputVo.getId())) {
            throw new InvalidParameterException("参数id为空");
        }
        BMainHazardousWasteOutput bMainHazardousWasteOutput = bMainHazardousWasteOutputService.getById(bMainHazardousWasteOutputVo.getId());
        if (ObjectUtil.isEmpty(bMainHazardousWasteOutput)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", bMainHazardousWasteOutputVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(bMainHazardousWasteOutputVo, bMainHazardousWasteOutput);
        return toAjax(bMainHazardousWasteOutputService.saveOrUpdate(bMainHazardousWasteOutput));
    }

    /**
     * 删除危废产生与处理
     */
    @ApiOperation(value = "删除危废产生与处理")
    @RequiresPermissions("talroad:output:remove")
    @Log(title = "危废产生与处理", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(bMainHazardousWasteOutputService.removeByIds(Arrays.asList(ids)));
    }
}
