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

import com.prolog.cs.biz.inbound.dao.task.TaskPendingDetailMapper;
import com.prolog.cs.biz.inbound.dto.PageParam;
import com.prolog.cs.biz.inbound.dto.task.TaskPendingDetailDTO;
import com.prolog.cs.biz.inbound.model.task.TaskPendingDetail;
import com.prolog.cs.biz.inbound.service.task.TaskPendingDetailService;
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
public class TaskPendingDetailServiceImpl implements TaskPendingDetailService {

	@Resource
	private TaskPendingDetailMapper mapper;

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

	@Override
	public Object edit(String pkId,TaskPendingDetailDTO taskPendingDetailDTO) {
		// TODO
		UserDTO userDTO = userUtils.getUserDTO();
		TaskPendingDetail taskPendingDetail = new TaskPendingDetail();
		BeanUtils.copyProperties(taskPendingDetailDTO, taskPendingDetail);
		this.vaildateModel(taskPendingDetail,true,userDTO);
		this.buildField(taskPendingDetail,true,userDTO);
		this.mapper.updateWithValidate(taskPendingDetail,
					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,TaskPendingDetail.class));
		return "";
	}

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

	@Override
	public Page<TaskPendingDetailDTO> findList(TaskPendingDetailDTO taskPendingDetailDTO,PageParam pageParam) {
		Criteria criteria = Criteria.forClass(TaskPendingDetail.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();
			TaskPendingDetailDTO condition = taskPendingDetailDTO;
			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<TaskPendingDetail> list = this.mapper.findByCriteria(criteria);
		List<TaskPendingDetailDTO> collect = Optional.ofNullable(list).orElse(Collections.emptyList()).stream()
				.map(p -> {
					TaskPendingDetailDTO target = new TaskPendingDetailDTO();
					BeanUtils.copyProperties(p, target);
					return target;
				}).collect(Collectors.toList());
		Page<TaskPendingDetail> page = PageUtils.getPage(list);
		Page<TaskPendingDetailDTO> 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<TaskPendingDetailDTO> taskPendingDetailDTOList) {
		// TODO
		UserDTO userDTO = userUtils.getUserDTO();
		for (TaskPendingDetailDTO taskPendingDetailDTO : taskPendingDetailDTOList) {
			TaskPendingDetail taskPendingDetail = new TaskPendingDetail();
			BeanUtils.copyProperties(taskPendingDetailDTO, taskPendingDetail);
			this.vaildateModel(taskPendingDetail,false,userDTO);
			this.buildField(taskPendingDetail,false,userDTO);
			this.mapper.save(taskPendingDetail);
		}
		return "";
	}

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

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

	@Override
	public TaskPendingDetailDTO findByPkId(String pkId) {
		// TODO
		TaskPendingDetail taskPendingDetail = this.findById(pkId);
		AssertUtils.notNull(taskPendingDetail,"model empty!");
		TaskPendingDetailDTO taskPendingDetailDTO = new TaskPendingDetailDTO();
		BeanUtils.copyProperties(taskPendingDetail, taskPendingDetailDTO);
		return taskPendingDetailDTO;
	}

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

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

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

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

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

	private Restriction buildCondition(TaskPendingDetailDTO taskPendingDetailDTO){
		// TODO
		return null;
	}

}
