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

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.infra.api.ChannelServiceI;
import com.xunhong.erp.turbo.api.infra.dto.cmd.ChannelCreateCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.ChannelDestroyCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.ChannelUpdateCmd;
import com.xunhong.erp.turbo.api.infra.dto.qry.ChannelPageQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.ChannelShowQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.ChannelVO;
import com.xunhong.erp.turbo.api.rbac.dto.constants.PermissionConstant;
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.*;

@Tag(name = "Channel", description = "渠道管理")
@RestController("operationChannelController")
@RequestMapping("/operation")
@RequiredArgsConstructor
public class ChannelController {

	@DubboReference(version = "1.0.0")
	private final ChannelServiceI channelService;

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_CHANNEL_VIEW})
	@GetMapping("/pageChannel")
	@Operation(summary = "渠道分页", method = "GET")
	public PageResponse<ChannelVO> pageChannel(@ModelAttribute @Validated ChannelPageQry channelPageQry) {
		PageDTO<ChannelVO> page = channelService.page(channelPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_CHANNEL_CREATE})
	@PostMapping("/createChannel")
	@Operation(summary = "创建渠道", method = "POST")
	public SingleResponse<ChannelVO> createChannel(@RequestBody @Validated ChannelCreateCmd channelCreateCmd) {
		return SingleResponse.of(channelService.create(channelCreateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_CHANNEL_VIEW})
	@GetMapping("/showChannel")
	@Operation(summary = "渠道详情", method = "GET")
	public SingleResponse<ChannelVO> showChannel(@ModelAttribute @Validated ChannelShowQry channelShowQry) {
		return SingleResponse.of(channelService.show(channelShowQry));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_CHANNEL_UPDATE})
	@RequestMapping(value = "/updateChannel", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "渠道更新", method = "PATCH")
	public SingleResponse<ChannelVO> updateChannel(@RequestBody @Validated ChannelUpdateCmd channelUpdateCmd) {
		return SingleResponse.of(channelService.update(channelUpdateCmd));
	}

	@SaCheckLogin
	@SaCheckPermission(value = {PermissionConstant.OPERATION_CHANNEL_DELETE})
	@DeleteMapping(value = "/destroyChannel")
	@Operation(summary = "渠道删除", method = "DELETE")
	public Response destroyChannel(@RequestBody @Validated ChannelDestroyCmd channelDestroyCmd) {
		channelService.destroy(channelDestroyCmd);
		return Response.buildSuccess();
	}
}
