package com.cirs.biz.controller;

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

import com.cirs.biz.domain.TElectronComponent;
import com.cirs.biz.domain.TVerificationTask;
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.domain.TElecCompRecord;
import com.cirs.biz.service.ITElecCompRecordService;
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-04
 */
@RestController
@RequestMapping("/biz/record")
public class TElecCompRecordController extends BaseController
{
    @Autowired
    private ITElecCompRecordService tElecCompRecordService;

    /**
     * 查询元器件使用经历列表
     */
    @PreAuthorize("@ss.hasPermi('biz:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TElecCompRecord tElecCompRecord)
    {
        startPage();
        List<TElecCompRecord> list = tElecCompRecordService.selectTElecCompRecordList(tElecCompRecord);
//        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出元器件使用经历列表
     */
    @PreAuthorize("@ss.hasPermi('biz:record:export')")
    @Log(title = "元器件使用经历", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TElecCompRecord tElecCompRecord)
    {
        List<TElecCompRecord> list = tElecCompRecordService.selectTElecCompRecordList(tElecCompRecord);
        ExcelUtil<TElecCompRecord> util = new ExcelUtil<TElecCompRecord>(TElecCompRecord.class);
        util.exportExcel(response, list, "元器件使用经历数据");
    }

    /**
     * 获取元器件使用经历详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tElecCompRecordService.selectTElecCompRecordById(id));
    }

    /**
     * 利用元器件编号获取元器件使用场景和使用日期 wangruilin
     */
    @PreAuthorize("@ss.hasPermi('biz:record:queryBycomponentId')")
    @GetMapping(value = "/componentId/{componentId}")
    public AjaxResult getBycomponentId(@PathVariable("componentId") Long componentId)
    {
//        System.out.println(tElecCompRecordService.getBycomponentId(componentId));
        return success(tElecCompRecordService.getBycomponentId(componentId));
    }

    /**
     * 新增元器件使用经历
     */
    @PreAuthorize("@ss.hasPermi('biz:record:add')")
    @Log(title = "元器件使用经历", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TElecCompRecord tElecCompRecord)
    {
        return toAjax(tElecCompRecordService.insertTElecCompRecord(tElecCompRecord));
    }

    /**
     * 修改元器件使用经历
     */
    @PreAuthorize("@ss.hasPermi('biz:record:edit')")
    @Log(title = "元器件使用经历", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TElecCompRecord tElecCompRecord)
    {
        return toAjax(tElecCompRecordService.updateTElecCompRecord(tElecCompRecord));
    }

    /**
     * 删除元器件使用经历
     */
    @PreAuthorize("@ss.hasPermi('biz:record:remove')")
    @Log(title = "元器件使用经历", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tElecCompRecordService.deleteTElecCompRecordByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('biz:record:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TElecCompRecord> util = new ExcelUtil<TElecCompRecord>(TElecCompRecord.class);
        List<TElecCompRecord> recordList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tElecCompRecordService.importRecord(recordList, updateSupport, operName);
        return success(message);
    }

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



}
