package com.yunzhupaas.base.controller;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import com.yunzhupaas.base.controller.SuperController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.base.entity.AdvancedQueryEntity;
import com.yunzhupaas.base.model.advancedquery.AdvancedQueryListVO;
import com.yunzhupaas.base.model.advancedquery.AdvancedQuerySchemeForm;
import com.yunzhupaas.base.service.AdvancedQueryService;
import com.yunzhupaas.base.vo.ListVO;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.exception.DataException;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.JsonUtilEx;
import com.yunzhupaas.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 高级查询方案管理
 *
 * @author 云筑产品开发平台组
 * @version V3.4.2
 * @copyright 深圳市乐程软件有限公司（http://www.szlecheng.cn）
 * @date  2022/5/30
 */
@Tag(name = "高级查询方案管理", description = "AdvancedQuery")
@RestController
@RequestMapping("/api/system/AdvancedQuery")
public class AdvancedQueryController extends SuperController<AdvancedQueryService, AdvancedQueryEntity> {

	@Autowired
	private AdvancedQueryService queryService;

	/**
	 * 新建
	 *
	 * @param advancedQuerySchemeForm 实体对象
	 * @return
	 */
	@Operation(summary = "新建方案")
	@Parameters({
			@Parameter(name = "advancedQuerySchemeForm", description = "实体对象", required = true)
	})
	@PostMapping
	public ActionResult create(@RequestBody @Valid AdvancedQuerySchemeForm advancedQuerySchemeForm) {
		AdvancedQueryEntity entity = JsonUtil.getJsonToBean(advancedQuerySchemeForm, AdvancedQueryEntity.class);
		queryService.create(entity);
		return ActionResult.success(MsgCode.SU001.get());
	}

	/**
	 * 修改方案
	 *
	 * @param id 主键
	 * @param advancedQuerySchemeForm 实体对象
	 * @return
	 */
	@Operation(summary = "修改方案")
	@Parameters({
			@Parameter(name = "id", description = "主键", required = true),
			@Parameter(name = "advancedQuerySchemeForm", description = "实体对象", required = true)
	})
	@PutMapping("/{id}")
	public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid AdvancedQuerySchemeForm advancedQuerySchemeForm) {
		AdvancedQueryEntity entity = JsonUtil.getJsonToBean(advancedQuerySchemeForm, AdvancedQueryEntity.class);
		entity.setId(id);
		queryService.updateById(entity);
		return ActionResult.success(MsgCode.SU004.get());
	}

	/**
	 * 删除
	 *
	 * @param id 主键值
	 * @return ignore
	 */
	@Operation(summary = "删除方案")
	@Parameters({
			@Parameter(name = "id", description = "主键", required = true)
	})
	@DeleteMapping("/{id}")
	public ActionResult delete(@PathVariable("id") String id) {
		UserInfo userInfo = UserProvider.getUser();
		AdvancedQueryEntity entity = queryService.getInfo(id,userInfo.getUserId());
		if (entity != null) {
			queryService.removeById(entity);
			return ActionResult.success(MsgCode.SU003.get());
		}
		return ActionResult.fail(MsgCode.FA003.get());
	}

	/**
	 * 列表
	 *
	 * @param moduleId 功能主键
	 * @return ignore
	 */
	@Operation(summary = "方案列表")
	@Parameters({
			@Parameter(name = "moduleId", description = "功能主键", required = true)
	})
	@GetMapping("/{moduleId}/List")
	public ActionResult<ListVO<AdvancedQueryListVO>> list(@PathVariable("moduleId") String moduleId) {
		UserInfo userInfo = UserProvider.getUser();
		List<AdvancedQueryEntity> data = queryService.getList(moduleId,userInfo);
		List<AdvancedQueryListVO> list = JsonUtil.getJsonToList(data, AdvancedQueryListVO.class);
		ListVO<AdvancedQueryListVO> vo = new ListVO<>();
		vo.setList(list);
		return ActionResult.success(vo);
	}
	/**
	 * 信息
	 *
	 * @param id 主键值
	 * @return ignore
	 * @throws DataException ignore
	 */
	@Operation(summary = "获取方案信息")
	@Parameters({
			@Parameter(name = "id", description = "主键值", required = true)
	})
	@GetMapping("/{id}")
	public ActionResult<AdvancedQuerySchemeForm> info(@PathVariable("id") String id) throws DataException {
		UserInfo userInfo = UserProvider.getUser();
		AdvancedQueryEntity entity = queryService.getInfo(id,userInfo.getUserId());
		AdvancedQuerySchemeForm vo = JsonUtilEx.getJsonToBeanEx(entity, AdvancedQuerySchemeForm.class);
		return ActionResult.success(vo);
	}

}

