package cn.iocoder.yudao.module.system.controller.admin.supplier;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.api.supplier.dto.SupplierRpcDTO;
import cn.iocoder.yudao.module.system.controller.admin.supplier.vo.SupplierPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.supplier.vo.SupplierProductRespVO;
import cn.iocoder.yudao.module.system.controller.admin.supplier.vo.SupplierRespVO;
import cn.iocoder.yudao.module.system.controller.admin.supplier.vo.SupplierSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.supplier.SupplierDO;
import cn.iocoder.yudao.module.system.dal.dataobject.supplier.SupplierProductDO;
import cn.iocoder.yudao.module.system.service.supplier.SupplierProductService;
import cn.iocoder.yudao.module.system.service.supplier.SupplierService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 供应商列表(OA已审核通过后插入)")
@RestController
@RequestMapping("/system/supplier")
@Validated
public class SupplierController {

    @Resource
    private SupplierService supplierService;
    @Resource
    private SupplierProductService supplierProductService;

    @PutMapping("/edit")
    @Operation(summary = "更新供应商列表(OA已审核通过后插入)")
    public CommonResult<Boolean> edit(@Valid @RequestBody SupplierRpcDTO supplierRpcDTO) {
        supplierService.saveOrEdit(supplierRpcDTO);
        return success(true);
    }


    @PutMapping("/editStatus")
    @Operation(summary = "更新供应商状态")
    public CommonResult<Boolean> editStatus(@Valid @RequestBody SupplierSaveReqVO vo) {
        supplierService.editStatus(vo);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商列表(OA已审核通过后插入)")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSupplier(@RequestParam("id") Long id) {
        supplierService.deleteSupplier(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商列表(OA已审核通过后插入)")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SupplierRespVO> getSupplier(@RequestParam("id") Long id) {
        SupplierDO supplier = supplierService.getSupplier(id);
        SupplierRespVO vo = BeanUtils.toBean(supplier, SupplierRespVO.class);
        List<SupplierProductDO> items = supplierProductService.getBySupplierId(id);
        List<SupplierProductRespVO> itemVos = BeanUtils.toBean(items, SupplierProductRespVO.class);
        vo.setItems(itemVos);
        return success(vo);
    }

    @GetMapping("/allList")
    @Operation(summary = "获得供应商列表")
    public CommonResult<List<SupplierRespVO>> productList(@RequestParam(name = "status", required = false) Integer status) {
        List<SupplierDO> items = supplierService.allList(status);
        return success(BeanUtils.toBean(items, SupplierRespVO.class));
    }

    @GetMapping("/productList")
    @Operation(summary = "获得供应商商品列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<SupplierProductRespVO>> productList(@RequestParam("id") Long id) {
        List<SupplierProductDO> items = supplierProductService.getBySupplierId(id);
        return success(BeanUtils.toBean(items, SupplierProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商列表(OA已审核通过后插入)分页")
    public CommonResult<PageResult<SupplierRespVO>> getSupplierPage(@Valid SupplierPageReqVO pageReqVO) {
        PageResult<SupplierDO> pageResult = supplierService.getSupplierPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商列表(OA已审核通过后插入) Excel")
    @OperateLog(type = EXPORT)
    public void exportSupplierExcel(@Valid SupplierPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierDO> list = supplierService.getSupplierPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商列表(OA已审核通过后插入).xls", "数据", SupplierRespVO.class,
                BeanUtils.toBean(list, SupplierRespVO.class));
    }

}
