package com.bonus.canteen.core.supply.controller;

import com.bonus.canteen.core.menu.dto.AppletWeekRecipeDTO;
import com.bonus.canteen.core.supply.dto.*;
import com.bonus.canteen.core.supply.service.DrpSupplierService;
import com.bonus.canteen.core.supply.vo.DrpMaterialSupplierVO;
import com.bonus.canteen.core.supply.vo.DrpSupplierMainInfoVO;
import com.bonus.canteen.core.supply.vo.DrpSupplierPageVO;
import com.bonus.canteen.core.supply.vo.DrpSupplierVO;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author xliu
 * @date 2025/5/12 10:43
 */
@Api(tags = "供应商接口")
@RestController
@RequestMapping("/supplier_manage")
public class SupplierController  extends BaseController {

    @Resource
    private DrpSupplierService drpSupplierService;

    @ApiOperation("分页获取供应商信息")
    @GetMapping({"/list"})
    public TableDataInfo getWeekRecipeDetailList( DrpSupplierPageDTO content) {
        try {
            if(content.getIsPaging() == 2){
                startPage();
            }
            List<DrpSupplierPageVO> list = this.drpSupplierService.getDrpSupplierPage(content);
            return getDataTable(list);
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }

    }

    @PostMapping({"/add"})
    @ApiOperation("新增供应商信息")
    public AjaxResult addDrpSupplier(@RequestBody @Valid DrpSupplierAddDTO content) {
        this.drpSupplierService.addDrpSupplier(content);
        return success();
    }

    @PostMapping({"/remove"})
    @ApiOperation("删除供应商信息")
    public AjaxResult removeBySupplierId(@Param("supplierId")  Long supplierId) {
        this.drpSupplierService.removeBySupplierId(supplierId);
        return success();
    }
    @PostMapping({"/edit"})
    @ApiOperation("修改供应商信息")
    public AjaxResult editDrpSupplier(@RequestBody @Valid DrpSupplierEditDTO content) {
        this.drpSupplierService.editDrpSupplier(content);
        return success();
    }

    @PostMapping({"/edit/status"})
    @ApiOperation("变更供应商状态")
    public AjaxResult editDrpSupplierStatus(@RequestBody @Valid DrpSupplierEditStatusDTO content) {
        this.drpSupplierService.editDrpSupplierStatus(content);
        return success();
    }

    @PostMapping({"/list/supplier"})
    @ApiOperation("查询供应商集合")
    public TableDataInfo listSupplier(@RequestBody DrpSupplierQueryDTO content) {
        List<DrpSupplierVO> resultList = this.drpSupplierService.listSupplier(content);
        return getDataTable(resultList);
    }

    @PostMapping({"/list/supplierByMaterialId"})
    @ApiOperation("根据原料或类别查询供应商集合")
    public Map<Long, List<DrpSupplierVO>> supplierByMaterialId(@RequestBody DrpSupplierDTO content) {
        return this.drpSupplierService.supplierByMaterialId(content);
    }

    @PostMapping({"/bidMaterialSupplier"})
    @ApiOperation("原料绑定供应商")
    public AjaxResult bidMaterialSupplier(@RequestBody DrpBidMaterialSupplierDTO content) {
        this.drpSupplierService.bidMaterialSupplier(content);
        return success();
    }

    @PostMapping({"/get/main/info"})
    @ApiOperation("获取供应商主要信息接口")
    public DrpSupplierMainInfoVO getMainInfoById(@RequestBody Long supplierId) {
        return this.drpSupplierService.getMainInfoById(supplierId);
    }

    @GetMapping({"/getMaterialSupplierPage"})
    @ApiOperation("获取原料供应商分页")
    public TableDataInfo getMaterialSupplier(DrpMaterialInfoDTO content) {
        if(content !=null && content.getIsPaging() ==2){
            startPage();
        }
//        content.setAreaIdList(this.drpAuthorityApi.authAreaList(content.getAreaIdList()));
//        return PageVO.of(this.drpSupplierService.getMaterialSupplier(content));
        List<DrpMaterialSupplierVO> list = drpSupplierService.getMaterialSupplier(content);
        return getDataTable(list);
    }

}
