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.FactoryReportingManifestQuery;

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.FactoryReportingManifest;
import cn.cecep.talroad.vo.FactoryReportingManifestVo;
import cn.cecep.talroad.service.IFactoryReportingManifestService;


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

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

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

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

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