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

import com.prolog.cs.biz.inbound.dao.task.TaskHostedDetailMapper;
import com.prolog.cs.biz.inbound.dto.PageParam;
import com.prolog.cs.biz.inbound.dto.task.TaskHostedDetailDTO;
import com.prolog.cs.biz.inbound.model.task.TaskHostedDetail;
import com.prolog.cs.biz.inbound.service.task.TaskHostedDetailService;
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 TaskHostedDetailServiceImpl implements TaskHostedDetailService {

	@Resource
	private TaskHostedDetailMapper mapper;

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

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

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

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

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

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

	@Override
	public TaskHostedDetailDTO findByPkId(String pkId) {
		// TODO
		TaskHostedDetail taskHostedDetail = this.findById(pkId);
		AssertUtils.notNull(taskHostedDetail,"model empty!");
		TaskHostedDetailDTO taskHostedDetailDTO = new TaskHostedDetailDTO();
		BeanUtils.copyProperties(taskHostedDetail, taskHostedDetailDTO);
		return taskHostedDetailDTO;
	}

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

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

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

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

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

	private Restriction buildCondition(TaskHostedDetailDTO taskHostedDetailDTO){
		// TODO
		return null;
	}

}
