package com.xunhong.erp.turbo.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.biz.api.SupplierServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.SupplierCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.SupplierDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.SupplierUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.SupplierCheckQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.SupplierListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.SupplierPageQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.SupplierShowQry;
import com.xunhong.erp.turbo.api.biz.dto.vo.SupplierVO;
import com.xunhong.erp.turbo.base.dto.PageDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author shenyifei
 */
@Tag(name = "Supplier", description = "供应商管理")
@RestController("operationSupplierController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class SupplierController {

	@DubboReference(version = "1.0.0")
	private final SupplierServiceI supplierService;

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SUPPLIER_VIEW})
	@GetMapping("listSupplier")
	@Operation(summary = "供应商列表", method = "GET")
	public MultiResponse<SupplierVO> listSupplier(@ModelAttribute @Validated SupplierListQry supplierListQry) {
		return MultiResponse.of(supplierService.list(supplierListQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SUPPLIER_CREATE})
	@PostMapping("createSupplier")
	@Operation(summary = "创建供应商", method = "POST")
	public SingleResponse<SupplierVO> createSupplier(@RequestBody @Validated SupplierCreateCmd supplierCreateCmd) {
		return SingleResponse.of(supplierService.create(supplierCreateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SUPPLIER_VIEW})
	@GetMapping("showSupplier")
	@Operation(summary = "供应商详情", method = "GET")
	public SingleResponse<SupplierVO> showSupplier(@ModelAttribute @Validated SupplierShowQry supplierShowQry) {
		return SingleResponse.of(supplierService.show(supplierShowQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SUPPLIER_VIEW})
	@GetMapping("pageSupplier")
	@Operation(summary = "供应商列表", method = "GET")
	public PageResponse<SupplierVO> pageSupplier(@ModelAttribute @Validated SupplierPageQry supplierPageQry) {
		PageDTO<SupplierVO> page = supplierService.page(supplierPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SUPPLIER_UPDATE})
	@RequestMapping(value = "updateSupplier", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "供应商更新", method = "PATCH")
	public SingleResponse<SupplierVO> updateSupplier(@RequestBody @Validated SupplierUpdateCmd supplierUpdateCmd) {
		return SingleResponse.of(supplierService.update(supplierUpdateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_SUPPLIER_DELETE})
	@DeleteMapping("destroySupplier")
	@Operation(summary = "供应商删除", method = "DELETE")
	public Response destroySupplier(@RequestBody @Validated SupplierDestroyCmd supplierDestroyCmd) {
		supplierService.destroy(supplierDestroyCmd);
		return Response.buildSuccess();
	}
	@SaCheckLogin
	@GetMapping("/checkSupplier")
	@Operation(summary = "检查供应商", method = "GET")
	public SingleResponse<SupplierVO> checkSupplier(@ModelAttribute @Validated SupplierCheckQry supplierCheckQry) {
		return SingleResponse.of(supplierService.checkSupplier(supplierCheckQry));
	}
}
