package com.shzg.project.unique.controller;

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

import com.shzg.framework.aspectj.lang.annotation.DataScope;
import com.shzg.project.worn.domain.dto.WornInboundItemDTO;
import com.shzg.project.worn.domain.dto.WornScanCodeDTO;
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.shzg.framework.aspectj.lang.annotation.Log;
import com.shzg.framework.aspectj.lang.enums.BusinessType;
import com.shzg.project.unique.domain.WornUniqueCode;
import com.shzg.project.unique.service.IWornUniqueCodeService;
import com.shzg.framework.web.controller.BaseController;
import com.shzg.framework.web.domain.AjaxResult;
import com.shzg.common.utils.poi.ExcelUtil;
import com.shzg.framework.web.page.TableDataInfo;

/**
 * 唯一码管理Controller
 * 
 * @author shzg
 * @date 2026-03-17
 */
@RestController
@RequestMapping("/unique/code")
public class WornUniqueCodeController extends BaseController
{
    @Autowired
    private IWornUniqueCodeService wornUniqueCodeService;

    /**
     * 查询唯一码管理列表
     */
    @PreAuthorize("@ss.hasPermi('unique:code:list')")
    @GetMapping("/list")
    public TableDataInfo list(WornUniqueCode wornUniqueCode)
    {
        startPage();
        List<WornUniqueCode> list = wornUniqueCodeService.selectWornUniqueCodeList(wornUniqueCode);
        return getDataTable(list);
    }

    /**
     * 导出唯一码管理列表
     */
    @PreAuthorize("@ss.hasPermi('unique:code:export')")
    @Log(title = "唯一码管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WornUniqueCode wornUniqueCode)
    {
        List<WornUniqueCode> list = wornUniqueCodeService.selectWornUniqueCodeList(wornUniqueCode);
        ExcelUtil<WornUniqueCode> util = new ExcelUtil<WornUniqueCode>(WornUniqueCode.class);
        util.exportExcel(response, list, "唯一码管理数据");
    }

    /**
     * 获取唯一码管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('unique:code:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wornUniqueCodeService.selectWornUniqueCodeById(id));
    }

    /**
     * 新增唯一码管理
     */
    @PreAuthorize("@ss.hasPermi('unique:code:add')")
    @Log(title = "唯一码管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WornUniqueCode wornUniqueCode)
    {
        return toAjax(wornUniqueCodeService.insertWornUniqueCode(wornUniqueCode));
    }

    /**
     * 修改唯一码管理
     */
    @PreAuthorize("@ss.hasPermi('unique:code:edit')")
    @Log(title = "唯一码管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WornUniqueCode wornUniqueCode)
    {
        return toAjax(wornUniqueCodeService.updateWornUniqueCode(wornUniqueCode));
    }

    /**
     * 删除唯一码管理
     */
    @PreAuthorize("@ss.hasPermi('unique:code:remove')")
    @Log(title = "唯一码管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody Long[] ids)
    {
        return toAjax(wornUniqueCodeService.deleteWornUniqueCodeByIds(ids));
    }
    /**
     * 根据唯一码编号查询物料信息
     *
     * @param code 唯一码编号
     * @return 物料信息列表
     */
    @PreAuthorize("@ss.hasPermi('worn:uniqueCode:list')")
    @GetMapping("/materialInfo/{code}")
    public AjaxResult getMaterialInfo(@PathVariable("code") Integer code)
    {
        return AjaxResult.success(wornUniqueCodeService.selectMaterialInfoByCode(code));
    }

    /**
     * 出库扫码查询物料信息
     */
    @PostMapping("/getMaterialInfo")
    public AjaxResult getMaterialInfo(@RequestBody WornScanCodeDTO dto)
    {
        List<WornInboundItemDTO> list =
                wornUniqueCodeService.selectMaterialInfoForOutbound(dto.getCode(), dto.getExistList());
        return AjaxResult.success(list);
    }
}
