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.ProductServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.ProductCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.ProductDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.ProductUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.ProductDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.ProductListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.ProductPageQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.ProductShowQry;
import com.xunhong.erp.turbo.api.biz.dto.vo.ProductVO;
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 = "Product", description = "产品管理")
@RestController("operationProductController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class ProductController {

	@DubboReference(version = "1.0.0")
	private final ProductServiceI productService;

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_COST_ITEM_VIEW})
	@GetMapping("listProduct")
	@Operation(summary = "产品列表", method = "GET")
	public MultiResponse<ProductVO> listProduct(@ModelAttribute @Validated ProductListQry productListQry) {
		return MultiResponse.of(productService.list(productListQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_COST_ITEM_CREATE})
	@PostMapping("createProduct")
	@Operation(summary = "创建产品", method = "POST")
	public SingleResponse<ProductVO> createProduct(@RequestBody @Validated ProductCreateCmd productCreateCmd) {
		return SingleResponse.of(productService.create(productCreateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_COST_ITEM_VIEW})
	@GetMapping("showProduct")
	@Operation(summary = "产品详情", method = "GET")
	public SingleResponse<ProductVO> showProduct(@ModelAttribute @Validated ProductShowQry productShowQry) {
		return SingleResponse.of(productService.show(productShowQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_COST_ITEM_VIEW})
	@GetMapping("pageProduct")
	@Operation(summary = "产品列表", method = "GET")
	public PageResponse<ProductVO> pageProduct(@ModelAttribute @Validated ProductPageQry productPageQry) {
		PageDTO<ProductVO> page = productService.page(productPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_COST_ITEM_UPDATE})
	@RequestMapping(value = "updateProduct", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "产品更新", method = "PATCH")
	public SingleResponse<ProductVO> updateProduct(@RequestBody @Validated ProductUpdateCmd productUpdateCmd) {
		return SingleResponse.of(productService.update(productUpdateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_COST_ITEM_DELETE})
	@DeleteMapping("destroyProduct")
	@Operation(summary = "产品删除", method = "DELETE")
	public Response destroyProduct(@RequestBody @Validated ProductDestroyCmd productDestroyCmd) {
		productService.destroy(productDestroyCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_COST_ITEM_UPDATE})
	@RequestMapping(value = "dragProduct", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "产品拖拽排序", method = "PATCH")
	public Response dragProduct(@RequestBody @Validated ProductDragCmd productDragCmd) {
		productService.drag(productDragCmd);
		return Response.buildSuccess();
	}
}
