package com.ruoyi.gysl.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.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gysl.entity.Enterprise;
import com.ruoyi.gysl.entity.request.ZwIdPageReq;
import com.ruoyi.gysl.service.EnterpriseService;
import io.swagger.annotations.Api;
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.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 入驻企业基本信息(Enterprise)表控制层
 *
 * @author makejava
 * @since 2025-02-24 11:26:49
 */

@Api(tags = "入驻企业")
@RestController
@RequestMapping("/gysl/enterprise")
public class EnterpriseController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private EnterpriseService enterpriseService;

    /**
     * 根据项目id分页查询入驻企业
     *
     * @param page       分页对象
     * @param enterprise 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    @ApiOperation(value = "根据项目id分页查询入驻企业", response = Enterprise.class)
    public AjaxResult selectAll(Page<Enterprise> page, ZwIdPageReq enterprise) {
        return success(enterpriseService.page(page, enterprise));
    }

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


    /**
     * 修改数据
     *
     * @param enterprise 实体对象
     * @return 修改结果
     */
    @PostMapping("/edit")
    @ApiOperation("修改数据")
    public AjaxResult update(@RequestBody Enterprise enterprise) {
        return success(enterpriseService.updateById(enterprise));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation("删除数据")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return success(enterpriseService.removeByIds(idList));
    }

    /**
     * 导入入驻企业信息
     */
    @ApiOperation(value = "导入入驻企业信息")
    @PostMapping(value = "/importEnterprise", consumes = "multipart/form-data")
    public AjaxResult importTemplateProject(@RequestPart("file") MultipartFile file, @RequestParam Long xmId) throws Exception {
        ExcelUtil<Enterprise> util = new ExcelUtil<>(Enterprise.class);
        List<Enterprise> proList = util.importExcel(file.getInputStream());
        StringBuilder successMsg = new StringBuilder();
        if (proList == null || proList.isEmpty()) {
            throw new ServiceException("入驻企业信息数据不能为空");
        } else {
            List<Enterprise> pl = new ArrayList<>(proList.stream()
                    .collect(Collectors.toMap(
                            Enterprise::getCode,   // 以 email 作为键
                            p -> p,            // 值保留对象本身
                            (existing, replacement) -> existing // 保留第一个出现的元素
                    ))
                    .values());
            pl.forEach(x->{
                x.setXmId(xmId);
            });
            enterpriseService.saveBatch(proList);
            successMsg.append("导入成功");
        }
        return AjaxResult.success(successMsg);
    }

    /**
     * 模板信息
     */
    @ApiOperation("导出基本信息模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Enterprise> util = new ExcelUtil<>(Enterprise.class);
        util.importTemplateExcel(response, "企业模板");
    }
}

