package com.dk.mdm.service.core;

import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.core.PostConvert;
import com.dk.mdm.mapper.core.PostMapper;
import com.dk.mdm.model.pojo.core.Post;
import com.dk.mdm.model.query.core.PostQuery;
import com.dk.mdm.model.response.core.PostResponse;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.vo.core.PostVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class PostService extends BaseService<Post> {

	@Override
	public BaseMapper<Post> getRepository() {
		return postMapper;
	}

	@Autowired
	private PostMapper postMapper;

	@Autowired
	private PostConvert postConvert;

	/**
	 * @desc : 重写主键
	 * @author : 夏常明
	 * @date : 2023/1/4 20:29
	 */
	@Override
	public String getPrimaryKey() {
		return "post_id";
	}

	/**
	 * @desc : 条件查询
	 * @author : 夏常明
	 * @date : 2023/1/4 9:27
	 */
	@Pagination
	public ResponseResultVO<PageList<PostResponse>> selectByCond(PostQuery postQuery) {
		return super.mergeListWithCount(postQuery, postMapper.selectByCond(postQuery), postMapper.countByCond(postQuery));
	}

	/**
	 * @desc   : 保存方法
	 * @author : 夏常明
	 * @date   : 2023/1/4 9:27
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<?> insert(PostVO postVO) {
		// 转化实体
		Post post = new Post();
//		Post post = postConvert.convertToPo(postVO);
		BeanUtils.copyProperties(postVO, post);
		return super.insert(post);
	}

	/**
	 * @desc   : 编辑方法
	 * @author : 夏常明
	 * @date   : 2023/1/4 9:27
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<Boolean> update(PostVO postVO) {
		// 转化实体
		Post post = postConvert.convertToPo(postVO);
		return super.update(post);
	}
}
