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.infra.api.MaterialServiceI;
import com.xunhong.erp.turbo.api.infra.dto.cmd.MaterialBatchAddCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.MaterialCreateCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.MaterialDestroyCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.MaterialUpdateCmd;
import com.xunhong.erp.turbo.api.infra.dto.qry.MaterialPageQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.MaterialShowQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.MaterialVO;
import com.xunhong.erp.turbo.api.user.dto.vo.OssTokenVO;
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 = "Material", description = "素材管理")
@RestController("operationMaterialController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class MaterialController {

	@DubboReference(version = "1.0.0")
	private final MaterialServiceI materialService;

	@SaCheckLogin
	@GetMapping(value = "/ossToken")
	@Operation(summary = "获取OSS上传凭证", method = "GET")
	public SingleResponse<OssTokenVO> ossToken() {
		return SingleResponse.of(materialService.ossToken());
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_VIEW}, mode = SaMode.OR)
	@GetMapping("/pageMaterial")
	@Operation(summary = "素材内容列表", method = "GET")
	public PageResponse<MaterialVO> pageMaterial(@ModelAttribute @Validated MaterialPageQry materialPageQry) {
		PageDTO<MaterialVO> page = materialService.page(materialPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_VIEW}, mode = SaMode.OR)
	@PostMapping("/createMaterial")
	@Operation(summary = "创建素材内容", method = "POST")
	public SingleResponse<MaterialVO> createMaterial(@RequestBody @Validated MaterialCreateCmd materialCreateCmd) {
		return SingleResponse.of(materialService.create(materialCreateCmd));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_CREATE}, mode = SaMode.OR)
	@PostMapping("/batchCreateMaterial")
	@Operation(summary = "创建素材内容", method = "POST")
	public MultiResponse<MaterialVO> batchCreateMaterial(@RequestBody @Validated MaterialBatchAddCmd materialAddCmdList) {
		return MultiResponse.of(materialService.batchCreate(materialAddCmdList));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_VIEW}, mode = SaMode.OR)
	@GetMapping("showMaterial")
	@Operation(summary = "素材内容详情", method = "GET")
	public SingleResponse<MaterialVO> showMaterial(@ModelAttribute @Validated MaterialShowQry materialShowQry) {
		return SingleResponse.of(materialService.show(materialShowQry));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_UPDATE}, mode = SaMode.OR)
	@RequestMapping(value = "updateMaterial", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "素材内容更新", method = "PATCH")
	public SingleResponse<MaterialVO> updateMaterial(@RequestBody @Validated MaterialUpdateCmd materialUpdateCmd) {
		return SingleResponse.of(materialService.update(materialUpdateCmd));
	}

	@SaCheckLogin
//	@SaCheckPermission(value = {PermissionConstant.OPERATION_MATERIAL_DELETE}, mode = SaMode.OR)
	@DeleteMapping("destroyMaterial")
	@Operation(summary = "删除素材内容", method = "DELETE")
	public Response destroyMaterial(@RequestBody @Validated MaterialDestroyCmd materialDestroyCmd) {
		materialService.destroy(materialDestroyCmd);
		return Response.buildSuccess();
	}
}
