package com.dk.mdm.service.mst;

import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.model.pojo.PageList;
import com.dk.common.model.response.mst.OrgResponse;
import com.dk.common.model.vo.mst.OrgVO;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.mst.OrgConvert;
import com.dk.mdm.infrastructure.convert.mst.PrintRemarkConvert;
import com.dk.mdm.model.pojo.mst.Org;
import com.dk.mdm.model.pojo.mst.PrintRemark;
import com.dk.mdm.mapper.mst.PrintRemarkMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.query.mst.OrgQuery;
import com.dk.mdm.model.query.mst.PrintRemarkQuery;
import com.dk.mdm.model.response.mst.PrintRemarkResponse;
import com.dk.mdm.model.vo.mst.PrintRemarkVO;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PrintRemarkService extends BaseService<PrintRemark> {

	@Override
	public BaseMapper<PrintRemark> getRepository() {
		return printRemarkMapper;
	}

	@Autowired
	private PrintRemarkMapper printRemarkMapper;

	@Autowired
	private PrintRemarkConvert printRemarkConvert;

	@Override
	public String getPrimaryKey() {
		return "setting_id";
	}
	/**
	 * @desc : 条件查询
	 * @author : 王英杰
	 * @date : 2023/1/10 17:19
	 */
	@Pagination
	public ResponseResultVO<List<PrintRemarkResponse>> selectByCond(PrintRemarkQuery printRemarkQuery) {
		List<PrintRemarkResponse> list = printRemarkMapper.selectByCond(printRemarkQuery);

		return ResponseResultUtil.success(list);
	}

	/**
	 * @desc : 新建组织机构
	 * @author : 王英杰
	 * @date : 2024/2/27 9:17
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<?> insert(PrintRemarkVO printRemarkVO) {
		List<PrintRemarkResponse> list = printRemarkMapper.selectByCond(new PrintRemarkQuery().setDocName(printRemarkVO.getDocName()).setFlgValid(true));
		// 转化实体
		PrintRemark printRemark = printRemarkConvert.convertToPo(printRemarkVO);

           if(list != null && list.size()>0){ //说明查到了 那就编辑
             //查到得话 也就一条 不会出现两条
//			   PrintRemarkResponse printRemarkResponseData = list.get(0);
			   super.updateByUuid(printRemark);
		   }else{//没查到 就新建
			   super.insert(printRemark);
		   }
		return ResponseResultUtil.success();

	}

}
