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.AgreementServiceI;
import com.xunhong.erp.turbo.api.infra.dto.cmd.AgreementCreateCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.AgreementDestroyCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.AgreementUpdateCmd;
import com.xunhong.erp.turbo.api.infra.dto.qry.AgreementListQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.AgreementPageQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.AgreementShowQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.AgreementVO;
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 = "Agreement", description = "协议管理")
@RestController("operationAgreementController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class AgreementController {

	@DubboReference(version = "1.0.0")
	private final AgreementServiceI agreementService;

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_AGREEMENT_VIEW})
	@GetMapping("listAgreement")
	@Operation(summary = "协议列表", method = "GET")
	public MultiResponse<AgreementVO> listAgreement(@ModelAttribute @Validated AgreementListQry agreementListQry) {
		return MultiResponse.of(agreementService.list(agreementListQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_AGREEMENT_CREATE})
	@PostMapping("createAgreement")
	@Operation(summary = "创建协议", method = "POST")
	public SingleResponse<AgreementVO> createAgreement(@RequestBody @Validated AgreementCreateCmd agreementCreateCmd) {
		return SingleResponse.of(agreementService.create(agreementCreateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_AGREEMENT_VIEW})
	@GetMapping("showAgreement")
	@Operation(summary = "协议详情", method = "GET")
	public SingleResponse<AgreementVO> showAgreement(@ModelAttribute @Validated AgreementShowQry agreementShowQry) {
		return SingleResponse.of(agreementService.show(agreementShowQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_AGREEMENT_VIEW})
	@GetMapping("pageAgreement")
	@Operation(summary = "协议列表", method = "GET")
	public PageResponse<AgreementVO> pageAgreement(@ModelAttribute @Validated AgreementPageQry agreementPageQry) {
		PageDTO<AgreementVO> page = agreementService.page(agreementPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_AGREEMENT_UPDATE})
	@RequestMapping(value = "updateAgreement", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "协议更新", method = "PATCH")
	public SingleResponse<AgreementVO> updateAgreement(@RequestBody @Validated AgreementUpdateCmd agreementUpdateCmd) {
		return SingleResponse.of(agreementService.update(agreementUpdateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_AGREEMENT_DELETE})
	@DeleteMapping("destroyAgreement")
	@Operation(summary = "协议删除", method = "DELETE")
	public Response destroyAgreement(@RequestBody @Validated AgreementDestroyCmd agreementDestroyCmd) {
		agreementService.destroy(agreementDestroyCmd);
		return Response.buildSuccess();
	}
}
