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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.module.supplier.controller.admin.product.vo.SupplierPageReqVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.SupplierRespVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.SupplierSaveReqVO;
import cn.iocoder.yudao.module.supplier.dal.dataobject.product.SupplierDO;
import cn.iocoder.yudao.module.supplier.service.product.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.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 供应商主数据")
@RestController
@RequestMapping("/supplier/base/")
@Validated
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商主数据")
    @PreAuthorize("@ss.hasPermission('supplier:base:create')")
    public CommonResult<Long> create(@Valid @RequestBody SupplierSaveReqVO createReqVO) {
        return success(supplierService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商主数据")
    @PreAuthorize("@ss.hasPermission('supplier:base:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody SupplierSaveReqVO updateReqVO) {
        supplierService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商主数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('supplier:base:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        supplierService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商主数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('supplier:base:query')")
    public CommonResult<SupplierRespVO> get(@RequestParam("id") Long id) {
        SupplierDO supplierDO = supplierService.get(id);
        return success(BeanUtils.toBean(supplierDO, SupplierRespVO.class));
    }

    @GetMapping("/allSupplierInfo")
    @Operation(summary = "获得供应商主数据及包含的所有子表数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('supplier:base:query')")
    public CommonResult<SupplierRespVO> allSupplierInfo(@RequestParam("id") Long id) {
        SupplierRespVO supplier = supplierService.allSupplierInfo(id);
        return success(supplier);
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商主数据分页")
    @PreAuthorize("@ss.hasPermission('supplier:base:query')")
    public CommonResult<PageResult<SupplierRespVO>> getPage(@Valid SupplierPageReqVO pageReqVO) {
        PageResult<SupplierDO> pageResult = supplierService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商主数据 Excel")
    @PreAuthorize("@ss.hasPermission('supplier:base:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid SupplierPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierDO> list = supplierService.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商主数据.xls", "数据", SupplierRespVO.class,
                        BeanUtils.toBean(list, SupplierRespVO.class));
    }

    @GetMapping("all")
    @Operation(summary = "获取所有供应商信息")
    @PreAuthorize("@ss.hasPermission('supplier:base:query')")
    public CommonResult<List<SupplierRespVO>> getAll() {
        SupplierPageReqVO supplierPageReqVO = new SupplierPageReqVO();
        supplierPageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierDO> all = supplierService.getPage(supplierPageReqVO).getList();
        List<SupplierRespVO> bean = BeanUtils.toBean(all, SupplierRespVO.class);
        return success(bean);
    }

    @PostMapping("/status/{id}/{status}")
    @Operation(summary = "修改状态")
    @PreAuthorize("@ss.hasPermission('supplier:base:changestatus')")
    public CommonResult<Boolean> changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        supplierService.changeStatus(id, status);
        return success(true);
    }

    @PutMapping("/submitForApproval")
    @Operation(summary = "提交审核")
    @PreAuthorize("@ss.hasPermission('supplier:base:create')")
    public CommonResult<Boolean> submitForApproval(@RequestParam("id") Long id) {
        supplierService.submitForApproval(id);
        return success(true);
    }
}
