package com.ruoyi.web.controller.app;

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

import com.ruoyi.app.model.HarmlessTreatment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.app.model.EntranceInspection;
import com.ruoyi.app.service.IEntranceInspectionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 入场检验Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/entranceInspection")
public class EntranceInspectionController extends BaseController
{
    @Autowired
    private IEntranceInspectionService entranceInspectionService;

    /**
     * 查询入场检验列表
     */
//    // @PreAuthorize("@ss.hasPermi('app:entranceInspection:list')")
    @GetMapping("/list")
    public TableDataInfo list(EntranceInspection entranceInspection)
    {
        startPage();
        List<EntranceInspection> list = entranceInspectionService.selectEntranceInspectionList(entranceInspection);
        return getDataTable(list);
    }

    /**
     * 导出入场检验列表
     */
//    // @PreAuthorize("@ss.hasPermi('app:entranceInspection:export')")
    @Log(title = "入场检验", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EntranceInspection entranceInspection)
    {
        List<EntranceInspection> list = entranceInspectionService.selectEntranceInspectionList(entranceInspection);
        ExcelUtil<EntranceInspection> util = new ExcelUtil<EntranceInspection>(EntranceInspection.class);
        util.exportExcel(response, list, "入场检验数据");
    }

    /**
     * 获取入场检验详细信息
     */
//    // @PreAuthorize("@ss.hasPermi('app:entranceInspection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(entranceInspectionService.selectEntranceInspectionById(id));
    }

    /**
     * 新增入场检验
     */
//    // @PreAuthorize("@ss.hasPermi('app:entranceInspection:add')")
    @Log(title = "入场检验", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated  @RequestBody EntranceInspection entranceInspection)
    {
        entranceInspection.setCreateBy(getUsername());
        //存在性判定，已存在入场批次号相同的数据，则更新原有数据，否则新增插入
        if(!ObjectUtils.isEmpty(entranceInspection.getEntranceBatchId())) {
            EntranceInspection queryEntity = new EntranceInspection();
            queryEntity.setEntranceBatchId(entranceInspection.getEntranceBatchId());
            List<EntranceInspection> list = entranceInspectionService.selectEntranceInspectionList(queryEntity);
            if(!ObjectUtils.isEmpty(list)) {
                return toAjax(entranceInspectionService.updateEntranceInspection(entranceInspection));
            }
        }
        return toAjax(entranceInspectionService.insertEntranceInspection(entranceInspection));
    }

    /**
     * 修改入场检验
     */
//    // @PreAuthorize("@ss.hasPermi('app:entranceInspection:edit')")
    @Log(title = "入场检验", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody EntranceInspection entranceInspection)
    {
        entranceInspection.setUpdateBy(getUsername());
        return toAjax(entranceInspectionService.updateEntranceInspection(entranceInspection));
    }

    /**
     * 删除入场检验
     */
//    // @PreAuthorize("@ss.hasPermi('app:entranceInspection:remove')")
    @Log(title = "入场检验", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(entranceInspectionService.deleteEntranceInspectionByIds(ids));
    }
}
