package cn.api.atv.app;

import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.facade.abs.IService;
import cn.common.enums.ResultCode;
import cn.common.page.response.Result;
import cn.orm.sqltoy.model.Page;
import cn.orm.sqltoy.plus.Wrappers;
import cn.orm.sqltoy.plus.query.QueryWrapper;
import cn.pojo.atv.AtvKnowledge;
import cn.pojo.atv.AtvKnowledgeFile;
import cn.pojo.atv.vo.AtvKnowledgePage;
import cn.util.core.IsKit;

@RestController
@RequestMapping("/app/atvKnowledge")
public class KnowledgeCtrl extends IService 
{
	@GetMapping(value = "/list")
	public Result<?> dealList(@RequestParam Map<String,Object> paramMap)
	{
		Page<?> page = daoService.getPage(paramMap);
		Page<?> pageResult = daoService.findPageBySql(page,"atv_findKnowledgeListApp",paramMap,Map.class);
		if(IsKit.isNullOrEmpty(pageResult)) {
			return Result.warnMsg(ResultCode.SQL_RUN.getMsg());
		}
		return Result.ok_ePlusList(pageResult.getRecordCount(),pageResult.getRows());
	}

	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id") String id)
	{
		QueryWrapper<AtvKnowledge> queryWrapper = Wrappers.wrapper(AtvKnowledge.class)
				.eq("id", id); 
		AtvKnowledge atvKnowledge = daoService.findOne(queryWrapper);
		
		if(IsKit.isEmpty(atvKnowledge)) {
			return Result.warnMsg("未找到对应数据");
		}
		AtvKnowledgePage atvKnowledgePage = new AtvKnowledgePage();
		BeanUtils.copyProperties(atvKnowledge,atvKnowledgePage);
		atvKnowledgePage.getAtvKnowledgeFileList().addAll(this._selectByAk(id));
		return Result.ok(atvKnowledgePage);
	}
	private List<AtvKnowledgeFile> _selectByAk(String afId) 
	{
		QueryWrapper<AtvKnowledgeFile> queryWrapper = Wrappers.wrapper(AtvKnowledgeFile.class)
				.eq("knowledgeId", afId);
		return daoService.findList(queryWrapper);
	} 
}
