package com.prolog.cs.biz.inbound.service.impl.task;

import com.prolog.cs.biz.inbound.dao.task.TaskRcvDetailMapper;
import com.prolog.cs.biz.inbound.dto.PageParam;
import com.prolog.cs.biz.inbound.dto.task.TaskRcvDetailDTO;
import com.prolog.cs.biz.inbound.model.task.TaskRcvDetail;
import com.prolog.cs.biz.inbound.service.task.TaskRcvDetailService;
import com.prolog.cs.biz.inbound.utils.ObjectUtils;
import com.prolog.cs.biz.inbound.utils.UserUtils;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.beans.BeanUtils;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.framework.utils.AssertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Deprecated
public class TaskRcvDetailServiceImpl implements TaskRcvDetailService {

	@Resource
	private TaskRcvDetailMapper mapper;

	@Resource
	private UserUtils userUtils;
	@Override
	public Object add(TaskRcvDetailDTO taskRcvDetailDTO) {
		// TODO vaildate
		UserDTO userDTO = userUtils.getUserDTO();
		TaskRcvDetail taskRcvDetail = new TaskRcvDetail();
		BeanUtils.copyProperties(taskRcvDetailDTO, taskRcvDetail);
		this.vaildateModel(taskRcvDetail,false,userDTO);
		this.buildField(taskRcvDetail,false,userDTO);
		this.mapper.save(taskRcvDetail);
		return "";
	}

	@Override
	public Object edit(String pkId,TaskRcvDetailDTO taskRcvDetailDTO) {
		// TODO
		UserDTO userDTO = userUtils.getUserDTO();
		TaskRcvDetail taskRcvDetail = new TaskRcvDetail();
		BeanUtils.copyProperties(taskRcvDetailDTO, taskRcvDetail);
		this.vaildateModel(taskRcvDetail,true,userDTO);
		this.buildField(taskRcvDetail,true,userDTO);
		this.mapper.updateWithValidate(taskRcvDetail,
					p -> ObjectUtils.isNotNull(p) && ObjectUtils.isNotNull(p.getValue()));
		return "";
	}

	@Override
	@Transactional
	public Object delIds(List<String> pkIdList) {
		// TODO
		pkIdList.stream().filter(p-> canEdit(p)).forEach(p -> 
			this.mapper.deleteById(p,TaskRcvDetail.class));
		return "";
	}

	@Override
	public Object deletePkId(String pkId) {
		// TODO
		if(canEdit(pkId))
		throw new BizException("can not del");
		this.mapper.deleteById(pkId,TaskRcvDetail.class);
		return "";
	}

	@Override
	public Page<TaskRcvDetailDTO> findList(TaskRcvDetailDTO taskRcvDetailDTO,PageParam pageParam) {
		Criteria criteria = Criteria.forClass(TaskRcvDetail.class);
		Integer pageNum = null;
		Integer pageSize = null;
		Restriction r = null;
		if (pageParam == null) {
			pageNum = PageParam.DEFAULT_PAGE_NUM;
			pageSize = PageParam.DEFAULT_PAGE_SIZE;
		} else {
			pageNum = pageParam.getPageNum();
			pageSize = pageParam.getPageSize();
			TaskRcvDetailDTO condition = taskRcvDetailDTO;
			r = buildCondition(condition);
		}
		if (pageNum == null || pageNum == 0) {
			pageNum = PageParam.DEFAULT_PAGE_NUM;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = PageParam.DEFAULT_PAGE_SIZE;
		}
		criteria.setRestriction(r);
		Order order = Order.newInstance().desc("gmtModified");
		criteria.setOrder(order);
		PageUtils.startPage(pageNum, pageSize);
		List<TaskRcvDetail> list = this.mapper.findByCriteria(criteria);
		List<TaskRcvDetailDTO> collect = Optional.ofNullable(list).orElse(Collections.emptyList()).stream()
				.map(p -> {
					TaskRcvDetailDTO target = new TaskRcvDetailDTO();
					BeanUtils.copyProperties(p, target);
					return target;
				}).collect(Collectors.toList());
		Page<TaskRcvDetail> page = PageUtils.getPage(list);
		Page<TaskRcvDetailDTO> page2 = new Page<>();
		page2.setStartRow(page.getStartRow());
		page2.setEndRow(page.getEndRow());
		page2.setPageCount(page.getPageCount());
		page2.setPageNum(page.getPageNum());
		page2.setPageSize(page.getPageSize());
		page2.setTotalCount(page.getTotalCount());
		page2.setList(collect);
		return page2;
	}

	@Override
	@Transactional
	public Object batchAdd(List<TaskRcvDetailDTO> taskRcvDetailDTOList) {
		// TODO
		UserDTO userDTO = userUtils.getUserDTO();
		for (TaskRcvDetailDTO taskRcvDetailDTO : taskRcvDetailDTOList) {
			TaskRcvDetail taskRcvDetail = new TaskRcvDetail();
			BeanUtils.copyProperties(taskRcvDetailDTO, taskRcvDetail);
			this.vaildateModel(taskRcvDetail,false,userDTO);
			this.buildField(taskRcvDetail,false,userDTO);
			this.mapper.save(taskRcvDetail);
		}
		return "";
	}

	@Override
	@Transactional
	public Object batchEdit(List<TaskRcvDetailDTO> taskRcvDetailDTOList) {
		// TODO
		UserDTO userDTO = userUtils.getUserDTO();
		for (TaskRcvDetailDTO taskRcvDetailDTO : taskRcvDetailDTOList) {
			TaskRcvDetail taskRcvDetail = new TaskRcvDetail();
			BeanUtils.copyProperties(taskRcvDetailDTO, taskRcvDetail);
			this.vaildateModel(taskRcvDetail,true,userDTO);
			this.buildField(taskRcvDetail,true,userDTO);
			this.mapper.updateWithValidate(taskRcvDetail,
					p -> ObjectUtils.isNotNull(p) && ObjectUtils.isNotNull(p.getValue()));
		}
		return "";
	}

	@Override
	public List<TaskRcvDetailDTO> findAll(TaskRcvDetailDTO taskRcvDetailDTO) {
		// TODO
		Criteria criteria = Criteria.forClass(TaskRcvDetail.class);
		TaskRcvDetailDTO condition = taskRcvDetailDTO;
		Restriction r = buildCondition(condition);
		criteria.setRestriction(r);
		Order order = Order.newInstance().desc("gmtModified");
		criteria.setOrder(order);
		List<TaskRcvDetail> list = this.mapper.findByCriteria(criteria);
		List<TaskRcvDetailDTO> collect = Optional.ofNullable(list).orElse(Collections.emptyList()).stream()
				.map(p -> {
					TaskRcvDetailDTO target = new TaskRcvDetailDTO();
					BeanUtils.copyProperties(p, target);
					return target;
				}).collect(Collectors.toList());
		return collect;
	}

	@Override
	public TaskRcvDetailDTO findByPkId(String pkId) {
		// TODO
		TaskRcvDetail taskRcvDetail = this.findById(pkId);
		AssertUtils.notNull(taskRcvDetail,"model empty!");
		TaskRcvDetailDTO taskRcvDetailDTO = new TaskRcvDetailDTO();
		BeanUtils.copyProperties(taskRcvDetail, taskRcvDetailDTO);
		return taskRcvDetailDTO;
	}

	private TaskRcvDetail findById(String id) {
		return this.mapper.findById(id,TaskRcvDetail.class);
	}

	private void vaildateModel(TaskRcvDetail taskRcvDetail,Boolean editFlag,UserDTO userDTO) {
		if(!editFlag){
			// TODO add
		}else{
			// TODO
		}
	}

	private void buildField(TaskRcvDetail taskRcvDetail,Boolean editFlag,UserDTO userDTO) {
		if(!editFlag){
			// TODO add
		}else{
			// TODO edit
		}
		vaildateModelRepeated(taskRcvDetail,editFlag,userDTO);
	}

	private void vaildateModelRepeated(TaskRcvDetail taskRcvDetail,Boolean editFlag,UserDTO userDTO) {
		if(!editFlag){
			// TODO add
		}else{
			// TODO
		}
	}

	private boolean canEdit(String pkId) {
		boolean flag = true;
		TaskRcvDetail taskRcvDetail = this.findById(pkId);
		AssertUtils.notNull(taskRcvDetail,"model empty!");
		// TODO
		if(true)
		flag = false;
		return flag;
	}

	private Restriction buildCondition(TaskRcvDetailDTO taskRcvDetailDTO){
		// TODO
		return null;
	}

}
