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.DealerServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.DealerCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.DealerDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.DealerDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.DealerUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.DealerListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.DealerPageQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.DealerShowQry;
import com.xunhong.erp.turbo.api.biz.dto.vo.DealerVO;
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 = "Dealer", description = "经销商表管理")
@RestController("operationDealerController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class DealerController {

	@DubboReference(version = "1.0.0")
	private final DealerServiceI dealerService;

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DEALER_VIEW})
	@GetMapping("listDealer")
	@Operation(summary = "经销商表列表", method = "GET")
	public MultiResponse<DealerVO> listDealer(@ModelAttribute @Validated DealerListQry dealerListQry) {
		return MultiResponse.of(dealerService.list(dealerListQry));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DEALER_CREATE})
	@PostMapping("createDealer")
	@Operation(summary = "创建经销商表", method = "POST")
	public SingleResponse<DealerVO> createDealer(@RequestBody @Validated DealerCreateCmd dealerCreateCmd) {
		return SingleResponse.of(dealerService.create(dealerCreateCmd));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DEALER_VIEW})
	@GetMapping("showDealer")
	@Operation(summary = "经销商表详情", method = "GET")
	public SingleResponse<DealerVO> showDealer(@ModelAttribute @Validated DealerShowQry dealerShowQry) {
		return SingleResponse.of(dealerService.show(dealerShowQry));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DEALER_VIEW})
	@GetMapping("pageDealer")
	@Operation(summary = "经销商表列表", method = "GET")
	public PageResponse<DealerVO> pageDealer(@ModelAttribute @Validated DealerPageQry dealerPageQry) {
		PageDTO<DealerVO> page = dealerService.page(dealerPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DEALER_UPDATE})
	@RequestMapping(value = "updateDealer", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "经销商表更新", method = "PATCH")
	public SingleResponse<DealerVO> updateDealer(@RequestBody @Validated DealerUpdateCmd dealerUpdateCmd) {
		return SingleResponse.of(dealerService.update(dealerUpdateCmd));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DEALER_DELETE})
	@DeleteMapping("destroyDealer")
	@Operation(summary = "经销商表删除", method = "DELETE")
	public Response destroyDealer(@RequestBody @Validated DealerDestroyCmd dealerDestroyCmd) {
		dealerService.destroy(dealerDestroyCmd);
		return Response.buildSuccess();
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DEALER_UPDATE})
	@RequestMapping(value = "dragDealer", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "经销商表拖拽排序", method = "PATCH")
	public Response dragDealer(@RequestBody @Validated DealerDragCmd dealerDragCmd) {
		dealerService.drag(dealerDragCmd);
		return Response.buildSuccess();
	}
}
