package com.ruoyi.web.controller.app;

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

import com.ruoyi.app.domain.Supplier;
import com.ruoyi.app.domain.request.ReqPurchaser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.domain.Purchaser;
import com.ruoyi.app.service.IPurchaserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 肉商Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@Api("肉商信息管理")
@RestController
@RequestMapping("/app/purchaser")
public class PurchaserController extends BaseController
{
    @Autowired
    private IPurchaserService purchaserService;

    /**
     * 查询肉商列表
     */
    @ApiOperation("查询肉商列表")
    @GetMapping("/list")
    public TableDataInfo list(ReqPurchaser purchaser)
    {
        startPage();
        List<Purchaser> list = purchaserService.selectPurchaserList(purchaser);
        return getDataTable(list);
    }

    /**
     * 获取肉商选择框列表
     */
    @ApiOperation("获取肉商选择框列表")
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<Purchaser> list = purchaserService.selectAllPurchaserList();
        return success(list);
    }

    /**
     * 导出肉商列表
     */
    @PreAuthorize("@ss.hasPermi('app:purchaser:export')")
    @Log(title = "肉商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReqPurchaser purchaser)
    {
        List<Purchaser> list = purchaserService.selectPurchaserList(purchaser);
        ExcelUtil<Purchaser> util = new ExcelUtil<Purchaser>(Purchaser.class);
        util.exportExcel(response, list, "肉商数据");
    }

    /**
     * 获取肉商详细信息
     */
    @ApiOperation("获取肉商详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(purchaserService.selectPurchaserById(id));
    }

    /**
     * 新增肉商
     */
    @ApiOperation("新增肉商")
    @PreAuthorize("@ss.hasPermi('app:purchaser:add')")
    @Log(title = "肉商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Purchaser purchaser)
    {
        if (!purchaserService.checkPurchaserNoUnique(purchaser))
        {
            return error("新增肉商'" + purchaser.getPurchaserName() + "'失败，列表中存在重复的肉商编号");
        }
        if (!purchaserService.checkPurchaserNameUnique(purchaser))
        {
            return error("新增肉商'" + purchaser.getPurchaserName() + "'失败，列表中存在重复的肉商名称");
        }
        if (!purchaserService.checkIDNumberUnique(purchaser))
        {
            return error("新增肉商'" + purchaser.getPurchaserName() + "'失败，列表中存在重复的身份证号");
        }
        purchaser.setCreateBy(getUsername());
        return toAjax(purchaserService.insertPurchaser(purchaser));
    }

    /**
     * 修改肉商
     */
    @ApiOperation("修改肉商")
    @PreAuthorize("@ss.hasPermi('app:purchaser:edit')")
    @Log(title = "肉商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Purchaser purchaser)
    {
        if (!purchaserService.checkPurchaserNoUnique(purchaser))
        {
            return error("修改肉商'" + purchaser.getPurchaserName() + "'失败，列表中存在重复的肉商编号");
        }
        if (!purchaserService.checkPurchaserNameUnique(purchaser))
        {
            return error("修改肉商'" + purchaser.getPurchaserName() + "'失败，列表中存在重复的肉商名称");
        }
        if (!purchaserService.checkIDNumberUnique(purchaser))
        {
            return error("修改肉商'" + purchaser.getPurchaserName() + "'失败，列表中存在重复的身份证号");
        }
        purchaser.setUpdateBy(getUsername());
        return toAjax(purchaserService.updatePurchaser(purchaser));
    }

    /**
     * 删除肉商
     */
    @ApiOperation("删除肉商")
    @PreAuthorize("@ss.hasPermi('app:purchaser:remove')")
    @Log(title = "肉商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(purchaserService.deletePurchaserByIds(ids));
    }
}
