package com.zhyc.module.produce.manage_sheep.controller;

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.web.bind.annotation.*;
import com.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.module.produce.manage_sheep.domain.ScTransitionInfo;
import com.zhyc.module.produce.manage_sheep.service.IScTransitionInfoService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 转场Controller
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@RestController
@RequestMapping("produce/manage_sheep/transition_info")
public class ScTransitionInfoController extends BaseController {
    @Autowired
    private IScTransitionInfoService scTransitionInfoService;

    /**
     * 查询转场列表
     */
    @PreAuthorize("@ss.hasPermi('produce:transition_info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScTransitionInfo scTransitionInfo) {
        startPage();
        List<ScTransitionInfo> list = scTransitionInfoService.selectScTransitionInfoList(scTransitionInfo);
        return getDataTable(list);
    }

    /**
     * 导出转场列表
     */
    @PreAuthorize("@ss.hasPermi('produce:transition_info:export')")
    @Log(title = "转场", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScTransitionInfo scTransitionInfo) {
        List<ScTransitionInfo> list = scTransitionInfoService.selectScTransitionInfoList(scTransitionInfo);
        for (ScTransitionInfo item : list) {
            item.setStatusText(scTransitionInfoService.convertStatus(item.getStatus()));
            item.setTransTypeText(scTransitionInfoService.convertTransType(item.getTransType()));
        }
        ExcelUtil<ScTransitionInfo> util = new ExcelUtil<>(ScTransitionInfo.class);
        util.exportExcel(response, list, "转场数据");
    }

    /**
     * 获取转场详细信息
     */
    @PreAuthorize("@ss.hasPermi('produce:transition_info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        ScTransitionInfo transitionInfo = scTransitionInfoService.selectScTransitionInfoById(id);
        transitionInfo.setTransTypeText(scTransitionInfoService.convertTransType(transitionInfo.getTransType()));
        return success(transitionInfo);
    }

    /**
     * 新增转场
     */
    @PreAuthorize("@ss.hasPermi('produce:transition_info:add')")
    @Log(title = "转场", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScTransitionInfo scTransitionInfo) {
        scTransitionInfo.setUserId(getUserId());
        scTransitionInfo.setDeptId(getDeptId());
        return toAjax(scTransitionInfoService.insertScTransitionInfo(scTransitionInfo));
    }

    //批量添加
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody List<ScTransitionInfo> transitionInfoList) {
        Long userId = getUserId();
        Long deptId = getDeptId();
        return toAjax(scTransitionInfoService.insertScTransitionInfoBatch(transitionInfoList));
    }

    /**
     * 修改转场
     */
    @PreAuthorize("@ss.hasPermi('produce:transition_info:edit')")
    @Log(title = "转场", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScTransitionInfo scTransitionInfo) {
        return toAjax(scTransitionInfoService.updateScTransitionInfo(scTransitionInfo));
    }

    /**
     * 删除转场
     */
    @PreAuthorize("@ss.hasPermi('produce:transition_info:remove')")
    @Log(title = "转场", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(scTransitionInfoService.deleteScTransitionInfoByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('produce:transition_info:approve')")
    @PutMapping("/approve")
    public AjaxResult approveScTransitionInfo(@RequestBody ScTransitionInfo scTransitionInfo) {
        int rows = scTransitionInfoService.approveScTransitionInfo(scTransitionInfo);
        return toAjax(rows);
    }

    @GetMapping("/search_ear_numbers")
    public AjaxResult searchEarNumbers(@RequestParam("query") String query){
        return success(scTransitionInfoService.searchEarNumbers(query.trim()));
    }


}
