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.DictionaryServiceI;
import com.xunhong.erp.turbo.api.infra.dto.cmd.DictionaryCreateCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.DictionaryDestroyCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.DictionaryUpdateCmd;
import com.xunhong.erp.turbo.api.infra.dto.qry.DictionaryListQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.DictionaryPageQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.DictionaryShowQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.DictionaryVO;
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 = "Dictionary", description = "字典管理")
@RestController("DictionaryController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class DictionaryController {

	@DubboReference(version = "1.0.0")
	private final DictionaryServiceI dictionaryService;

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DICTIONARY_VIEW})
	@GetMapping("listDictionary")
	@Operation(summary = "字典列表", method = "GET")
	public MultiResponse<DictionaryVO> listDictionary(@ModelAttribute @Validated DictionaryListQry dictionaryListQry) {
		return MultiResponse.of(dictionaryService.list(dictionaryListQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DICTIONARY_CREATE})
	@PostMapping("createDictionary")
	@Operation(summary = "创建字典", method = "POST")
	public SingleResponse<DictionaryVO> createDictionary(@RequestBody @Validated DictionaryCreateCmd dictionaryCreateCmd) {
		return SingleResponse.of(dictionaryService.create(dictionaryCreateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DICTIONARY_VIEW})
	@GetMapping("showDictionary")
	@Operation(summary = "字典详情", method = "GET")
	public SingleResponse<DictionaryVO> showDictionary(@ModelAttribute @Validated DictionaryShowQry dictionaryShowQry) {
		return SingleResponse.of(dictionaryService.show(dictionaryShowQry));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DICTIONARY_VIEW})
	@GetMapping("pageDictionary")
	@Operation(summary = "字典列表", method = "GET")
	public PageResponse<DictionaryVO> pageDictionary(@ModelAttribute @Validated DictionaryPageQry dictionaryPageQry) {
		PageDTO<DictionaryVO> page = dictionaryService.page(dictionaryPageQry);
		return PageResponse.of(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DICTIONARY_UPDATE})
	@RequestMapping(value = "updateDictionary", method = {RequestMethod.PATCH, RequestMethod.PUT})
	@Operation(summary = "字典更新", method = "PATCH")
	public SingleResponse<DictionaryVO> updateDictionary(@RequestBody @Validated DictionaryUpdateCmd dictionaryUpdateCmd) {
		return SingleResponse.of(dictionaryService.update(dictionaryUpdateCmd));
	}

	@SaCheckLogin
//    @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_DICTIONARY_DELETE})
	@DeleteMapping("destroyDictionary")
	@Operation(summary = "字典删除", method = "DELETE")
	public Response destroyDictionary(@RequestBody @Validated DictionaryDestroyCmd dictionaryDestroyCmd) {
		dictionaryService.destroy(dictionaryDestroyCmd);
		return Response.buildSuccess();
	}
}
