package com.goafanti.order.service.impl;

import com.goafanti.admin.service.DepartmentService;
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.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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@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;
	@Autowired
	private DepartmentService	 departmentService;


	@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);
		if (billNew.getDeps()!=null){
			params.put("deps", departmentService.parseArray(billNew.getDeps()));
		}
		return (Pagination<TOrderBillNew>)findPage("myBillList", "myBillCount", params, pageNo, pageSize);
	}





	@Override
	public int deleteMyBill(String billNo) {
		TOrderBillNew tbn=billNewMapper.selectByPrimaryKey(billNo);
		TOrderNew o=tOrderNewMapper.queryById(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.update(o2);
		TOrderBillNew ntbn=new TOrderBillNew();
		ntbn.setBillNo(tbn.getBillNo());
		ntbn.setDeleteSign((byte)1);
		ntbn.setDeleteTime(new Date());
		billNewMapper.updateByPrimaryKeySelective(ntbn);
		return 1;
	}

}
