package com.goafanti.order.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.dao.TOrderBillNewMapper;
import com.goafanti.common.dao.TOrderNewMapper;
import com.goafanti.common.model.TOrderBillNew;
import com.goafanti.common.model.TOrderNew;
import com.goafanti.common.utils.ExportExcelUtil;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.enums.LiquidationNewState;
import com.goafanti.order.service.OrderBillService;

@Service
public class OrderBillServiceImpl extends BaseMybatisDao<TOrderBillNewMapper> implements OrderBillService {
	
	@Autowired
	private TOrderBillNewMapper billNewMapper;
	@Autowired
	private TOrderNewMapper	tOrderNewMapper;
	@Autowired
	private AdminMapper	adminMapper;
	@Value(value = "${upload.path}")
	private String	uploadPath = null;


	@SuppressWarnings("unchecked")
	@Override
	public Pagination<TOrderBillNew> myBillList(TOrderBillNew billNew,
			Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		List<String>fids= new ArrayList<>();
		if (TokenManager.hasRole(AFTConstants.FINANCE)) {
			fids.add(TokenManager.getAdminId());
		}else if (TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)) {
			fids=adminMapper.selectBySuperId(TokenManager.getAdminId());
		}
		params.put("fids", fids);
		params.put("b", billNew);
		return (Pagination<TOrderBillNew>)findPage("myBillList", "myBillCount", params, pageNo, pageSize);
	}


	@Override
	public XSSFWorkbook exportBillData(TOrderBillNew billNew) {
		// 判断导出是否有部门
		String deptid = billNew.getDepartmentId();
		Map<String, List<TOrderBillNew>> map = new HashMap<String, List<TOrderBillNew>>();
		if(StringUtils.isBlank(deptid)){
			//获得所有有数据的部门
			List<TOrderBillNew> dlist = billNewMapper.getDeptBySome(billNew);
			if(null == dlist || dlist.size() == 0){
				return null;
			}
			for (int i = 0; i < dlist.size(); i++) {
				TOrderBillNew ob = dlist.get(i);
				billNew.setDepartmentId(ob.getDepartmentId());
				map.put(ob.getDepartmentName(), billNewMapper.getBillData(billNew));
			}
		}else{
			map.put(billNew.getDepartmentName(), billNewMapper.getBillData(billNew));
		}
		String[] comment = {"平台流水编号","平台流水时间","交易金额(万元)","付款方","收款方","流水科目","交易渠道","交易时间","财务流水号","订单编号","订单负责人"};
		String filePath = uploadPath + "/billtemp";
//		File f = new File(filePath);
//		if(!f.exists()){
//			f.mkdirs();
//		}
		//filePath += "/" + System.nanoTime() +".xls";
		XSSFWorkbook wb = ExportExcelUtil.exportDeptBillInfo(map,  comment, filePath);
		
		return wb;
	}


	@Override
	public int deleteMyBill(String billNo) {
		TOrderBillNew tbn=billNewMapper.selectByPrimaryKey(billNo);
		TOrderNew o=tOrderNewMapper.selectByPrimaryKey(tbn.getOrderNo());
		TOrderNew o2=new TOrderNew();
		o2.setOrderNo(o.getOrderNo());
		o2.setSettlementAmount(o.getSettlementAmount().subtract(tbn.getTransactionAmount()));
		if(o2.getSettlementAmount().compareTo(o.getFirstAmount())== -1) {
			o2.setLiquidationStatus(LiquidationNewState.WAIT_PAY_FIRST_BALANCE.getCode());
		}else if(o2.getSettlementAmount().compareTo(o.getFirstAmount())> -1&&
				o2.getSettlementAmount().compareTo(o.getTotalAmount())== -1) {
			o2.setLiquidationStatus(LiquidationNewState.WAIT_PAY_LAST_BALANCE.getCode());
		}else if(o2.getSettlementAmount().compareTo(o.getTotalAmount())== 0) {
			o2.setLiquidationStatus(LiquidationNewState.ALREADY_PAY.getCode());
		}
		tOrderNewMapper.updateByPrimaryKeySelective(o2);
		TOrderBillNew ntbn=new TOrderBillNew();
		ntbn.setBillNo(tbn.getBillNo());
		ntbn.setDeleteSign((byte)1);
		ntbn.setDeleteTime(new Date());
		billNewMapper.updateByPrimaryKeySelective(ntbn);
		return 1;
	}
	
}
