package com.ruoyi.web.controller.app;

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

import com.ruoyi.app.DTO.PigpenDTO;
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.Pigpen;
import com.ruoyi.app.service.IPigpenService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 待宰圈Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/pigpen")
public class PigpenController extends BaseController
{
    @Autowired
    private IPigpenService pigpenService;

    /**
     * 查询待宰圈列表
     */
    // @PreAuthorize("@ss.hasPermi('app:pigpen:list')")
    @GetMapping("/list")
    public TableDataInfo list(Pigpen pigpen)
    {
        startPage();
        List<Pigpen> list = pigpenService.selectPigpenList(pigpen);
        return getDataTable(list);
    }

    /**
     * 导出待宰圈列表
     */
    // @PreAuthorize("@ss.hasPermi('app:pigpen:export')")
    @Log(title = "待宰圈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Pigpen pigpen)
    {
        List<PigpenDTO> list = pigpenService.printPigpenList(pigpen);
        list.forEach(item -> item.setQrcode(item.getPigpenName()));
        ExcelUtil<PigpenDTO> util = new ExcelUtil<PigpenDTO>(PigpenDTO.class);
        util.exportExcel(response, list, "待宰圈数据");
    }

    /**
     * 获取待宰圈详细信息
     */
    // @PreAuthorize("@ss.hasPermi('app:pigpen:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pigpenService.selectPigpenById(id));
    }

    /**
     * 新增待宰圈
     */
    // @PreAuthorize("@ss.hasPermi('app:pigpen:add')")
    @Log(title = "待宰圈", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Pigpen pigpen)
    {
        if (!pigpenService.checkPigpenNameUnique(pigpen))
        {
            return error("新增待宰圈'" + pigpen.getPigpenName() + "'失败，列表中存在重复的待宰圈号");
        }
        pigpen.setCreateBy(getUsername());
        return toAjax(pigpenService.insertPigpen(pigpen));
    }

    /**
     * 修改待宰圈
     */
    // @PreAuthorize("@ss.hasPermi('app:pigpen:edit')")
    @Log(title = "待宰圈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Pigpen pigpen)
    {
        if (!pigpenService.checkPigpenNameUnique(pigpen))
        {
            return error("修改待宰圈'" + pigpen.getPigpenName() + "'失败，列表中存在重复的待宰圈号");
        }
        if (pigpen.getIsUse() != 0) {
            return error("修改待宰圈失败，待宰圈正在使用");
        }
        pigpen.setUpdateBy(getUsername());
        return toAjax(pigpenService.updatePigpen(pigpen));
    }

    /**
     * 删除待宰圈
     */
    // @PreAuthorize("@ss.hasPermi('app:pigpen:remove')")
    @Log(title = "待宰圈", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            Pigpen pigpen = pigpenService.selectPigpenById(id);
            if (pigpen.getIsUse() != 0) {
                return error("删除待宰圈失败，待宰圈正在使用");
            }
        }
        return toAjax(pigpenService.deletePigpenByIds(ids));
    }

    /**
     * 获取待宰圈选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect(Pigpen pigpen)
    {
        List<Pigpen> list = pigpenService.selectPigpenList(pigpen);
        return success(list);
    }

    /**
     * 获取下一个自动生成的猪圈编号
     */
    @GetMapping("/nextNo")
    public AjaxResult nextNo()
    {
        return success(new HashMap<String, String>() {{ put("nextNo", pigpenService.getNextNo());}});
    }
}
