package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.cecep.talroad.vo.query.FactoryReportingTaskQuery;

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.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
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.FactoryReportingTask;
import cn.cecep.talroad.vo.FactoryReportingTaskVo;
import cn.cecep.talroad.service.IFactoryReportingTaskService;


/**
 * * 开发公司：中节能天融科技有限公司
 * 企业信息上报任务Controller 控制层
 *
 * @author szhpt
 * @date 2023-05-09*/
@Api(tags = "企业信息上报任务")
@RestController
@RequestMapping("/reportingTask")
public class FactoryReportingTaskController extends BasesController
{
    @Autowired
    private IFactoryReportingTaskService factoryReportingTaskService;

    /**
     * 查询企业信息上报任务列表
     */
    @ApiOperation(value = "查询企业信息上报任务")
    @GetMapping("/list")
    public AjaxResults<IPage<FactoryReportingTask>> list(FactoryReportingTaskQuery factoryReportingTask)
    {
        Page<FactoryReportingTask> page = new Page<>(factoryReportingTask.getPageNum(), factoryReportingTask.getPageSize());
        IPage<FactoryReportingTask> list = factoryReportingTaskService.page(page, FactoryReportingTaskQuery.createLambdaQueryWrapper(factoryReportingTask));
        return AjaxResults.success(list);
    }
    

    /**
     * 导出企业信息上报任务列表
     */
    @ApiOperation(value = "导出企业信息上报任务")
    @Log(title = "企业信息上报任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FactoryReportingTask factoryReportingTask)
    {
        List<FactoryReportingTask> list = factoryReportingTaskService.selectFactoryReportingTaskList(factoryReportingTask);
        ExcelUtil<FactoryReportingTask> util = new ExcelUtil<FactoryReportingTask>(FactoryReportingTask.class);
        util.exportExcel(response, list, "企业信息上报任务数据");
    }

    /**
     * 获取企业信息上报任务详细信息
     */
    @ApiOperation(value = "获取企业信息上报任务详细信息")
    @PostMapping(value = "/{frId}")
    public AjaxResults<FactoryReportingTask> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(factoryReportingTaskService.getById(id));
    }
    /**
     * 新增企业信息上报任务
     */
    @ApiOperation(value = "新增企业信息上报任务")
    @Log(title = "企业信息上报任务", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody FactoryReportingTask factoryReportingTask) {
        return toAjax(factoryReportingTaskService.save(factoryReportingTask));
    }
    /**
     * 修改企业信息上报任务
     */
    @ApiOperation(value = "修改企业信息上报任务")
    @Log(title = "企业信息上报任务", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody FactoryReportingTaskVo factoryReportingTaskVo) {
        if(ObjectUtil.isEmpty(factoryReportingTaskVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(factoryReportingTaskVo.getFrId())){
            throw new InvalidParameterException("参数id为空");
        }
        FactoryReportingTask factoryReportingTask = factoryReportingTaskService.getById(factoryReportingTaskVo.getFrId());
        if(ObjectUtil.isEmpty(factoryReportingTask)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", factoryReportingTaskVo.getFrId()));
        }
        //赋值S
        BeanUtil.copyProperties(factoryReportingTaskVo,factoryReportingTask);
        return toAjax(factoryReportingTaskService.saveOrUpdate(factoryReportingTask));
    }

    /**
     * 删除企业信息上报任务
     */
    @ApiOperation(value = "删除企业信息上报任务")
    @RequiresPermissions("talroad:task:remove")
    @Log(title = "企业信息上报任务", businessType = BusinessType.DELETE)
    @PostMapping("/{frIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] frIds) {
        return toAjax(factoryReportingTaskService.removeByIds(Arrays.asList(frIds)));
    }
    @ApiOperation(value = "企业信息上报任务清单")
    @GetMapping("informationReporting")
    public void informationReporting(){
        factoryReportingTaskService.informationReporting();
    }
}
