package com.goafanti.order.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.NewOrderDunMapper;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.InputNewOrderDunBo;
import com.goafanti.order.bo.OutNewOrderDunBo;
import com.goafanti.order.enums.OrderDunProjectType;
import com.goafanti.order.service.NewOrderDunService;
@Service
public class NewOrderDunServiceImpl extends BaseMybatisDao<NewOrderDunMapper> implements NewOrderDunService {

	@Autowired
	private NewOrderDunMapper	newOrderDunMapper;
	
	
	@Override
	public int createDun(InputNewOrderDunBo ib) {
		ib.setAid(TokenManager.getAdminId());
		if(ib.getCustomizeTimes()!=null)ib.setCustomizeTime(DateUtils.StringToDate(ib.getCustomizeTimes(), AFTConstants.YYYYMMDD));
		if (ib.getAppropriationRatio()!=null)ib.setMoney(null);
		if (ib.getProjectType()==OrderDunProjectType.HY.getCode()&&ib.getDunType()==2&&ib.getEffectiveCount()>1) {
			for (int i = 1; i <= ib.getEffectiveCount(); i++) {
				int x=ib.getEffectiveCount();
				ib.setEffectiveCount(i);
				newOrderDunMapper.insertSelective(ib);
				ib.setEffectiveCount(x);
			}
		}else 	newOrderDunMapper.insertSelective(ib);
		return 0;
	}
	
	
	@Override
	public int updateDun(InputNewOrderDunBo ib) {
		if (ib.getAppropriationRatio()!=null)ib.setMoney(null);
		if(ib.getCustomizeTimes()!=null)ib.setCustomizeTime(DateUtils.StringToDate(ib.getCustomizeTimes(), AFTConstants.YYYYMMDD));
		return newOrderDunMapper.updateByPrimaryKeySelective(ib);
	}
	@Override
	public int deleteDun(Integer id) {
		return newOrderDunMapper.deleteByPrimaryKey(id);
	}
	@Override
	public List<OutNewOrderDunBo> selectListNewOrderDun(String orderNo) {
		return newOrderDunMapper.selectListNewOrderDun(orderNo,null,null);
	}
	@Override
	public OutNewOrderDunBo selectDun(Integer id) {
		OutNewOrderDunBo ob=new OutNewOrderDunBo();
		try {
			ConvertUtils.register(new DateConverter(null), java.util.Date.class);
			BeanUtils.copyProperties(ob, newOrderDunMapper.selectByPrimaryKey(id));
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return ob;
	}
	
	
	@Override
	public List<OutNewOrderDunBo> selectAllOrderDun() {
		return newOrderDunMapper.selectAllOrderDun();
	}
	
	

}
