package com.ruoyi.web.controller.app;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.EntranceBatch;
import com.ruoyi.app.service.IEntranceBatchService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 入场批次Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/entranceBatch")
public class EntranceBatchController extends BaseController
{
    @Autowired
    private IEntranceBatchService entranceBatchService;

    /**
     * 查询入场批次列表
     */
    @PreAuthorize("@ss.hasPermi('app:entranceBatch:list')")
    @GetMapping("/list")
    public TableDataInfo list(EntranceBatch entranceBatch)
    {
        startPage();
        List<EntranceBatch> list = entranceBatchService.selectEntranceBatchList(entranceBatch);
        return getDataTable(list);
    }

    /**
     * 入场批次列表
     */
    @PreAuthorize("@ss.hasPermi('app:entranceBatch:page')")
    @GetMapping("/page")
    public TableDataInfo page(EntranceBatch entranceBatch)
    {
        //List<EntranceBatch> list = entranceBatchService.selectEntranceBatchList1(entranceBatch);
        List<EntranceBatch> list = entranceBatchService.selectEntranceBatchList2(entranceBatch);
        return getDataTable(list);
    }

    /**
     * 导出入场批次列表
     */
    @PreAuthorize("@ss.hasPermi('app:entranceBatch:export')")
    @Log(title = "入场批次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EntranceBatch entranceBatch)
    {
        List<EntranceBatch> list = entranceBatchService.selectEntranceBatchList(entranceBatch);
        ExcelUtil<EntranceBatch> util = new ExcelUtil<EntranceBatch>(EntranceBatch.class);
        util.exportExcel(response, list, "入场批次数据");
    }

    /**
     * 获取入场批次详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:entranceBatch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(entranceBatchService.selectEntranceBatchById(id));
    }

    /**
     * 新增入场批次
     */
    @PreAuthorize("@ss.hasPermi('app:entranceBatch:add')")
    @Log(title = "入场批次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EntranceBatch entranceBatch)
    {
        if (entranceBatch.getAnimalCertNo()!=null && !entranceBatch.getAnimalCertNo().isEmpty()){
            if (!entranceBatchService.checkAnimalCertNoUnique(entranceBatch))
            {
                return error("新增入场批次失败，列表中存在重复的检疫证号");
            }
        }

        entranceBatch.setCreateBy(getUsername());
        return toAjax(entranceBatchService.insertEntranceBatch(entranceBatch));
    }

    /**
     * 修改入场批次
     */
    @PreAuthorize("@ss.hasPermi('app:entranceBatch:edit')")
    @Log(title = "入场批次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody EntranceBatch entranceBatch)
    {
        if (entranceBatch.getAnimalCertNo()!=null && !entranceBatch.getAnimalCertNo().isEmpty()){
            if (!entranceBatchService.checkAnimalCertNoUnique(entranceBatch))
            {
                return error("修改入场批次失败，列表中存在重复的检疫证号");
            }
        }
        entranceBatch.setUpdateBy(getUsername());
        return toAjax(entranceBatchService.updateEntranceBatch(entranceBatch));
    }

    /**
     * 删除入场批次
     */
    @PreAuthorize("@ss.hasPermi('app:entranceBatch:remove')")
    @Log(title = "入场批次", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(entranceBatchService.deleteEntranceBatchByIds(ids));
    }
}
