package com.cirs.biz.controller;

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

import com.cirs.biz.domain.*;
import com.cirs.biz.mapper.RecommendInfoMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cirs.common.annotation.Log;
import com.cirs.common.core.controller.BaseController;
import com.cirs.common.core.domain.AjaxResult;
import com.cirs.common.enums.BusinessType;
import com.cirs.biz.service.ITElectronComponentService;
import com.cirs.common.utils.poi.ExcelUtil;
import com.cirs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 元器件Controller
 * 
 * @author allen
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/biz/component")
public class TElectronComponentController extends BaseController
{
    @Autowired
    private ITElectronComponentService tElectronComponentService;

    @Resource
    private RecommendInfoMapper recommendInfoMapper;

    /**
     * 查询元器件列表
     */
    @PreAuthorize("@ss.hasPermi('biz:component:list')")
    @GetMapping("/list")
    public TableDataInfo list(TElectronComponent tElectronComponent)
    {
        startPage();
        List<TElectronComponent> list = tElectronComponentService.selectTElectronComponentList(tElectronComponent);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('biz:component:all')")
    @GetMapping("/all")
    public List<TElectronComponent> allinfo(TElectronComponent tElectronComponent)
    {
        return tElectronComponentService.getAll();
    }

    /**
     * 导出元器件列表
     */
    @PreAuthorize("@ss.hasPermi('biz:component:export')")
    @Log(title = "元器件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TElectronComponent tElectronComponent)
    {
        List<TElectronComponent> list = tElectronComponentService.selectTElectronComponentList(tElectronComponent);
        ExcelUtil<TElectronComponent> util = new ExcelUtil<TElectronComponent>(TElectronComponent.class);
        util.exportExcel(response, list, "元器件数据");
    }

    /**
     * 获取元器件详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:component:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tElectronComponentService.selectTElectronComponentById(id));
    }

    /**
     * 获取查询的推荐 wangruilin
     */
    @PreAuthorize("@ss.hasPermi('biz:recommend:querylist')")
    @GetMapping("/recommend/querylist")
    public TableDataInfo querylist(TrainReturn recommend_return)
    {
        Long[] ids = new Long[5];

        ids[0] = recommend_return.getResult1Id();
        ids[1] = recommend_return.getResult2Id();
        ids[2] = recommend_return.getResult3Id();
        ids[3] = recommend_return.getResult4Id();
        ids[4] = recommend_return.getResult5Id();
//        System.out.println(ids[0]);
        startPage();
        List<RecommendInfo> list = recommendInfoMapper.RecommendQueryList(ids);
        return getDataTable(list);
    }

    /**
     * 新增元器件
     */
    @PreAuthorize("@ss.hasPermi('biz:component:add')")
    @Log(title = "元器件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TElectronComponent tElectronComponent)
    {
        return toAjax(tElectronComponentService.insertTElectronComponent(tElectronComponent));
    }

    /**
     * 修改元器件
     */
    @PreAuthorize("@ss.hasPermi('biz:component:edit')")
    @Log(title = "元器件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TElectronComponent tElectronComponent)
    {
        return toAjax(tElectronComponentService.updateTElectronComponent(tElectronComponent));
    }

    /**
     * 删除元器件
     */
    @PreAuthorize("@ss.hasPermi('biz:component:remove')")
    @Log(title = "元器件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tElectronComponentService.deleteTElectronComponentByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('biz:component:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TElectronComponent> util = new ExcelUtil<TElectronComponent>(TElectronComponent.class);
        List<TElectronComponent> componentList = util.importExcel(file.getInputStream());

        String operName = getUsername();
        String message = tElectronComponentService.importComponent(componentList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TElectronComponent> util = new ExcelUtil<TElectronComponent>(TElectronComponent.class);
        util.importTemplateExcel(response, "元器件数据");
    }

    @GetMapping("/getDetailById/{id}")
    public String getDetailById(@PathVariable Long id) {
        return tElectronComponentService.getDetailById(id);
    }


}
