package org.security.trans.workHandOverUsers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.security.common.Constant;
import org.security.ht.vo.common.UploadVO;
import org.security.ht.workHandOverUsers.entity.HandoverHistory;
import org.security.ht.workHandOverUsers.entity.WorkHandoverUsers;
import org.security.ht.workHandOverUsers.vo.HandoverHistoryVO;
import org.security.ht.workHandOverUsers.dto.HandoverHistoryDTO;
import org.security.trans.common.service.IFileService;
import org.security.trans.workHandOverUsers.mapper.HandoverHistoryMapper;
import org.security.trans.workHandOverUsers.service.HandoverHistoryService;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author zj 2022/9/7
 */
@Service
@AllArgsConstructor
public class HandoverHistoryServiceImpl extends ServiceImpl<HandoverHistoryMapper, HandoverHistory> implements HandoverHistoryService {
	private final IFileService fileService;

	@Override
	public IPage<HandoverHistoryVO> selectProjectPage(IPage<HandoverHistoryVO> page, HandoverHistoryDTO handoverHistoryDTO) {
		List<HandoverHistory> list = this.list(new QueryWrapper<HandoverHistory>().lambda()
			.like(StringUtils.isNotBlank(handoverHistoryDTO.getHandoverFrom()),HandoverHistory::getHandoverFrom,handoverHistoryDTO.getHandoverFrom())
			.like(StringUtils.isNotBlank(handoverHistoryDTO.getHandoverTo()),HandoverHistory::getHandoverTo,handoverHistoryDTO.getHandoverTo() )
			.like(StringUtils.isNotBlank(handoverHistoryDTO.getContent()),HandoverHistory::getContent,handoverHistoryDTO.getContent())
			.orderByDesc(HandoverHistory::getHandoverTime));
		return page.setRecords(Func.copy(list,HandoverHistoryVO.class));
	}

	@Override
	public R saveForm(HandoverHistoryVO handoverHistoryVO){
		super.saveOrUpdate(handoverHistoryVO);
		List<UploadVO> fileList = handoverHistoryVO.getImgUrl();
		if (Objects.nonNull(fileList) && !fileList.isEmpty()) {
			fileService.saveAttachments(fileList, handoverHistoryVO.getId());
		}
		return R.data(handoverHistoryVO);
	}

	@Override
	public HandoverHistoryDTO getDetail(String id) {
		HandoverHistory detail = super.getById(id);
		HandoverHistoryDTO handoverHistoryDTO = new HandoverHistoryDTO();
		BeanUtils.copyProperties(detail, handoverHistoryDTO);
		handoverHistoryDTO.setImgUrl(fileService.getAttachments(detail.getId(), detail.getGroupName()));
		return handoverHistoryDTO;
	}
}
