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

import com.prolog.cs.biz.inbound.dao.InterfaceRqcBillMapper;
import com.prolog.cs.biz.inbound.dto.InterfaceRqcBillDTO;
import com.prolog.cs.biz.inbound.dto.PageParam;
import com.prolog.cs.biz.inbound.model.InterfaceRqcBill;
import com.prolog.cs.biz.inbound.service.InterfaceRqcBillService;
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 InterfaceRqcBillServiceImpl implements InterfaceRqcBillService {

	@Resource
	private InterfaceRqcBillMapper mapper;

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

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

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

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

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

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

	@Override
	public InterfaceRqcBillDTO findByPkId(String pkId) {
		// TODO
		InterfaceRqcBill interfaceRqcBill = this.findById(pkId);
		AssertUtils.notNull(interfaceRqcBill,"model empty!");
		InterfaceRqcBillDTO interfaceRqcBillDTO = new InterfaceRqcBillDTO();
		BeanUtils.copyProperties(interfaceRqcBill, interfaceRqcBillDTO);
		return interfaceRqcBillDTO;
	}

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

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

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

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

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

	private Restriction buildCondition(InterfaceRqcBillDTO interfaceRqcBillDTO){
		// TODO
		return null;
	}

}
