package com.ruoyi.jjh.ent.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.poi.ProjectExcelUtil;
import com.ruoyi.common.utils.poi.ProjectValueUtil;
import com.ruoyi.jjh.ent.entity.JProject;
import com.ruoyi.jjh.ent.entity.request.JProjectExcel;
import com.ruoyi.jjh.ent.service.JProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * 项目表(JProject)表控制层
 *
 * @author makejava
 * @since 2024-03-25 11:49:34
 */
@RestController
@RequestMapping("/jjh/jProject")
@Api(tags = "项目表")
public class JProjectController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private JProjectService jProjectService;

    /**
     * 分页查询项目
     *
     * @param page     分页对象
     * @param jProject 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询项目", response = JProject.class)
    @GetMapping
    public AjaxResult selectAll(Page<JProject> page, JProject jProject) {
        return success(jProjectService.page(page,jProject));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条项目", response = JProject.class)
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(this.jProjectService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param jProject 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增项目")
    @PostMapping
    public AjaxResult insert(@RequestBody JProject jProject) {
        return success(this.jProjectService.save(jProject));
    }

    /**
     * 修改数据
     *
     * @param jProject 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改项目")
    @PutMapping
    public AjaxResult update(@RequestBody JProject jProject) {
        return success(this.jProjectService.updateById(jProject));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @ApiOperation(value = "删除项目")
    @DeleteMapping
    public AjaxResult delete(@RequestParam("id") Long id) {
        return success(this.jProjectService.removeById(id));
    }

    /**
     * 导入已有的excel项目数据
     *
     */
    @ApiOperation(value = "导入已有的excel项目数据")
    @PostMapping(value = "/export",consumes ="multipart/form-data")
    public AjaxResult export(@RequestPart("file") MultipartFile file) throws Exception {
        ProjectExcelUtil<JProject> util = new ProjectExcelUtil<>(JProject.class);
        List<JProject> proList = util.importExcel(file.getInputStream());
        StringBuilder successMsg = new StringBuilder();
        if(proList == null &&  proList.isEmpty()){
            throw new ServiceException("项目导入数据不能为空");
        }else {
            jProjectService.saveBatch(proList);
            successMsg.append("导入成功");
        }
        return AjaxResult.success(successMsg);
    }

    /**
     * 导入下载的excel模板和参数项目数据
     *
     */
    @ApiOperation(value = "导入下载的excel模板和参数项目数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="projectClassify",value="项目分类",required=true),
            @ApiImplicitParam(name="projectYear",value="年份",required=true),
    })
    @PostMapping(value = "/importTemplateProject",consumes ="multipart/form-data")
    public AjaxResult importTemplateProject(@RequestPart("file") MultipartFile file,
                                            @RequestParam("projectClassify")String projectClassify,
                                            @RequestParam("projectYear")String projectYear) throws Exception {
        ProjectValueUtil<JProject> util = new ProjectValueUtil<>(JProject.class);
        List<JProject> proList = util.importExcel(file.getInputStream(),projectClassify,projectYear);
        StringBuilder successMsg = new StringBuilder();
        if(proList == null &&  proList.isEmpty()){
            throw new ServiceException("项目导入数据不能为空");
        }else {
            jProjectService.saveBatch(proList);
            successMsg.append("导入成功");
        }
        return AjaxResult.success(successMsg);
    }

    /**
     * 下载项目模板
     *
     */
    @ApiOperation(value = "下载项目模板")
    @PostMapping("/updateProjectTemplate")
    public void updateProjectTemplate(HttpServletResponse response)
    {
        ExcelUtil<JProjectExcel> util = new ExcelUtil<>(JProjectExcel.class);
        util.importTemplateExcel(response, "项目模板");
    }
}

